所以我试图结合查询并做到这一点我需要弄清楚这个是怎么回事。我仍然是相对较新的SQL服务器,我被迫潜入一些复杂的qrys,有时我会陷入这样的简单事情。我的问题是使用Sum(1)函数,我不完全确定如何。意思是我相信它正在计算重复数,但我无法根据它正在做什么信息来判断。
这是查询
SELECT
qryReinsuranceDPA1.POLICY_NO,
qryReinsuranceDPA1.PHASE_CODE,
qryReinsuranceDPA1.SUB_PHASE_CODE,
qryReinsuranceDPA1.ProdType,
TotalDPA = Sum(case when [SumOfNetDefExtraAdj] Is Null then [SumOfNetDefPremiumAdj] else [SumOfNetDefPremiumAdj] + SumOfNetDefExtraAdj end),
Sum(1) AS Expr1
FROM qryPolicyListforNYDefPRemAsset_Re RIGHT JOIN qryReinsuranceDPA1
ON
qryReinsuranceDPA1.POLICY_NO = qryPolicyListforNYDefPRemAsset_Re.POLICY_NO AND
qryReinsuranceDPA1.PHASE_CODE= qryPolicyListforNYDefPRemAsset_Re.PHASE_CODE AND
qryReinsuranceDPA1.SUB_PHASE_CODE = qryPolicyListforNYDefPRemAsset_Re.SUB_PHASE_CODE
GROUP BY qryReinsuranceDPA1.POLICY_NO,
qryReinsuranceDPA1.PHASE_CODE,
qryReinsuranceDPA1.SUB_PHASE_CODE,
qryReinsuranceDPA1.ProdType
--HAVING (((Sum(1))<>1))
GO
这是它产生的一小部分样本(实际结果数约为77,000)
POLICY_NO PHASE_CODE SUB_PHASE_CODE ProdType TotalDPA Expr1
228433800 0 1 TERM 282.324223 1
228439200 0 1 PERM 53.17048634 1
228439200 6 1 PERM 10.3805065 1
228441500 0 1 PERM 526.6883742 1
228441500 0 2 PERM 10.63320899 1
228441700 0 1 PERM 20.86247317 1
228448100 0 1 PERM 345.2117169 1
228460200 0 1 TERM 302.7574933 1
228464900 0 1 TERM 191.2597906 1
228468000 0 1 PERM 8445.190912 1
228473600 0 1 TERM 339.8413682 **2**
228473800 0 1 TERM 686.1766864 **2**
228477200 0 1 TERM 583.7580207 1
228481200 0 1 TERM 362.9472595 1
228481200 0 2 PERM 4.217792443 1
228482500 0 1 PERM 1894.303507 1
228482500 1 1 TERM 1312.183889 1
228491600 0 1 TERM 325.0796843 **2**
228494400 0 1 PERM 748.2710255 1
228501000 0 1 TERM 47.78070676 1
228501100 0 1 TERM 47.78070676 1
228501300 0 1 PERM 365.5651862 1
228501300 0 2 PERM 12.20547324 1
228501300 1 1 TERM 706.0961491 1
228501300 1 2 PERM 12.46769547 1
228502000 0 1 PERM 6562.164879 1
228502000 0 2 PERM 184.7741277 1
最右边的列是Sum(1)的结果,我想知道的是它何时以及为什么产生2。
答案 0 :(得分:5)
sum(1)
完全等同于count(*)
- 它返回组内所有行的计数。
因此,对于POLICY_NO,PHASE_CODE,SUB_PHASE_CODE和ProdType中的每一个的给定值,它将返回值2,所选数据集中有两行(在分组之前)。