结合3个从同一表中选择陈述到SQL中的1个表
我正在尝试运行3个不同的在1个表上选择查询
,我返回创建的计数,完成,未决基于 create_date 和 modified_date 的任务 我可以在上区分 与等待在 modified_date中基于字段状态
分别运行每个查询时返回正确的答案。
但是我想要的是将这3个查询组合在一个表中,如下所示:
+---------------------- +------------------+---------------------+
| Total_created_files | Total_done_files | Total_pending_files |
+-----------------------+------------------+---------------------+
| 14 | 40 | 9 |
+-----------------------+------------------+---------------------+
代码:
select
count([create_date]) as Total_created_files
FROM [TEC_APP].[dbo].[case_to_do]
where [create_date] >='2022-05-01 00:00:00.000'
AND
CAST([create_date] AS date) <= CAST(GETDATE() AS date)
select
count([modified_date]) as Total_done_files
FROM [TEC_APP].[dbo].[case_to_do]
where [modified_date] >='2022-05-01 00:00:00.000'
AND
CAST([modified_date] AS date) <= CAST(GETDATE() AS date)
AND
status = 'DONE'
select
count([modified_date]) as Total_pending_files
FROM [TEC_APP].[dbo].[case_to_do]
where [modified_date] >='2022-05-01 00:00:00.000'
AND
CAST([modified_date] AS date) <= CAST(GETDATE() AS date)
AND
status = 'Pending'
I am trying to run 3 different select query
on 1 table where i return the count of created, done, pending tasks based on created_date and modified_date
where i can differentiate the done from pending in modified_date based on the field status
when i run each query separately it return correct answer.
But what i want is to combine these 3 queries in one table as below:
+---------------------- +------------------+---------------------+
| Total_created_files | Total_done_files | Total_pending_files |
+-----------------------+------------------+---------------------+
| 14 | 40 | 9 |
+-----------------------+------------------+---------------------+
code:
select
count([create_date]) as Total_created_files
FROM [TEC_APP].[dbo].[case_to_do]
where [create_date] >='2022-05-01 00:00:00.000'
AND
CAST([create_date] AS date) <= CAST(GETDATE() AS date)
select
count([modified_date]) as Total_done_files
FROM [TEC_APP].[dbo].[case_to_do]
where [modified_date] >='2022-05-01 00:00:00.000'
AND
CAST([modified_date] AS date) <= CAST(GETDATE() AS date)
AND
status = 'DONE'
select
count([modified_date]) as Total_pending_files
FROM [TEC_APP].[dbo].[case_to_do]
where [modified_date] >='2022-05-01 00:00:00.000'
AND
CAST([modified_date] AS date) <= CAST(GETDATE() AS date)
AND
status = 'Pending'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据需要,将条件聚合使用
计数
。我在这里做了几个假设,但这应该是您所追求的:Use conditional aggregation to
COUNT
as needed. I make a couple of assumptions here, but this should be what you are after:将条件聚合与
案例表达式
:Use conditional aggregation with
case expression
: