1) My Table:
Query: select mcdesp,mcopsts from machine
Table:
| mcdesp | mcopsts |
| A | GOOD |
| A | GOOD |
| B | GOOD |
| C | URWP |
| C | GOOD |
| A | URWT |
| A | URWT |
2) My Quries and their outputs:
select mcdesp,count(mcopsts)as TotalMachine from machine group by mcdesp
| mcdesp | TotalMahine |
| A | 4 |
| B | 1 |
| C | 2 |
select mcdesp, count(mcopsts) as GOOD from machine where mcopsts='GOOD' group by mcdesp
| mcdesp | GOOD |
| A | 2 |
| B | 1 |
| C | 1 |
select mcdesp,count(mcopsts) as URWT from machine where mcopsts='URWT' group by mcdesp
| mcdesp | URWT |
| A | 2 |
| B | |
| C |
select mcdesp,count(mcopsts) as URWP from machine where mcopsts='URWP' group by mcdesp
| mcdesp | URWP |
| A | |
| B | |
| C | 1 |
BUT I NEED A RESULT LIKE
| mcdesp | TotalMachine | GOOD | URWP | URWT |
| A | 4 | 2 | 0 | 2 |
| B | 1 | 1 | 0 | 0 |
| C | 2 | 1 | 1 | 0 |
Please some one help me..
Sunny SharmaPosted May 23, 2013, 4:49 AM
Use the query below:
SUM(case when mcopsts='GOOD' then 1 else 0 end) AS 'GOOD',
SUM(case when mcopsts='URWT' then 1 else 0 end) AS 'URWP',
SUM(case when mcopsts='URWT' then 1 else 0 end) AS 'URWP'
FROM machine GROUP BY mcdesp
this will solve the purpose. :)
Don't forget to accept this as answer.
Happy to help !
VenkatesanPosted May 23, 2013, 6:10 AM
Sunny SharmaPosted May 23, 2013, 5:41 AM
Thanks.
VenkatesanPosted May 23, 2013, 5:06 AM
Sunny SharmaPosted May 23, 2013, 4:35 AM