webForumDet fria alternativet

Sortering i Sql-Server

4 svar · 478 visningar · startad av KrpSvedberg

KrpSvedbergMedlem sedan okt. 200558 inlägg
#1

Hej!

Jag använder mig av MS SQL-Server 2000, och har för tillfället en fråga som lyder:

SELECT DISTINCT tAccounts.tAccountId, 
       sAccountName, 
       tVerifications.tImportSessionId, 
       sCategoryName, sSubCategoryName,      
       sAccountNo, tAccounts.tAccountCategoryId, 
       tAccounts.tAccountSubCategoryId 
FROM     tVerifications  

INNER JOIN tAccountSubCategory  
INNER JOIN tAccounts 
     ON tAccountSubCategory.tAccountSubCategoryId = tAccounts.tAccountSubCategoryId  
INNER JOIN tAccountCategory 
     ON tAccounts.tAccountCategoryId = tAccountCategory.tAccountCategoryId  
INNER JOIN tVerTransactions 
     ON tAccounts.tAccountId = tVerTransactions.tAccountId 
     ON tVerifications.tVerificationId = tVerTransactions.tVerificationId 

WHERE (tVerifications.tImportSessionId = '48C85E4A-4802-4DBF-B7DC-C52586F2019B') 
     AND substring(sVerificationDate, 1, 4) = '2007' 
     AND sAccountNo >= 1000 
     AND sAccountNo <= 2999 

ORDER BY tAccounts.tAccountCategoryId, tAccounts.tAccountSubCategoryId, tAccounts.tAccountId

Resultatet vill jag spotta ut i en XML som ser ut ungefär så här:

<area type="part" value="3249407.9000" name="Likvida medel">
   <subarea name="Kassa och bank" value="1624703.9500">
      <account id="1920" name="Växelkassa" value="8669.0000" />    
      <account id="1916" name="Utländsk valuta" value="38935.2100" />    
      <account id="1910" name="Kassa" value="164990.0000" />       
   </subarea>
   <subarea name="Kortfristiga placeringar" value="162000.0000">
      <account id="1800" name="Kortfristiga placeringar" value="162000.0000" />
   </subarea>
</area>
<area type="part" value="75830.0000" name="Övriga fordringar">
   <subarea name="Förutbetalda kostnader och upplupna intäkter" value="37915.0000">
      <account id="1790" name="Öv förutbet kostn o upplup intäkte" value="0.0000" />
      <account id="1720" name="Förutbetalda leasingavgifter" value="37915.0000" />
   </subarea>
   <subarea name="Övriga kortfristiga fordringar" value="14217.0000">   
      <account id="1682" name="Vinstinlösen Bingolotto" value="11659.5000" />
      <account id="1630" name="Skattekonto" value="-374.0000" />    
      <account id="1683" name="Vinstinlösen ATG/Svenska spel" value="5203.0000" />
      <account id="1610" name="Fordringar hos anställda" value="0.0000" />    
      <account id="1651" name="Priskrigsersättning" value="-2271.5000" />       
   </subarea>
</area>

Problemet jag har är att jag vill sortera på account id (sAccountNo i db) så att de kommer i fallande ordning men ändå grupperat i subarea (tAccountSubCategory) och area (tAccountCategory), men gör jag det, sätter sAccountNo först i ORDER BY, kommer resultatet helt fel när jag skriver ut det i XML'n. Kontonummer kommer i ordning, men inte grupperat enligt min önskan.

Är inte helt het på SQL enligt denna (enligt mig) monstruösa typen, så hjälp skulle uppskattas :)

Fredde MannenMedlem sedan nov. 20014 054 inlägg
#2

Du får väl använda dig av group by för att gruppera och kunna sortera efter id, area, subarea...

Är ingen hejjare på SQL, så vette tusan om det funkar...

SELECT DISTINCT tAccounts.tAccountId, 
       sAccountName, 
       tVerifications.tImportSessionId, 
       sCategoryName, sSubCategoryName,      
       sAccountNo, tAccounts.tAccountCategoryId, 
       tAccounts.tAccountSubCategoryId 
FROM     tVerifications  

INNER JOIN tAccountSubCategory  
INNER JOIN tAccounts 
     ON tAccountSubCategory.tAccountSubCategoryId = tAccounts.tAccountSubCategoryId  
INNER JOIN tAccountCategory 
     ON tAccounts.tAccountCategoryId = tAccountCategory.tAccountCategoryId  
INNER JOIN tVerTransactions 
     ON tAccounts.tAccountId = tVerTransactions.tAccountId 
     ON tVerifications.tVerificationId = tVerTransactions.tVerificationId 

WHERE (tVerifications.tImportSessionId = '48C85E4A-4802-4DBF-B7DC-C52586F2019B') 
     AND substring(sVerificationDate, 1, 4) = '2007' 
     AND sAccountNo >= 1000 
     AND sAccountNo <= 2999 

GROUP BY tAccounts.tAccountId, 
       sAccountName, 
       tVerifications.tImportSessionId, 
       sCategoryName, sSubCategoryName,      
       sAccountNo, tAccounts.tAccountCategoryId, 
       tAccounts.tAccountSubCategoryId 

ORDER BY tAccounts.tAccountCategoryId, tAccounts.tAccountSubCategoryId, tAccounts.tAccountId
KrpSvedbergMedlem sedan okt. 200558 inlägg
#3

Hjälpte dessvärre inte. Kanske inte ens är möjligt? Tips på alternativa lösningar tages tacksamt emot! Kanske stoppa in allt i en array (bygger i ASP.NET/C#) och sortera där i?

KrpSvedbergMedlem sedan okt. 200558 inlägg
#4

Verkar som att jag löste det ändå, tack vare group by :)

SELECT 
    DISTINCT tAccounts.tAccountId, 
    sAccountName, 
    tVerifications.tImportSessionId, 
    sCategoryName, 
    sSubCategoryName, 
    sAccountNo, 
    tAccounts.tAccountCategoryId, 
    tAccounts.tAccountSubCategoryId 
FROM
    tVerifications 
INNER JOIN
    tAccountSubCategory 
INNER JOIN
    tAccounts 
    ON tAccountSubCategory.tAccountSubCategoryId = tAccounts.tAccountSubCategoryId 
INNER JOIN
    tAccountCategory 
    ON tAccounts.tAccountCategoryId = tAccountCategory.tAccountCategoryId 
INNER JOIN
    tVerTransactions 
    ON tAccounts.tAccountId = tVerTransactions.tAccountId 
    ON tVerifications.tVerificationId = tVerTransactions.tVerificationId
WHERE     
    (tVerifications.tImportSessionId = '48C85E4A-4802-4DBF-B7DC-C52586F2019B')
AND
    substring(sVerificationDate, 1, 4) = '2007'
AND
	sAccountNo >= 1000
AND
	sAccountNo <= 2999

GROUP BY	tAccounts.tAccountCategoryId, 
			tAccounts.tAccountSubCategoryId, 
			tAccounts.tAccountId, sAccountName, tVerifications.tImportSessionId, sCategoryName, sSubCategoryName, 
    sAccountNo

ORDER BY sAccountNo

Tack Fredde Mannen för att du gav mig en knuff i rätt riktning :)

Fredde MannenMedlem sedan nov. 20014 054 inlägg
#5

Oftast är det allt man behöver.. :)

138 ms totalt · 3 externa anrop · v20260731065814-full.30151723
0 ms — hämta forumlista (cache)
0 ms — hämta statistik (cache)
135 ms — hämta tråd, inlägg och bilagor (db)