在postgres中加入两个SQL语句

时间:2015-04-09 13:54:55

标签: sql postgresql

我如何将这两个声明加在一起,以便它一起出现?这两项计数是分开进行的,因为它来自两个不同的表格。

SELECT ril.invoice_label_id, ril.invoice_label, ril.invoice_label_code,
fp.price as fee, count(*) as ct, l.link_id
FROM consultation_chl c
INNER JOIN link_service_pct_location l on l.link_id=c.link_id
INNER JOIN medication m ON c.consult_id=m.consult_id
INNER JOIN ref_invoice_label ril ON m.formulary_id = ril.formulary_id
INNER JOIN pharmacy ph ON ph.pharmacy_id = l.id AND l.location_type_id = 3
INNER JOIN formulary f ON f.formulary_id=m.formulary_id
INNER JOIN formulary_price fp ON fp.formulary_id=f.formulary_id
WHERE l.pct_id = 1425
AND l.service_id = 4
AND c.invoice_period = '2015-04-30'
AND ril.section_id=2
AND ril.invoice_label_code in ('MEDTABS','MEDCAPS','DOXYCAPS','DOXYTABS')
AND fp.valid_from <= c.consult_date
AND (fp.valid_to >= c.consult_date OR fp.valid_to IS NULL) GROUP BY ril.invoice_label_id, ril.invoice_label, ril.invoice_label_code,
fp.price, l.link_id

SELECT ril.invoice_label_id, ril.invoice_label, ril.invoice_label_code,
ricf.fee, count(*) as ct, l.link_id
FROM consultation_chl c
INNER JOIN link_service_pct_location l on l.link_id=c.link_id
INNER JOIN medication m ON c.consult_id=m.consult_id
INNER JOIN ref_invoice_label ril ON m.formulary_id = ril.formulary_id
INNER JOIN pharmacy ph ON ph.pharmacy_id = l.id AND l.location_type_id = 3
INNER JOIN formulary f ON f.formulary_id=m.formulary_id
INNER JOIN formulary_price fp ON fp.formulary_id=f.formulary_id
INNER JOIN ref_invoice_consult_fee ricf ON ricf.invoice_label_id = ril.invoice_label_id
WHERE l.pct_id = 1425
AND l.service_id = 4
AND c.invoice_period = '2015-04-30'
AND ril.section_id=2
AND ril.invoice_label_code in ('MEDSUSP15-25','MEDSUSP16-35','MEDSUSP26-35','MEDSUSP36-45','MEDSUSP45+')
AND fp.valid_from <= c.consult_date
AND (fp.valid_to >= c.consult_date OR fp.valid_to IS NULL) GROUP BY ril.invoice_label_id, ril.invoice_label, ril.invoice_label_code,
ricf.fee, l.link_id

2 个答案:

答案 0 :(得分:0)

您应该查看UNION运算符。

SQL UNION Operator at W3schools.com

答案 1 :(得分:0)

由于您选择的列每个都具有相同的名称 您应该能够将选择与中间的“UNION”关键字连接起来。