how to use mysql "count" and "group by" to include 0 counts? - mysql

I'm rookie with sql. I have the following table:
task_name
status
task_01
done
task_02
failed
task_03
done
task_04
done
task_05
failed
task_06
done
task_07
failed
task_08
failed
task_09
failed
task_10
done
task_11
done
task_12
failed
task_13
done
task_14
done
I know that another option for "status" column is "pending", but for now, no row has that status.
So, I want to obtain the count of the status, including the result "pending" to be counted as "0".
I am running:
SELECT status,count(*) FROM test.data where status in ("done","failed","pending") group by status;
And the result is:
status
count(*)
done
8
failed
6
But the desired output is:
status
count(*)
done
8
failed
6
pending
0
How can I make a query to obtain that result?
Note 1: I can't create another table (like a status_label table)
Note 2: sorry for my english, my native language is spanish

Try this:
WITH
-- your input ...
indata(task,status) AS (
SELECT 'task_01','done'
UNION ALL SELECT 'task_02','failed'
UNION ALL SELECT 'task_03','done'
UNION ALL SELECT 'task_04','done'
UNION ALL SELECT 'task_05','failed'
UNION ALL SELECT 'task_06','done'
UNION ALL SELECT 'task_07','failed'
UNION ALL SELECT 'task_08','failed'
UNION ALL SELECT 'task_09','failed'
UNION ALL SELECT 'task_10','done'
UNION ALL SELECT 'task_11','done'
UNION ALL SELECT 'task_12','failed'
UNION ALL SELECT 'task_13','done'
UNION ALL SELECT 'task_14','done'
)
,
-- add an in-line three row table with the three statuses
status(status) AS (
SELECT 'done'
UNION ALL SELECT 'failed'
UNION ALL SELECT 'pending'
)
-- left join your in-line table with the base table
-- and count the base table's statuses
SELECT
status.status
, COALESCE(count(indata.status),0) AS statuscount
FROM status
LEFT JOIN indata USING(status)
GROUP BY status.status;
status | statuscount
---------+-------------
pending | 0
failed | 6
done | 8

The simplest way is a UNION query.
SELECT status,count(*) FROM test.data group by status;
UNION
SELECT 'pending', 0
UNION allows you to combine the output of two identical queries, and even manufacture fake output for a query.
Note: This isn't a robust solution that would be used as a general solution as hard-coding results should be frowned upon, it is the simplest answer to your specific question.

Related

Redshift nested json extraction

I have a table with two columns, one column named user, one json column named js that looks like this:
{"1":{"partner_id":54,"provider_id":13},
"2":{"partner_id":56,"provider_id":8},
"3":{"partner_id":2719,"provider_id":274}}
I want to select all 'provider_id' in one column/row.So it should look like this:
user| provider_ids
0001| 13,8,274
0002| 21,36,57,12
How can I do this? Thanks in advance!
Your provided json format is not so easy to work with.
Crated table for test purposes:
create table json_test as
select '0001' as usr, '{"1":{"partner_id":54,"provider_id":13},
"2":{"partner_id":56,"provider_id":8},
"3":{"partner_id":2719,"provider_id":274}}'
as json_text
union all
select '0002' as usr, '{"1":{"partner_id":54,"provider_id":21},
"2":{"partner_id":56,"provider_id":36},
"2":{"partner_id":56,"provider_id":57},
"3":{"partner_id":2719,"provider_id":12}}'
as json_text;
Query to return results:
with NS AS (
select 1 as n union all
select 2 union all
select 3 union all
select 4 union all
select 5 union all
select 6 union all
select 7 union all
select 8 union all
select 9 union all
select 10
)
select usr,
listagg(trim(TRIM(split_part(SPLIT_PART(js.json_text, '},', NS.n),'"provider_id":',2)),'}'),',') within group(order by null) AS t
from NS
join json_test js ON true and NS.n <= REGEXP_COUNT(js.json_text, '\\},') + 1
group by usr;
Notes:
1) do not name column "user" as it is reserved keyword
2) add as many dummy rows in NS subquery as there is maximum of json provider records
3) Yes, I know, this isn't very readable SQL :D

