SQL脚本从一个创建两个单独的列

时间:2014-08-29 13:48:25

标签: sql

我想为Miles创建两列。一个用于'E'或'Empty',一个用于'L'或“Loaded”来自标有Miles and Loaded的列。

select
    billing_history.order_id,
    movement_order.order_id,
    movement_order.movement_id,
    movement.id,
    movement.loaded,
    Miles
from
    billing_history
left join
    movement_order on
        billing_history.order_id = movement_order.order_id
left join
    (
        select
            movement.id,
            movement.loaded,
            sum(movement.move_distance) as Miles
        from
            movement
        group by
            movement.id,
            movement.loaded,
            movement.move_distance
    ) movement on
        movement_order.movement_id = movement.id
where
    billing_history.delivery_date between '2014-07-01 00:00:00.000' and '2014-07-31 23:59:59.000'

目前返回:

enter image description here

想要回复:

enter image description here

我知道如何通过订单执行该组并删除移动ID。我只是不知道怎么去两列“Loaded Miles”和“Empty Miles”?

1 个答案:

答案 0 :(得分:3)

时使用大写

Select Case when loaded = 'L' then miles else 0 end as loaded_mile,
Case when loaded = 'E' then miles else 0 end as empty_mile,
from billing_history;