简体   繁体   中英

How to sort sql query having an union statement without specific section

I have a sql query in which I am trying to get sub groups on the database and bind to a dropdown list in asp.net. On the database side I am using MSSQL server 2008 R2 and I have the below query on it:

SELECT -1   ItemSubGroupId,
'Select Sub Group' AS ItemSubGroupName

    UNION

SELECT ItemSubGroupId,
ItemSubGroupName
FROM dbo.tblSetupItemSubGroup SG
WHERE SG.ItemMainGroupId = 17

and the result is: 结果不排序

Now I want to sort all sub groups by ItemSubGroupName and after I sort it it will give me a result like

     SELECT -1  ItemSubGroupId,
'Select Sub Group' AS ItemSubGroupName

    UNION

SELECT ItemSubGroupId,
ItemSubGroupName
FROM dbo.tblSetupItemSubGroup SG
WHERE SG.ItemMainGroupId = 17
ORDER BY ItemSubGroupName

排序后

After I sort it 'Select Sub Group' name also sorted as displayed. Is there any way to sort it without 'Select Sub Group' section?

It is unclear what you are asking.

I think you may be trying to have 'Select Sub Group' at the top of your list, with the rest of the list sorted alphabetically.

To do that, you would need to do something like:

SELECT -1, 'Select an item', 0
UNION ALL
SELECT 1, '1-test', null
UNION ALL
SELECT 2, '2-test', null
UNION ALL 
SELECT 3, '0-test', null
ORDER BY 3 DESC,2;

The third column has a 0 for the item you want at the top, and NULL for the rest of the rows. The query is then sorted by that column in DESC order, then the 2nd column. This will put the desired item at the top, and retain the alphabetic sort for the remaining items.

在此处输入图片说明

Here is a more concrete example:

CREATE TABLE OrderTest
(
    ID INT NOT NULL
    , Data NVARCHAR(255)
);

INSERT INTO OrderTest (ID, Data) VALUES (1, '1st test');
INSERT INTO OrderTest (ID, Data) VALUES (1, '2nd test');
INSERT INTO OrderTest (ID, Data) VALUES (1, '3rd test');
INSERT INTO OrderTest (ID, Data) VALUES (1, '4th test');

SELECT -1 AS ID, 'Select an item' AS Data, 0 AS OrderByCol
UNION ALL
SELECT ID, Data, NULL AS OrderByCol
FROM OrderTest
ORDER BY OrderByCol DESC, Data;

在此处输入图片说明

You have to order both by creating a subquery

SELECT ItemSubGroupId, ItemSubGroupName
FROM (
    SELECT -1  ItemSubGroupId,
    'Select Sub Group' AS ItemSubGroupName

        UNION

    SELECT ItemSubGroupId,
    ItemSubGroupName
    FROM dbo.tblSetupItemSubGroup SG
    WHERE SG.ItemMainGroupId = 17
) X
ORDER BY 
    CASE ItemSubGroupId WHEN -1 THEN 0 ELSE 1 END,
    ItemSubGroupName

Yo can add a dummy column tag to do order by and use derived table . No need to retrieve order by column tag in final resultset.

SELECT ItemSubGroupId,ItemSubGroupName
FROM
(
 SELECT -1  ItemSubGroupId,
'Select Sub Group' AS ItemSubGroupName, 1 as tag

    UNION

SELECT ItemSubGroupId, 
ItemSubGroupName,2 as tag
FROM dbo.tblSetupItemSubGroup SG
WHERE SG.ItemMainGroupId = 17
) z
ORDER BY tag,ItemSubGroupName

Don't forget to give alias name to derived table , z in code above else syntax error will be thrown by sql server.

Just add an additional "hardcoded" column to your query and then sort by that.

SELECT -1  ItemSubGroupId, 'Select Sub Group' AS ItemSubGroupName, 'A' OrderCol

UNION

SELECT ItemSubGroupId, ItemSubGroupName, 'B' OrderCol
FROM dbo.tblSetupItemSubGroup SG
WHERE SG.ItemMainGroupId = 17
ORDER BY OrderCol, ItemSubGroupName

The simplest way is to use a CASE statement in your ORDER BY

...
ORDER BY
    CASE WHEN ItemSubGroupName = 'Select Sub Group' 
        THEN 0 
        ELSE 1 END

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM