我的表格为
eId FName LName Type SubType
1 a aa 1 S11a
1 a aa 1 S12a
1 a aa 1 S13a
1 a aa 3 S31a
1 a aa 3 S32a
2 b bb 1 S11b
2 b bb 1 S12b
2 b bb 3 S31b
2 b bb 3 S32b
我希望得到一张这样的表
eId FName LName SubType1 SubType2
1 a aa S11a;S12a;S13a S31a;S32a
2 b bb S11b;S12b S31b;S32b
换句话说,我想运行一个sql查询,它给我一个员工的子类型组,作为类型1和类型2的两列。我试图在sql查询中使用STUFF命令但是没有成功。这是我写的查询,但它不起作用。基本上它为SubType列提供了Null值。
SELECT e1.eId, e1.FName, e1.LName
, STUFF((
SELECT N'; ' + [SubType]
FROM e e2
WHERE e1.eId = e2.eId
AND e1.FName = e2.FName
AND e1.LName = e2.LName
AND e1.[Type] = e2.[Type]
AND e1.[Type] = 1
FOR XML PATH ('')), 1, 2, '') AS SubType1
, STUFF((
SELECT N'; ' + [SubType]
FROM e e2
WHERE e1.eId = e2.eId
AND e1.FName = e2.FName
AND e1.LName = e2.LName
AND e1.[Type] = e2.[Type]
AND e1.[Type] = 3
FOR XML PATH ('')), 1, 2, '') AS SubType2
FROM e e1
GROUP BY e1.eId, e1.FirstName, e1.LastName, e1.[Type]
答案 0 :(得分:3)
嗯。 group by . . . type
是可疑的。也许这会奏效:
SELECT e1.eId, e1.FName, e1.LName,
STUFF((SELECT N'; ' + [SubType]
FROM e e2
WHERE e1.EmployeeId = e2.EmployeeId AND
e2.[Type] = 1
FOR XML PATH ('')
), 1, 2, '') AS SubType1,
STUFF((SELECT N'; ' + [SubType]
FROM e e2
WHERE e1.EmployeeId = e2.EmployeeId AND
e2.[Type] = 3
FOR XML PATH ('')
), 1, 2, '') AS SubType1
FROM e e1
GROUP BY e1.eId, e1.FirstName, e1.LastName;
我还认为对名称的比较是多余的,所以我删除了它们。
答案 1 :(得分:3)
您的查询中存在多个问题。
1-您使用的列名称不正确,在您正在使用的FName
和LName
以及Group by
正在使用的FirstName
和LastName
}。
2- EmployeeId
列为eId
,您曾在一个地方唱eId
而另一个地方正在使用EmployeeId
3- Type
不在select中,不能属于group by
。
以下查询应该有效。
SELECT e1.eId, e1.FName, e1.LName,
STUFF((SELECT N'; ' + [SubType]
FROM e e2
WHERE e1.[eId] = e2.[eId] AND
e2.[Type] = 1
FOR XML PATH ('')
), 1, 2, '') AS SubType1,
STUFF((SELECT N'; ' + [SubType]
FROM e e2
WHERE e1.[eId] = e2.[eId] AND
e2.[Type] = 3
FOR XML PATH ('')
), 1, 2, '') AS SubType1
FROM e e1
GROUP BY e1.eId, e1.FName, e1.LName;
<强> DEMO 强>
修改强>
您也可以尝试以下操作,这可以通过子查询清楚地区分小组。
SELECT *,
STUFF((SELECT N'; ' + [SubType]
FROM e e2
WHERE e3.[eId] = e2.[eId] AND
e2.[Type] = 1
FOR XML PATH ('')
), 1, 2, '') AS SubType1,
STUFF((SELECT N'; ' + [SubType]
FROM e e2
WHERE e3.[eId] = e2.[eId] AND
e2.[Type] = 3
FOR XML PATH ('')
), 1, 2, '') AS SubType1
FROM
(
SELECT e1.eId, e1.FName, e1.LName
FROM e e1
GROUP BY e1.eId, e1.FName, e1.LName
)e3
答案 2 :(得分:2)
试试这个...
表脚本和示例数据
CREATE TABLE [TableName](
[eId] [int] NULL,
[FName] [nvarchar](50) NULL,
[LName] [nvarchar](50) NULL,
[Type] [int] NULL,
[SubType] [nvarchar](50) NULL
)
INSERT [TableName] ([eId], [FName], [LName], [Type], [SubType]) VALUES (1, N'a', N'aa', 1, N'S11a')
INSERT [TableName] ([eId], [FName], [LName], [Type], [SubType]) VALUES (1, N'a', N'aa', 1, N'S12a')
INSERT [TableName] ([eId], [FName], [LName], [Type], [SubType]) VALUES (1, N'a', N'aa', 1, N'S13a')
INSERT [TableName] ([eId], [FName], [LName], [Type], [SubType]) VALUES (1, N'a', N'aa', 3, N'S31a')
INSERT [TableName] ([eId], [FName], [LName], [Type], [SubType]) VALUES (1, N'a', N'aa', 3, N'S32a')
INSERT [TableName] ([eId], [FName], [LName], [Type], [SubType]) VALUES (2, N'b', N'bb', 1, N'S11b')
INSERT [TableName] ([eId], [FName], [LName], [Type], [SubType]) VALUES (2, N'b', N'bb', 1, N'S12b')
INSERT [TableName] ([eId], [FName], [LName], [Type], [SubType]) VALUES (2, N'b', N'bb', 3, N'S31b')
INSERT [TableName] ([eId], [FName], [LName], [Type], [SubType]) VALUES (2, N'b', N'bb', 3, N'S32b')
查询(使用PIVOT功能)
SELECT eid,
fname,
lname,
[1] AS SubType1,
[3] AS SubType2
FROM (SELECT eid, [type], Max(fname) AS FName, Max(lname) AS LName,
Stuff((SELECT '; ' + subtype
FROM tablename t2
WHERE t2.eid = t1.eid
AND t2.[type] = t1.[type]
FOR xml path('')), 1, 2, '') AS SubType
FROM tablename t1
GROUP BY eid, [type]) sq
PIVOT ( Max(subtype)
FOR [type] IN ([1], [3]) ) piv
输出
+-----+-------+-------+------------------+------------+
| eid | fname | lname | SubType1 | SubType2 |
+-----+-------+-------+------------------+------------+
| 1 | a | aa | S11a; S12a; S13a | S31a; S32a |
| 2 | b | bb | S11b; S12b | S31b; S32b |
+-----+-------+-------+------------------+------------+