question

AbhilashPullakanti-0721 avatar image
0 Votes"
AbhilashPullakanti-0721 asked LiHongMSFT-3908 answered

Combine 2 sql quest which as declare statements

Hello everyone,

I have below SQL Queries

Query - 1

203039-sql-1.jpg


Query - 2


How can join both queries



203107-sql-2.jpg


sql-server-transact-sql
sql-1.jpg (74.6 KiB)
sql-2.jpg (95.4 KiB)
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.


Did you find a difference between Query 1 and Query 2?


1 Vote 1 ·

Hi @Viorel-1 i have updated the second Query 2.

0 Votes 0 ·

Hi @AbhilashPullakanti-0721
IMHO, aren't these two queries the same?

Best regards,
LiHong

0 Votes 0 ·
JingyangLi avatar image
0 Votes"
JingyangLi answered AbhilashPullakanti-0721 edited

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
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Since both queries have TOP 100, maybe the ORDER BY should not be removed. Then again, we know very little of what Abhilash mean with "join both queries".

What absolutely should be removed, though, are all those NOLOCK hints.

0 Votes 0 ·

@JingyangLi @ErlandSommarskog thanks for the reply Top 100 is just for filtering the data, in original queries top 100 won't bet there.
I want to create 2 CTE and then merge both tables into one.
Apologies i have only screenshots of the queries.

0 Votes 0 ·
OlafHelper-2800 avatar image
0 Votes"
OlafHelper-2800 answered

I have below SQL Queries

That are not queries, that are screenshots.
Please post the SQL queries as code here.
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

LiHongMSFT-3908 avatar image
0 Votes"
LiHongMSFT-3908 answered

Hi @AbhilashPullakanti-0721

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

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.