我的查询输出如下:
ID customer_name_Now customer_name_Before MOVEMENT
123451 Rustle Bock ltd N £2,121
123451 N Rustle Bock ltd -£25,666,899
123452 Little Garage Ltd N £6,987
123453 N The Big Shop £15,850
故事是,我有2个月的数据。在这两个月中,客户可能会或可能不会根据上个月的客户情况或现在是客户的情况进行调整。很多情况下它在两个月都是客户,因此我得到了如上所述的2行。
理想的输出应该是:
ID customer_name_Now customer_name_Before MOVEMENT
123451 Rustle Bock ltd Rustle Bock ltd -£25,664,778
123452 Little Garage Ltd N £6,987
123453 N The Big Shop £15,850
因此,假设客户在两个月内都有关系,那么移动应该总和给我几个月的实际移动,客户的名字应该在两列中。
请你帮我解决这个问题,非常紧急。
@DMK我用来得到初始输出的查询是:
select /*+ NO_REWRITE */
customer_id,
customer_name_now,
customer_name_before,
movement
from
(select /*+ NO_REWRITE */
main.customer_id,
main.customer_name_now,
main.customer_name_before,
main.limits_before,
main.limits_now,
sum(main.limits_now-main.limits_before) as movement
from
(select /*+ NO_REWRITE */
customer_id,
(customer_name_before) as customer_name_before,
(customer_name_now) as customer_name_now,
sum(limits_current) as limits_now,
sum(limits_previous) as limits_before
from
(select /*+ NO_REWRITE */
sub.customer_id,
sub.customer_name_now,
sub.customer_name_before,
sub.limits_current,
sub.limits_previous
from
(select /*+ NO_REWRITE */
T2.customer_ID,
(T2.customer_name) customer_name_now,
'N' customer_name_before,
sum(T26.AGREED_LIMIT) limits_current,
0 limits_previous
from
DWH_customer_HISTORY T2,
DWH_TIME_DIM T25,
DWH_FACILITY_MONTHLY T2
where
---some internal filters are applied here, i habe ot shown coz of security reasons----
and
T25.MONTH_END = '2012-11-30' and
group by
T2.customer_ID,
T2.customer_name,
) sub
union all
select /*+ NO_REWRITE */
sub.customer_id,
sub.customer_name_now,
sub.customer_name_before,
sub.limits_current,
sub.limits_previous
from
(select /*+ NO_REWRITE */
T2.customer_ID,
'N' as customer_name_now,
(T2.customer_name)customer_name_before,
0 limits_current,
sum(T2.AGREED_LIMIT) limits_previous,
from
DWH_customer_HISTORY T2,
DWH_TIME_DIM T25,
DWH_FACILITY_MONTHLY T2
where
---some internal filters are applied here, i habe ot shown coz of security reasons----
and
T25.MONTH_END = '2012-10-31'
group by
T2.customer_ID,
T2.customer_name,) sub
) un
group by
customer_id,
customer_name_now,
customer_name_before,) main
group by
main.customer_id,
main.customer_name_now,
main.customer_name_before,
main.limits_before,
main.limits_now)
答案 0 :(得分:1)
我假设你使用的是SqlServer,不过下面的查询也适用于MySql。
Select c1.ID, c1.customer_name_Now, c2.customer_name_Before, Total
from Customers c1
left Join Customers c2
on c2.ID = c1.ID
left join
(select ID as ID2, sum(MOVEMENT) as Total, count(*) as Cnt
from Customers
group by ID) t1
on ID2 = c1.ID
where (c1.customer_name_Now <> 'N' and c2.customer_name_Before <> 'N')
or CNT = 1
如果您不确定,请查看以下演示
查看您刚刚添加的查询后,上面应该仍然有效。你需要
Customers
Customers
替换为临时表我会选择第二个。保存重新运行相同的查询。
答案 1 :(得分:1)
在SQLServer2005 +中使用OVER子句
SELECT DISTINCT
ID,MAX(customer_name_Now) OVER (PARTITION BY ID) AS customer_name_Now,
MAX(customer_name_Before) OVER (PARTITION BY ID) AS customer_name_Before,
SUM(MOVEMENT) OVER (PARTITION BY ID) AS MOVEMENT
FROM your_table
OR
SELECT ID, MAX(customer_name_Now) AS customer_name_Now,
MAX(customer_name_Before) AS customer_name_Before,
SUM(MOVEMENT) AS MOVEMENT
FROM your_table
GROUP BY ID
SQLFiddle上的演示