【发布时间】:2016-04-14 12:36:28
【问题描述】:
我有疑问:
select
coalesce(t1.L1, t4.L2) as L1,
coalesce(t1.L2, t4.L2) as L2,
coalesce(t1.S, t4.S) as S,
coalesce(t1.Q, t4.Q) as Q,
coalesce(t1.Value, 0) as Value,
coalesce(t4.Prev_Value, 0) as Prev_Value
from (
select L1, L2, S, Q, sum(value) as Value
from table1
group by L1, L2, S, Q
) as t1
left join
(
select L1, L2, S, Q, sum(value) as Prev_Value
from table1
group by L1, L2, S, Q
) as t4 on
t1.L1 = t4.L1 and
t1.L2 = t4.L2 and
t1.Q = cast((cast(left(t4.Q,4) as numeric) +1) as varchar)+right(t4.Q,2) and
t1.S = t4.S
【问题讨论】:
-
我尝试了完全加入,但我得到了很多行......
-
table1中是否存在Q列
FY2015Q1和S列C的行? -
是的,C 值只存在于 FY15Q1
-
我认为这是您的问题:
t1.L1 = t4.[Sales Level 1] and t1.L1 = t4.[Sales Level 2] and t1.Q = cast((cast(left(t4.Q,4) as numeric) +1) as varchar)+right(t4.Q,2) and t1.S = t4.S=> 第二行应该是 t1.L2 = ..... 或者这可能只是您解决方案的一部分。无论如何都要改变它:) -
打错了,我有 t1.L1=t2.L1 和 t1.L2=t2.L2....
标签: sql-server sql-server-2008 join null jointable