sql - UNION ALL and inner join in the other side

315 views Asked by At

I want queries that want to achieve where all the same value with this my inner join should be their only value as the first value column, in the picture here:

enter image description here

and I just want to be output is the red line in the picture.

here is my queries:

  SELECT it.Date AS 'Date', it.invoice AS 'Invoice No.', 
         it.company AS 'Company', it.total_amount AS 'Total Amount'
  FROM invoicesummary_tbl it
  WHERE it.total !=0 
  UNION ALL
  SELECT itt.Date AS 'Date',rt.revises_no AS 'Invoice_No.', itt.company AS 
  'Company', 
       itt.total_amount AS 'Total Amount'   
  FROM revises_tbl rt
  INNER JOIN invoicesummary_tbl itt
  ON itt.invoice=rt.invoice
2

There are 2 answers

0
lubilis On BEST ANSWER

Use DISTINCT keyword:

SELECT DISTINCT *
  FROM (SELECT it.Date AS 'Date',
               it.invoice AS 'Invoice No.',
               it.company AS 'Company',
               it.total_amount AS 'TotalAmount'
          FROM invoicesummary_tbl it
         WHERE it.total != 0
        UNION ALL
        SELECT itt.Date AS 'Date',
               rt.revises_no AS 'Invoice_No.',
               itt.company AS 'Company',
               itt.total_amount AS 'Total Amount'
          FROM revises_tbl rt
               INNER JOIN invoicesummary_tbl itt ON itt.invoice = rt.invoice
) AS a
0
Trung Duong On

I guess that data be duplicated by your second query. You could try

 SELECT it.Date AS 'Date', it.invoice AS 'Invoice No.', 
         it.company AS 'Company', it.total_amount AS 'Total Amount'
  FROM invoicesummary_tbl it
  WHERE it.total !=0 
  UNION ALL
  SELECT DISTINCT itt.Date AS 'Date',rt.revises_no AS 'Invoice_No.', itt.company AS 
  'Company', itt.total_amount AS 'Total Amount'   
  FROM revises_tbl rt
  INNER JOIN invoicesummary_tbl itt
  ON itt.invoice = rt.invoice