如何为以下结果编写 SQL 查询?
How to write a SQL query for the following result?
这是我当前的 table 数据:
Godown_Column
Product_Column
Quantity
Godown 1
Product 1
10
Godown 1
Product 2
20
Godown 2
Product 3
30
Godown 3
Product 3
40
此处,Godowns_Columns
具有不同货仓的无限行数。
如何编写 SQL 查询以获得此结果:
Product_Col
Godown 1
Godown 2
Godown 3
Product 1
10
Product 2
20
Product 3
30
40
WITH GODOWNS(Godown_Column, Product_Column, Quantity) AS
(
SELECT 'Godown 1', 'Product 1', 10 UNION ALL
SELECT 'Godown 1', 'Product 2', 20 UNION ALL
SELECT 'Godown 2', 'Product 3', 30 UNION ALL
SELECT 'Godown 3', 'Product 3', 40
)
SELECT D.Product_Column,
MAX(CASE WHEN Godown_Column='Godown 1' THEN QUANTITY ELSE 0 END)AS GODOWN_1,
MAX(CASE WHEN Godown_Column='Godown 2' THEN QUANTITY ELSE 0 END)AS GODOWN_2,
MAX(CASE WHEN Godown_Column='Godown 3' THEN QUANTITY ELSE 0 END)AS GODOWN_3
FROM GODOWNS AS D
GROUP BY D.Product_Column
GODOWNS 是你的数据示例。请将对它的引用替换为对实际 table
的引用
您可以使用动态数据透视表,因为仓库数量未知。
架构:
create table mytable(Godown_Column varchar(50), Product_Column varchar(50), Quantity int)
insert into mytable values('Godown 1', 'Product 1' ,10);
insert into mytable values('Godown 1', 'Product 2' ,20);
insert into mytable values('Godown 2', 'Product 3' ,30);
insert into mytable values('Godown 3', 'Product 3' ,40);
查询:
DECLARE @cols AS NVARCHAR(MAX)='';
DECLARE @query AS NVARCHAR(MAX)='';
SET @cols = STUFF((SELECT distinct ',' + quotename(Godown_Column)
FROM mytable
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
set @query = 'SELECT Product_Column,' + @cols + '
from
(
select * from mytable
) x
pivot
(
sum(quantity)
for Godown_Column in (' + @cols + ')
) p'
execute(@query)
输出:
Product_Column
Godown 1
Godown 2
Godown 3
Product 1
10
null
null
Product 2
20
null
null
Product 3
null
30
40
dbhere
这是我当前的 table 数据:
Godown_Column | Product_Column | Quantity |
---|---|---|
Godown 1 | Product 1 | 10 |
Godown 1 | Product 2 | 20 |
Godown 2 | Product 3 | 30 |
Godown 3 | Product 3 | 40 |
此处,Godowns_Columns
具有不同货仓的无限行数。
如何编写 SQL 查询以获得此结果:
Product_Col | Godown 1 | Godown 2 | Godown 3 |
---|---|---|---|
Product 1 | 10 | ||
Product 2 | 20 | ||
Product 3 | 30 | 40 |
WITH GODOWNS(Godown_Column, Product_Column, Quantity) AS
(
SELECT 'Godown 1', 'Product 1', 10 UNION ALL
SELECT 'Godown 1', 'Product 2', 20 UNION ALL
SELECT 'Godown 2', 'Product 3', 30 UNION ALL
SELECT 'Godown 3', 'Product 3', 40
)
SELECT D.Product_Column,
MAX(CASE WHEN Godown_Column='Godown 1' THEN QUANTITY ELSE 0 END)AS GODOWN_1,
MAX(CASE WHEN Godown_Column='Godown 2' THEN QUANTITY ELSE 0 END)AS GODOWN_2,
MAX(CASE WHEN Godown_Column='Godown 3' THEN QUANTITY ELSE 0 END)AS GODOWN_3
FROM GODOWNS AS D
GROUP BY D.Product_Column
GODOWNS 是你的数据示例。请将对它的引用替换为对实际 table
的引用您可以使用动态数据透视表,因为仓库数量未知。
架构:
create table mytable(Godown_Column varchar(50), Product_Column varchar(50), Quantity int)
insert into mytable values('Godown 1', 'Product 1' ,10);
insert into mytable values('Godown 1', 'Product 2' ,20);
insert into mytable values('Godown 2', 'Product 3' ,30);
insert into mytable values('Godown 3', 'Product 3' ,40);
查询:
DECLARE @cols AS NVARCHAR(MAX)='';
DECLARE @query AS NVARCHAR(MAX)='';
SET @cols = STUFF((SELECT distinct ',' + quotename(Godown_Column)
FROM mytable
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
set @query = 'SELECT Product_Column,' + @cols + '
from
(
select * from mytable
) x
pivot
(
sum(quantity)
for Godown_Column in (' + @cols + ')
) p'
execute(@query)
输出:
Product_Column | Godown 1 | Godown 2 | Godown 3 |
---|---|---|---|
Product 1 | 10 | null | null |
Product 2 | 20 | null | null |
Product 3 | null | 30 | 40 |
db