Query to get count of rows with and without a value - 1query - mysql

I want to count how many rows has given value (1) and count how many row has value(0)
id XXX 1 <== value 0 or 1
id YYY 1
id ZZZ 0
so the result would be
ones | zeros
2 | 0
thx in advance

Something like this should work:
select sum(case when value = 1 then 1 else 0 end) ones
, sum(case when value = 0 then 1 else 0 end) zeros

Try this
select sum(if(value=0,1,0)) as zeros,
sum(if(value=1,1,0)) as ones
from mytable

Related

How to use count in sql based on a IF condition

From this table
groupId
flag
flagValue
1
0
500
2
0
100
1
1
10
2
1
50
3
0
100
1
1
200
3
1
1000
2
1
50
I need this result
groupId
flag1
flag0
valFlag1
valFlag0
totalFlags
1
2
1
210
500
3
2
2
1
100
100
3
3
1
1
1000
100
2
where
flag1 is number of times flag is 1 for a particular group
flag0 is number of times flag is 0 for a particular group
valFlag1 is sum of flagVal when flag is 1
valFlag0 is sum of flagVal when flag is 0
totalFlags is sum of total flags associated with a group
I am stuck as to how to actually count values based on an IF condition.
Anyhelp is appreciated. Thanks.
I have used a table named group_table with your values
Try using this:
SELECT
g.`groupId`,
SUM(g.`flag`=1 ) AS flag1,
SUM(g.`flag`=0) AS flag0,
SUM(CASE WHEN g.`flag`=1 THEN g.`flagValue` ELSE 0 END) AS valFalg1,
SUM(CASE WHEN g.`flag`=0 THEN g.`flagValue` ELSE 0 END) AS valFalg0,
COUNT(*) AS totalFlags
FROM
`group_table` g
GROUP BY g.`groupId`
If you have to use the IF,
SELECT
g.`groupId`,
IF(g.`flag`=1,1,0 ) AS flag1,
IF(g.`flag`=0,1,0) AS flag0,
SUM(IF(g.`flag`=1,g.`flagValue`,0 )) AS valFalg1,
SUM(IF(g.`flag`=0,g.`flagValue`,0 )) AS valFalg0,
COUNT(*) AS totalFlags
FROM
`group_table` g
GROUP BY g.`groupId`, flag1, flag0
They'll produce the same result

SQL division returns NULL value instead of Numeric

I am looking to calculate daily revenue from the data-set below. When there is a 0 that means the asset was not producing revenue and should not be included into the daily revenue calculation. Instead of retrieving a numeric daily revenue, I am returned with a NULL value. The following is my code and the data-set.
**asset_revenue**
15
15
213
0
32
89
-47
SUM([asset_revenue]) / SUM(CASE WHEN asset_revenue <> 0 THEN 7 ELSE NULL END)
In the example above, I am expecting the daily revenue to be 7.54. Is there a reason why SQL is returning a NULL value?
Reason:
SELECT 1 + NULL;
+----------+
| 1 + NULL |
+----------+
| NULL |
+----------+
1 row in set (0.00 sec)
It looks like you are trying to get an average.
Just use 0 instead of NULL. This will result in your denominator equaling the number of rows that have a non-zero asset_revenue.
If the rows represent daily revenue:
SELECT SUM([asset_revenue]) /
CASE
WHEN SUM([asset_revenue]) <> 0 THEN SUM(CASE WHEN asset_revenue <> 0 THEN 1 ELSE 0 END)
ELSE 1 -- Special case where asset_revenue is zero across the entire time period. 0/1 is still zero, so that represents an average revenue of zero.
END
If the rows represent weekly revenue or if the 7 needs to be retained for some other reason, do this:
SELECT SUM([asset_revenue]) /
CASE
WHEN SUM([asset_revenue]) <> 0 THEN SUM(CASE WHEN asset_revenue <> 0 THEN 7 ELSE 0 END)
ELSE 1 -- Special case where asset_revenue is zero across the entire time period. 0/1 is still zero, so that represents an average revenue of zero.
END
#Strawberry is correct as to the reason that your script returns NULL. NULL means you don't know what the value is. If you don't know what a value is, you can't know what happens when that unknown value is put into an arithmetic operation and the result is requested. What is 55 plus some unknown value? I don't know!
In some situations the COALESCE function is handy:
mysql> SELECT COALESCE(123, 1), COALESCE(NULL, 1);
+------------------+-------------------+
| COALESCE(123, 1) | COALESCE(NULL, 1) |
+------------------+-------------------+
| 123 | 1 |
+------------------+-------------------+
1 row in set (0.00 sec)
More specifically:
SUM(asset_revenue) /
COALESCE(SUM(CASE WHEN asset_revenue <> 0 THEN 7 ELSE NULL END), 0)
or maybe
COALESCE( SUM(asset_revenue) /
SUM(CASE WHEN asset_revenue <> 0 THEN 7 ELSE NULL END)
1)
For brevity:
SUM(CASE WHEN asset_revenue <> 0 THEN 7 ELSE NULL END)
-->
7 * SUM(asset_revenue <> 0)
(Except that it can yield 0 instead of NULL)

