DB2中OLAP函数使用示例
下面的需求是将不仅获取查询的结果集,还要将结果集的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