我有以下sql查询:
with all_stock as (
select distinct stock_items.item_id,
stock_item.owner,
stock_item.group
from stock_item
),
balances as
(select
b.year,
b.month,
b.item_id,
sum(balance) as balance_value,
from balances_table b
group by
b.year,
b.month,
b.item_id
), transactions_1 AS (
select t.month, t.year, t.item_id, t.value as t_value
from transactions_table t
where type = 1
group by t.year, t.month, t.item_id
), transactions_2 AS (
select t.month, t.year, t.item_id, t.value as t_value
from transactions_table t
where type = 2
groupy by t.year,t.period, t.item_id
), transactions_3 AS (
select t.month, t.year, t.item_id, t.value as t_value
from transactions t
where type = 3
group by t.year, t.period, t.item_id
)
select
all_stock.group,
all_stock.owner,
all_stock.item_id,
current_bal.year,
current_bal.month,
coalesce(opening_bal.balance_value, 0) as opening_balance,
coalesce(current_bal.balance_value, 0) as balance_value,
coalesce(previous_bal.balance_value, 0) as previous_blance_value,
sum(coalesce(transactions_1.t_value,0)) as type_1_value,
sum(coalesce(transactions_2.t_value,0)) as type_2_value,
sum(coalesce(transactions_3.t_value,0)) as type_3_value,
coalesce(current_bal.balance_value,0) - coalesce(opening_bal.balance_value,0) - (sum(coalesce(transactions_1.t_value,0) + sum(coalesce(transactions_2.t_value, 0) + sum(coalesce(transactions_3.t_value,0)) as difference
from all_stock
left join balances current_bal
on (all_stock.item_id = current_bal.item_id)
left join balances previous_bal
on (current_bal.item_id = previous_bal.item_id
and current_bal.month - 1 = previous_bal.month
and current_bal.year = previous_bal.year
)
left join balances opening_bal
on (all_stock.item_id = opening_bal.item_id
and opening_bal.month = '0'
and current_bal.year = previous_bal.year)
left join transactions_1
on ( current_bal.item_id = transactions_1.item_id
and current_bal.month >= transactions_1.month
and current_bal.year = transactions_1.year
)
left join transactions_2
on (current_bal.item_id = transactions_2.item_id
and current_bal.month >= transactions_2.month
and current_bal.year = transactions_2.year
)
left join transactions_3
on (current_bal.item_id = transactions_3.item_id
and current_bal.month >= transactions_3.month
and current_bal.year = transactions_3.year)
where current_bal > 0
group by
all_stock.group,
all_stock.owner,
all_stock.item_id,
current_bal.year,
current_bal.month,
coalesce(opening_bal.balance_value, 0),
coalesce(current_bal.balance_value, 0),
coalesce(previous_bal.balance_value, 0);
我试图在所有月份显示所有交易类型(1,2,3)的累计值。因此,如果我在第5个月,那么我希望所有transactions_1,transactions_2,transactions_3都是从第1,2,3,4和5个月累积的值。
我目前拥有的查询不会这样做。任何人都知道如何实现这一目标。
提前致谢
编辑:
以下是示例输出:
GROUP OWNER ITEM_ID YEAR MONTH OPENING_BAL BALANCE_VALUE TRANS_1 TRANS_2 TRANS_3 DIFFERENCE
12313 2016 1 67450.11 64564.97 -2486.64 -398.5
12313 2016 2 67450.11 61831.26 -2135.96 -3482.89
我所追求的DIFFERENCE由以下sql行计算:
coalesce(opening_bal.balance_value,0) - (sum(coalesce(transactions_1.t_value,0) + sum(coalesce(transactions_2.t_value, 0) + sum(coalesce(transactions_3.t_value,0)) as difference
仅使用每个月的交易进行计算。例如,对于第2个月,我们有-3482.89这是不正确的,因为它只从第2个月获得transa_1,2,3而忽略了第1个月的那些。但是,Month = 2的DIFFERENCE值实际应该大约为-998(不包括小数)。
此值来自执行以下计算:Current_bal - opening_bal - (每个月的transactions_1 + transactions_2 + transactions_3)。
答案 0 :(得分:0)
简单累积(analytic)总和:
select * from student
where StudentId NOT IN
(select distinct StudentId from PlaygroundMap)
or StudentId IN
(select distinct StudentId from PlaygroundMap where PlaygroundMapName = @playground)