我有以下2个select语句,我想根据必须匹配的Date列在表格中将输入和退出时间相互合并。
SELECT NAME, SURNAME,IDENTIFIER, CONVERT(VARCHAR,(MIN(TRANSIT_DATE)),114) AS [ENTRY],'' AS [EXIT], CONVERT(VARCHAR(10),TRANSIT_DATE,103)AS [DATE]
FROM [10.230.0.15].[SCAT].[dbo].[HA_TRANSIT] a
WHERE STR_DIRECTION = 'Entry'
and a.IDENTIFIER = '20045619'
AND STR_TRANSIT_STATUS = 'Granted'
AND TRANSIT_DATE BETWEEN @CompleteSDate AND @CompleteEDate
GROUP BY NAME, SURNAME,IDENTIFIER,CONVERT(VARCHAR(10),TRANSIT_DATE,103)
SELECT NAME, SURNAME,IDENTIFIER, '' AS [ENTRY],CONVERT(VARCHAR,(MAX(TRANSIT_DATE)),114) AS [EXIT], CONVERT(VARCHAR(10),TRANSIT_DATE,103)AS [DATE]
FROM [10.230.0.15].[SCAT].[dbo].[HA_TRANSIT] a
WHERE STR_DIRECTION = 'Exit'
and a.IDENTIFIER = '20045619'
AND STR_TRANSIT_STATUS = 'Granted'
AND TRANSIT_DATE BETWEEN @CompleteSDate AND @CompleteEDate
GROUP BY NAME, SURNAME,IDENTIFIER,CONVERT(VARCHAR(10),TRANSIT_DATE,103)
查询1的结果NAME SURNAME IDENTIFIER ENTRY EXIT DATE ANTONETTE COLEMAN 20045619 07:11:01:000 10/09/2013 ANTONETTE COLEMAN 20045619 06:58:58:000 11/09/2013 ANTONETTE COLEMAN 20045619 07:04:27:000 12/09/2013 ANTONETTE COLEMAN 20045619 07:02:48:000 13/09/2013 ANTONETTE COLEMAN 20045619 07:17:43:000 16/09/2013 ANTONETTE COLEMAN 20045619 06:54:35:000 17/09/2013 ANTONETTE COLEMAN 20045619 07:00:36:000 18/09/2013 ANTONETTE COLEMAN 20045619 06:57:33:000 19/09/2013
查询2结果姓名SURNAME标识符输入出口
DATE ANTONETTE COLEMAN 20045619 16:09:39:000 10/09/2013 ANTONETTE COLEMAN 20045619 16:01:19:000 11/09/2013 ANTONETTE COLEMAN 20045619 16:18:49:000 12/09/2013 ANTONETTE COLEMAN 20045619 16:04:28:000 13/09/2013 ANTONETTE COLEMAN 20045619 16:06:04:000 16/09/2013 ANTONETTE COLEMAN 20045619 16:05:30:000 17/09/2013 ANTONETTE COLEMAN 20045619 16:02:00:000 18/09/2013
答案 0 :(得分:1)
您可能不需要两个SELECT,WHERE条件的唯一区别是STR_DIRECTION。只需将其移至CASE:
SELECT NAME, SURNAME,IDENTIFIER,
CONVERT(VARCHAR,(MIN(CASE WHEN STR_DIRECTION = 'Entry' THEN TRANSIT_DATE END)),114) AS [ENTRY],
CONVERT(VARCHAR,(MAX(CASE WHEN STR_DIRECTION = 'Exit' THEN TRANSIT_DATE END)),114) AS [EXIT],
CONVERT(VARCHAR(10),TRANSIT_DATE,103)AS [DATE]
FROM [10.230.0.15].[SCAT].[dbo].[HA_TRANSIT] a
WHERE STR_DIRECTION IN ('Entry', 'Exit')
AND a.IDENTIFIER = '20045619'
AND STR_TRANSIT_STATUS = 'Granted'
AND TRANSIT_DATE BETWEEN @CompleteSDate AND @CompleteEDate
GROUP BY NAME, SURNAME,IDENTIFIER,CONVERT(VARCHAR(10),TRANSIT_DATE,103)
答案 1 :(得分:0)
SELECT NAME, SURNAME,IDENTIFIER, CONVERT(VARCHAR,(MIN(TRANSIT_DATE)),114) AS [ENTRY],'' AS [EXIT], CONVERT(VARCHAR(10),TRANSIT_DATE,103)AS [DATE]
FROM [10.230.0.15].[SCAT].[dbo].[HA_TRANSIT] a
WHERE STR_DIRECTION = 'Entry'
and a.IDENTIFIER = '20045619'
AND STR_TRANSIT_STATUS = 'Granted'
AND TRANSIT_DATE BETWEEN @CompleteSDate AND @CompleteEDate
GROUP BY NAME, SURNAME,IDENTIFIER,CONVERT(VARCHAR(10),TRANSIT_DATE,103)
UNION
SELECT NAME, SURNAME,IDENTIFIER, '' AS [ENTRY],CONVERT(VARCHAR, (MAX(TRANSIT_DATE)),114) AS [EXIT], CONVERT(VARCHAR(10),TRANSIT_DATE,103)AS [DATE]
FROM [10.230.0.15].[SCAT].[dbo].[HA_TRANSIT] a
WHERE STR_DIRECTION = 'Exit'
and a.IDENTIFIER = '20045619'
AND STR_TRANSIT_STATUS = 'Granted'
AND TRANSIT_DATE BETWEEN @CompleteSDate AND @CompleteEDate
GROUP BY NAME, SURNAME,IDENTIFIER,CONVERT(VARCHAR(10),TRANSIT_DATE,103)