如何将三个查询的结果集合并在同一个表horizantaly组中并按顺序排列

时间:2015-10-14 13:07:26

标签: sql

第一个查询和结果

select distinct USERID, min(JOBDATE) AS 'DATA_INSERT_DATE', FILENAME, LOTNO, count(*) as 'TOTAL' 
from CF10_BULK_RCPT_RMK  
group by USERID, FILENAME, LOTNO  
order by USERID, FILENAME, LOTNO

| USERID  | DATA_INSERT_DATE  | FILENAME |LOTNO| TOTAL |
| 5023294 | 09/30/15 07:14 PM | File3.xls|  3  | 4239  |
| 5023294 | 09/30/15 07:39 PM | File2.xls|  2  | 5     | 
| 5023294 | 09/30/15 07:52 PM | File1.xls|  1  | 4498  |

第二个查询和结果

select count(1) AS 'SUCCESS' 
from CF10_BULK_RCPT_RMK 
where STATUS = 'Completed'
group by LOTNO
order by LOTNO

|SUCCESS|
|4239   |
|  5    |
| 4498  |

第三次查询和结果

select count(1) AS 'FAILED' 
from CF10_BULK_RCPT_RMK 
where STATUS = 'Failed'
group by LOTNO
order by LOTNO

|FAILED |

我希望将所有查询的结果水平组合为

| USERID  | DATA_INSERT_DATE  | FILENAME |LOTNO| TOTAL |SUCCESS|FAILED |
| 5023294 | 09/30/15 07:14 PM | File3.xls|  3  | 4239  |4239   |  0    |    
| 5023294 | 09/30/15 07:39 PM | File2.xls|  2  | 5     |  5    |  0    |  
| 5023294 | 09/30/15 07:52 PM | File1.xls|  1  | 4498  | 4498  |  0    |

1 个答案:

答案 0 :(得分:0)

如何在以下内容中添加CASE

select distinct USERID,min(JOBDATE) AS 
'DATA_INSERT_DATE',FILENAME,LOTNO,count(*) as 'TOTAL',
SUM(case when STATUS = 'Completed' then count(1) end) as 'SUCCESS',
SUM(case when STATUS = 'Failed' then count(1) end) as 'FAILED'
from CF10_BULK_RCPT_RMK  
group by USERID,FILENAME,LOTNO  
order by USERID,FILENAME,LOTNO