0

我有一个(简化的)表,其结构如下:

Table: ItemData

PK | ScanCode | StoreFK | Retail | ReceiptAlias
1  | 100101   | 1       | 4.99   | Milk
4  | 100101   | 2       | 4.99   | Milk
7  | 100101   | 3       | 0.99   | Milk
2  | 100102   | 1       | 6.99   | Eggs
5  | 100102   | 2       | 6.99   | Eggs
8  | 100102   | 3       | 6.99   | Eggs
3  | 100103   | 1       | 7.99   | Bread
6  | 100103   | 2       | 8.99   | Bread
9  | 100103   | 3       | 9.99   | Bread

我可以使用以下 SQL 查询来获取以下返回结果,其中显示了在一个或多个商店具有不同零售的所有商品:请参阅此先前提出的问题

SQL查询:

SELECT   im.INV_ScanCode     AS ScanCode,
         name.ItemData AS im
GROUP BY ScanCode, Receipt_Alias
HAVING   COUNT(DISTINCT im.Retail) > 1
ORDER BY ScanCode

当前回报结果:

ScanCode | Receipt_Alias 
100101   | Milk
100103   | Bread

我想退回在一个或多个商店具有不同零售的所有商品以及每个商店位置的零售

期望的回报结果:

ScanCode | Receipt_Alias | Str_1 | Str_2 | Str_3
100101   | Milk          | 4.99  | 4.99  | 0.99
100103   | Bread         | 7.99  | 8.99  | 9.99

如何更改我的 SQL 查询以包括每个商店位置的零售?

4

1 回答 1

2
SELECT  ScanCode as ItemID,
        ReceiptAlias,
        MAX(CASE WHEN StoreFK = 1 THEN Retail ELSE NULL END) Store_1,
        MAX(CASE WHEN StoreFK = 2 THEN Retail ELSE NULL END) Store_2,
        MAX(CASE WHEN StoreFK = 3 THEN Retail ELSE NULL END) Store_3
FROM    ItemData
GROUP   BY ScanCode, ReceiptAlias
HAVING  COUNT(DISTINCT Retail) > 1
于 2013-01-19T17:14:42.480 回答