我有一张桌子
id col1 col2 namecol1 datetime1 teamcol1 namecol2 datetime2
1 12345 2345 name1 2014-10-13 11:57:24.713 teama
2 12345 2345 name1 2014-10-13 11:57:24.713 teamb abc 2014-11-29 09:55:38.533
3 12345 2345 name1 2014-10-13 11:57:24.713 teamb bcd 2014-12-02 06:35:38.917
4 12345 2345 name1 2014-10-13 11:57:24.713 teamc def 2014-12-22 11:57:54.863
5 12345 2345 name1 2014-10-13 11:57:24.713 teamd efg 2015-01-03 13:28:24.717
我需要这个输出:
col1 col2 Team1 DateTime1 Team2 DateTime2 Team3 DateTime3
12345 2345 bcd 2014-12-02 06:35:38.917 def 2014-12-22 11:57:54.863 efg 2015-01-03 13:28:24.717
我尝试了这个查询:
SELECT
MAX(CASE WHEN teamcol1='teamb' THEN namecol2 END) AS Team1,
CONVERT(DATE, MAX(CASE WHEN teamcol1='teamb' THEN datetime2 END), 105) AS DateTime1,
MAX(CASE WHEN teamcol1='teamc' THEN namecol2 END) AS PRECON_AUDIT,
CONVERT(DATE,MAX(CASE WHEN teamcol1='teamc' THEN datetime2 END), 105) AS DateTime2,
MAX(CASE WHEN teamcol1 IN ('teamd') THEN namecol2 END) AS Team3,
CONVERT(DATE,MAX(CASE WHEN teamcol1 IN ('teamd') THEN datetime2 END),105) AS DateTime3,
col1, col2
FROM
(SELECT *
FROM table1) Z
WHERE
col1 = '12345'
GROUP BY
col1, col2
此查询的输出:
col1 col2 Team1 DateTime1 Team2 DateTime2 Team3 DateTime3
12345 2345 abc 2014-12-02 06:35:38.917 def 2014-12-22 11:57:54.863 efg 2015-01-03 13:28:24.717
我正在使用SQL Server 2008。
提前致谢。
[编辑] Table1就像审计表一样,它有多个条目,col1,col2的组合不同。 我需要为col1,col2的每个组合显示不同的团队名称以及相应的namecol2和datetime2列。 当我使用上面提到的查询时,如果teamcol1中没有重复,它会正确地给我输出。如果teamcol1中有重复(如上所述)它给了我错误的namecol2。 如果在teamcol1中重复,我需要最新的namecol2和datetime2(从表中我需要namecol2 - bcd和datetime2 - 2014-12-02 06:35:38.917)
答案 0 :(得分:0)
首先列出所有组(DISTINCT
col1,col2)。
然后为每个组找到一行,其中包含最新的datetime2
。对于每个teamb
,teamc
,teamd
,请执行三次。
您可以使用您需要的任何逻辑和顺序将复杂查询放在OUTER APPLY
中。
DECLARE @T TABLE (id int, col1 int, col2 int, namecol1 varchar(255), [datetime1] datetime, teamcol1 varchar(255), namecol2 varchar(255), [datetime2] datetime);
INSERT INTO @T (id, col1, col2, namecol1, datetime1, teamcol1, namecol2, datetime2) VALUES (1, 12345, 2345, 'name1', '2014-10-13 11:57:24.713', 'teama', NULL, NULL);
INSERT INTO @T (id, col1, col2, namecol1, datetime1, teamcol1, namecol2, datetime2) VALUES (2, 12345, 2345, 'name1', '2014-10-13 11:57:24.713', 'teamb', 'abc', '2014-11-29 09:55:38.533');
INSERT INTO @T (id, col1, col2, namecol1, datetime1, teamcol1, namecol2, datetime2) VALUES (3, 12345, 2345, 'name1', '2014-10-13 11:57:24.713', 'teamb', 'bcd', '2014-12-02 06:35:38.917');
INSERT INTO @T (id, col1, col2, namecol1, datetime1, teamcol1, namecol2, datetime2) VALUES (4, 12345, 2345, 'name1', '2014-10-13 11:57:24.713', 'teamc', 'def', '2014-12-22 11:57:54.863');
INSERT INTO @T (id, col1, col2, namecol1, datetime1, teamcol1, namecol2, datetime2) VALUES (5, 12345, 2345, 'name1', '2014-10-13 11:57:24.713', 'teamd', 'efg', '2015-01-03 13:28:24.717');
WITH
CTE_Groups
AS
(
SELECT DISTINCT col1, col2
FROM @T
)
SELECT *
FROM
CTE_Groups
OUTER APPLY
(
SELECT TOP(1)
TT.namecol2 AS Team1
, TT.[datetime2] AS DateTime1
FROM @T AS TT
WHERE
TT.teamcol1 = 'teamb'
AND TT.col1 = CTE_Groups.col1
AND TT.col2 = CTE_Groups.col2
ORDER BY TT.[datetime2] DESC
) OA_teamb
OUTER APPLY
(
SELECT TOP(1)
TT.namecol2 AS Team2
, TT.[datetime2] AS DateTime2
FROM @T AS TT
WHERE
TT.teamcol1 = 'teamc'
AND TT.col1 = CTE_Groups.col1
AND TT.col2 = CTE_Groups.col2
ORDER BY TT.[datetime2] DESC
) OA_teamc
OUTER APPLY
(
SELECT TOP(1)
TT.namecol2 AS Team3
, TT.[datetime2] AS DateTime3
FROM @T AS TT
WHERE
TT.teamcol1 = 'teamd'
AND TT.col1 = CTE_Groups.col1
AND TT.col2 = CTE_Groups.col2
ORDER BY TT.[datetime2] DESC
) OA_teamd
结果集:
col1 col2 Team1 DateTime1 Team2 DateTime2 Team3 DateTime3
12345 2345 bcd 2014-12-02 06:35:38.917 def 2014-12-22 11:57:54.863 efg 2015-01-03 13:28:24.717
如果teamb
和col1
的特定组合没有col2
,则NULL
和Team1
中会有DateTime1
个teamc
。 teamd
和{{1}}相同。