想要改进这个问题吗? 添加细节,并通过
Closed just now.
这是餐桌货和猫咪
Item | Key |
---|---|
Electronics | 0 |
Smartphones | 1 |
Laptops | 1 |
Cameras | 1 |
Headphones | 1 |
Clothing | 0 |
T-shirts | 1 |
Jeans | 1 |
Dresses | 1 |
Jackets | 1 |
Column Item has names of categories and goods with their respective 0 and 1 in column Key. It is required to fetch all categories and goods into two diferent columns like Goods and Categories. So, every goods-item must have its respective category-item in each row.
我试过做这个SQL
SELECT
CASE WHEN Key = 0 THEN Item ELSE NULL END AS Category,
CASE WHEN Key = 1 THEN Item ELSE NULL END AS Goods
FROM goodsandcat;
查询的回音是 :
Category | Goods |
---|---|
Electronics | NULL |
NULL | Smartphones |
NULL | Laptops |
NULL | Cameras |
NULL | Headphones |
Clothing | NULL |
NULL | T-shirts |
NULL | Jeans |
NULL | Dresses |
NULL | Jackets |
但预计它会以相应的类别项目填写列类中的所有NULLs。 因此,它必须像这样:
Category | Goods |
---|---|
Electronics | NULL |
Electronics | Smartphones |
Electronics | Laptops |
Electronics | Cameras |
Electronics | Headphones |
Clothing | NULL |
Clothing | T-shirts |
Clothing | Jeans |
Clothing | Dresses |
Clothing | Jackets |
如何写上上述结果的 sql- query fo?