2017-08-21 5 views
2

内SUM私はtransaction_weekSQL - サブクエリ

SELECT a.transaction_week, 
SUM(CASE WHEN record_type IN (6,37,13) THEN quantity ELSE 0 END) as SalesVol 
FROM table 1 a 
LEFT JOIN table 2 b ON b.Date = a.transaction_date 
LEFT JOIN table 3 c ON c.sku = a.product 
WHERE series in (62,236,501,52) 
GROUP BY a.transaction_week 
ORDER BY a.transaction_week 

| tw | SalesVol | 
| 1 | 4768 |   
| 2 | 4567 | 
| 3 | 4354 | 
| 4 | 4678 | 

によって異なる製品やグループのSalesVolそれを見て、次のコードを持って、私はどこ複数のサブクエリを持つことができるようにしたいですシリーズ番号を変更します。

SELECT a.transaction_week, 

(SELECT SUM(CASE WHEN record_type IN (6,37,13) THEN quantity ELSE 0 END) as SalesVol 
FROM table 1 a 
LEFT JOIN table 2 b ON b.Date = a.transaction_date 
LEFT JOIN table 3 c ON c.sku = a.product 
WHERE series in (62,236,501,52)) as personal care 

(SELECT SUM(CASE WHEN record_type IN (6,37,13) THEN quantity ELSE 0 END) as SalesVol 
FROM table 1 a 
LEFT JOIN table 2 b ON b.Date = a.transaction_date 
LEFT JOIN table 3 c ON c.sku = a.product 
WHERE series in (37,202,203,456)) as white goods 

FROM table 1 a 
LEFT JOIN table 2 b ON b.Date = a.transaction_date 
LEFT JOIN table 3 c ON c.sku = a.product 
GROUP BY a.transaction_week 
ORDER BY a.transaction_week 

それは私に全体の合計値を与え、あなただけサブクエリでa.transaction_weekを欠場transaction_week

+2

どの[DBMS](https://en.wikipedia.org/wiki/DBMS)を使用していますか? Postgres?オラクル? –

答えて

1

:以下のクエリを参照してください

SELECT a.transaction_week, 
    sum(CASE WHEN series IN (62,236,501,52) AND record_type IN (6,37,13) 
     THEN quantity ELSE 0 END) as personal_care, 
    sum(CASE WHEN series IN (37,202,203,456) AND record_type IN (6,37,13) 
     THEN quantity ELSE 0 END) as white_goods 
FROM table 1 a 
LEFT JOIN table 2 b ON b.Date = a.transaction_date 
LEFT JOIN table 3 c ON c.sku = a.product 
GROUP BY a.transaction_week 
ORDER BY a.transaction_week; 
+0

優秀、完璧に動作します! –

0

ことによってそれをグループ化されていないように私は仕事でサブクエリを取得することはできません。外部クエリのJOINは不必要です。

SELECT a.transaction_week, 
(
    SELECT SUM(CASE WHEN record_type IN (6,37,13) THEN quantity ELSE 0 END) as SalesVol 
    FROM table 1 a2 
    LEFT JOIN table 2 b ON b.Date = a2.transaction_date 
    LEFT JOIN table 3 c ON c.sku = a2.product 
    WHERE series in (62,236,501,52) AND a2.transaction_week = a.transaction_week 
) as personal care, 
(
    SELECT SUM(CASE WHEN record_type IN (6,37,13) THEN quantity ELSE 0 END) as SalesVol 
    FROM table 1 a 2 
    LEFT JOIN table 2 b ON b.Date = a2.transaction_date 
    LEFT JOIN table 3 c ON c.sku = a2.product 
    WHERE series in (37,202,203,456) AND a2.transaction_week = a.transaction_week 
) as white goods 
FROM table 1 a 
GROUP BY a.transaction_week 
ORDER BY a.transaction_week 
0

UNION演算子を使用する必要があります。 CASE文の条件にseriesを追加し、代わりにサブクエリを使用しての

select a.transaction_week, SalesVol from 
(SELECT a.transaction_week as transaction_week, 
SUM(CASE WHEN record_type IN (6,37,13) THEN quantity ELSE 0 END) as SalesVol 
FROM table 1 a 
LEFT JOIN table 2 b ON b.Date = a.transaction_date 
LEFT JOIN table 3 c ON c.sku = a.product 
WHERE series in (62,236,501,52) 
UNION 
SELECT a.transaction_week as transaction_week, 
SUM(CASE WHEN record_type IN (6,37,13) THEN quantity ELSE 0 END) as SalesVol 
FROM table 1 a 
LEFT JOIN table 2 b ON b.Date = a.transaction_date 
LEFT JOIN table 3 c ON c.sku = a.product 
WHERE series in (37,202,203,456) 
) AS tbl1 
GROUP BY tbl1.transaction_week 
ORDER BY tbl1.transaction_week 
0

は、それはだけでなく、あなた次第速いを働くだろう、これを試してみてください要件:

SELECT a.transaction_week , 
     whitegoods.SalesVol AS 'White Goods' , 
     personalcare.SalesVol1 AS 'Personal Care' 
FROM table1 a 
     LEFT JOIN table2 b ON b.[Date] = a.transaction_date 
     LEFT JOIN table3 c ON c.sku = a.product 
     CROSS APPLY (SELECT SUM(CASE WHEN record_type IN (6, 37, 13) 
             THEN quantity 
             ELSE 0 
            END) AS SalesVol 
         FROM  table1 a2 
         WHERE  b.[Date] = a2.transaction_date 
           AND c.sku = a2.product 
           AND series IN (37, 202, 203, 456) 
           AND a2.transaction_week = a.transaction_week 
        ) whitegoods 
     CROSS APPLY (SELECT SUM(CASE WHEN record_type IN (6, 37, 13) 
             THEN quantity 
             ELSE 0 
            END) AS SalesVol1 
         FROM  table1 a2 
         WHERE  b.[Date] = a2.transaction_date 
           AND c.sku = a2.product 
           AND series IN (62, 236, 501, 52) 
           AND a2.transaction_week = a.transaction_week 
        ) personalcare 
GROUP BY a.transaction_week 
ORDER BY a.transaction_week