Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to join two SELECT queries

/* Process 01 */
Select Fld1, Fld2, Fld3, Fld4
From Table01
Where xDate = convert(date, '20240101', 112) as P01

/* Process 02 */
Select Fld1, Fld2, Fld3, Fld4, 1 as Late
From Table01
Where xDate < Convert(date, '20241201', 112) as P02
    
-- Combined Process

In Combine Process, I want to join P01 & P02 queries. How can I join two queries given above?

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Here are two queries using a UNION or UNION ALL

/* Combine Process */
SELECT Fld1, Fld2, Fld3, Fld4, NULL AS Late
FROM Table01
WHERE xDate = CONVERT(date, '20240101', 112)

UNION ALL

SELECT Fld1, Fld2, Fld3, Fld4, 1 AS Late
FROM Table01
WHERE xDate < CONVERT(date, '20241201', 112);

Please find below the difference between UNION vs UNION ALL:

  • Use UNION if you want to remove duplicate rows.
  • Use UNION ALL to include all rows, even if duplicates exist.

Output:

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading