Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GROUP BY with COUNT condition

I have a result set such as:  

  Code      No
   1         *
   1         -
   1         4
   1         
   1

Now i basically want a query that has 2 columns, a count for the total amount and a count for those that dont have numbers.

  Code      No_Number    Total
   1         4             5

Im assuming this needs a group by and a count but how can i do the 2 different counts in a query like this?

This is what i had so far, but i am a bit stuck with the rest of it

 SELECT CODE,NO
 Sum(Case when No IN ('*', '-', '') then 1 else 0 end) as Count
like image 398
Ryan Gadsdon Avatar asked Dec 21 '25 14:12

Ryan Gadsdon


1 Answers

I think you basically just need GROUP BY:

SELECT CODE,
       SUM(Case when No IN ('*', '-', '') then 1 else 0 end) as Count,
       COUNT(*) as total
FROM t
GROUP BY CODE;
like image 185
Gordon Linoff Avatar answered Dec 24 '25 03:12

Gordon Linoff



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!