这个SQL写法拼三张表成一张,那里有问题,地市与项目单独组合就能查,多个组合就不行?哪里出了问题

select  

${IF(LEN($AREA)>0,"地市,","")}

${IF(LEN($XMFL)>0,"项目类,","")}

SUM(case when 周期='1'  then [本币金额] else 0 end) as [第一周],

SUM(case when 周期='2'  then [本币金额] else 0 end) as [第二周],

SUM(case when 周期='3'  then [本币金额] else 0 end) as [第三周],

SUM(case when 周期='4'  then [本币金额] else 0 end) as [第四周],

SUM(case when 周期='5'  then [本币金额] else 0 end) as [第五周],

SUM(case when 周期='6'  then [本币金额] else 0 end) as [第六周],

(select SUM(case when  [科目描述]<> '未核销收款'  then 本币余额  else 0 end)  as [本币余额]

 from 应收预收账龄余额表 B where left([科目],4)='1131' and 来源='应收账款' and 日期='2023-02-23' AND A.地市=B.地市 AND A.项目类=B.项目段 ) as bbye,

 (SELECT SUM(case when  [地市]<> '未知'  then 期末余额  else 0 end)  as [期末余额]

 from 账户组合余额表 C where 会计要素='收入'  and 日期='2023-02-22' and 地市<>'未知' AND A.地市=C.地市 AND A.项目类=C.项目类 ) as qmye

 from [收款明细表] A WHERE  日期='2023-02-21'

 ${IF(LEN(AREA)>0," AND 地市 in ('"+AREA+"')","")}

  ${IF(LEN(XMFL)>0," AND 项目类 in ('"+XMFL+"')","")} 

  ${IF(LEN(XMFL)>0&&LEN(AREA)>0," group by 地市,项目类","")}

  ${IF(LEN(XMFL)>0&&LEN(AREA)=0," group by 项目类","")}

  ${IF(LEN(XMFL)=0&&LEN(AREA)>0," group by 地市","")}

image.pngimage.png

FineReport 用户W5SzQ5073 发布于 2023-2-24 18:27 (编辑于 2023-2-24 18:36)
1min目标场景问卷 立即参与
回答问题
悬赏:3 F币 + 添加悬赏
提示:增加悬赏、完善问题、追问等操作,可使您的问题被置顶,并向所有关注者发送通知
共1回答
最佳回答
0
CD20160914Lv8专家互助
发布于2023-2-24 18:28(编辑于 2023-2-24 19:16)

-----------第一段固定的值-------------------

select  

项目类,

SUM(case when 周期='1'  then [本币金额] else 0 end) as [第一周],

SUM(case when 周期='2'  then [本币金额] else 0 end) as [第二周],

SUM(case when 周期='3'  then [本币金额] else 0 end) as [第三周],

SUM(case when 周期='4'  then [本币金额] else 0 end) as [第四周],

SUM(case when 周期='5'  then [本币金额] else 0 end) as [第五周],

SUM(case when 周期='6'  then [本币金额] else 0 end) as [第六周],

(select SUM(case when  [科目描述]<> '未核销收款'  then 本币余额  else 0 end)  as [本币余额]

 from 应收预收账龄余额表 B  

 where left([科目],4)='1131' 

 and 来源='应收账款' 

 and 日期='2023-02-23' 

 AND A.地市=B.地市 AND A.项目类=B.项目段 ) as bbye,

 

 (SELECT SUM(case when  [地市]<> '未知'  then 期末余额  else 0 end)  as [期末余额]

 from 账户组合余额表 C 

 where 会计要素='收入'  

 and 日期='2023-02-22' 

 and 地市<>'未知' 

 AND A.地市=C.地市 

 AND A.项目类=C.项目类 ) as qmye

 from [收款明细表] A 

 WHERE  日期='2023-02-21'

   AND a.项目类 in ('st')  

   group by a.项目类

---------------------第二段带参数------------------

select  

${IF(LEN(AREA)>0,"地市,","")}

${IF(LEN(XMFL)>0,"项目类,","")}

SUM(case when 周期='1'  then [本币金额] else 0 end) as [第一周],

SUM(case when 周期='2'  then [本币金额] else 0 end) as [第二周],

SUM(case when 周期='3'  then [本币金额] else 0 end) as [第三周],

SUM(case when 周期='4'  then [本币金额] else 0 end) as [第四周],

SUM(case when 周期='5'  then [本币金额] else 0 end) as [第五周],

SUM(case when 周期='6'  then [本币金额] else 0 end) as [第六周],

(select SUM(case when  [科目描述]<> '未核销收款'  then 本币余额  else 0 end)  as [本币余额]

 from 应收预收账龄余额表 B where left([科目],4)='1131' and 来源='应收账款' and 日期='2023-02-23' AND A.地市=B.地市 AND A.项目类=B.项目段 ) as bbye,

 (SELECT SUM(case when  [地市]<> '未知'  then 期末余额  else 0 end)  as [期末余额]

 from 账户组合余额表 C where 会计要素='收入'  and 日期='2023-02-22' and 地市<>'未知' AND A.地市=C.地市 AND A.项目类=C.项目类 ) as qmye

 from [收款明细表] A WHERE  日期='2023-02-21'

 ${IF(LEN(AREA)>0," AND 地市 in ('"+AREA+"')","")}

  ${IF(LEN(XMFL)>0," AND 项目类 in ('"+XMFL+"')","")} 

  ${IF(LEN(XMFL)>0&&LEN(AREA)>0," group by 地市,项目类","")}

  ${IF(LEN(XMFL)>0&&LEN(AREA)=0," group by 项目类","")}

  ${IF(LEN(XMFL)=0&&LEN(AREA)>0," group by 地市","")}

  • 2关注人数
  • 343浏览人数
  • 最后回答于:2023-2-24 19:16
    请选择关闭问题的原因
    确定 取消
    返回顶部