如何获得列数

问题描述 投票:-1回答:3

我有一个如下查询

select project_task_id,
       status_id,
       sum(case when StatusID=1 then 1 else 0 end) as task_id=1,
       sum(case whenStatusID=2 then 1 else 0 end) as task_id=2,
       sum(case when StatusID=3 then 1 else 0 end) as task_id=3,
       sum(case when StatusID=4 then 1 else 0 end) as task_id=4,
       sum(case when StatusID=5 then 1 else 0 end) as task_id=5,
       sum(case when StatusID=6 then 1 else 0 end) as task_id=6,
       sum(case when StatusID=7 then 1 else 0 end) as task_id=7,
from"Projects".work_unit_status 
group by project_task_id,status_id;

我得到以下附加输出:

https://i.stack.imgur.com/1wfD1.png

我想得到以下预期的输出:

https://i.stack.imgur.com/Zql9z.png

如果status_id为空,请包括零,请对此提供任何帮助

mysql sql postgresql
3个回答
0
投票

试试这个:使用所有sum列的添加

 select project_task_id,status_id,
isnull(sum(case when StatusID=1 then 1 else 0 end),0)+
isnull(sum(case whenStatusID=2 then 1 else 0 end),0) +
isnullsum(case when StatusID=3 then 1 else 0 end),0) +
isnullsum(case when StatusID=4 then 1 else 0 end),0)+
isnullsum(case when StatusID=5 then 1 else 0 end),0) +
isnullsum(case when StatusID=6 then 1 else 0 end),0) +
isnullsum(case when StatusID=7 then 1 else 0 end),0) as count_status
from"Projects".work_unit_status group by project_task_id,status_id

0
投票

在你的情况下使用in

with t1 as (
 select project_task_id,
           status_id,
           sum(case when StatusID in (1,2,3,4,5,6,7) then 1 else 0) 
           as sum_s               
    from "Projects".work_unit_status

    group by project_task_id,status_id
              ) ,
           t2 as
             (
                 select * from (
                 select 1 as statusid 
                          union
                 select 2
                 union 
                 select 3
                 union
                 select 4 
                 union
                 select 5 
                 union
                 select 6
                  union
                select 7 ) t
             ) select t1.project_task_id,
                   t2.statusid,
                    case when t1.sum_s>0 or not null
                    then sum_s else 0 end as total
                  t2 full join t1 on t2.statusid=t1.status_id

0
投票

在不知道确切的表结构的情况下,我假设status_id和statusId引用同一列。 (如果它们是不同的列,我们需要在COUNT中使用StatusId。)

根据预期的输出,您希望按project_task_id计算status_id和group。为了确保为每个任务表示每个状态,首先,我们需要创建所有可能的project_task_id / status_id组合的子查询。然后我们将其与原始表的聚合值一起使用。

select
  ps.project_task_id,
  ps.status_id,
  count(w.status_id) as total
from (
  select distinct
      project_task_id,
      s.status_id
  from work_unit_status
  cross join (select distinct status_id from work_unit_status) s
  ) ps
left join work_unit_status w
    on ps.project_task_id = w.project_task_id and ps.status_id = w.status_id 
group by
    ps.project_task_id,
    ps.status_id

如果您确实需要将状态从1到7进行硬编码,请使用以下查询。

select
  ps.project_task_id,
  ps.status_id,
  count(w.status_id) as total
from (
  select distinct
      project_task_id,
      s.status_id
  from work_unit_status
    cross join (
          select 1 as status_id
    union select 2
    union select 3
    union select 4
    union select 5
    union select 6
    union select 7
    ) s
  ) ps
left join work_unit_status w
  on ps.project_task_id = w.project_task_id and ps.status_id = w.status_id 
group by
  ps.project_task_id,
  ps.status_id
order by
  ps.project_task_id,
  ps.status_id
© www.soinside.com 2019 - 2024. All rights reserved.