Multiple SUM fields

时间:2016-04-21 22:28:38

标签: php mysql

EDIT: The code works, my bad, i was searching in the wrong date column and the SQL database needed adjustments. Thanks again for the help!


I have multiple columns that I need to SUM and one specifically that has a = to a specific word. The reason why I don't have multiple queries is because I want to display a table in PHP.

When I run the last part it shows up incorrectly for me:

SUM(CASE WHEN STATS = 'cancel' THEN 1 ELSE 0 END) as cancel_stats

Full code:

SELECT suppportrep,

(SELECT SUM(
SUM(option1 IS NOT NULL and option1 <> '') +

SUM(option2 IS NOT NULL and option2 <> '') + 
SUM(option3 IS NOT NULL and option3 <> '')
))  AS total1, 

   SUM(option1 IS NOT NULL and option1 <> '') as option1,
   SUM(option2 IS NOT NULL and option2 <> '') as option2,
   SUM(option3 IS NOT NULL and option3 <> '') as option3,

   SUM(CASE WHEN STATS = 'cancel' THEN 1 ELSE 0 END) as cancel_stats

FROM salestable 
where date2 >= '2016/04/01' 
AND   date2 <= '2016/04/21'
     GROUP BY supportrep
     ORDER BY total1 DESC 

Here is the example of the output, the cancel_stats area is pulling up numbers that are confusing to me

0 个答案:

没有答案