MySQL select from custom set and compare with table data

Hi I'm trying to solve which elements doesn't exists in my database. In order to do so I want to compare list of integers (output from external script) with data in table. How to do such thing like:
SELECT * FROM (1,1,2,3,5,8,13...) l WHERE l NOT IN (select id from table1);
This is probably best done with a left outer join. But, your problem is creating the table of constants:
SELECT *
FROM (select 1 as id union all select 2 union all select 3 union all select 5 union all
select 8 union all select 13 union all select 21 . . .
) ids
where ids.id NOT IN (select id from table1);
This can have odd behavior, if table1.id is ever NULL. The following works more generally:
SELECT *
FROM (select 1 as id union all select 2 union all select 3 union all select 5 union all
select 8 union all select 13 union all select 21 . . .
) ids left outer join
table1 t1
on ids.id = t1.id
where t1.id is null;
EDIT:
The size of a MySQL query is dictated by the parameter max_packet_size (see here). The most recent version has a limit of 1 Gbyte. You should be able to fit 18,000 rows of:
select <n> union all
into that limit, quite easily. Gosh, I don't even think it would be 1 megabyte. I would say, though, that passing a list of 18,000 ids through the application seems inefficient. It would be nice if one database could just pull the data from the other database, without going through the application.
If your set to compare is huge I'd recommend you to create a temporary table myids with the only column id, put there all your 18K values and run query like that:
select id from myids where myids.id not in (select id from table1);

Force MySQL to return a result for all option within IN

I have a simple MySQL statement:
SELECT q1, COUNT(q1) FROM results WHERE q1 IN ('1','2','3');
Currently there are only results for 1 and 3 - results are:
1 = 6
3 = 7
But what I need is for MySQL to bring back a result for 1,2 and 3 even though 2 has no data, as this:
1 = 6
2 = 0
3 = 7
Any ideas?
This is tricky because no rows match your value (2), they cannot be counted.
I would solve this by creating a temp table containing the list of values I want counts for:
CREATE TEMPORARY TABLE q ( q1 INT PRIMARY KEY );
INSERT INTO q (q1) VALUES (1), (2), (3);
Then do an OUTER JOIN to your results table:
SELECT q.q1, COALESCE(COUNT(*), 0) AS count
FROM q LEFT OUTER JOIN results USING (q1)
GROUP BY q.q1;
This way each value will be part of the final result set, even if it has no matching rows.
Re comment from #Mike Christensen:
MySQL doesn't support CTE's, in spite of it being requested as far back as 2006: http://bugs.mysql.com/bug.php?id=16244
You could do the same thing with a derived table:
SELECT q.q1, COALESCE(COUNT(*), 0) AS count
FROM (SELECT 1 AS q1 UNION ALL SELECT 2 UNION ALL SELECT 3) AS q
LEFT OUTER JOIN results USING (q1)
GROUP BY q.q1;
But this creates a temp table anyway.
A SQL query doesn't really have a way to refer to the values in your IN clause. I think you'd have to break this down into one query for each value. Something like:
SELECT 1 as q1, COUNT(1) FROM results WHERE q1 = '1'
UNION ALL
SELECT 2 as q1, COUNT(1) FROM results WHERE q1 = '2'
UNION ALL
SELECT 3 as q1, COUNT(1) FROM results WHERE q1 = '3'
Fiddle
Note: If there are a lot of values in your IN clause, you might be better off to write your code in a way where missing values are assumed to have zero.
In general, you cannot query something that does not exists. So, you must create data for it. Use union to add those missing data values.
select q1, COUNT(*)
from results
where q1 in ('1','2','3')
group by q1
union
select q1, 0
from (
select '1' as q1
union
select '2'
union
select '3'
) as q
where q1 not in (
select q1
from results
)

