Combine 2 sql quest which as declare statements

Abhilash Pullakanti 41 Reputation points
2022-05-18T06:34:31.867+00:00

Hello everyone,

I have below SQL Queries

Query - 1

203039-sql-1.jpg

Query - 2

How can join both queries

203107-sql-2.jpg

Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,552 questions
{count} votes

Accepted answer
  1. Jingyang Li 5,891 Reputation points
    2022-05-18T17:04:41.49+00:00

    You can use UNION/INTERSECT/EXCEPT to combine your two queries but we don't have more information to help.
    Screenshots of your queries are not a good way to ask question.

    By looking at your query1 and query2,
    you can remove the ORDER BY from your query1 and use UNION (ALL) to combine from there.

    If you want more help, you can mark up your sample tables DDL/data and your expected result from your queries.


2 additional answers

Sort by: Most helpful
  1. Olaf Helper 40,741 Reputation points
    2022-05-18T07:31:14.22+00:00

    I have below SQL Queries

    That are not queries, that are screenshots.
    Please post the SQL queries as code here.

    0 comments No comments

  2. LiHong-MSFT 10,046 Reputation points
    2022-05-19T07:11:36.337+00:00

    Hi @Abhilash Pullakanti

    I want to create 2 CTE and then merge both tables into one.

    Do you mean this ?

    DECLARE @WorkbookViewID INT = 2,......  
       
    ;WITH CTE1 AS  
    (  
     SELECT COLUMN_LIST,ROW_NUMBER()OVER(ORDER BY SKU,ProjectionDate)AS RN  
     FROM IMB JOIN IM ON ...  
              JOIN LP ON ...  
     WHERE ...  
    ),CTE2 AS  
    (  
     SELECT COLUMN_LIST,ROW_NUMBER()OVER(ORDER BY SKU,ProjectionDate)AS RN  
     FROM IMB JOIN IM ON ...  
              JOIN LP ON ...  
     WHERE ...  
    )  
    SELECT COLUMN_LIST FROM CTE1 WHERE RN <= 100  
    UNION   
    SELECT COLUMN_LIST FROM CTE2 WHERE RN <= 100  
    ORDER BY SKU,ProjectionDate  
    

    Best regards,
    LiHong

    0 comments No comments