Skip to content

Commit e14cbe2

Browse files
authored
Create 1795.Rearrange-Products-Table.sql
1 parent fcb79be commit e14cbe2

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
--MySQL
2+
SELECT product_id, 'store1' AS store, store1 AS price
3+
FROM Products WHERE store1 IS NOT NULL
4+
5+
UNION
6+
SELECT product_id, 'store2' AS store, store2 AS price
7+
FROM Products WHERE store2 IS NOT NULL
8+
9+
UNION
10+
SELECT product_id, 'store3' AS store, store3 AS price
11+
FROM Products WHERE store3 IS NOT NULL
12+
13+
ORDER BY 1,2;
14+
15+
--MS SQL Server
16+
SELECT product_id, store, price
17+
FROM Products
18+
UNPIVOT (
19+
price FOR store IN (
20+
store1,store2,store3)
21+
) AS T;

0 commit comments

Comments
 (0)