MySQL group by empty and non-empty values - mysql

At present, I have something like:
select sum(total) from table_name where field != ''
UNION
select sum(total) from table_name where field = ''
It works but I'm curious if it is possible to use "group by" to filter by empty and non-empty values?

select SUM(CASE WHEN field != '' THEN total ELSE 0) NONEMPTY,
SUM(CASE WHEN field = '' THEN total ELSE 0) EMPTY from table_name
Try above query.
Here i had used CASE WHEN.

Related

How to write the a new select query with backward compatibility after adding a new column to a MySQL database

I have a MySQL table running for 4 months and I have a select statement in that table, like below.
SELECT
CONCAT(
YEAR(FROM_UNIXTIME(creation_time)),
'-',
IF(
MONTH(FROM_UNIXTIME(creation_time)) < 10,
CONCAT('0', MONTH(FROM_UNIXTIME(creation_time))),
MONTH(FROM_UNIXTIME(creation_time))
)
) AS Period,
(
COUNT(CASE
WHEN system_name = 'System' THEN 1
ELSE NULL
END)
) AS "Some data",
FROM table_name
GROUP BY
Period
ORDER BY
Period DESC
Lately, I've added a new feature and a column, let's say is_rerun. This value is just added and not exist previously. Now, i would like to write a query with the current statement which checks the system_name and also the is_rerun field and if this field exists and value is 1 then return 1 and if the column not exist or it its value is zero, then return null.
I tried IF EXISTS re_run THEN 1 ELSE NULL, but no luck. I can also insert values for the previous runs but i don't want to do that. Is there any solution. Thanks.
SELECT
CONCAT(
YEAR(FROM_UNIXTIME(creation_time)),
'-',
IF(
MONTH(FROM_UNIXTIME(creation_time)) < 10,
CONCAT('0', MONTH(FROM_UNIXTIME(creation_time))),
MONTH(FROM_UNIXTIME(creation_time))
)
) AS Period,
(
COUNT(CASE
WHEN system_name = 'System' AND IF EXISTS is_rerun THEN 1
ELSE NULL
END)
) AS "Some data",
FROM table_name
GROUP BY
Period
ORDER BY
Period DESC
As a starter: you have a group by query, so you need to put is_rerun in an aggregate function.
Based on your description, I think that something like case(case when is_rerun = 1 then 1 end) should do the work: it returns 1 if any is_rerun in the group is 1, else null.
Or if you can live with 0 instead of null, then you can use a simpler expression: max(is_rerun = 1).
Note that your query could be largely simplified as for the date formating logic and the conditional count. I would phrase it as:
select
date_format(from_unixtime(creation_time),'%Y-%m') period,
sum(system_name = 'System') some_data,
max(is_rerun = 1) is_rerun
from mytable
group by period
order by period desc

GROUP BY WITH CASE

I want to select a table with group by like this:
and i want to have a result like this
i compare the state, if i have one up state so the result will up
SELECT name,MAX(state)
FROM table
GROUP BY name
Because state is a varchar (assumption), the maximum is an alphabetical order, favouring 'up'.
If its an enum, it still will work if 'up' is the second element of the enum.
Try this:
Demo on DB Fiddle
select
name,
case
when max(state = 'up') then 'up'
else 'down'
end state
from
mytable
group by name;
result:
name | state
PLR | up
VRL | down
Please, try with below query:
select A.name, (case when A.up_state>0 then 'up' else 'down' end) as state
from (
select name, sum(case when state='up' then 1 else 0 end) as up_state
from table_name
group by name
) as A

How to Perform Subtraction in SQL query based on some conditions?

I want to sum up qty*price where type is 'in' and then subtract the sum of qty*price where type is 'out'. Something like this.
SELECT cid, name, SUM(paid_amt), (SELECT SUM(qty*price) WHERE type = 'in' - SELECT SUM(qty*price) WHERE type = 'out'), type FROMtransactionsGROUP BY cid
Here's is my SQL query.
You can solve your problem by using two different sum expressions, each with a slightly different case statement inside of it. The idea is that your case statement only returns a value to the aggregate sum expression if it's of the correct typing.
select
cid
, sum(case when type = "in" then qty*price else 0 end)
- sum(case when type = "out" then qty*price else 0 end)
from
your_table
group by
cid

check whether all rows in a table have unique values for a column

How to check whether all rows in a table have unique values for a column
having char datatype in a table in MySQL and return the value as yes or no ?
You can try something like this, where ? is the column you want to check :
SELECT IF(t.total = t.total_distinct, 'YES', 'NO') AS result
FROM ( SELECT COUNT(*) AS total
, COUNT(DISTINCT ?) AS total_distinct
FROM tbl
) t
If you ignore NULL values, you can just compare count() and count(distinct):
select (case when count(col) = count(distinct col) then 'All Unique' else 'Duplicates' end)
from table t;
If NULL values are a concern (so NULL would be allowed at most one time), then you can aggregate and look at the maximum count:
select (case when max(cnt) = 1 then 'All Unique' else 'Duplicates' end)
from (select col, count(*) as cnt
from table t
group by col
) col
I would go with the first version of Gordon's answer, but grouped by the Primary key of the table. In other words :
select primary_key_field, (case when count(col) = count(distinct col) then 'All Unique' else 'Duplicates' end)
from table t
group by primary_key_field.

CASE with AS query

I have about the following query:
SELECT *
FROM Product
ORDER BY (SELECT CASE
(SELECT MIN(date) FROM SomethingElse WHERE productId = Product.id) AS here
WHEN NULL
THEN 0
ELSE here
) DESC
However, AS in CASE doesn't work. I need a way to save the value in a variable and use it again.
So you are trying to do CASE when expression IS NULL THEN 0 ELSE expression END?
In this case you can just use COALESCE(expression, 0)