删除空值并修复对齐
Removing Nulls and Fixing Alignment
我正在使用 SQL 服务器并且我使用 SMSS 进行了查询。我不是全职 SQL 用户,我正在尝试使用 SQL 作为数据收集器为我们的员工制作一些仪表板,下面是这个查询。
查询几乎可以正常工作,但我需要将所有结果放入成对的行中,而不是显示空值。这是我写的查询:
SELECT convert(varchar(11), start_time, 103) AS Date, DATENAME(weekday, start_time) AS Week_Day,
CASE WHEN CAST(start_time AS time) = '05:00:00'
THEN name
END AS 'Morning',
CASE WHEN CAST(start_time AS time) ='12:59:59'
THEN name
END AS 'Afternoon',
CASE WHEN CAST(start_time AS time) = '20:59:59'
THEN name
END AS 'Evening'
FROM [somedb].[dbo].[tb_some_table]
WHERE [start_time] >= dateadd(day, 2-datepart(dw, getdate()), CONVERT(date,getdate()))
AND [start_time] < dateadd(day, 9-datepart(dw, getdate()), CONVERT(date,getdate()))
这给了我这样的结果:
Date Week_Day Morning Afternoon Evening
24/08/2020 Monday Fred NULL NULL
24/08/2020 Monday Markus NULL NULL
24/08/2020 Monday NULL Chuck NULL
24/08/2020 Monday NULL Bill NULL
24/08/2020 Monday NULL NULL John
24/08/2020 Monday NULL NULL Wayne
但是,我想得到这个:
Date Week_Day Morning Afternoon Evening
24/08/2020 Monday Fred Chuck John
24/08/2020 Monday Markus Bill Wayne
谁能帮帮我? :/
编辑:数据如下所示:
如果我 运行 a select:
,数据看起来像这样
start_time end_time name
2020-08-24 05:00:00.000 2020-08-24 12:59:59.000 Frank
2020-08-24 05:00:00.000 2020-08-24 12:59:59.000 Markus
2020-08-24 12:59:59.000 2020-08-24 20:59:59.000 Chuck
2020-08-24 12:59:59.000 2020-08-24 20:59:59.000 Bill
2020-08-24 20:59:59.000 2020-08-25 05:00:00.000 John
2020-08-24 20:59:59.000 2020-08-25 05:00:00.000 Wayne
我想你想要聚合:
SELECT CONVERT(DATE, start_time) as date,
DATENAME(weekday, MIN(start_time)) AS Week_Day,
MAX(CASE WHEN CAST(start_time AS time) = '05:00:00' THEN name END) AS Morning,
MAX(CASE WHEN CAST(start_time AS time) = '12:59:59' THEN name END) AS Afternoon,
MAX(CASE WHEN CAST(start_time AS time) = '20:59:59' THEN name END) AS Evening
FROM (SELECT t.*,
ROW_NUMBER() OVER (PARTITION BY start_time ORDER BY start_time) as seqnum
FROM [somedb].[dbo].[tb_some_table] t
WHERE start_time >= dateadd(day, 2-datepart(dw, getdate()), CONVERT(date, getdate())) AND
start_time < dateadd(day, 9-datepart(dw, getdate()), CONVERT(date, getdate()))
) t
GROUP BY CONVERT(DATE, start_time), seqnum;
请注意,每个日期需要多行。 ROW_NUMBER()
正在确定。
备注:
- 您不需要将日期转换为字符串来提取日期部分。仅当您确实需要特定格式的日期时才使用
convert()
。
- 时间竟然如此准确,这似乎很奇怪。我希望范围。如果确实如此,请提出一个新问题。它会改变查询。
- 仅对字符串和日期常量使用单引号。不要将它们用于列名——当(有一天)您认为您是使用单引号引用列时,这只会导致 hard-to-debug 错误。
我正在使用 SQL 服务器并且我使用 SMSS 进行了查询。我不是全职 SQL 用户,我正在尝试使用 SQL 作为数据收集器为我们的员工制作一些仪表板,下面是这个查询。
查询几乎可以正常工作,但我需要将所有结果放入成对的行中,而不是显示空值。这是我写的查询:
SELECT convert(varchar(11), start_time, 103) AS Date, DATENAME(weekday, start_time) AS Week_Day,
CASE WHEN CAST(start_time AS time) = '05:00:00'
THEN name
END AS 'Morning',
CASE WHEN CAST(start_time AS time) ='12:59:59'
THEN name
END AS 'Afternoon',
CASE WHEN CAST(start_time AS time) = '20:59:59'
THEN name
END AS 'Evening'
FROM [somedb].[dbo].[tb_some_table]
WHERE [start_time] >= dateadd(day, 2-datepart(dw, getdate()), CONVERT(date,getdate()))
AND [start_time] < dateadd(day, 9-datepart(dw, getdate()), CONVERT(date,getdate()))
这给了我这样的结果:
Date Week_Day Morning Afternoon Evening
24/08/2020 Monday Fred NULL NULL
24/08/2020 Monday Markus NULL NULL
24/08/2020 Monday NULL Chuck NULL
24/08/2020 Monday NULL Bill NULL
24/08/2020 Monday NULL NULL John
24/08/2020 Monday NULL NULL Wayne
但是,我想得到这个:
Date Week_Day Morning Afternoon Evening
24/08/2020 Monday Fred Chuck John
24/08/2020 Monday Markus Bill Wayne
谁能帮帮我? :/
编辑:数据如下所示:
如果我 运行 a select:
,数据看起来像这样start_time end_time name
2020-08-24 05:00:00.000 2020-08-24 12:59:59.000 Frank
2020-08-24 05:00:00.000 2020-08-24 12:59:59.000 Markus
2020-08-24 12:59:59.000 2020-08-24 20:59:59.000 Chuck
2020-08-24 12:59:59.000 2020-08-24 20:59:59.000 Bill
2020-08-24 20:59:59.000 2020-08-25 05:00:00.000 John
2020-08-24 20:59:59.000 2020-08-25 05:00:00.000 Wayne
我想你想要聚合:
SELECT CONVERT(DATE, start_time) as date,
DATENAME(weekday, MIN(start_time)) AS Week_Day,
MAX(CASE WHEN CAST(start_time AS time) = '05:00:00' THEN name END) AS Morning,
MAX(CASE WHEN CAST(start_time AS time) = '12:59:59' THEN name END) AS Afternoon,
MAX(CASE WHEN CAST(start_time AS time) = '20:59:59' THEN name END) AS Evening
FROM (SELECT t.*,
ROW_NUMBER() OVER (PARTITION BY start_time ORDER BY start_time) as seqnum
FROM [somedb].[dbo].[tb_some_table] t
WHERE start_time >= dateadd(day, 2-datepart(dw, getdate()), CONVERT(date, getdate())) AND
start_time < dateadd(day, 9-datepart(dw, getdate()), CONVERT(date, getdate()))
) t
GROUP BY CONVERT(DATE, start_time), seqnum;
请注意,每个日期需要多行。 ROW_NUMBER()
正在确定。
备注:
- 您不需要将日期转换为字符串来提取日期部分。仅当您确实需要特定格式的日期时才使用
convert()
。 - 时间竟然如此准确,这似乎很奇怪。我希望范围。如果确实如此,请提出一个新问题。它会改变查询。
- 仅对字符串和日期常量使用单引号。不要将它们用于列名——当(有一天)您认为您是使用单引号引用列时,这只会导致 hard-to-debug 错误。