日期:2014-05-17  浏览次数:20777 次

SQL求大侠指教
本帖最后由 yimiyan 于 2014-03-05 13:37:32 编辑

------解决方案--------------------
SELECT 1 ID,'1001' usercode,'2013-03-05' updatecode FROM dual UNION ALL
SELECT 2,'1002','2014-03-05' FROM dual UNION ALL 
SELECT 3,'1001','2014-03-05' FROM dual UNION ALL 
SELECT 4,'1005','2014-03-01' FROM dual ),
 b AS(
SELECT 1 ID,'1003' usercode,'2014-03-05' createdate FROM dual UNION ALL
SELECT 2,'1002','2014-03-05' FROM dual UNION ALL 
SELECT 3,'1004','2014-03-05' FROM dual UNION ALL 
SELECT 4,'1005','2014-03-05' FROM dual UNION ALL 
SELECT 5,'1005','2014-03-05' FROM dual )
SELECT DECODE(C.USERCODE, NULL, D.USERCODE, C.USERCODE),
       DECODE(C.ANUM, NULL, 0, C.ANUM),
       DECODE(D.BNUM, NULL, 0, D.BNUM)
  FROM ((SELECT USERCODE, COUNT(1) ANUM FROM A GROUP BY USERCODE) C FULL JOIN
        (SELECT USERCODE, COUNT(1) BNUM FROM B GROUP BY USERCODE) D ON
        C.USERCODE = D.USERCODE)
 ORDER BY DECODE(C.USERCODE, NULL, D.USERCODE, C.USERCODE)


DECODE(C.USERCODE,NULL,D.USERC DECODE(C.ANUM,NULL,0,C.ANUM) DECODE(D.BNUM,NULL,0,D.BNUM)
------------------------------ ---------------------------- ----------------------------
1001                                                      2                            0
1002                                                      1                            1
1003                                                      0                            1
1004                                                      0