sql – 组合2个或更多行,其中包含字符,其他行具有货币

我通过查询得到这样的输出:

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)

最佳答案 我假设你使用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 

如果您不确定,请查看以下演示

SqlFiddle

看完你刚刚添加的查询后,上面应该仍然有效.你要么需要

>用您的查询替换我的表客户
>或者将查询中的结果移动到临时表,并用临时表替换我的表Customers

我会去第二个.保存重新运行相同的查询.

点赞