与UNION一起使用时,每个SQL Server查询的排序都不起作用

时间:2013-01-09 05:48:48

标签: sql sql-server sql-server-2008 tsql greatest-n-per-group

我必须合并来自3个不同列的数据,这些表有不同的列,我想要的输出应该基于每个表的不同排序条件。

我在下面使用此查询,但此查询根据菜单位置对整体结果进行排序,如何更改此查询以对单个查询的结果进行排序

SELECT DISTINCT 
     PID, MENU, Handler, PageLangID, ParentID, IssueID, CatID, MenuPosition 
FROM (
   --Pages Table 
   SELECT TOP 50 
       PageId AS PID,SUBSTRING(PageName,0,20) AS MENU,SUBSTRING(PageInternalLinkURL,0,24) AS Handler, PageLangID,PageInheritance AS ParentID, 1 AS IssueID, 1 AS CatID, 
        PageLinkPosition as MenuPosition  FROM pg_Pages  WHERE PageLangID = 1  ORDER BY PageLinkPosition DESC 
UNION 
--Article Category Table
SELECT TOP 20 p.PageID AS PID, SUBSTRING(c.ArticleCategoryName,0,20) AS MENU,SUBSTRING(p.PageInternalLinkURL,0,24) AS Handler, 
c.LangID,p.PageID,1, ArticleCategoryID, 1 AS MenuPosition FROM art_Category c JOIN pg_Pages p 
ON c.PageID = p.PageID  JOIN art_CategoryType ct ON c.Type = ct.CategoryTypeID WHERE c.LangID =1  AND c.Type =1 
ORDER BY c.ArticleCategoryName  
UNION 
--Magazine Table
SELECT TOP 10 p.PageID AS PID, CAST(IssueCode AS varchar(10)),SUBSTRING(p.PageInternalLinkURL,0,24) AS Handler,LangID,p.PageID AS ParentID, m.IssueID AS IssueID, 1, 
       1 AS MenuPosition FROM Magazine m   JOIN pg_pages p ON m.PageID = p.PageID   WHERE LangID =1  ORDER BY 2 DESC  
) AS T  WHERE T.PageLangID = 1 ORDER BY MenuPosition

第二种方式

如果我使用以下查询,那么我会收到以下错误

  

Msg 156,Level 15,State 1,Line 4
  关键字'UNION'附近的语法不正确。
  消息156,15级,状态1,行10   关键字“UNION”附近的语法不正确。

QUERY

--Pages Table 
SELECT TOP 50 PageId AS PID,SUBSTRING(PageName,0,20) AS MENU,SUBSTRING(PageInternalLinkURL,0,24) AS Handler, PageLangID,PageInheritance AS ParentID, 1 AS IssueID, 1 AS CatID, 
        PageLinkPosition as MenuPosition  FROM pg_Pages  WHERE PageLangID = 1  ORDER BY PageLinkPosition DESC 
UNION 
--Article Category Table
SELECT TOP 20 p.PageID AS PID, SUBSTRING(c.ArticleCategoryName,0,20) AS MENU,SUBSTRING(p.PageInternalLinkURL,0,24) AS Handler, 
c.LangID,p.PageID,1, ArticleCategoryID, 1 AS MenuPosition FROM art_Category c JOIN pg_Pages p 
ON c.PageID = p.PageID  JOIN art_CategoryType ct ON c.Type = ct.CategoryTypeID WHERE c.LangID =1  AND c.Type =1 
ORDER BY c.ArticleCategoryName  ASC
UNION 
--Magazine Table
SELECT TOP 10 p.PageID AS PID, CAST(IssueCode AS varchar(10)),SUBSTRING(p.PageInternalLinkURL,0,24) AS Handler,LangID,p.PageID AS ParentID, m.IssueID AS IssueID, 1, 
       1 AS MenuPosition FROM Magazine m   JOIN pg_pages p ON m.PageID = p.PageID   WHERE LangID =1  ORDER BY 2 DESC  

示例数据

PID         MENU                 Handler                  PageLangID  ParentID    IssueID     CatID       MenuPosition
----------- -------------------- ------------------------ ----------- ----------- ----------- ----------- ------------
6           Book Review          Page.aspx                1           6           1           18          1
6           Business             Page.aspx                1           6           1           16          1
6           Culture              Page.aspx                1           6           1           3           1
6           Economy              Page.aspx                1           6           1           2           1
6           Finance              Page.aspx                1           6           1           19          1
6           Infrastructure       Page.aspx                1           6           1           17          1
6           Lifestyle            Page.aspx                1           6           1           20          1
6           Others               Page.aspx                1           6           1           21          1
6           People               Page.aspx                1           6           1           7           1
6           Politics             Page.aspx                1           6           1           1           1
6           Sports               Page.aspx                1           6           1           4           1
12          102                  Page.aspx                1           12          3           1           1
12          103                  Page.aspx                1           12          4           1           1
12          106                  Page.aspx                1           12          1           1           1
12          109                  Page.aspx                1           12          5           1           1
1           Home                 Default.aspx             1           0           1           1           10
11          Video                Videos.aspx              1           10          1           1           10
2           About Us             Page.aspx                1           0           1           1           20
5           Articles             Articles.aspx            1           0           1           1           30
6           Categories           Page.aspx                1           0           1           1           40
12          Archive              Page.aspx                1           0           1           1           50
3           News                 News.aspx                1           0           1           1           60
10          Multimedia           Multimedia.aspx          1           0           1           1           70

