Discover
Discover

Reputation: 13

Sql Count by multiple Where condition

i've really bad problem with this sql query. base on my tables i need some thing like this result:

table1

Id | Type | Size |Count | OwnerId
___________________________________
1     A      1      12      1
2     A      2      12      1
3     B      1      14      1
4     B      1      20      1
5     A      1      12      2
6     A      1      17      2

table2

Id | name
_________
1     A
2     B

The Result

______________________
Name |  Size1Type1 Count |  Size2Type1 Count |  Size1Type2 Count

thanks indeeeeed .

Upvotes: 0

Views: 109

Answers (2)

Taryn
Taryn

Reputation: 247620

You did not specify what RDBMS you are using but you should be able to get the result by implementing an aggregate function with a CASE statement. This process is similar to a PIVOT:

select t2.name,
  sum(case when t1.size = 1 and t1.type = 'a' then 1 else 0 end) Size1Type1Count,
  sum(case when t1.size = 2 and t1.type = 'a' then 1 else 0 end) Size2Type1Count,
  sum(case when t1.size = 1 and t1.type = 'b' then 1 else 0 end) Size1Type2Count,
  sum(case when t1.size = 2 and t1.type = 'b' then 1 else 0 end) Size2Type2Count
from table1 t1
inner join table2 t2
  on t1.ownerid = t2.id
group by t2.name

See SQL Fiddle with Demo

Result:

| NAME | SIZE1TYPE1COUNT | SIZE2TYPE1COUNT | SIZE1TYPE2COUNT | SIZE2TYPE2COUNT |
--------------------------------------------------------------------------------
|    A |               1 |               1 |               2 |               0 |
|    B |               2 |               0 |               0 |               0 |

If you want to include your count field, then you would use something like this:

select t2.name,
  sum(case when t1.size = 1 and t1.type = 'a' then "Count" end) Size1Type1Count,
  sum(case when t1.size = 2 and t1.type = 'a' then "Count" end) Size2Type1Count,
  sum(case when t1.size = 1 and t1.type = 'b' then "Count" end) Size1Type2Count,
  sum(case when t1.size = 2 and t1.type = 'b' then "Count" end) Size2Type2Count
from table1 t1
inner join table2 t2
  on t1.ownerid = t2.id
group by t2.name;

See SQL Fiddle with Demo

Result:

| NAME | SIZE1TYPE1COUNT | SIZE2TYPE1COUNT | SIZE1TYPE2COUNT | SIZE2TYPE2COUNT |
--------------------------------------------------------------------------------
|    A |              12 |              12 |              34 |          (null) |
|    B |              29 |          (null) |          (null) |          (null) |

Or you could even perform multiple joins on the tables to get the result that you want:

select t2.name, 
  sum(t1_a1."count") Size1Type1Count,
  sum(t1_a2."count") Size2Type1Count,
  sum(t1_b1."count") Size1Type2Count,
  sum(t1_b2."count") Size2Type2Count
from table2 t2
left join table1 t1_a1
  on t1_a1.ownerid = t2.id
  and t1_a1.size = 1 
  and t1_a1.type = 'a'
left join table1 t1_a2
  on t1_a2.ownerid = t2.id
  and t1_a2.size = 2
  and t1_a2.type = 'a'
left join table1 t1_b1
  on t1_b1.ownerid = t2.id
  and t1_b1.size = 1
  and t1_b1.type = 'b'
left join table1 t1_b2
  on t1_b2.ownerid = t2.id
  and t1_b2.size = 2
  and t1_b2.type = 'b'
group by t2.name

See SQL Fiddle with Demo

Upvotes: 2

Mojtaba
Mojtaba

Reputation: 1620

SELECT Name, Type, Size, SUM(Count) AS 'Count' FROM Table1, Table2
WHERE Table1.OwnerID = Tabel2.Id
GROUP BY Name, Type, Size

Upvotes: 0

Related Questions