| Author |
Topic |
|
monfu
Yak Posting Veteran
81 Posts |
Posted - 2008-07-22 : 06:40:38
|
| Dear AllI have a stored procedure that is outputting a PageID and StatusID. PageIDs can be any integer and also 0, while StatusID can be 1,4 or null.I wish to output something first in the StatusID order, and then in the PageID order. So I wish to output something like PageID StatusID1 12 14 16 17 19 12 44 45 46 48 49 40 10 10 40 40 4WITH ResourcesRN AS( SELECT ROW_NUMBER() OVER (ORDER BY pr.PageID DESC, er.StatusId ASC) AS RowNum, COALESCE(pr.PageID, 0) AS PageID, er.StatusID FROM dbo.Resources er LEFT JOIN ( SELECT fk_resourceId, MAX(fk_PageID) AS PageID FROM PageResources GROUP BY fk_resourceId ) AS pr ON pr.fk_resourceId = er.resourceId)SELECT * FROM ResourcesRN WHERE RowNum BETWEEN (1 - 1) * 1000 + 1 AND 1 * 1000Thanks for your help and timeJohann |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-07-22 : 06:45:17
|
[code]SELECT PageID, StatusIDFROM Table1ORDER BY CASE PageID WHEN 0 THEN 0 ELSE 1 END, StatusID, PageID[/code] E 12°55'05.25"N 56°04'39.16" |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-07-22 : 06:51:05
|
quote: Originally posted by Peso
SELECT PageID, StatusIDFROM Table1ORDER BY CASE PageID WHEN 0 THEN 0 ELSE 1 END, StatusID, PageID E 12°55'05.25"N 56°04'39.16"
shouldnt there be a DESC for first filed in order by? |
 |
|
|
monfu
Yak Posting Veteran
81 Posts |
Posted - 2008-07-22 : 06:56:33
|
| Hi PesoThanksThat partially worked, however I am getting the PageID 0 first before the other PageIDS. Is it possible to get the PageID 0 last? |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-07-22 : 06:59:40
|
Good point.Or change the constants?DECLARE @Sample TABLE (PageID INT, StatusID INT)INSERT @SampleSELECT 2, 4 UNION ALLSELECT 1, 1 UNION ALLSELECT 0, 4 UNION ALLSELECT 2, 1 UNION ALLSELECT 5, 4 UNION ALLSELECT 4, 1 UNION ALLSELECT 6, 4 UNION ALLSELECT 9, 1 UNION ALLSELECT 4, 4 UNION ALLSELECT 0, 1 UNION ALLSELECT 8, 4 UNION ALLSELECT 0, 4 UNION ALLSELECT 6, 1 UNION ALLSELECT 0, 4 UNION ALLSELECT 7, 1 UNION ALLSELECT 0, 1 UNION ALLSELECT 9, 4SELECT PageID, StatusIDFROM @SampleORDER BY CASE PageID WHEN 0 THEN 1 ELSE 0 END, StatusID, PageID E 12°55'05.25"N 56°04'39.16" |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-07-22 : 07:00:07
|
quote: Originally posted by monfu Hi PesoThanksThat partially worked, however I am getting the PageID 0 first before the other PageIDS. Is it possible to get the PageID 0 last?
SELECT PageID, StatusIDFROM Table1ORDER BY CASE PageID WHEN 0 THEN 0 ELSE 1 END DESC, StatusID, PageID |
 |
|
|
monfu
Yak Posting Veteran
81 Posts |
Posted - 2008-07-22 : 07:01:18
|
| COOLthat is what I was afterCan you explain to me what this meansORDER BY CASE PageID WHEN 0 THEN 1 ELSE 0 END, StatusID, PageID |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-07-22 : 07:15:07
|
All records with PageID = 0 should be sorted last (group 1) and all other PageID should be sorted first (group 1).Within the groups, records are sorted by PageID and StatusID. E 12°55'05.25"N 56°04'39.16" |
 |
|
|
monfu
Yak Posting Veteran
81 Posts |
Posted - 2008-07-22 : 07:23:34
|
| Thanks PesoRegardsJohann |
 |
|
|
|