SQL查询同一天开始的多个用户
SQL Query multiple users who started on the same date
我有一个 table 像这样的东西:
UserID TeamID StartedDate
36202 1213 27/11/2019 9:00
36203 1213 1/11/2019 10:30
36203 1207 24/11/2019 10:00
36205 1207 21/11/2019 9:15
36203 1213 1/11/2019 10:30
36214 1217 10/11/2019 10:00
36205 1207 24/11/2019 10:00
36202 1213 1/11/2019 10:30
如何查询每个唯一日期具有相同“StartedDate”的用户列表?
输出应该是这样的。
StartedDate UserID's
24/11/2019 10:00 36203 & 36205
1/11/2019 10:30 36202 & 36203
我需要忽略时间,只关注日期。
你可以通过这种方式实现,现场演示here
SELECT DISTINCT C2.StartedDate,
SUBSTRING(
(
SELECT ', ' + CAST(C1.UserID AS VARCHAR(20))
FROM TempTable C1
WHERE C1.StartedDate = C2.StartedDate
ORDER BY C1.StartedDate
FOR XML PATH ('')
), 2, 1000) AS "UserList"
FROM TempTable C2
输出
StartedDate UserList
1/11/2019 10:30 36203, 36203, 36202
10/11/2019 10:00 36214
21/11/2019 9:15 36205
24/11/2019 10:00 36203, 36205
27/11/2019 9:00 36202
试试这个:
DECLARE @DataSource TABLE
(
[UserID] INT
,[TeamID] INT
,[StartedDate] VARCHAR(24)
);
INSERT INTO @DataSource ([UserID], [TeamID], [StartedDate])
VALUES (36202, 1213, '27/11/2019 9:00')
,(36203, 1213, '1/11/2019 10:30')
,(36203, 1207, '24/11/2019 10:00')
,(36205, 1207, '21/11/2019 9:15')
,(36203, 1213, '1/11/2019 10:30')
,(36214, 1217, '10/11/2019 10:00')
,(36205, 1207, '24/11/2019 10:00')
,(36202, 1213, '1/11/2019 10:30');
-- SQL Server 2017+
SELECT [StartedDate]
,STRING_AGG([UserID], ',') AS [UserID's]
FROM
(
SELECT DISTINCT [StartedDate]
,[UserID]
FROM @DataSource
) DS
GROUP BY [StartedDate];
-- SQL Server
WITH DataSoruce AS
(
SELECT DISTINCT [StartedDate]
FROM @DataSource
)
SELECT *
FROM DataSoruce A
CROSS APPLY
(
SELECT STUFF
(
(
SELECT DISTINCT ',' + CAST([UserID] AS VARCHAR(12))
FROM @DataSource S
WHERE A.[StartedDate] = S.[StartedDate]
FOR XML PATH, TYPE
).value('.', 'VARCHAR(MAX)')
,1
,1
,''
)
) R ([UserID's])
您可以使用下面的代码。
你需要 FOR XML 和 PATH
SELECT DISTINCT
CONVERT(DATE, StartedDate) AS StartedDate,
STUFF(
(
SELECT DISTINCT ' & ' + CAST(userid AS VARCHAR(MAX))
FROM #YourTable
WHERE (
CONVERT(DATE, StartedDate) = CONVERT(DATE, Results.StartedDate)
)
FOR XML PATH(''),TYPE
).value('(./text())[1]', 'VARCHAR(MAX)'),
1,
2,
''
) AS 'UserID''s'
FROM #YourTable Results
我有一个 table 像这样的东西:
UserID TeamID StartedDate
36202 1213 27/11/2019 9:00
36203 1213 1/11/2019 10:30
36203 1207 24/11/2019 10:00
36205 1207 21/11/2019 9:15
36203 1213 1/11/2019 10:30
36214 1217 10/11/2019 10:00
36205 1207 24/11/2019 10:00
36202 1213 1/11/2019 10:30
如何查询每个唯一日期具有相同“StartedDate”的用户列表?
输出应该是这样的。
StartedDate UserID's
24/11/2019 10:00 36203 & 36205
1/11/2019 10:30 36202 & 36203
我需要忽略时间,只关注日期。
你可以通过这种方式实现,现场演示here
SELECT DISTINCT C2.StartedDate,
SUBSTRING(
(
SELECT ', ' + CAST(C1.UserID AS VARCHAR(20))
FROM TempTable C1
WHERE C1.StartedDate = C2.StartedDate
ORDER BY C1.StartedDate
FOR XML PATH ('')
), 2, 1000) AS "UserList"
FROM TempTable C2
输出
StartedDate UserList
1/11/2019 10:30 36203, 36203, 36202
10/11/2019 10:00 36214
21/11/2019 9:15 36205
24/11/2019 10:00 36203, 36205
27/11/2019 9:00 36202
试试这个:
DECLARE @DataSource TABLE
(
[UserID] INT
,[TeamID] INT
,[StartedDate] VARCHAR(24)
);
INSERT INTO @DataSource ([UserID], [TeamID], [StartedDate])
VALUES (36202, 1213, '27/11/2019 9:00')
,(36203, 1213, '1/11/2019 10:30')
,(36203, 1207, '24/11/2019 10:00')
,(36205, 1207, '21/11/2019 9:15')
,(36203, 1213, '1/11/2019 10:30')
,(36214, 1217, '10/11/2019 10:00')
,(36205, 1207, '24/11/2019 10:00')
,(36202, 1213, '1/11/2019 10:30');
-- SQL Server 2017+
SELECT [StartedDate]
,STRING_AGG([UserID], ',') AS [UserID's]
FROM
(
SELECT DISTINCT [StartedDate]
,[UserID]
FROM @DataSource
) DS
GROUP BY [StartedDate];
-- SQL Server
WITH DataSoruce AS
(
SELECT DISTINCT [StartedDate]
FROM @DataSource
)
SELECT *
FROM DataSoruce A
CROSS APPLY
(
SELECT STUFF
(
(
SELECT DISTINCT ',' + CAST([UserID] AS VARCHAR(12))
FROM @DataSource S
WHERE A.[StartedDate] = S.[StartedDate]
FOR XML PATH, TYPE
).value('.', 'VARCHAR(MAX)')
,1
,1
,''
)
) R ([UserID's])
您可以使用下面的代码。
你需要 FOR XML 和 PATH
SELECT DISTINCT
CONVERT(DATE, StartedDate) AS StartedDate,
STUFF(
(
SELECT DISTINCT ' & ' + CAST(userid AS VARCHAR(MAX))
FROM #YourTable
WHERE (
CONVERT(DATE, StartedDate) = CONVERT(DATE, Results.StartedDate)
)
FOR XML PATH(''),TYPE
).value('(./text())[1]', 'VARCHAR(MAX)'),
1,
2,
''
) AS 'UserID''s'
FROM #YourTable Results