下面的需求是將不只獲取查詢的結果集,還要將結果集的SIZE返回。結果集的SIZE是沒法經過CURSOR獲取的。
一般聚合函數在二種狀況下,使用默認分組即沒有分組。能夠直接使用,好比
select
COUNT(1) as toalrows
FROM FTP.NBZZ_KHLR_NEW_201312 a,
DB2ADMIN.JAF_OM_EMPLOYEE b,
FTP.ORG_EMP_HISTORY c
WHERE a.ZH = '7271010103100076601'
AND a.OP_ID = b.userid
AND b.EMPID = c.EMPID
AND c.HISDATE = '2013-12-31';
或者
SELECT a.OP_ID,
a.FHLRHZ,
c.ORGNAME_LEVEL2,
c.ORGNAME_LEVEL3,
c.ORGSEQ,
a.kmh,
COUNT(1) as toalrows
FROM FTP.NBZZ_KHLR_NEW_201312 a,
DB2ADMIN.JAF_OM_EMPLOYEE b,
FTP.ORG_EMP_HISTORY c
WHERE a.ZH = '7271010103100076601'
AND a.OP_ID = b.userid
AND b.EMPID = c.EMPID
AND c.HISDATE = '2013-12-31';
35000164 52560.00 客戶四部 .citic.1.2221.2266. 1210100 1
35000414 9855.00 客戶四部 .citic.1.2221.2266. 1210100 1
35000889 3285.00 客戶四部 .citic.1.2221.2266. 1210100 1
而有些時候需求是查詢結果的同時,不想再寫一條查詢結果集大小的sql. 但願一併能查詢出來。OLAP的提供的count(*) over() as nums能夠解決。
SELECT a.OP_ID,
a.FHLRHZ,
c.ORGNAME_LEVEL2,
c.ORGNAME_LEVEL3,
c.ORGSEQ,
a.kmh,
COUNT(1) OVER () as toalrows
FROM FTP.NBZZ_KHLR_NEW_201312 a,
DB2ADMIN.JAF_OM_EMPLOYEE b,
FTP.ORG_EMP_HISTORY c
WHERE a.ZH = '7271010103100076601'
AND a.OP_ID = b.userid
AND b.EMPID = c.EMPID
AND c.HISDATE = '2013-12-31';
35000164 52560.00 客戶四部 .citic.1.2221.2266. 1210100 3
35000414 9855.00 客戶四部 .citic.1.2221.2266. 1210100 3
35000889 3285.00 客戶四部 .citic.1.2221.2266. 1210100 3
OLAP函數參考:http://www.cnblogs.com/Fskjb/archive/2011/02/28/1967429.html