如何在同一日期加入 3 条记录作为 1 条记录?
How to join 3 records as 1 record base on a same date?
这是我写的一些示例查询:
SELECT
CAST(datecolumn AS DATE) AS DateColumn,
COUNT(*) AS count
FROM
dbo.myTableName
WHERE
status = 'stage1'
GROUP BY CAST(datecolumn AS DATE) ORDER BY DateColumn DESC;
SELECT
CAST(datecolumn AS DATE) AS DateColumn,
COUNT(*) AS count
FROM
dbo.myTableName
WHERE
status = 'stage2'
GROUP BY CAST(datecolumn AS DATE) ORDER BY DateColumn DESC;
这是第一个查询的输出:
DateColumn count
------------------
2022-05-26 23
2022-05-25 51
2022-05-24 39
2022-05-23 55
2022-05-22 27
2022-05-21 90
这是第二个查询的输出:
DateColumn count
-----------------
2022-05-26 31
2022-05-25 67
2022-05-24 38
2022-05-23 54
2022-05-22 28
我只想有一个像这样输出的查询
DateColumn stage1count stage2count
-----------------------------------
2022-05-26 23 31
2022-05-25 51 67
2022-05-24 39 38
2022-05-23 55 54
2022-05-22 27 28
感谢回答
你能试试这个吗:
select cast(datecolumn as DATE) as DateColumn,
sum(case when status = 'stage1' then 1 else 0 end) as stage1count,
sum(case when status = 'stage2' then 1 else 0 end) as stage2count
from dbo.myTableName
where status in ('stage1', 'stage2')
group by cast(datecolumn as DATE)
order by DateColumn DESC
另一个注意事项:大多数 SQL 系统将 datecolumn 和 DateColumn 视为相同,因此它在 group by 和 order by 子句中实际使用的有点不明确。我认为 order by 使用的是 select 列表中的转换值,而 groupby 可能使用的是基列(未转换),但我不确定。如果要避免歧义,可以改用分隔标识符“DateColumn”。
@hewszz,你提到在你有两个 table 的情况下你也需要这个。如果您有两个 tables:
,这可能会起作用
select t1.DateColumn, stage1count, stage2count
from (select cast(datecolumn as DATE) as DateColumn,
count(*) as stage1count
from dbo.myTableName1
where status = 'stage1'
group by cast(datecolumn as DATE)) t1
full outer join
(select cast(datecolumn as DATE) as DateColumn,
count(*) as stage2count
from dbo.myTableName2
where status = 'stage2'
group by cast(datecolumn as DATE)) t2
on t1.DateColumn = t2.DateColumn
order by t1.DateColumn DESC
通过将每个 table 单独分组,我们确保 DateColumn 在每一侧都是唯一的,因此每一行最多将与另一个分组查询中的一行连接。通过使用完全外部联接,我们可以确保在给定日期只有 stage1 或 stage2 记录时不会丢失任何行。
这是我写的一些示例查询:
SELECT
CAST(datecolumn AS DATE) AS DateColumn,
COUNT(*) AS count
FROM
dbo.myTableName
WHERE
status = 'stage1'
GROUP BY CAST(datecolumn AS DATE) ORDER BY DateColumn DESC;
SELECT
CAST(datecolumn AS DATE) AS DateColumn,
COUNT(*) AS count
FROM
dbo.myTableName
WHERE
status = 'stage2'
GROUP BY CAST(datecolumn AS DATE) ORDER BY DateColumn DESC;
这是第一个查询的输出:
DateColumn count
------------------
2022-05-26 23
2022-05-25 51
2022-05-24 39
2022-05-23 55
2022-05-22 27
2022-05-21 90
这是第二个查询的输出:
DateColumn count
-----------------
2022-05-26 31
2022-05-25 67
2022-05-24 38
2022-05-23 54
2022-05-22 28
我只想有一个像这样输出的查询
DateColumn stage1count stage2count
-----------------------------------
2022-05-26 23 31
2022-05-25 51 67
2022-05-24 39 38
2022-05-23 55 54
2022-05-22 27 28
感谢回答
你能试试这个吗:
select cast(datecolumn as DATE) as DateColumn,
sum(case when status = 'stage1' then 1 else 0 end) as stage1count,
sum(case when status = 'stage2' then 1 else 0 end) as stage2count
from dbo.myTableName
where status in ('stage1', 'stage2')
group by cast(datecolumn as DATE)
order by DateColumn DESC
另一个注意事项:大多数 SQL 系统将 datecolumn 和 DateColumn 视为相同,因此它在 group by 和 order by 子句中实际使用的有点不明确。我认为 order by 使用的是 select 列表中的转换值,而 groupby 可能使用的是基列(未转换),但我不确定。如果要避免歧义,可以改用分隔标识符“DateColumn”。
@hewszz,你提到在你有两个 table 的情况下你也需要这个。如果您有两个 tables:
,这可能会起作用select t1.DateColumn, stage1count, stage2count
from (select cast(datecolumn as DATE) as DateColumn,
count(*) as stage1count
from dbo.myTableName1
where status = 'stage1'
group by cast(datecolumn as DATE)) t1
full outer join
(select cast(datecolumn as DATE) as DateColumn,
count(*) as stage2count
from dbo.myTableName2
where status = 'stage2'
group by cast(datecolumn as DATE)) t2
on t1.DateColumn = t2.DateColumn
order by t1.DateColumn DESC
通过将每个 table 单独分组,我们确保 DateColumn 在每一侧都是唯一的,因此每一行最多将与另一个分组查询中的一行连接。通过使用完全外部联接,我们可以确保在给定日期只有 stage1 或 stage2 记录时不会丢失任何行。