Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

ORDER BY CASE GIVING WRONG RESULTS

$
0
0

SELECT FirstWord + substring(Rest, 1, charindex('~', Rest+'~') - 1) AS INVOICE, FirstWord + substring(Rest, 1, charindex('~', Rest+'~') - 1) AS INVOICESTRING,
x.CUSTOMER, x.CUSTOMERSTRING,x.NAME,x.TOTAL,x.DATE,x.ARHID,x.DRIVERID,x.DRIVER,x.COUNT, x.STATUS
FROM (
SELECT TempWoid
,substring(TempWoid, 1, charindex('~', TempWoid) - 1) AS FirstWord
,substring(TempWoid, charindex('~', TempWoid) + 1, len(TempWoid)) AS Rest,
A.CUSTOMER, CAST(A.CUSTOMER AS NVARCHAR(50)) AS CUSTOMERSTRING, C.NAME, SUBTOTAL + TAXES AS TOTAL, A.DATE,ARHID, CAST(A.DRIVERID AS NVARCHAR(5))AS DRIVERID, D.Name AS DRIVER,
1 AS COUNT, STATUS
FROM ARH A
JOIN CUSTOMER C
ON A.CUSTOMER = C.CUSTOMER
LEFT JOIN DRIVERS D
ON A.DRIVERID = D.DRIVERID
WHERE STATUS = 'WO'
) x
ORDER BY CASE X.Rest WHEN 'S' THEN 1 WHEN 'M' THEN 2 WHEN 'T' THEN 3 WHEN 'W' THEN 4
WHEN 'TH' THEN 5 WHEN 'F' THEN 6 WHEN 'SH' THEN 7 ELSE 8 END, x.FirstWord

RESULTS

1th1th 659659 1106 OCEAN AVE148.922014-02-13105481-1NULL1wo
1t1t6596591106 OCEAN AVE216.912014-02-18105482-1NULL1 wo
1Th1Th 659659 1106 OCEAN AVE148.922014-02-27105501-1NULL1wo
1Th1Th 659659 1106 OCEAN AVE148.922014-02-27105502-1NULL1wo
1Th1Th 659659 1106 OCEAN AVE148.922014-02-27105503-1NULL1wo
1Th1Th 659659 1106 OCN AVE148.922014-02-27105504-1NULL1wo
1Th1Th 659659 1106 OCN AVE148.922014-02-27105505-1NULL1wo
1Th1Th 659659 1106 OCN AVE148.922014-02-27105506-1NULL1wo
1Th1Th 659659 1106 OCN AVE148.922014-02-27105507-1NULL1wo
1W1W6596591106 OCN AVE160.112014-03-05105533-1NULL1 wo
2Th2Th 659659 1106 OCN AVE148.922014-02-27105508-1NULL1wo
3Th3Th 659659 1106 OCN AVE148.922014-02-27105509-1NULL1wo
3Th3Th 659659 1106 OCN AVE148.922014-02-27105511-1NULL1wo
3T3T6596591106 OCN AVE203.972014-03-04105522-1NULL1 wo
3T3T6596591106 OCN AVE284.902014-03-04105529-1NULL1 wo
4Th4Th 659659 1106 OCN AVE148.922014-02-27105512-1NULL1wo
5Th5Th 659659 1106 OCN AVE148.922014-02-27105513-1NULL1wo
7Th7Th 659659 1106 OCN AVE162.762014-02-27105515-1NULL1wo

I WANTED THE FIRST COLUMN WHICH IS THE INVOICE COLUMN TO BE IN ORDER OF THE WEEK 

S,M,T,W,TH,F,SA AND WITHIN THAT TO BE IN NUMERICAL ORDER LIKE SO 

1S

2S

3S

1M

2M

3M

1T

2T

3T

1W

2W

3W


Debra has a question


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>