需要使用来自一个查询的信息来指示对另一个查询的操作

时间:2013-05-29 18:06:07

标签: sql-server function aggregate-functions jointable

好的,这听起来有点复杂。我想以某种方式放置某种函数,当它的策略号与另一个表中的策略号匹配时,将表值除以2。 这是我想要那些函数的查询

SELECT 
qryReinsuranceDPA1_izzy.POLICY_NO, 
qryReinsuranceDPA1_izzy.PHASE_CODE, 
qryReinsuranceDPA1_izzy.SUB_PHASE_CODE, 
qryReinsuranceDPA1_izzy.SchedNP,
qryReinsuranceDPA1_izzy.ProdType, 
Sum(qryReinsuranceDPA1_izzy.SumOfAMOUNT_INFORCE) AS SumOfSumOfAMOUNT_INFORCE, 
Sum(qryReinsuranceDPA1_izzy.SumOfPUA_FACE) AS SumOfSumOfPUA_FACE, 
Sum(qryReinsuranceDPA1_izzy.SumOfOYT_FACE) AS SumOfSumOfOYT_FACE, TotalDPA = sum(case when qryReinsuranceDPA1_izzy.[SumOfNetDefExtraAdj] Is Null then qryReinsuranceDPA1_izzy.[SumOfNetDefPremiumAdj] else qryReinsuranceDPA1_izzy.[SumOfNetDefPremiumAdj] +qryReinsuranceDPA1_izzy.[SumOfNetDefExtraAdj] end), 
qryReinsuranceDPA1_izzy.SumOfGROSS_ANNLZD_PREM, 
qryReinsuranceDPA1_izzy.SumOfStatNetPremium, 
qryReinsuranceDPA1_izzy.[SumOfStatNetPremium]/qryReinsuranceDPA1_izzy.[SumOfGROSS_ANNLZD_PREM] AS NetToGrossRatio, 
qryReinsuranceDPA1_izzy.NetvsGrossInd, 
DPA_NetPrem = case when qryReinsuranceDPA1_izzy.[NetvsGrossInd]='Net' then sum(qryReinsuranceDPA1_izzy.[SumOfStatNetPremium]) else sum([qryReinsuranceDPA1_izzy].[SumOfGROSS_ANNLZD_PREM]) end, 
qryPolicyListforNYDefPRemAsset_Re.ReinType AS ReinType, 
Sum(qryPolicyListforNYDefPRemAsset_Re.ReinsAmount) AS SumOfReinsAmount, 
qryPolicyListforNYDefPRemAsset_Re.[ReinsAmount]/(qryReinsuranceDPA1_izzy.[SumOfAMOUNT_INFORCE]+qryReinsuranceDPA1_izzy.[SumOfOYT_FACE]+qryReinsuranceDPA1_izzy.[SumOfPUA_FACE]) AS [Reins%], 
qryPolicyListforNYDefPRemAsset_Re.ReinsStatRsv AS ReinsStatRsv, 
Sum(qryPolicyListforNYDefPRemAsset_Re.SumOfGROSS_ANNLZD_PREM) AS ReinsPrem, qryReinsuranceDPA1_izzy.PAID_TO_DATE, 
qryReinsuranceDPA1_izzy.VAL_DATE, 
qryReinsuranceDPA1_izzy.ISSUE_DATE


FROM qryPolicyListforNYDefPRemAsset_Re  RIGHT JOIN 
qryReinsuranceDPA1_izzy
ON 
qryReinsuranceDPA1_izzy.POLICY_NO = qryPolicyListforNYDefPRemAsset_Re.POLICY_NO   
 AND qryReinsuranceDPA1_izzy.PHASE_CODE = qryPolicyListforNYDefPRemAsset_Re.PHASE_CODE AND  
qryReinsuranceDPA1_izzy.SUB_PHASE_CODE= qryPolicyListforNYDefPRemAsset_Re.SUB_PHASE_CODE 
GROUP BY 
qryReinsuranceDPA1_izzy.POLICY_NO, 
qryReinsuranceDPA1_izzy.PHASE_CODE, 
qryReinsuranceDPA1_izzy.SUB_PHASE_CODE,
qryReinsuranceDPA1_izzy.SchedNP, 
qryReinsuranceDPA1_izzy.ProdType, 
qryReinsuranceDPA1_izzy.SumOfGROSS_ANNLZD_PREM, 
qryReinsuranceDPA1_izzy.SumOfStatNetPremium, 
qryReinsuranceDPA1_izzy.[SumOfStatNetPremium]/qryReinsuranceDPA1_izzy.[SumOfGROSS_ANNLZD_PREM], 
qryReinsuranceDPA1_izzy.NetvsGrossInd, 
qryPolicyListforNYDefPRemAsset_Re.ReinType, 
qryPolicyListforNYDefPRemAsset_Re.[ReinsAmount]/(qryReinsuranceDPA1_izzy.[SumOfAMOUNT_INFORCE]+qryReinsuranceDPA1_izzy.[SumOfOYT_FACE]+qryReinsuranceDPA1_izzy.[SumOfPUA_FACE]), 
qryPolicyListforNYDefPRemAsset_Re.ReinsStatRsv,
 qryReinsuranceDPA1_izzy.PAID_TO_DATE, 
 qryReinsuranceDPA1_izzy.VAL_DATE, 
 qryReinsuranceDPA1_izzy.ISSUE_DATE

GO

这是包含我想要的政策号码的qry在上面的qry中除以2。

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

快速举例。假设策略号064543200位于第二个qry的结果中。然后,我想要将与该政策编号相关的总DPA中的第一个qry中的数字除以2。

如果这仍然令人困惑,请告诉我,我会尝试更好地解释。两者都是观点。

0 个答案:

没有答案