(mssql) 如何在提取的数据中添加额外的列
(mssql) How can i add extra columns in extracted data
我在下面有这段代码(运行 在 sql 服务器 2017 上):
WITH selection AS (
SELECT servertimestamp
FROM eventlog
WHERE servertimestamp BETWEEN '5/29/2018' AND DATEADD(dd, +1, '6/29/2019')
AND (attributes LIKE '%N<=>PeopleIn%'))
(SELECT DATEADD(HOUR, DATEDIFF(HOUR, 0, servertimestamp) - (DATEDIFF(HOUR, 0, servertimestamp) % 2), 0) as timestamp , COUNT(servertimestamp) AS GONE_OUT
FROM selection
WHERE DATEPART(hh, servertimestamp) BETWEEN 8 AND 20
GROUP BY DATEADD(HOUR, DATEDIFF(HOUR, 0, servertimestamp) - (DATEDIFF(HOUR, 0, servertimestamp) % 2), 0))
ORDER BY timestamp
下面的屏幕截图也显示了执行代码的结果:
这段代码的作用是显示每天有多少人进入建筑物。数据按 2 小时分组。
我想要做的是添加一个列,显示有多少人已经离开大楼,而我已经在使用相同的时间段。
下面我给你一个我想做的例子:
请注意,在第 6 行我使用了 LIKE 运算符(属性 LIKE '%N<=>PeopleIn%')。这意味着对于附加列,我必须做出类似的选择,但不同之处在于使用 属性 LIKE '%N<=>PeopleOut%'。
我可以使用 UNION 运算符吗?还有其他更明显或更简单的方法吗?
我们将不胜感激您的帮助,
谢谢。
UNION 按执行顺序追加结果集。
UNION 不会是我解决这个问题的首选。
我在您的屏幕截图中看到的是,GONE_OUT 和 CAME_IN 是根据唯一的日期时间和聚合值分组的类别进行分组的。
您可能有两个(子)查询,一个用于 GONE_OUT,一个用于 CAME_IN,然后建立关系。
SELECT *
FROM GONE_OUT AS go
LEFT JOIN CAME_IN AS ci ON go.timestamp = ci.timestamp
您可以根据 activity 在您的 CTE 中标记 servertimestamp 字段,然后总结这些标签。
WITH selection
AS (
SELECT
servertimestamp
,CASE WHEN attributes LIKE '%N<=>PeopleIn%' THEN 1 ELSE 0 END AS PPL_IN
,CASE WHEN attributes LIKE '%N<=>PeopleOut%' THEN 1 ELSE 0 END AS PPL_OUT
FROM eventlog
WHERE
servertimestamp BETWEEN '5/29/2018' AND DATEADD(dd, + 1, '6/29/2019')
AND
(attributes LIKE '%N<=>PeopleIn%'
OR
attributes LIKE '%N<=>PeopleOut%')
)
(
SELECT
DATEADD(HOUR, DATEDIFF(HOUR, 0, servertimestamp) - (DATEDIFF(HOUR, 0, servertimestamp) % 2), 0) AS TIMESTAMP
,SUM(PPL_OUT) AS GONE_OUT
,SUM(PPL_IN) AS CAME_IN
FROM selection
WHERE DATEPART(hh, servertimestamp) BETWEEN 8
AND 20
GROUP BY DATEADD(HOUR, DATEDIFF(HOUR, 0, servertimestamp) - (DATEDIFF(HOUR, 0, servertimestamp) % 2), 0)
)
ORDER BY TIMESTAMP
我在下面有这段代码(运行 在 sql 服务器 2017 上):
WITH selection AS (
SELECT servertimestamp
FROM eventlog
WHERE servertimestamp BETWEEN '5/29/2018' AND DATEADD(dd, +1, '6/29/2019')
AND (attributes LIKE '%N<=>PeopleIn%'))
(SELECT DATEADD(HOUR, DATEDIFF(HOUR, 0, servertimestamp) - (DATEDIFF(HOUR, 0, servertimestamp) % 2), 0) as timestamp , COUNT(servertimestamp) AS GONE_OUT
FROM selection
WHERE DATEPART(hh, servertimestamp) BETWEEN 8 AND 20
GROUP BY DATEADD(HOUR, DATEDIFF(HOUR, 0, servertimestamp) - (DATEDIFF(HOUR, 0, servertimestamp) % 2), 0))
ORDER BY timestamp
下面的屏幕截图也显示了执行代码的结果:
这段代码的作用是显示每天有多少人进入建筑物。数据按 2 小时分组。
我想要做的是添加一个列,显示有多少人已经离开大楼,而我已经在使用相同的时间段。 下面我给你一个我想做的例子:
请注意,在第 6 行我使用了 LIKE 运算符(属性 LIKE '%N<=>PeopleIn%')。这意味着对于附加列,我必须做出类似的选择,但不同之处在于使用 属性 LIKE '%N<=>PeopleOut%'。
我可以使用 UNION 运算符吗?还有其他更明显或更简单的方法吗?
我们将不胜感激您的帮助, 谢谢。
UNION 按执行顺序追加结果集。 UNION 不会是我解决这个问题的首选。
我在您的屏幕截图中看到的是,GONE_OUT 和 CAME_IN 是根据唯一的日期时间和聚合值分组的类别进行分组的。 您可能有两个(子)查询,一个用于 GONE_OUT,一个用于 CAME_IN,然后建立关系。
SELECT *
FROM GONE_OUT AS go
LEFT JOIN CAME_IN AS ci ON go.timestamp = ci.timestamp
您可以根据 activity 在您的 CTE 中标记 servertimestamp 字段,然后总结这些标签。
WITH selection
AS (
SELECT
servertimestamp
,CASE WHEN attributes LIKE '%N<=>PeopleIn%' THEN 1 ELSE 0 END AS PPL_IN
,CASE WHEN attributes LIKE '%N<=>PeopleOut%' THEN 1 ELSE 0 END AS PPL_OUT
FROM eventlog
WHERE
servertimestamp BETWEEN '5/29/2018' AND DATEADD(dd, + 1, '6/29/2019')
AND
(attributes LIKE '%N<=>PeopleIn%'
OR
attributes LIKE '%N<=>PeopleOut%')
)
(
SELECT
DATEADD(HOUR, DATEDIFF(HOUR, 0, servertimestamp) - (DATEDIFF(HOUR, 0, servertimestamp) % 2), 0) AS TIMESTAMP
,SUM(PPL_OUT) AS GONE_OUT
,SUM(PPL_IN) AS CAME_IN
FROM selection
WHERE DATEPART(hh, servertimestamp) BETWEEN 8
AND 20
GROUP BY DATEADD(HOUR, DATEDIFF(HOUR, 0, servertimestamp) - (DATEDIFF(HOUR, 0, servertimestamp) % 2), 0)
)
ORDER BY TIMESTAMP