Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
ereader
Yak Posting Veteran
50 Posts |
Posted - 2003-04-15 : 06:32:07
|
| Hi ALLI have a query to sort the column value by year and month 030402120211 --------------------------------------------------- SELECT RIGHT(year1,2)+''+ month1 'Day' ,SUM(Ire)AS 'IE', SUM(Final)AS 'FQ',SUM(Others) AS 'Others',SUM(NA) AS 'NA' FROM ( SELECT COUNT(ref)AS 'REF' ,CAST(DATEPART(mm,date)AS VARCHAR(11))AS 'month1' ,CAST(DATEPART(yy,date)AS VARCHAR(11))AS 'year1' ,Ire=CASE bm.company_id WHEN 'IE' THEN COUNT(ref) ELSE 0 END,Final=CASE bm.company_id WHEN 'FQ' THEN COUNT(ref) ELSE 0 END,Others=CASE bm.company_id WHEN ISNULL(company_id,0) THEN 0 ELSE COUNT(ref) END,NA=CASE bm.company_id WHEN '' THEN COUNT(ref) ELSE 0 END ,company_id FROM booking_master bm GROUP BY CAST(DATEPART(yy,date)AS VARCHAR(11)),CAST(DATEPART(mm,date)AS VARCHAR(11)),company_id ) TABLE1 GROUP BY year1,month1 ORDER BY year1 DESC,month1 DESC --- result as follows038 0037 0034 10033 13032 12031 19029 0028 0027 0026 0025 0024 00212 80211 2I am counting the number of REF against the each companyidbut the problem is that i want the year value and month value in a single column but it giving not in a sorted manner and i am looking in the following manner0304021202110210UpdatedYEAR MONTH----------------2030 82009 82003 42003 32003 22003 12002 122002 112002 102002 92002 82002 72002 62002 52002 42001 91900 1I want the above result in a SINGLE column(DAY) sorted according to the year and monthEdited by - ereader on 04/15/2003 07:40:42 |
|
|
mohdowais
Sheikh of Yak Knowledge
1456 Posts |
Posted - 2003-04-15 : 09:34:11
|
Nothing major, its sorting both year1 and month1 by their ASCII values, since they have been cast to varchar: quote: CAST(DATEPART(mm,date)AS VARCHAR(11))AS 'month1' ,CAST(DATEPART(yy,date)AS VARCHAR(11))AS 'year1'
Just add this to the end:ORDER BY CAST(year1 AS INT) DESC, CAST(month1 AS INT) DESC OS |
 |
|
|
ereader
Yak Posting Veteran
50 Posts |
Posted - 2003-04-15 : 09:52:29
|
Cool Onequote: Just add this to the end:ORDER BY CAST(year1 AS INT) DESC, CAST(month1 AS INT) DESC OS
Thanks mohdowais    |
 |
|
|
|
|
|
|
|