MySQL How get record who have exactly label 1 and label 2

I have table:
ID Name label
1 Jan 1
2 Jan 2
3 Adam 2
4 Adam 10
5 Kasia 1
I would like get records only with label 1 and label 2
For example:
1 Jan
I have many labels, so subselect in subselect in subselect... is it bad idea
You can do this in various ways. My preferred method is group by and having:
select min(id), name
from table
group by name
having sum(case when label = 1 then 1 else 0 end) > 0 and
sum(case when label = 2 then 1 else 0 end) > 0 ;
For each name, this counts the number of times that "1" appears and that "2" appears. The > 0 ensure that each appears at least once.
One other approach is to use a intersect operator.
select id, name
from tablename where label = 1
intersect
select id, name
from tablename where label = 2

Max and Min in the same table

Suppose I have the following table:
id flag date
1 1 2012-01-01
2 1 2012-02-01
3 1 2012-03-01
4 0 2012-04-01
5 0 2012-05-01
6 0 2012-06-01
Is there anyway I can get maximum date for rows with flag as 1 and minimum date for row with flag as 0 using the same query?
Edited:
I want the result to looks something like this:
max min
2012-03-01 2012-04-01
Thank you.
You can try something like
SELECT MAX(CASE WHEN FLAG = 1 THEN Date ELSE NULL END) MaxFlag1,
MIN(CASE WHEN FLAG = 0 THEN Date ELSE NULL END) MinFlag0
FROM [Table]
try
select flag,
case when flag = 1 then max([date])
when flag = 0 then min([date])
end as date
from your_table
group by flag
You can use a UNION (ALL) to combine the results of two statements. The only restriction on those statements is that they both return the same amount and same type of columns.
Applied to your example, this would become
SELECT MAX(date) FROM YourTable WHERE flag = 1
UNION ALL SELECT MIN(date) FROM YourTable WHERE flag = 0

MySQL query to count non-null values in a single row

I'm trying to put together a MYSQL query that will count the number of Non-Null (or better yet, non-zero) values in select fields in a single row and then sort from lowest to highest (based on the count). For example, I have a table with 5 fields... ID, Name, Score_1, Score_2, Score_3. I want to count how many times the value "0" exists in Score_1, Score_2 and Score_3 for each record, then sort from most non zero values to least.
ID Name Score_1 Score_2 Score_3
1 Dan 8 7 0
2 Joe 0 0 3
3 Chris 0 0 0
4 Mike 4 5 5
I assume the query has to look something like this...
Select ID, Name, Score_1, Score_2, Score_3 where (???) ORDER BY (???)
Output should look like this (ID 4 is displayed first since it has the least amount of non-zero entries)...
ID Name Score_1 Score_2 Score_3
4 Mike 4 5 5
1 Dan 8 7 0
2 Joe 0 0 3
3 Chris 0 0 0
I'm somewhat new to mysql query's, so any help would be greatly appreciated. I thought the COUNT function would help, but that function appears to count columns from all rows. Perhaps there is a way to use the COUNT function and limit it to a singel row so it can be sorted by that row count?
This should do what you want:
SELECT ID, Name, Score_1, Score_2, Score_3
FROM Table1
ORDER BY (Score_1 = 0) + (Score_2 = 0) + (Score_3 = 0)
Result:
ID Name Score_1 Score_2 Score_3
4 Mike 4 5 5
1 Dan 8 7 0
2 Joe 0 0 3
3 Chris 0 0 0
try This:
Select id, Count1, Count2, Count3, Count4
From
(Select
Sum(Case When IsNull(Score_1,0) = 0 Then 1 Else 0 End) Count1,
Sum(Case When IsNull(Score_2,0) = 0 Then 1 Else 0 End) Count2,
Sum(Case When IsNull(Score_3,0) = 0 Then 1 Else 0 End) Count3,
Sum(Case When IsNull(Score_4,0) = 0 Then 1 Else 0 End) Count4
From Table
Group By Id) Z -- This column (Id) better not be the PK for this table!!!
Order By Count1 + Count2 + Count3 + Count4