Evgeniy Kleban Evgeniy Kleban - 1 month ago 8
SQL Question

Combine 2 SQL statements different date

I have 2 SQL queries, first one is:

SELECT a1.shop_id, a1.val, a1.descr, a2.title
FROM report_ct_shops a1,
shops a2
WHERE report_id = (SELECT id FROM reports
WHERE is_deleted = 0 AND is_published = 1
AND report_date BETWEEN ? AND ? )
AND a1.component_type_id = 3
AND a1.shop_id = a2.id


The second query is actually the same. If you notice "?", that you will understand that i pass data parameters here. What i want, is add to result statement single column - something like a2.val, which will be shown with different
report_date BETWEEN ? AND ?


Is that possible to combine that and output additional row i want to? Thanks.

Answer

Try this query:

 SELECT a1.shop_id, a1.val, a1.descr, a2.title, dateFrom, dateTo
   FROM report_ct_shops a1,
        shops a2,
        reports a3,
        (select ? as dateFrom, ? as dateTo) dateRange
  WHERE a1.component_type_id = 3
    AND a1.shop_id = a2.id
    and a1.report_id = a3.report_id
    and a3.is_deleted = 0 AND a3.is_published = 1
    AND a3.report_date BETWEEN dateRange.dateFrom AND dateRange.dateFrom