插入语句

时间:2016-06-20 13:57:49

标签: sql sql-server tsql

我收到的文件如下:

Order_ID     - Customer_ID     - Name

1            - 1               - Offer 1

1            - 1               - Offer 1

1            - 1               - Offer 2

1            - 1               - Offer 3

2            - 2               - Offer 4

2            - 2               - Offer 4

3            - 1               - Offer 4

现在我需要将数据转换为这样的

Order_id - Customer_ID - Offer_1 - Offer_2 - Offer_ 3 - Offer_4

1 - 1 - 2 - 1 - 1 - 0 

2 - 2 - 0 - 0 - 0 - 2

3 - 1 - 0 - 0 - 0 - 1

现在我使用以下代码:

INSERT INTO OutputFile
SELECT 
            [Unique id]       = INPUT.ORDER_ID,
            [klantnummer]     = INPUT.CUSTOMER_ID,
            Offer_1     = Offer_1.aantal,
            Offer_2             = Offer_2.aantal,
    Offer_3             = Offer_3.aantal,
    Offer_4             = Offer_4.aantal

FROM ( SELECT DISTINCT ORDER_ID, CUSTOMER_ID FROM INPUT_HOURLY )  AS INPUT
    LEFT JOIN (
        select ORDER_ID, CUSTOMER_ID, OFFER_ID, name, COUNT(name)  AS AANTAL  from 
            ( select ORDER_ID, CUSTOMER_ID, OFFER_ID, name from INPUT_HOURLY where offer_id ='1') TAB
         group by ORDER_ID, CUSTOMER_ID, OFFER_ID, name 
            ) AS offer_1 on offer_1.order_id = INPUT.order_id and offer_1.customer_id = INPUT.customer_id
    LEFT JOIN (
        select ORDER_ID, CUSTOMER_ID, OFFER_ID, name, COUNT(name)  AS AANTAL  from 
            ( select ORDER_ID, CUSTOMER_ID, OFFER_ID, name from INPUT_HOURLY where offer_id ='2') TAB
         group by ORDER_ID, CUSTOMER_ID, OFFER_ID, name 
            ) AS offer_2 on offer_2.order_id = INPUT.order_id and offer_2.customer_id = INPUT.customer_id
    LEFT JOIN (
        select ORDER_ID, CUSTOMER_ID, OFFER_ID, name, COUNT(name)  AS AANTAL  from 
            ( select ORDER_ID, CUSTOMER_ID, OFFER_ID, name from INPUT_HOURLY where offer_id ='3') TAB
         group by ORDER_ID, CUSTOMER_ID, OFFER_ID, name 
            ) AS offer_3 on offer_3.order_id = INPUT.order_id and offer_3.customer_id = INPUT.customer_id
    LEFT JOIN (
        select ORDER_ID, CUSTOMER_ID, OFFER_ID, name, COUNT(name)  AS AANTAL  from 
            ( select ORDER_ID, CUSTOMER_ID, OFFER_ID, name from INPUT_HOURLY where offer_id ='4') TAB
         group by ORDER_ID, CUSTOMER_ID, OFFER_ID, name 
            ) AS offer_4 on offer_4.order_id = INPUT.order_id and offer_4.customer_id = INPUT.customer_id

现在我的问题是:有更好更简单的方法吗?因为在真实文件中我需要填写超过25种不同的优惠,如果我这样做,它将成为一个巨大的查询。

2 个答案:

答案 0 :(得分:2)

你应该使用CASE EXPRESSION的条件聚合而不是所有这些不必要的LEFT JOIN

SELECT ORDER_ID, CUSTOMER_ID,
       COUNT(CASE WHEN offer_id = '1' THEN 1 END) as offer_1,
       COUNT(CASE WHEN offer_id = '2' THEN 1 END) as offer_2,
       COUNT(CASE WHEN offer_id = '3' THEN 1 END) as offer_3,
       COUNT(CASE WHEN offer_id = '4' THEN 1 END) as offer_4
FROM INPUT_HOURLY 
GROUP BY ORDER_ID, CUSTOMER_ID

COUNT()忽略NULL个值,因此,当CASE内的条件为false时,默认值(因为没有ELSE)将为{{1只有当它回答条件时它才会计数。

答案 1 :(得分:0)

您可以使用PIVOT将行创建为列。如果您希望/需要使用PIVOT解决聚合要求,请查看以下文章: http://sqlmag.com/t-sql/pivoting-without-aggregation

否则,只需通过Google查看PIVOT功能即可。许多好文章都涵盖了各个方面。