修改
A A A A A A A (A or B) B B B B
Acct Desc Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec Ttl
100 | Retail| 0| 0 | 400|400 | 400 | 400|400| 1000|1k | 1k | 1k | 1k | 7k
101 | Repair|200| 0 | 0 |200 | 200 | 200|200| 1000|1k | 1k | 1k | 0 | 5k
我已经能够得到以下几乎所需要的东西。它显示从1月到7月发生的年初至今的实际情况+当前月份(8月)发生的任何实际情况,并显示9月至12月的预算金额。我想对当前月份实际值与当前月份预算进行比较,并将更大值显示为当前月份金额。理想情况下,如果当前月份的实际数字在路上变大,并且随着月份的进展继续下去,这将能够改变。
select Acct, ActDesc, sum(Jan) as January, sum(Feb) as February, sum(Mar) as
March, sum(Apr) as April, sum(May) as May, sum(Jun) as June, sum(Jul) as
July, sum(Aug) as August, sum(Sep) as September,
sum(Oct) as October, sum(Nov) as November, sum(Dec) as December, sum(Jan +
Feb + Mar + Apr + May + Jun + Jul + Aug + Sep + Oct + Nov + [Dec]) as Total
from (
select
a.ACTNUMST Acct
,g.ACTDESCR ActDesc
,sum(case
when g.PERIODID <= datepart(month, sysdatetime()) and g.PERIODID = 1
then g.DEBITAMT-g.CRDTAMNT else 0 end) Jan
,sum(case
when g.PERIODID <= datepart(month, sysdatetime()) and g.PERIODID = 2
then g.DEBITAMT-g.CRDTAMNT else 0 end) Feb
,sum(case
when g.PERIODID <= datepart(month, sysdatetime()) and g.PERIODID = 3
then g.DEBITAMT-g.CRDTAMNT else 0 end) Mar
,sum(case
when g.PERIODID <= datepart(month, sysdatetime()) and g.PERIODID = 4
then g.DEBITAMT-g.CRDTAMNT else 0 end) Apr
,sum(case
when g.PERIODID <= datepart(month, sysdatetime()) and g.PERIODID = 5
then g.DEBITAMT-g.CRDTAMNTelse 0 end) May
,sum(case
when g.PERIODID <= datepart(month, sysdatetime()) and g.PERIODID = 6
then g.DEBITAMT-g.CRDTAMNTelse 0 end) Jun
,sum(case
when g.PERIODID <= datepart(month, sysdatetime()) and g.PERIODID = 7
then g.DEBITAMT-g.CRDTAMNTelse 0 end) Jul
,sum(case
when g.PERIODID <= datepart(month, sysdatetime()) and g.PERIODID = 8
then g.DEBITAMT-g.CRDTAMNTelse 0 end) Aug
,sum(case
when g.PERIODID <= datepart(month, sysdatetime()) and g.PERIODID = 9
then g.DEBITAMT-g.CRDTAMNTelse 0 end) Sep
,sum(case
when g.PERIODID <= datepart(month, sysdatetime()) and g.PERIODID = 10
then g.DEBITAMT-g.CRDTAMNTelse 0 end) Oct
,sum(case
when g.PERIODID <= datepart(month, sysdatetime()) and g.PERIODID = 11
then g.DEBITAMT-g.CRDTAMNTelse 0 end) Nov
,sum(case
when g.PERIODID <= datepart(month, sysdatetime()) and g.PERIODID = 12
then g.DEBITAMT-g.CRDTAMNTelse 0 end) [Dec]
from GL111 g inner join
GL100 d on g.ACTINDX = d.ACTINDX inner join
GL105 as a on g.ACTINDX = a.ACTINDX
where (g.YEAR1 = 2017) and (g.PERIODID <= DATEPART(month, sysdatetime()))
group by a.ACTNUMST, g.ACTDESCR
UNION ALL
SELECT a.ACTNUMST Acct,
m.ACTDESCR ActDesc
,sum(case
when b.PERIODID > datepart(month, sysdatetime()) and b.PERIODID = 1 then b.BUDGETAMT else 0 end) Jan
,sum(case
when b.PERIODID > datepart(month, sysdatetime()) and b.PERIODID = 2 then b.BUDGETAMT else 0 end) Feb
,sum(case
when b.PERIODID > datepart(month, sysdatetime()) and b.PERIODID = 3 then b.BUDGETAMT else 0 end) Mar
,sum(case
when b.PERIODID > datepart(month, sysdatetime()) and b.PERIODID = 4 then b.BUDGETAMT else 0 end) Apr
,sum(case
when b.PERIODID > datepart(month, sysdatetime()) and b.PERIODID = 5 then b.BUDGETAMT else 0 end) May
,sum(case
when b.PERIODID > datepart(month, sysdatetime()) and b.PERIODID = 6 then b.BUDGETAMT else 0 end) Jun
,sum(case
when b.PERIODID > datepart(month, sysdatetime()) and b.PERIODID = 7 then b.BUDGETAMT else 0 end) Jul
,sum(case
when b.PERIODID > datepart(month, sysdatetime()) and b.PERIODID = 8 then b.BUDGETAMT else 0 end) Aug
,sum(case
when b.PERIODID > datepart(month, sysdatetime()) and b.PERIODID = 9 then b.BUDGETAMT else 0 end) Sep
,sum(case
when b.PERIODID > datepart(month, sysdatetime()) and b.PERIODID = 10 then b.BUDGETAMT else 0 end) Oct
,sum(case
when b.PERIODID > datepart(month, sysdatetime()) and b.PERIODID = 11 then b.BUDGETAMT else 0 end) Nov
,sum(case
when b.PERIODID > datepart(month, sysdatetime()) and b.PERIODID = 12 then b.BUDGETAMT else 0 end) [Dec]
FROM GL201 AS b INNER JOIN
GL105 AS a ON b.ACTINDX = a.ACTINDX inner join
GL100 AS m ON a.ACTINDX = m.ACTINDX
WHERE (b.BUDGETID = '2017FORECAST') and (b.PERIODID > DATEPART(month, sysdatetime()))
GROUP BY a.ACTNUMST, m.ACTDESCR
) as derivedtbl_1
group by Acct, ActDesc
order by Acct
答案 0 :(得分:1)
不完全熟悉表格和数据模型,我要做的就是建议这段逻辑:
g.PERIODID <= datepart(month, sysdatetime())
似乎确定实际值和预测之间的断点。因此,将此逻辑合并到您当前用于将数据对齐数月的各种案例表达式中。这可能类似于以下内容:
, SUM(CASE
WHEN g.PERIODID <= datepart(month, sysdatetime()) and b.PERIODID = 1 THEN actual_amount
WHEN g.PERIODID > datepart(month, sysdatetime()) and b.PERIODID = 1 THEN b.BUDGETAMT
ELSE 0 END) Jan
并重复12列中的每一列。你是如何得出我未尝试提供的“actual_amount”的。