How to get count of columns using single queries - mysql

I have a table in my database from which i want to fetch data on the basis of count
my table name is syncbill and columns are Cancelled and Noofprints i am trying to write a query to fetch the count of cancelled and count of noofbills using single query
For cancelled i want to fetch count of cancelled where cancelled<>Y
for Noofprints i want to fetch count of noofprints where noofprints>1
I am using
select count(CANCELLED) as canceledbill,count(NOOFPRINTS) as duplicatebill
from syncbill where CANCELLED<>'Y' and NOOFPRINTS>1`
this query but it is giving me the same no of counts for both
any one out-here please guide me

you can use sum() with case expreseeion
SELECT
sum(CASE WHEN CANCELLED <> 'Y' THEN 1 else 0 END) AS canceledbill,
sum(CASE WHEN NOOFPRINTS > 1 THEN 1 else 0 END) AS duplicatebill
FROM table_name;

Use conditional aggregation:
SELECT
COUNT(CASE WHEN CANCELLED <> 'Y' THEN 1 END) AS cancelledbill,
COUNT(CASE WHEN NOOFPRINTS > 1 THEN 1 END) AS duplicatebill
FROM yourTable;
If you are actually using MySQL, then the above can be simplified to this:
SELECT
SUM(CANCELLED <> 'Y') AS cancelledbill,
SUM(NOOFPRINTS > 1) AS duplicatebill
FROM yourTable;

USE CASE WHEN Expression
select
sum(case when CANCELLED <> 'Y' then 1 else 0 end) AS cancelledbill,
sum(case when NOOFPRINTS > 1 then 1 else 0 end) AS duplicatebill
FROM tablename;

Related

Why are my various CASE WHEN functions returning the same values?

Im trying to write a query that returns a count depending on the value of a feedback field that ranges from 0-5 (0 meaning that it was not rated).
I want:
Count of all rows ( anything rated 1 or greater)
Count of all rows rated as 1 (anything = 1)
And all rows rated as 1 and also is the first iteration of a given task (anything rated =1 and iteration = 0)
I have written this query but I am getting the same value for all counts:
select
DATE_FORMAT(created_at,'%M') as Month,
COUNT(CASE WHEN rate > 0 THEN 1 ELSE 0 END) AS total,
COUNT(CASE WHEN rate = 1 THEN 1 ELSE 0 END) AS Rated_1,
COUNT(CASE WHEN client_feedback = 1 AND index = 0 THEN 1 ELSE 0 END) AS first_iteration_rated_1
from tablexxx
where created_at between date('2022-04-01') and date('2022-10-01')
GROUP BY Month
Try to use SUM() instead of COUNT().
Count() will count up regardless of the value being 0 or 1.
you can have two approaches:
method 1: use NULL in else part of the CASE
select
DATE_FORMAT(created_at,'%M') as Month,
COUNT(CASE WHEN rate > 0 THEN 1 ELSE null END) AS total,
COUNT(CASE WHEN rate = 1 THEN 1 ELSE null END) AS Rated_1,
COUNT(CASE WHEN client_feedback = 1 AND index = 0 THEN 1 ELSE null END) AS first_iteration_rated_1
from tablexxx
where created_at between date('2022-04-01') and date('2022-10-01')
GROUP BY Month
method 2: use sum instead of count
select
DATE_FORMAT(created_at,'%M') as Month,
SUM(CASE WHEN rate > 0 THEN 1 ELSE 0 END) AS total,
SUM(CASE WHEN rate = 1 THEN 1 ELSE 0 END) AS Rated_1,
SUM(CASE WHEN client_feedback = 1 AND index = 0 THEN 1 ELSE 0 END) AS first_iteration_rated_1
from tablexxx
where created_at between date('2022-04-01') and date('2022-10-01')
GROUP BY Month

Multiple Count with Multiple column

I am new in sql. I want to count something like:
Select count(*) from table where col1= x and col2=x and Col3=x.
I need to count the same value in all different column.
Any help will be appreciated.
You can use conditional aggregation :
Select sum(case when col1='x' then 1 else 0 end) as count_col1,
sum(case when col2='x' then 1 else 0 end) as count_col2,
sum(case when col3='x' then 1 else 0 end) as count_col3
from tab;
If you want to have sum of these count values, consider the above query as an inner and use the following :
Select q.*,
q.count_col1 + q.count_col2 + q.count_col3 whole_sum
from
(
Select sum(case when col1='x' then 1 else 0 end) as count_col1,
sum(case when col2='x' then 1 else 0 end) as count_col2,
sum(case when col3='x' then 1 else 0 end) as count_col3
from tab
) q
Rextester Demo

SQL count with having clause wrong result

I'm trying to understand how to count mysql row's according to HAVING.
SELECT COUNT(*),
SUM(CASE WHEN sentby='$user_id' AND hiddenbysentby=0 THEN 1 ELSE 0 END) as sentbyuser,
SUM(CASE WHEN sentto='$user_id' AND hiddenbysentto=0 THEN 1 ELSE 0 END) as senttouser
FROM cb_users.user_pm
WHERE title LIKE '%$search%'
HAVING senttouser = 1 OR sentbyuser = 1
I want to count rows that are matching these criteria
SUM(CASE WHEN sentby='$user_id' AND hiddenbysentby=0 THEN 1 ELSE 0 END)
SUM(CASE WHEN sentto='$user_id' AND hiddenbysentto=0 THEN 1 ELSE 0 END)
I have second function that display this data without COUNT(*), and it works fine.
But this query selects all rows no matter if hiddenbysentby = 1 or 0 AND hiddenbysentto = 1 or 0
You should change your HAVING clause to:
HAVING senttouser >= 1 OR sentbyuser >= 1
When you are summing rows, your values are going to equal the total number of rows located for each, therefore, unless you only had exactly one row that meets your criteria, you won't get the result you're looking for.
You should move condition by hiddenbysentby and hiddenbysentto fields in WHERE clause and add a condition by user id
SELECT COUNT(*),
SUM(CASE WHEN sentby='$user_id' THEN 1 ELSE 0 END) as sentbyuser,
SUM(CASE WHEN sentto='$user_id' THEN 1 ELSE 0 END) as senttouser
FROM cb_users.user_pm
WHERE title LIKE '%$search%' AND (hiddenbysentby=0 AND sentby='$user_id') OR (hiddenbysentto=0 AND sentto='$user_id')
HAVING senttouser = 1 OR sentbyuser = 1

SQL using CASE in count and group by

I'm using CASE to categorize data in the table and count them but the results aren't accurate
live demo [here]
select DATE(date) as day, count(*),
count(distinct case when name = 'fruit' then 1 else 0 end) as fruits,
count(distinct case when name = 'vege' then 1 else 0 end) as vege,
count(distinct case when name = 'sweets' then 1 else 0 end) as sweets
from food
group by day
with rollup
I'm not sure if the issue is with CASE or in the string matching = because there's no 'sweets' still it counts 1?
any pointers I'd be grateful
Your problem is that COUNT counts every result that is not NULL. In your case you are using:
COUNT(distinct case when name = 'sweets' then 1 else 0 end)
So, when the name is not sweets, it counts the 0. Furthermore, since you are using DISTINCT, it counts just one or two values. You should either use SUM or remove the DISTINCT and the ELSE 0:
SELECT DATE(date) as day,
COUNT(*),
SUM(CASE WHEN name = 'fruit' THEN 1 ELSE 0 END) as fruits,
SUM(CASE WHEN name = 'vege' THEN 1 ELSE 0 END) as vege,
SUM(CASE WHEN name = 'sweets' THEN 1 ELSE 0 END) as sweets
FROM food
GROUP BY DAY
WITH ROLLUP
Or:
SELECT DATE(date) as day,
COUNT(*),
COUNT(CASE WHEN name = 'fruit' THEN 1 ELSE NULL END) as fruits,
COUNT(CASE WHEN name = 'vege' THEN 1 ELSE NULL END) as vege,
COUNT(CASE WHEN name = 'sweets' THEN 1 ELSE NULL END) as sweets
FROM food
GROUP BY DAY
WITH ROLLUP
Here is a modified sqlfiddle.
You can't group by an alias. You have to group by the expression.
group by date(date)
You can group on an Alias:
SELECT
FROM_UNIXTIME(UnixTimeField, '%Y') AS 'Year'
,FROM_UNIXTIME(UnixTimeField, '%m') AS 'Month'
FROM table p
GROUP BY Year, Month

mysql views my query output i need different manner

This is my query
Select Count(Case When campaign_pid != 0 Then 1 End)
As Email, Count(Case When sms_pid != 0 Then 1 End)
As Sms, Count(Case When survey_pid != 0 Then 1 End)
As Survey From tablename
Please help me This is my query output
Email Sms Survey
21 1 4
i need output like this
name value
Email 21
Sms 1
Survey 4
Please tell me how to get like above optput
Something like the following should work
SELECT 'Email' AS Name, Count(Case When campaign_pid != 0 Then 1 End) As Value From tablename
UNION SELECT 'SMS', Count(Case When sms_pid != 0 Then 1 End)s From tablename
UNION SELECT 'Survey', Count(Case When survey_pid != 0 Then 1 End) From tablename
or better still
SELECT 'Email' AS Name, COUNT() AS Value FROM tablename WHERE campaign_pid <> 0
UNION SELECT 'SMS', COUNT() FROM tablename WHERE sms_pid <> 0
UNION SELECT 'Survey', COUNT() FROM tablename WHERE survey_pid <> 0
Not 100% sure of your additional question, maybe this is what you want
SELECT COUNT(DISTINCT some_unique_col)
FROM table_name
WHERE campaign_pid <> 0
AND sms_pid <> 0
AND survey_pid <> 0
Maybe you want SUM instead of COUNT, if you could provide some sample data and expected results from that it would help