我有一个查询,它返回一个结果集,现在我需要修改查询以也返回另一个结果集
我已经运行了下面的代码,并且返回了两个CTE中的tagID的结果,并为您提供了超时时间。 我现在需要修改查询,这样它将向我显示所有已扫描的标签,其中包含Time in和out out时间,反之亦然,而没有重复。
With CTE AS
(
select Tag as 'Tag ID', UID_KEG, CONVERT(VARCHAR(10), MOVEMENT_DATE, 105)
as [DATE],CONVERT(VARCHAR(10), MOVEMENT_DATE, 108) as [Time Out]
from MOVEMENT M
Inner join KEG K on K.UNIQUE_ID = M.UID_KEG
where Convert(varchar(10),MOVEMENT_DATE,120) = '2019-06-13'
and UID_STATION = 4
and TAG <> 'NO TAG'
) ,
CTE2 AS
(select Tag as 'Tag ID', UID_KEG, CONVERT(VARCHAR(10), MOVEMENT_DATE, 105)
as [DATE],CONVERT(VARCHAR(10), MOVEMENT_DATE, 108) as [Time IN]
from MOVEMENT M
Inner join KEG K on K.UNIQUE_ID = M.UID_KEG
where Convert(varchar(10),MOVEMENT_DATE,120) = '2019-06-13'
and UID_STATION = 5
and TAG <> 'NO TAG'
)
Select CTE.[Tag ID], CTE.[DATE], [Time IN], [Time Out],DATEDIFF(MINUTE,
[Time IN], [Time Out]) as [Time in Process]
from CTE
Inner Join CTE2 on CTE2.[Tag ID] = CTE.[Tag ID]
where Exists (Select CTE2.[Tag ID]
from CTE2
where CTE2.[Tag ID] = CTE.[Tag ID] )
此刻的查询为我提供了以下结果:
TAG ID DATE Time_In Time_Out DIF
33154A36D00F46C000007144 6/13/2019 4:43:05 AM 6:25:27 AM 102
33154A36D00F46C00000464A 6/13/2019 4:43:47 AM 6:06:45 AM 83
33154A36D00F46C000006DFF 6/13/2019 4:46:22 AM 6:25:27 AM 99
33154A36D00F46C0000040A8 6/13/2019 4:54:23 AM 6:10:55 AM 76
33154A36D00F46C000002ECB 6/13/2019 4:55:59 AM 6:10:55 AM 75
33154A36D00F46C000002A2F 6/13/2019 5:03:18 AM 6:20:40 AM 77
33154A36D00F46C000000499 6/13/2019 5:34:35 AM 6:25:27 AM 51
33154A36D00F46C00000627C 6/13/2019 5:38:04 AM 6:25:27 AM 47
33154A36D00F46C000006F74 6/13/2019 5:38:06 AM 6:28:42 AM 50
我现在也在寻找以下内容:
33154A36D00F46C000006F38 6/13/2019 6:28:42 AM
33154A36D00F46C000006F62 6/13/2019 6:47:42 AM
33154A36D00F46C000006F90 6/13/2019 7:47:12 AM
答案 0 :(得分:0)
您可以做的是创建第二个CTE子句,该子句选择您要描述的内容,并具有相同的字段布局和不相关的NULL。
然后从这两者中进行选择,就完成了
SELECT * FROM CTE_A
UNION ALL
SELECT * FROM CTE_B
WHERE CTE_B.tag NOT IN (SELECT CTE_A.tag FROM CTE_A)
答案 1 :(得分:0)
为什么不简化查询而只做一次通过?
select
Tag as 'Tag ID'
, UID_KEG
, max(iif(UID_STATION=4,CONVERT(VARCHAR(10), MOVEMENT_DATE, 105),null)) as [DATE]
, max(iif(UID_STATION=4,CONVERT(VARCHAR(10), MOVEMENT_DATE, 108),null)) as [Time Out]
, max(iif(UID_STATION=5,CONVERT(VARCHAR(10), MOVEMENT_DATE, 108),null)) as [Time IN]
from MOVEMENT M
Inner join KEG K on
K.UNIQUE_ID = M.UID_KEG
AND Convert(varchar(10),MOVEMENT_DATE,120) = '2019-06-13'
and UID_STATION in (4,5)
and TAG <> 'NO TAG'
group by
Tag
, UID_KEG
如果您想保留cte的权限,那么首先,由于您要进行内部联接,因此可能会丢失:
where Exists (Select CTE2.[Tag ID]
from CTE2
where CTE2.[Tag ID] = CTE.[Tag ID] )
如果您在[标记ID]上进行内部联接,则只会在两个表中都获得值
我会丢失该where子句,并将其更改为外部联接,在这种情况下,如果希望所有带有in的标签,则将获得所有标签ID(无论它们是入内还是出),或者是左外部联接。日期,像这样:
With CTE AS
(
select Tag as 'Tag ID', UID_KEG, CONVERT(VARCHAR(10), MOVEMENT_DATE, 105)
as [DATE],CONVERT(VARCHAR(10), MOVEMENT_DATE, 108) as [Time Out]
from MOVEMENT M
Inner join KEG K on K.UNIQUE_ID = M.UID_KEG
where Convert(varchar(10),MOVEMENT_DATE,120) = '2019-06-13'
and UID_STATION = 4
and TAG <> 'NO TAG'
) ,
CTE2 AS
(select Tag as 'Tag ID', UID_KEG, CONVERT(VARCHAR(10), MOVEMENT_DATE, 105)
as [DATE],CONVERT(VARCHAR(10), MOVEMENT_DATE, 108) as [Time IN]
from MOVEMENT M
Inner join KEG K on K.UNIQUE_ID = M.UID_KEG
where Convert(varchar(10),MOVEMENT_DATE,120) = '2019-06-13'
and UID_STATION = 5
and TAG <> 'NO TAG'
)
Select CTE.[Tag ID], CTE.[DATE], [Time IN], [Time Out],DATEDIFF(MINUTE,
[Time IN], [Time Out]) as [Time in Process]
from CTE
LEFT Join CTE2 on CTE2.[Tag ID] = CTE.[Tag ID]
---已在编辑中添加
如果您希望在出入港时有份量,怎么办:
SELECT
Tag AS 'Tag ID',
UID_KEG,
MAX(iif(UID_STATION = 4, CONVERT(varchar(10), MOVEMENT_DATE, 105), NULL)) AS [DATE],
MAX(iif(UID_STATION = 4, CONVERT(varchar(10), MOVEMENT_DATE, 108), NULL)) AS [Time Out],
MAX(iif(UID_STATION = 5, CONVERT(varchar(10), MOVEMENT_DATE, 108), NULL)) AS [Time IN],
DATEDIFF(MINUTE,
MAX(iif(UID_STATION = 4, MOVEMENT_DATE, NULL)),
MAX(iif(UID_STATION = 5, MOVEMENT_DATE, NULL))) AS [Time Elapsed]
FROM MOVEMENT M
INNER JOIN KEG K
ON K.UNIQUE_ID = M.UID_KEG
AND CONVERT(varchar(10), MOVEMENT_DATE, 120) = '2019-06-13'
AND UID_STATION IN (4, 5)
AND TAG <> 'NO TAG'
GROUP BY Tag,
UID_KEG