日期:2014-05-16  浏览次数:20970 次

SQL问题,谢谢!!!
Select Decode(A.IS_QUITWORK, '1', '在职', '2', '退休', '合计') As GROUPTYPE, /*人员类型*/
  (SELECT Count(Distinct C.ID_CARD)
  FROM V_M_ZF_COMBINATION_STAT C
  WHERE A.IS_QUITWORK = C.IS_QUITWORK
  AND C.ST_DATE >= '20040901'
  AND C.ST_DATE <= '20040902') As PSNCOUNT /*总人数*/
  From M_ZF_COMBINATION_STAT_SUMMARY A
 Where a.ST_DATE >= '20040901'
  And a.ST_DATE <= '20040902'
 Group By Rollup(A.IS_QUITWORK)
 Order By Decode(A.IS_QUITWORK, Null, 0)
  GROUPTYPE PSNCOUNT 
1 合计 0
2 在职 1658
3 退休 6012
其中合计行为0,应为7669,怎么修改SQL语句,谢谢!!!

------解决方案--------------------
提供一个思路
----------------------------------------------

with tmp as (
 select '1' IS_QUITWORK, 1 ID_CARD from dual
 union all
 select '2',1 from dual
 union all
 select '1',2 from dual
union all
 select '1',3 from dual
union all
 select '1',1 from dual
 )
 select Decode(IS_QUITWORK, '1', '在职', '2', '退休', '合计') As GROUPTYPE, 
count(distinct IS_QUITWORK||to_char(ID_CARD)) 
from tmp
group by 
cube(IS_QUITWORK)
order by grouping_id(IS_QUITWORK) asc

GROUPTYPE PSNCOUNT
--------- ----------
在职 3
退休 1
合计 4

3 rows selected.



------解决方案--------------------
SELECT DECODE (A.IS_QUITWORK, '1', '在职', '2', '退休', '合计') GROUPTYPE,
COUNT (1) PSNCOUNT
FROM M_ZF_COMBINATION_STAT_SUMMARY a, V_M_ZF_COMBINATION_STAT c
WHERE A.IS_QUITWORK = C.IS_QUITWORK
AND C.ST_DATE >= '20040901'
AND C.ST_DATE <= '20040902'
AND A.ST_DATE >= '20040901'
AND A.ST_DATE <= '20040902'
GROUP BY ROLLUP (A.IS_QUITWORK)

关联没错的话,就没有问题