SQL Server 2008 R2:在 VARCHAR 列上转换 table
SQL Server 2008 R2: Pivot table on VARCHAR column
Table:
CREATE TABLE EMPDetails
(
ID int,
EmpName varchar(20),
ColumnName varchar(20),
ColumnValue varchar(20)
);
记录:
INSERT INTO EMPDetails Values(1,'S','Company','Microsoft');
INSERT INTO EMPDetails Values(1,'S','Profession','Database');
INSERT INTO EMPDetails Values(1,'S','Location','USA');
INSERT INTO EMPDetails Values(1,'S','Company','Unisys');
INSERT INTO EMPDetails Values(1,'S','Company','TATA');
INSERT INTO EMPDetails Values(2,'U','Company','Microsoft');
INSERT INTO EMPDetails Values(2,'U','Profession','Software');
INSERT INTO EMPDetails Values(2,'U','Location','UK');
INSERT INTO EMPDetails Values(2,'U','Company','DXC');
INSERT INTO EMPDetails Values(2,'U','Company','AOL');
INSERT INTO EMPDetails Values(3,'R','Company','Microsoft');
INSERT INTO EMPDetails Values(3,'R','Profession','Software');
INSERT INTO EMPDetails Values(3,'R','Location','UK');
INSERT INTO EMPDetails Values(3,'R','Company','AOL');
我的尝试
条件 1 : Company = 'Microsoft' AND Profession = 'Database'
预期结果:
ID EmpName Company Profession Location
-----------------------------------------
1 S Microsoft Database USA
查询:
SELECT *
FROM
(
SELECT ID, EmpName,ColumnValue, ColumnName
FROM EMPDetails
) src
PIVOT
(
MAX(ColumnValue) /*OR MIN()*/
FOR ColumnName in ([Company], [Profession], [Location])
) piv
WHERE Company = 'Microsoft' AND Profession = 'Database'
条件 2 : Company = 'Microsoft' AND Profession = 'Software'
预期结果:
ID EmpName Company Profession Location
-----------------------------------------
3 R Microsoft Software UK
2 U Microsoft Software UK
查询:
SELECT *
FROM
(
SELECT ID, EmpName,ColumnValue, ColumnName
FROM EMPDetails
) src
PIVOT
(
MAX(ColumnValue) /*OR MIN()*/
FOR ColumnName in ([Company], [Profession], [Location])
) piv
WHERE Company = 'Microsoft' AND Profession = 'Software'
问题:如果我在条件 1 中使用 MIN()
聚合函数,则将得到结果,但在 MAX()
中不会得到结果,而在条件 2 中则相反。为什么?
您可以GROUP BY
员工并在HAVING
子句中使用条件计数,以获得满足条件的员工:
SELECT ID, EmpName
FROM EMPDetails
GROUP BY ID, EmpName
HAVING COUNT(CASE
WHEN ColumnName = 'Company' AND
ColumnValue = 'Microsoft' THEN 1
END) > 0
AND
COUNT(CASE
WHEN ColumnName = 'Profession' AND
ColumnValue = 'Database' THEN 1
END) > 0
以上查询选择 Microsoft
作为 Company
和 Database
作为 Profession
的员工。
输出:
ID EmpName
------------
1 S
您现在可以使用条件聚合来透视员工行:
SELECT ID, EmpName,
'Microsoft' AS Company,
'Database' AS Profession,
MAX(CASE WHEN ColumnName = 'Location' THEN ColumnValue END) AS Location
FROM EMPDetails
GROUP BY ID, EmpName
HAVING COUNT(CASE
WHEN ColumnName = 'Company' AND
ColumnValue = 'Microsoft' THEN 1
END) > 0
AND
COUNT(CASE
WHEN ColumnName = 'Profession' AND
ColumnValue = 'Database' THEN 1
END) > 0
不需要对Company或Profession值进行聚合,因为期望的值保证属于员工组。
输出:
ID EmpName Company Profession Location
-----------------------------------------
1 S Microsoft Database USA
要获得第二个结果集,只需将Database
替换为Software
:
SELECT ID, EmpName,
'Microsoft' AS Company,
'Software' AS Profession,
MAX(CASE WHEN ColumnName = 'Location' THEN ColumnValue END) AS Location
FROM EMPDetails
GROUP BY ID, EmpName
HAVING COUNT(CASE
WHEN ColumnName = 'Company' AND
ColumnValue = 'Microsoft' THEN 1
END) > 0
AND
COUNT(CASE
WHEN ColumnName = 'Profession' AND
ColumnValue = 'Software' THEN 1
END) > 0
输出:
ID EmpName Company Profession Location
---------------------------------------------
3 R Microsoft Software UK
2 U Microsoft Software UK
Table:
CREATE TABLE EMPDetails
(
ID int,
EmpName varchar(20),
ColumnName varchar(20),
ColumnValue varchar(20)
);
记录:
INSERT INTO EMPDetails Values(1,'S','Company','Microsoft');
INSERT INTO EMPDetails Values(1,'S','Profession','Database');
INSERT INTO EMPDetails Values(1,'S','Location','USA');
INSERT INTO EMPDetails Values(1,'S','Company','Unisys');
INSERT INTO EMPDetails Values(1,'S','Company','TATA');
INSERT INTO EMPDetails Values(2,'U','Company','Microsoft');
INSERT INTO EMPDetails Values(2,'U','Profession','Software');
INSERT INTO EMPDetails Values(2,'U','Location','UK');
INSERT INTO EMPDetails Values(2,'U','Company','DXC');
INSERT INTO EMPDetails Values(2,'U','Company','AOL');
INSERT INTO EMPDetails Values(3,'R','Company','Microsoft');
INSERT INTO EMPDetails Values(3,'R','Profession','Software');
INSERT INTO EMPDetails Values(3,'R','Location','UK');
INSERT INTO EMPDetails Values(3,'R','Company','AOL');
我的尝试
条件 1 : Company = 'Microsoft' AND Profession = 'Database'
预期结果:
ID EmpName Company Profession Location
-----------------------------------------
1 S Microsoft Database USA
查询:
SELECT *
FROM
(
SELECT ID, EmpName,ColumnValue, ColumnName
FROM EMPDetails
) src
PIVOT
(
MAX(ColumnValue) /*OR MIN()*/
FOR ColumnName in ([Company], [Profession], [Location])
) piv
WHERE Company = 'Microsoft' AND Profession = 'Database'
条件 2 : Company = 'Microsoft' AND Profession = 'Software'
预期结果:
ID EmpName Company Profession Location
-----------------------------------------
3 R Microsoft Software UK
2 U Microsoft Software UK
查询:
SELECT *
FROM
(
SELECT ID, EmpName,ColumnValue, ColumnName
FROM EMPDetails
) src
PIVOT
(
MAX(ColumnValue) /*OR MIN()*/
FOR ColumnName in ([Company], [Profession], [Location])
) piv
WHERE Company = 'Microsoft' AND Profession = 'Software'
问题:如果我在条件 1 中使用 MIN()
聚合函数,则将得到结果,但在 MAX()
中不会得到结果,而在条件 2 中则相反。为什么?
您可以GROUP BY
员工并在HAVING
子句中使用条件计数,以获得满足条件的员工:
SELECT ID, EmpName
FROM EMPDetails
GROUP BY ID, EmpName
HAVING COUNT(CASE
WHEN ColumnName = 'Company' AND
ColumnValue = 'Microsoft' THEN 1
END) > 0
AND
COUNT(CASE
WHEN ColumnName = 'Profession' AND
ColumnValue = 'Database' THEN 1
END) > 0
以上查询选择 Microsoft
作为 Company
和 Database
作为 Profession
的员工。
输出:
ID EmpName
------------
1 S
您现在可以使用条件聚合来透视员工行:
SELECT ID, EmpName,
'Microsoft' AS Company,
'Database' AS Profession,
MAX(CASE WHEN ColumnName = 'Location' THEN ColumnValue END) AS Location
FROM EMPDetails
GROUP BY ID, EmpName
HAVING COUNT(CASE
WHEN ColumnName = 'Company' AND
ColumnValue = 'Microsoft' THEN 1
END) > 0
AND
COUNT(CASE
WHEN ColumnName = 'Profession' AND
ColumnValue = 'Database' THEN 1
END) > 0
不需要对Company或Profession值进行聚合,因为期望的值保证属于员工组。
输出:
ID EmpName Company Profession Location
-----------------------------------------
1 S Microsoft Database USA
要获得第二个结果集,只需将Database
替换为Software
:
SELECT ID, EmpName,
'Microsoft' AS Company,
'Software' AS Profession,
MAX(CASE WHEN ColumnName = 'Location' THEN ColumnValue END) AS Location
FROM EMPDetails
GROUP BY ID, EmpName
HAVING COUNT(CASE
WHEN ColumnName = 'Company' AND
ColumnValue = 'Microsoft' THEN 1
END) > 0
AND
COUNT(CASE
WHEN ColumnName = 'Profession' AND
ColumnValue = 'Software' THEN 1
END) > 0
输出:
ID EmpName Company Profession Location
---------------------------------------------
3 R Microsoft Software UK
2 U Microsoft Software UK