如何连接来自两个单独的SQL查询输出的行

时间:2019-03-07 23:14:19

标签: sql sql-server

让我们看一下下表:

Name    TIMESTAMP   STATUS

Task1   01-01-2019  COMPLETE
Task1   01-01-2019  COMPLETE
Task2   01-01-2019  COMPLETE
Task3   02-01-2019  NOT COMPLETE
Task4   01-01-2019  COMPLETE    
Task4   02-01-2019  COMPLETE

我希望输出是这样的:

Date              TaskName    How many completed?

January stats     Task1           2
February stats    Task2           1
Day 1 of march    Task3           0
Day 2 of march    Task4           1
Day 3 of march    Task1           0

基本上,我希望输出表显示当月前每个月每个任务的已完成总数,然后在接下来的行中显示该月中每一天的已完成任务数。目前,我有两个单独的输出表,其中一个显示每个月的已完成任务总数,然后另一个输出表显示当前月每天的总已完成任务总数。我如何才能加入这两个国家?

1 个答案:

答案 0 :(得分:0)

这应该有效

with c as 
(
select timestamp, taskname, count(status) as Counter from your_table
group by date, name )
select timestamp as date, name as TaskName, Counter as HowManyCompleted
from c