在子查询中引用主查询

yshpjwxd  于 2021-07-26  发布在  Java
关注(0)|答案(0)|浏览(247)

我创建了一个包含多个cte语句的sql查询。我从这四个表中提取了policynumber和fundvalue:ind、int、ract1和ract3。
下面是代码的最后一部分:

  1. table8_CTE as
  2. (Select PolicyNumber, IND_FundValue, INT_FundValue, RACT1_FundValue,
  3. RACT3_FundValue, FOPER_FundValue, RXXXXV05_FundValue, FDECRSTD_FundValue,
  4. abs(FDECRSTD_FundValue - IND_FundValue) as IND_Diff, abs(FDECRSTD_FundValue-INT_FundValue) as INT_Diff, abs(FDECRSTD_FundValue - RACT1_FundValue) as RACT1_Diff,
  5. abs(FDECRSTD_FundValue - RACT3_FundValue) as RACT3_Diff, abs(FDECRSTD_FundValue - FOPER_FundValue) as FOPER_Diff,
  6. abs(FDECRSTD_FundValue - RXXXXV05_FundValue) as RXXXXV05_Diff
  7. from table7_CTE
  8. --where InvestmentCode > 4
  9. group by PolicyNumber, IND_FundValue, INT_FundValue, RACT1_FundValue, RACT3_FundValue, FOPER_FundValue, RXXXXV05_FundValue, FDECRSTD_FundValue
  10. )
  11. --Only View Policies were Differences Exist
  12. Select distinct PolicyNumber, IND_FundValue, INT_FundValue, RACT1_FundValue,
  13. RACT3_FundValue, FOPER_FundValue, RXXXXV05_FundValue, FDECRSTD_FundValue,
  14. abs(FDECRSTD_FundValue - IND_FundValue) as IND_Diff, abs(FDECRSTD_FundValue-INT_FundValue) as INT_Diff, abs(FDECRSTD_FundValue - RACT1_FundValue) as RACT1_Diff,
  15. abs(FDECRSTD_FundValue - RACT3_FundValue) as RACT3_Diff, abs(FDECRSTD_FundValue - FOPER_FundValue) as FOPER_Diff,
  16. abs(FDECRSTD_FundValue - RXXXXV05_FundValue) as RXXXXV05_Diff
  17. from table8_CTE
  18. where (IND_Diff <> 0 or RACT1_Diff <> 0 or RACT3_Diff <> 0 or FOPER_Diff <> 0 or RXXXXV05_Diff <> 0 or INT_Diff <> 0)
  19. group by PolicyNumber, IND_FundValue, INT_FundValue, RACT1_FundValue, RACT3_FundValue, FOPER_FundValue, RXXXXV05_FundValue, FDECRSTD_FundValue

代码返回所有这些表的fundvalue以及它们之间的差异。
输出如下所示:

  1. PolicyNumber IND_FundValue INT_FundValue RACT1_FundValue RACT3_FundValue
  2. --------------------------------------------------------------------------------
  3. 1309480 18686.10 14880.07 11074.04 11074.04
  4. 813659 281099.22 75649.98 83659.88 339863.98

300000行等
然后,我添加了一个select语句来从源表返回policycount:

  1. Select (select count(1)
  2. from (Select distinct PolicyNumber
  3. from FinancialExtracts.dbo.INDCONEX
  4. where RunDate = '20200531') as IND_PolicyCount
  5. ) as IND_PolicyCount,
  6. (Select count(1)
  7. from (Select distinct PolicyNumber
  8. from FinancialExtracts.dbo.INTCRDEX
  9. where RunDate = '20200531') as INT_PolicyCount
  10. ) as INT_PolicyCount,
  11. (Select count(1)
  12. from (Select distinct PolicyNumber
  13. from FinancialExtracts.dbo.RACT1CR1_01
  14. where RunDate = '20200531') as RACT1_PolicyCount
  15. ) as RACT1_PolicyCount,
  16. (Select count(1)
  17. from (Select distinct PolicyNumber
  18. from FinancialExtracts.dbo.RACT3CR1_01
  19. where RunDate = '20200531') as RACT3_PolicyCount
  20. ) as RACT3_PolicyCount,

我希望我的输出有两个独立的面板,其中有两个不同的结果表。第一个是原始输出。第二个是输出表中所有policynumbers的计数,与原始源表中所有policynumbers的计数进行比较。
如何在select语句中引用主查询输出表,而不添加my cte
我刚刚开始学习sql,所以任何建议都是有用的。谢谢

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题