List:General Discussion« Previous MessageNext Message »
From:Paul DuBois Date:April 27 2002 7:29pm
Subject:Re: Multiple Sum Queries
View as plain text  
At 14:24 -0500 4/27/02, dstark@stripped wrote:
>I have a table full of cell phone data with three important columns: 
>ServiceNumber, ratingPeriod (1,2 or 3) and minutes. I want to run a 
>query grouped by serviceNumber that gives me the sum of minutes per 
>rating period for each of the numbers in the other cloumns. So the 
>query result will look like this:
>
>serviceNumber | SUM(of minutes where ratingPeriod =1) | SUM(of 
>minutes where ratingPeriod =2) | SUM(of minutes where ratingPeriod 
>=3)
>
>I was able to do this with other tools using "Transform" and "Pivot" 
>commands which are not present in MySQL.
>
>Is this particular query possible in MySQL? If it is not possible 
>with a query perhaps it would be possible to build a table in memory 
>and add the three columns to it individually?

SELECT serviceNumber, SUM(IF(ratingPeriod=1,minutes,0)),
SUM(IF(ratingPeriod=2,minutes,0)),SUM(IF(ratingPeriod=3,minutes,0))
FROM tbl_name GROUP BY serviceNumber;
Thread
Multiple Sum Queriesdstark27 Apr
  • Re: Multiple Sum QueriesPaul DuBois27 Apr