---
title: "Sortering i Sql-Server"
type: "forum-thread"
url: "https://www.webforum.nu/amne/databaser-sql/172805-sortering-i-sql-server"
topic: "Databaser & SQL"
topic_url: "https://www.webforum.nu/amne/databaser-sql"
author: "KrpSvedberg"
published: "2008-07-16T09:32:53.000Z"
updated: "2008-07-17T08:51:40.000Z"
replies: 4
views: 505
page: 1
pages: 1
language: "sv-SE"
site: "webForum — webforum.nu"
rights: "Upphovsrätten till varje inlägg tillhör dess författare."
attribution: "Citera som: webForum, https://www.webforum.nu/amne/databaser-sql/172805-sortering-i-sql-server"
---

# Sortering i Sql-Server

## #1 — KrpSvedberg, 2008-07-16T09:32Z

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 :)

Permalänk: https://www.webforum.nu/p/172805

## #2 — Fredde Mannen, 2008-07-16T10:02Z

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
```

Permalänk: https://www.webforum.nu/p/2125962

## #3 — KrpSvedberg, 2008-07-16T13:02Z

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?

Permalänk: https://www.webforum.nu/p/2125988

## #4 — KrpSvedberg, 2008-07-17T08:50Z

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 :)

Permalänk: https://www.webforum.nu/p/2126081

## #5 — Fredde Mannen, 2008-07-17T08:51Z

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

Permalänk: https://www.webforum.nu/p/2126083

---

Tråden på webben: https://www.webforum.nu/amne/databaser-sql/172805-sortering-i-sql-server
