表1:《科目余额表-个表穿透》

更新日期:2022-05-29

  1. begin
  2. select tt.*,t1.igrade,t1.ccode as ccode2
  3. into #temp_kemuyuechuantou111
  4. from
  5. (
  6. select dbtype,dbName ,companyName,accidname,iyear,iperiod,isbook,ruleversion,rownum,indexname,ccode,jdfx,jsfs,hesuan
  7. from [dbo].[VW_CO_JC_BalanceSheet_zb]
  8. where 1=1
  9. and accidname in ($(合并报表-账套名称))
  10. and iyear=$(合并报表-年度)
  11. and iperiod=$(合并报表-期间)
  12. and isbook='$(合并报表-是否包含未记账)'
  13. $(合并报表_报表行号)
  14. $(合并报表_指标名称)
  15. union all
  16. select dbtype,dbName ,companyName,accidname,iyear,iperiod,isbook,ruleversion,rownum,indexname,ccode,jdfx,jsfs,NULL as hesuan
  17. from [dbo].[VW_CO_JC_Profit_zb]
  18. where 1=1
  19. and accidname in ($(合并报表-账套名称))
  20. and iyear=$(合并报表-年度)
  21. and iperiod=$(合并报表-期间)
  22. and isbook='$(合并报表-是否包含未记账)'
  23. $(合并报表_报表行号)
  24. $(合并报表_指标名称)
  25. )as tt
  26. left join
  27. (
  28. select distinct dbtype,dbName ,companyName,accidname,ccode,igrade,iyear
  29. from CO_JC_Ccode
  30. where 1=1
  31. and accidname in ($(合并报表-账套名称))
  32. and iyear=$(合并报表-年度)
  33. ) as t1 on tt.dbtype=t1.dbtype and tt.accidname=t1.accidname and tt.iyear=t1.iyear and left(t1.ccode,len(tt.ccode))=tt.ccode
  34. where 1=1
  35. --and (case when len(isnull(tt.jdfx,N''))<1 and len(isnull(tt.hesuan,N''))<1 then tt.ccode else t1.ccode end) =t1.ccode
  36. select
  37. cast(tt.iyear as varchar(10))+'年'+cast(tt.iperiod as varchar(4))+'月' as iyperiod,
  38. tt.dbtype,tt.dbName ,tt.companyName,tt.accidname,tt.iyear,tt.iperiod,
  39. accountTypeID,accountTypeName,accountCode,
  40. currencyCode,currencyName,igrade,isdetail,
  41. case when igrade=6 then N' '+accountCode2 when igrade=5 then N' '+accountCode2 when igrade=4 then N' '+accountCode2 when igrade=3 then N' '+accountCode2 when igrade=2 then N' '+accountCode2
  42. else accountCode2 end as accountCode2,
  43. accountName,ccodefx,
  44. case when isdetail=0 then
  45. (case when igrade=6 then N' '+accountName when igrade=5 then N' '+accountName when igrade=4 then N' '+accountName when igrade=3 then N' '+accountName when igrade=2 then N' '+accountName
  46. else accountName end) else (case when igrade=6 then N' '+accountName2 when igrade=5 then N' '+accountName2 when igrade=4 then N' '+accountName2 when igrade=3 then N' '+accountName2 when igrade=2 then N' '+accountName2
  47. else accountName2 end) end as accountName2,
  48. localqc,localjfqc,localdfqc,localjffs,localdffs,localytdjffs,localytddffs,localqm,localjfqm,localdfqm
  49. from #temp_kemuyuechuantou111 as tt
  50. inner join
  51. (
  52. SELECT
  53. dbtype,dbName ,companyName,accidname,iyear,iperiod,isbook,
  54. accountTypeID,accountTypeCode,accountTypeName,calDirection,
  55. --currencyID,accountID,
  56. currencyCode,currencyName,
  57. accountCode,accountName,
  58. (Case When len(isnull(isauxaccdepartmentcode,N''))<1 then N'' Else N' [' + isnull(isauxaccdepartmentcode,N'') + N'] ' End) + ISNULL(isauxaccdepartment, N'')
  59. + (Case When len(isnull(isauxaccpersoncode,N''))<1 then N'' Else N' [' + isnull(isauxaccpersoncode,N'') + N'] ' End) + ISNULL(isauxaccperson, N'')
  60. + (Case When len(isnull(isauxacccustomercode,N''))<1 then N'' Else N' [' + isnull(isauxacccustomercode,N'') + N'] ' End) + ISNULL(isauxacccustomer, N'')
  61. + (Case When len(isnull(isauxaccinventorycode,N''))<1 then N'' Else N' [' + isnull(isauxaccinventorycode,N'') + N'] ' End) + ISNULL(isauxaccinventory, N'')
  62. + (Case When len(isnull(isauxaccprojectcode,N''))<1 then N'' Else N' [' + isnull(isauxaccprojectcode,N'') + N'] ' End) + ISNULL(isauxaccproject, N'')
  63. + (Case When len(isnull(isexauxacc1code,N''))<1 then N'' Else N' [' + isnull(isexauxacc1code,N'') + N'] ' End) + ISNULL(isexauxacc1, N'')
  64. + (Case When len(isnull(isexauxacc2code,N''))<1 then N'' Else N' [' + isnull(isexauxacc2code,N'') + N'] ' End) + ISNULL(isexauxacc2, N'')
  65. + (Case When len(isnull(isexauxacc3code,N''))<1 then N'' Else N' [' + isnull(isexauxacc3code,N'') + N'] ' End) + ISNULL(isexauxacc3, N'')
  66. + (Case When len(isnull(isexauxacc4code,N''))<1 then N'' Else N' [' + isnull(isexauxacc4code,N'') + N'] ' End) + ISNULL(isexauxacc4, N'')
  67. + (Case When len(isnull(isexauxacc5code,N''))<1 then N'' Else N' [' + isnull(isexauxacc5code,N'') + N'] ' End) + ISNULL(isexauxacc5, N'')
  68. + (Case When len(isnull(isexauxacc6code,N''))<1 then N'' Else N' [' + isnull(isexauxacc6code,N'') + N'] ' End) + ISNULL(isexauxacc6, N'')
  69. + (Case When len(isnull(isexauxacc7code,N''))<1 then N'' Else N' [' + isnull(isexauxacc7code,N'') + N'] ' End) + ISNULL(isexauxacc7, N'')
  70. + (Case When len(isnull(isexauxacc8code,N''))<1 then N'' Else N' [' + isnull(isexauxacc8code,N'') + N'] ' End) + ISNULL(isexauxacc8, N'')
  71. + (Case When len(isnull(isexauxacc9code,N''))<1 then N'' Else N' [' + isnull(isexauxacc9code,N'') + N'] ' End) + ISNULL(isexauxacc9, N'')
  72. + (Case When len(isnull(isexauxacc10code,N''))<1 then N'' Else N' [' + isnull(isexauxacc10code,N'') + N'] ' End) + ISNULL(isexauxacc10, N'') as accountName2,
  73. case when accountDcdirection='652' then '借' else '贷' end as ccodefx,
  74. --accountID2,accountUnit,
  75. case when isdetail=0 then accountCode else N'' end as accountCode2,
  76. isEndNode,isdetail,
  77. isauxacccustomer,isauxaccdepartment,
  78. isauxaccperson,isauxaccproject,isauxaccinventory,
  79. isexauxacc1,isexauxacc2,isexauxacc3,isexauxacc4,isexauxacc5,
  80. isexauxacc6,isexauxacc7,isexauxacc8,isexauxacc9,isexauxacc10,
  81. --quantityBalance,origAmountBalance,
  82. amountBalance as localqc,
  83. --case when amountBalance>0 then '借' when amountBalance<0 then '贷' else '平' end as localqcfx,
  84. case when amountBalance>0 then amountBalance else 0 end as localjfqc,
  85. case when amountBalance<0 then abs(amountBalance) else 0 end as localdfqc,
  86. --quantityDr,quantityCr,origAmountDr,origAmountCr,
  87. amountDr as localjffs,
  88. amountCr as localdffs,
  89. --cumQuantityDr,cumQuantityCr,
  90. --cumOrigAmountDr,cumOrigAmountCr,
  91. cumAmountDr as localytdjffs,cumAmountCr as localytddffs,
  92. --endquantityBalance,endorigAmountBalance,
  93. endamountBalance as localqm,
  94. case when endamountBalance>0 then endamountBalance else 0 end as localjfqm,
  95. case when endamountBalance<0 then abs(endamountBalance) else 0 end as localdfqm
  96. FROM CO_JC_ERP_Balance
  97. where 1=1
  98. and accidname in ($(合并报表-账套名称))
  99. and iyear=$(合并报表-年度)
  100. and iperiod=$(合并报表-期间)
  101. and isbook='$(合并报表-是否包含未记账)'
  102. ) as t1 on tt.dbtype=t1.dbtype and tt.accidname=t1.accidname and tt.iyear=t1.iyear
  103. --and left(tt.accountcode,len(t1.ccode2))=t1.ccode2
  104. and tt.ccode2=t1.accountcode
  105. order by accounttypecode,accountcode,igrade, isdetail,accountName2
  106. drop table #temp_kemuyuechuantou111
  107. end