0

異なる列を持つ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

第二の方法

以下のクエリを使用すると、次のエラーが発生します

メッセージ156、レベル15、状態1、行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

並べ替える2番目のクエリORDER BY c.ArticleCategoryName ASC

並べ替える3番目のクエリORDER BY IssueCode DESC

いろいろ試してみましたが、うまくいきませんでした。

この点で助けに感謝します

更新:小さな変更を加えたMarkSolutionに基づく作業クエリ

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

マーク後で彼のソリューションをCTEバージョンに更新してください。彼の答えを確認してください。

4

1 に答える 1

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
于 2013-01-09T06:02:58.723 に答える