sql server - SQL Outer Join to show rows with no data -


need out data , im stuck in join swamp. run on ms sql server.

i have list of clients in 1 table (clientgroup)

clientgroup groupid | clientid 1       | 11 1       | 12 1       | 13 1       | 14 1       | 15 

and have table have if clientid has clientactivity

clientcontactlog logid | clientid 1     | 11 2     | 14 3     | 15 4     | 11 5     | 11 6     | 11 

then have table info clientactivity

contactlog logid | logtype | logdate      | logtext 1     | 1       | ’2016-05-16’ | ’toys’ 2     | 1       | ’2016-05-16’ | ’toys’ 3     | 1       | ’2016-05-16’ | ’toys’ 4     | 2       | ’2016-05-17’ | ’lunch’ 5     | 2       | ’2016-05-18’ | ’dinner on mars’ 6     | 1       ! ’2016-05-19’ | ’dinner on mars’ 

i want make mothly statistic (sum on logtypes in contactlog) , include clients have in client list id 1. output shows clients have no record clientcontactlog. need with.. data have input need show 0 on clients has no record.

output should be

clientname | sum(logtype1) client11   | 2 client12   | 0 client13   | 0 client14   | 1 client15   | 1 

thanks input , help

pretty sure mean count(logtype), not sum(logtype), since summing date makes no sense whatsoever. in case, can asking for.

select clientname, isnull(ct, 0) logcount clientgroup cg inner join clients c     on cg.clientid = c.clientid left join (     select logid,          count(*) ct     clientcontactlog     group clientid     ) cl 

notes:

  • i assumed clients table containing client name.
  • i not looking @ contactlog table because not needed provide requesting.

Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -