case when with count and combining similar values in sql

Solutions on MaxInterview for case when with count and combining similar values in sql by the best coders in the world

we are a community of more than 2 million smartest coders
registration for
employee referral programs
are now open
get referred to google, amazon, flipkart and more
register now
  
pinned-register now
showing results for - "case when with count and combining similar values in sql"
Jerónimo
04 Feb 2016
1SELECT 
2    data.id,
3    data.lastname,
4    data.firstname,
5    SUM(data.WebData) AS WebData,
6    SUM(data.InternalData) AS InternalData,
7    SUM(data.countid) AS Countid
8FROM
9    (
10       SELECT id,lastname,
11              firstname,datasource,
12              CASE WHEN Datasource = 'Web' THEN Count(Datasource) ELSE 0 END WebData,
13              CASE WHEN Datasource = 'Internal' THEN Count(Datasource)ELSE 0 END InternalData,
14             count(id) AS Countid
15
16        FROM Table
17        GROUP BY
18                 id,lastname,firstname,datasource
19     ) AS data
20GROUP BY 
21    data.id,data.firstname,data.lastname