我希望第一个查询按ORDER BY PageLinkPosition DESC

排序

ORDER BY c.ArticleCategoryName ASC

排序的第二个查询

和第三个查询按ORDER BY IssueCode DESC

排序

我尝试了很多方法,但无法使其正常工作。

在这方面获得帮助

更新:基于Mark解决方案的工作查询,只需很少的更改

SELECT DISTINCT PID, MENU, Handler, PageLangID, ParentID, IssueID, CatID, MenuPosition, block, rNum
FROM    
(   --Pages Table 
    SELECT TOP 50 
        PageId AS PID,SUBSTRING(PageName,0,20) AS MENU,SUBSTRING(PageInternalLinkURL,0,24) AS Handler, PageLangID,PageInheritance AS ParentID, 1 AS IssueID, 1 AS CatID, 
        PageLinkPosition as MenuPosition
        ,Block = 1
        ,rNum = ROW_NUMBER() OVER (ORDER BY PageLinkPosition DESC)  
    FROM pg_Pages  
    WHERE PageLangID = 1  
    UNION ALL   -- Better to UNION ALL and then DISTINCT outside
--Article Category Table
    SELECT TOP 20 
        p.PageID AS PID, SUBSTRING(c.ArticleCategoryName,0,20) AS MENU,SUBSTRING(p.PageInternalLinkURL,0,24) AS Handler, 
        c.LangID,p.PageID,1, ArticleCategoryID, 1 AS MenuPosition
        ,Block = 2 
        ,rNum = ROW_NUMBER() OVER (ORDER BY c.ArticleCategoryName )
    FROM art_Category c 
    JOIN pg_Pages p             ON c.PageID = p.PageID  
    JOIN art_CategoryType ct    ON c.Type = ct.CategoryTypeID 
    WHERE c.LangID =1  AND c.Type =1 
    UNION ALL
--Magazine Table
    SELECT TOP 10 p.PageID AS PID, CAST(IssueCode AS varchar(10)),SUBSTRING(p.PageInternalLinkURL,0,24) AS Handler,LangID,p.PageID AS ParentID, m.IssueID AS IssueID, 1, 
       1 AS MenuPosition
       ,Block = 3 
       ,rNum = ROW_NUMBER() OVER (ORDER BY CAST(IssueCode AS varchar(10))DESC)
    FROM Magazine m   
    JOIN pg_pages p ON m.PageID = p.PageID   
    WHERE LangID =1  
) AS T  WHERE T.PageLangID = 1 
ORDER BY Block, rNum

MARK Later更新他对CTE版本的解决方案请检查他的答案。

1 个答案:

答案 0 :(得分:2)

这应该有用;

;WITH CTE AS
(
    SELECT TOP 50 
         PageId AS PID
        ,SUBSTRING(PageName,0,20) AS MENU
        ,SUBSTRING(PageInternalLinkURL,0,24) AS Handler
        ,PageLangID
        ,PageInheritance AS ParentID
        ,1 AS IssueID
        ,1 AS CatID
        ,PageLinkPosition as MenuPosition
        ,Block = 1
        ,rNum = ROW_NUMBER() OVER (ORDER BY PageLinkPosition DESC)  
    FROM pg_Pages  
    WHERE PageLangID = 1  
    UNION ALL   
--Article Category Table
    SELECT TOP 20 
         p.PageID AS PID
        ,SUBSTRING(c.ArticleCategoryName,0,20) AS MENU
        ,SUBSTRING(p.PageInternalLinkURL,0,24) AS Handler
        ,c.LangID
        ,p.PageID
        ,1
        ,ArticleCategoryID
        ,1 AS MenuPosition
        ,Block = 2 
        ,rNum = ROW_NUMBER() OVER (ORDER BY c.ArticleCategoryName )
    FROM art_Category c 
    JOIN pg_Pages p             ON c.PageID = p.PageID  
    JOIN art_CategoryType ct    ON c.Type = ct.CategoryTypeID 
    WHERE c.LangID =1  AND c.Type =1 
    UNION ALL
--Magazine Table
    SELECT TOP 10 
         p.PageID AS PID
        ,CAST(IssueCode AS varchar(10))
        ,SUBSTRING(p.PageInternalLinkURL,0,24) AS Handler
        ,LangID
        ,p.PageID AS ParentID
        ,m.IssueID AS IssueID
        ,1
        ,1 AS MenuPosition
        ,Block = 3 
        ,rNum = ROW_NUMBER() OVER (ORDER BY CAST(IssueCode AS varchar(10)))
    FROM Magazine m   
    JOIN pg_pages p ON m.PageID = p.PageID   
    WHERE LangID =1  
)
,Deduplicate AS
(
    SELECT   PID
            ,MENU
            ,Handler
            ,PageLangID
            ,ParentID
            ,IssueID
            ,CatID
            ,MenuPosition 
            ,Block
            ,rNum
            ,Occ = ROW_NUMBER() OVER (PARTITION BY PID, MENU, Handler, PageLangID, ParentID, IssueID, CatID, MenuPosition ORDER BY (SELECT NULL))
    FROM CTE
)
SELECT  PID
       ,MENU
       ,Handler
       ,PageLangID
       ,ParentID
       ,IssueID
       ,CatID
       ,MenuPosition 
FROM Deduplicate
WHERE Occ = 1
ORDER BY Block, rNum