Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9139567
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:24:16+00:00 2026-06-17T09:24:16+00:00

I have to merge data from 3 different tables which have different columns and

  • 0

I have to merge data from 3 different tables which have different columns and my desired output should be based on different sort condition of each table.

I am using this query below but this query sort the overall result on based on Menu Position, How can I change this query to sort the result on individual query

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

Second Way

If I use below query then I get following error

Msg 156, Level 15, State 1, Line 4
Incorrect syntax near the keyword ‘UNION’.
Msg 156, Level 15, State 1, Line 10
Incorrect syntax near the keyword ‘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  

SAMPLE DATA

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

I want first query to sort by ORDER BY PageLinkPosition DESC

Second query to sort by ORDER BY c.ArticleCategoryName ASC

and third query to sort by ORDER BY IssueCode DESC

I tried so many way but could not get it work properly.

Appreciate help in this regard

UPDATE: Working Query based on Mark Solution with small changes

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 update his solution to CTE version please check his answer.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T09:24:17+00:00Added an answer on June 17, 2026 at 9:24 am

    This should work;

    ;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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two datasets each with one data table pulled from different sources and
I have two data tables which have a primary key column in common but
I have imported data relating to about 70 human subjects from three data tables
I have 2 tables with similar, but different data. Both tables have an insert
I am supposed to write an XQuery based SQL script to merge data from
I have 2 sets of data from different systems. About 20,000 records a piece.
I have some C#/Linq code used to merge data from excel file into db,
I have two tables - Keys and KeysTemp . KeysTemp contains temporary data which
I have a data frame in which each column is a time series of
I have a large data set and I want to write a custom merge

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.