If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Complex sum and case statement(for me!)

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 11-30-06, 11:11
mudyo30 mudyo30 is offline
Registered User
 
Join Date: Nov 2006
Posts: 5
Complex sum and case statement(for me!)

I have 1 table
TABLE 1
software os osversion manager
office windows 2000 man1
apache solaris 5.6 man2
adobe solaris 5.8 man1
data2 someOS 2.4 man3

.....and goes on
Objective :
Is to list distinct software,os,osversion and count for each manager.
I want to get this table

software os osversion man1 man2 man3
apache solaris 5.6 10 0 0
apache solaris 5.8 1 4 0
adobe solaris 5.8 0 1 0 ...etc

Thanks !

Last edited by mudyo30; 11-30-06 at 11:24.
Reply With Quote
  #2 (permalink)  
Old 12-01-06, 10:59
mudyo30 mudyo30 is offline
Registered User
 
Join Date: Nov 2006
Posts: 5
Anyone ?

I am putting here the problem again . Fields are seperate by ,
TABLE
Software,Version,Manager
software1, 1.1,Manager1
software 2,2.1,Manager 1
software 3,2.0 ,Manager 3
software 1,1.0,Manager 2
software1,1.1,Manager1

And the result should be

Software,Version ,Manager1,Manager2,Manager3
software1,1.1, 2 , 0, 0
software2,2.1,1,0,0
software3,2.0,0,0,1

Basically group by software,version and count for every manager.
Reply With Quote
  #3 (permalink)  
Old 12-01-06, 13:36
guelphdad guelphdad is offline
Registered User
 
Join Date: Mar 2004
Posts: 440
So what have you tried for this query so far?
Reply With Quote
  #4 (permalink)  
Old 12-01-06, 13:44
mudyo30 mudyo30 is offline
Registered User
 
Join Date: Nov 2006
Posts: 5
select software,version,sum(case manager when 'Manager1' then 1 else 0 end) as Manager1, sum(case manager when 'Manager2' then 1 else 0 end) as Manager2, sum(case manager when 'Manager3' then 1 else 0 end) as Manager3 from table GROUP BY software,version;

This kind of query screws up the number if software name is same .

Another query that I tried -
select software,version,count(distinct hostname) as manager1 where manager='manager1' from table GROUP BY software,version;

This WORKS ...but then the output is only for one manager.
software, version, manager1
software1, 1.1, 2
software2, 2.0, 3
etc...etc..etc..


I want
Software,Version ,Manager1,Manager2,Manager3
software1,1.1, 2 , 0, 0
software2,2.1,1,0,0
software3,2.0,0,0,1
Reply With Quote
  #5 (permalink)  
Old 12-01-06, 14:13
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
Quote:
Originally Posted by mudyo30
select software,version,sum(case manager when 'Manager1' then 1 else 0 end) as Manager1, sum(case manager when 'Manager2' then 1 else 0 end) as Manager2, sum(case manager when 'Manager3' then 1 else 0 end) as Manager3 from table GROUP BY software,version;

This kind of query screws up the number if software name is same
please explain why

because that query looks correct

p.s. please do not cross-post the same question into other forums (the other thread has been removed)
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #6 (permalink)  
Old 12-01-06, 14:28
mudyo30 mudyo30 is offline
Registered User
 
Join Date: Nov 2006
Posts: 5
If I use -
select software,version,sum(case manager when 'Manager1' then 1 else 0 end) as Manager1, sum(case manager when 'Manager2' then 1 else 0 end) as Manager2, sum(case manager when 'Manager3' then 1 else 0 end) as Manager3 from table GROUP BY software,version;

Sample Output -
Software | Version | Manager1 | Manager2 |....etc.
+------------+---------------+-------+------+
| Software1 | 1.0 | 204 | 0 |
| Software1 | 2.0 | 2940 | 228 |
| Software1 | 3.0 | 3060 | 1104 |

This is incorrect data.

If I use -
select software,version,count(distinct hostname) as manager1 from table where manager='manager1' GROUP BY software,version;

Output is -
| Software | Version | manager1 |
+------------+-------------+--------------------------+
| Software1 | 1.0 | 34 |
| Software1 | 2.0 | 490 |
| Software1 | 3.0 | 510 |

These are correct numbers.But then I don't have manager2 and 3 numbers here.
Reply With Quote
  #7 (permalink)  
Old 12-01-06, 14:50
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
Quote:
Originally Posted by mudyo30
This is incorrect data.
if that's true, then you did not explain yout situation correctly

here is the test using exactly the same data that you provided
Code:
create table mudyo30
( software  varchar(13)
, version  varchar(13)
, manager  varchar(13)
);
insert into mudyo30 values
 ( 'software1','1.1','Manager1' )
,( 'software2','2.1','Manager1' )
,( 'software3','2.0','Manager3' )
,( 'software1','1.0','Manager2' )
,( 'software1','1.1','Manager1' )
;
select software
     , version
     , sum(case manager when 'Manager1'
                        then 1 else 0 end) as Manager1
     , sum(case manager when 'Manager2'
                        then 1 else 0 end) as Manager2
     , sum(case manager when 'Manager3'
                        then 1 else 0 end) as Manager3
  from mudyo30
group
    by software
     , version

software  version  Manager1  Manager2  Manager3
software1   1.0        0         1         0
software1   1.1        2         0         0
software2   2.1        1         0         0
software3   2.0        0         0         1
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #8 (permalink)  
Old 12-01-06, 16:15
mudyo30 mudyo30 is offline
Registered User
 
Join Date: Nov 2006
Posts: 5
Thank you so much for puttint it like this.

What was happening was probably my master table had bad data, it had several other rows. I was able to fix it eventually by selecting right data from the table in a view.

One more question I had was , if the manager column has the value of 'NULL' how can I use it in sum statement ?
will it be ..

......,sum(case manager when NULL then 1 else 0 end ) as NULL .......?
Reply With Quote
  #9 (permalink)  
Old 12-01-06, 16:31
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
case when manager is null then 1 else 0 end
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On