绑定两个select语句

时间:2014-02-05 12:01:17

标签: sql oracle select join subquery

我有两个oracle select statememts,从同一个表中选择数据。

SELECT   empid, empname, days, SUM (amount) Amount1
FROM tbl_details
WHERE particulars IN ('Basic Pay', 'Allowances')
GROUP BY empid, empname, days

SELECT   empid, empname, days, SUM (amount) Amount2
FROM tbl_details
WHERE particulars IN ('Water Charge', 'Housing Allowance') 
GROUP BY empid, empname,tdays

我想将它绑定到一个select语句,以便我得到一个resulatant,列有empid,empname,days,Amount1,Amount2

有人可以帮我这样做吗? 提前谢谢。

3 个答案:

答案 0 :(得分:1)

使用UNION

SELECT   empid, empname, days, SUM (amount) Amount
FROM tbl_details
WHERE particulars IN ('Basic Pay', 'Allowances')
GROUP BY empid, empname, days
UNION
SELECT   empid, empname, days, SUM (amount) Amount
FROM tbl_details
WHERE particulars IN ('Water Charge', 'Housing Allowance') 
GROUP BY empid, empname,tdays

默认情况下,如果您希望将重复记录替换为UNION

,联合将消除重复记录

答案 1 :(得分:1)

尝试联盟

SELECT   empid, empname, days, SUM (amount) Amount1
FROM tbl_details
WHERE particulars IN ('Basic Pay', 'Allowances')
GROUP BY empid, empname, days

UNION

SELECT   empid, empname, days, SUM (amount) Amount2
FROM tbl_details
WHERE particulars IN ('Water Charge', 'Housing Allowance') 
GROUP BY empid, empname,tdays

答案 2 :(得分:1)

我认为下面会帮助你

SELECT empid,
   empname,
   days,
   Sum (CASE
          WHEN particulars IN ( 'Water Charge', 'Housing Allowance' ) THEN
          amount
          ELSE 0
        END) Amount2,
   Sum (CASE
          WHEN particulars IN ( 'Basic Pay', 'Allowances' ) THEN 
          amount
          ELSE 0
        END) Amount1
FROM   tbl_details
GROUP  BY empid,
      empname,
      days