问个菜鸟问题,欢迎大家来捡菜,正在报表中,此问题令我手足无措
SQLServer如图所示,acc_id字段按A和B分组后查询acc_id=A 的m1之和除以acc_id=B的m1之和
------解决思路----------------------
我不知道你这个A,B到底是如何处理的,我就是按照行的奇偶来处理了
WITH cet AS (
SELECT Para,SUM(m1)m1 FROM (
SELECT *,CASE WHEN ID%2=1 THEN 'A' ELSE 'B' END AS Para FROM (
select
fd_accounts acc_id,
sum(fd_month_1th where xxxxx=yyyyy???),
sum(fd_month_2th) m2,
sum(fd_month_3th) m3,
sum(fd_month_4th) m4,
sum(fd_month_5th) m5,
sum(fd_month_6th) m6,
sum(fd_month_7th) m7,
sum(fd_month_8th) m8,
sum(fd_month_9th) m9,
sum(fd_month_10th) m10,
sum(fd_month_11th) m11,
sum(fd_month_12th) m12,
ROW_NUMBER()OVER(ORDER BY GETDATE())ID
from
vienna_budget_info info
inner join vienna_budget_main budget on info.fd_budget_main = budget.fd_id
inner join vienna_budget_accounts acc on info.fd_accounts = acc.fd_id
where
budget.fd_type = 0 and budget.doc_status = 30
group by
info.fd_accounts
)a
)b
GROUP BY Para
)
SELECT number/(SELECT * FROM cet WHERE cet.para='B') FROM cet WHERE cet.para='A'
------解决思路----------------------
select sum((case when acc_id='A' then m1 else 0 end))/sum((case when acc_id='A' then m1 else 0 end)) from fd_accounts group by acc_id