views:

105

answers:

3

I am most confused with this one, so i d better ask the experts!

These are the rows returned by a custom query of mine.

Col1   Col2   Result
Font   Bold   a
Font   Bold   b
Font   Bold   a
Font   Italic a 

Is there any way of using selecting count in the above (table) results in order getting this one?

Col1  Col2   ResultA  ResultB
Font  Bold   2        1
Font  Italic 1        0

*Update:*The values that should be counted as results are a and b.

p.s. Unfortunately i cannot post the full schema of the table.

+7  A: 

Something like:

SELECT Col1, Col2, 
  SUM(CASE WHEN Result=1 THEN 1 ELSE 0 END) Result1, 
  SUM(CASE WHEN Result=2 THEN 1 ELSE 0 END) Result2 
  FROM yourTable
  GROUP BY Col1, Col2
Lucero
A: 
SELECT col1, col2, COUNT( result ) as result1  FROM table GROUP BY col1, col2

Please explain what it should count to get 1 in first and 0 in second row of your example.

cichy
A: 

Was just typing the same thing as what Lucero posted! +1 to Lucero (not enough rep to do it myself yet).

James Holland