SQL Server 2012 Query Multiple but not all Column Counts -


i'm building query application in sql server 2012. query supposed grab sorts of statuses single table sql server.

tables structured this:

projecttype | team | projectstatus  ----------------------------------  proj1          b      rec  proj1          b      rec  proj1          b      hold  proj2          b      rec  proj3               hold  proj4          c      

my wanted output be:

projecttype | total | team | rec| hold | | -----------------------------------------------   proj1         3      b      2     1      0   proj2         1      b      1     0      0   proj3         1           0     1      0   proj4         1      c      0     0      1 

i think possible because know statuses ever be:

all statuses

rec, ba, dq, p, prev, pred, pcom, 90, 90rev, 90red, 90com, ss, ssred, d, c 

what have tried far:

select      projecttype, team, count(projectstatus)       sites       (projecttype not null , projecttype <> '') , team <> ''  group      projecttype, team    select      s.projecttype, s.team, s.projecttype, c.cnt      sites s inner join      (select projecttype, count(projectstatus) cnt       sites       projectstatus = 'rec' group projecttype) c on s.projecttype = c.projecttype 

here thought add

inner join ( select projecttype, count(projectstatus) cnt sites  projectstatus='rec' group projecttype) c  on s.projecttype = c.projecttype 

per status have in order count them individually...

edit: output wrong counted same projecttype different ones ( had 10 diff ones ) displayed same ones 10 times over

is there better way of doing - can me complete?

based on question, seems have multiple statuses (all known) , want show counts status -- placing statuses across top. there ways using pivot, i'm not @ computer dev resources right now, how (out of head -- untested).

select    projecttype,   count(*) total,   team,   sum(case when projectstatus = 'rec' 1 else 0 end) 'rec',   sum(case when projectstatus = 'hold' 1 else 0 end) 'hold',   sum(case when projectstatus = 'some' 1 else 0 end) 'some'   sites   projectstatus in ('rec', 'hold', 'some') group   projecttype,   team 

sqlfiddle


Comments

Popular posts from this blog

curl - PHP fsockopen help required -

HTTP/1.0 407 Proxy Authentication Required PHP -

c# - Resource not found error -