SQL query return what's NOT in table [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
SQL: find missing IDs in a table
getting values which dont exist in mysql table
Just wondering, is it possible to have a query that somehow tells you the values it did not find in a table?
So if I had a query SELECT * FROM mytable WHERE id IN (1,2,3,4,5,6,7,8,9) and only 2,3,6,7,9 was returned. I wouldd like to know that 1,4,5,8 were not found.
It will be a little hard to do a manual comparision, because this is going to be run over apx 2,000+ rows in a table (the id's are going to be provided via a csv file which can be copied into the query)
Thanks in advance
This is probably silly, but what about creating a temporary table containing all your IDs from which you'll substract the result of your SELECT query ?
Untested, but in theory:
Table 1:
+----+-----+
| id | num |
+----+-----+
Table 2:
+----+
| id |
+----+
Table 1 contains the data you're looking for (and num is any field containing any data)
Table 2 contains the IDs from the CSV
SQL:
SELECT COUNT(`Table1`.`num`) AS `count`
FROM `Table1`
LEFT JOIN `Table2` ON `Table1`.`id` = `Table2`.`id`
WHERE `count` = 0
Quick solution, open your csv file, replace all comma's with " union select " put select in front of that line and use it as the first line of the query at the bottom query.
So 1,2,3 becomes
Select 1 union select 2 union select 3
Use this in the query below
Select 1 union select 2 union select x -- replace this line with the line generated from your csv
Except
(
Select id from mytable
)
What about:
SELECT *
FROM (select 1 as f
UNION
SELECT 2 as f
UNION
SELECT 3 as f
UNION
SELECT 4 as f
UNION
SELECT 5 as f
UNION
SELECT 6 as f
UNION
SELECT 7 as f
UNION
SELECT 8 as f
UNION
SELECT 9 ) as s1
WHERE f NOT IN (SELECT id FROM mytable);

Counting rows matching each of the multiple conditions in MySQL

Please help me figure a single query that will transform the data below...
|id |status_a |status_b |
|+++++++++++++++++++++++|
| 1|active |inactive |
...into this one.
|status_group |count|
|++++++++++++++++++++++++|
|status_a.active | 1|
|status_b.inactive | 1|
edit: If a single pass query is possible then that will be better. Also, does a query with unions does a single pass?
If status can be either only active or inactive, I'd suggest a different approach:
SELECT
sum(if(status_a='active',1,0)) AS status_a_active,
sum(if(status_a='inactive',1,0)) AS status_a_inactive,
sum(if(status_b='active',1,0)) AS status_b_active,
sum(if(status_b='inactive',1,0)) AS status_b_inactive
FROM table
Otherwise you need to use the UNION approach, but I'd do it a little differently. First, you can use UNION ALL, because you don't need to remove duplicates in the result. I'd also use GROUP BY only once like this:
SELECT status_group, count(id)
FROM (
SELECT CONCAT('status_a.', status_a) AS status_group, id FROM table
UNION ALL
SELECT CONCAT('status_b.', status_b) AS status_group, id FROM table
) a
GROUP BY status_group
I have a solution that uses UNIONs. Shown here:
SELECT 'status_a.active' AS status_group, COUNT(*) AS count FROM `test` WHERE status_a = 'active'
UNION
SELECT 'status_a.inactive' AS status_group, COUNT(*) AS count FROM `test` WHERE status_a = 'inactive'
UNION
SELECT 'status_b.active' AS status_group, COUNT(*) AS count FROM `test` WHERE status_b = 'active'
UNION
SELECT 'status_b.inactive' AS status_group, COUNT(*) AS count FROM `test` WHERE status_b = 'inactive'
Basically, it queries each condition for status_a or status_b being active or not. We get four such queries and we apply UNION to all of them.
I suppose, I've to move my comment a while ago which is also a shorter solution here than hw's.
SELECT CONCAT('status_a.', status_a) AS stat, COUNT(id) FROM base GROUP BY stat
UNION
SELECT CONCAT('status_b.', status_b) AS stat, COUNT(id) FROM base GROUP BY stat