I have run into an issue with MySQL's ROLLUP and dealing with the resulting NULLs. The IFNULL/COALESCE functions work well with plain columns, but seem to break down when used with date functions. Examples follow:
SELECT
YEAR(date_time) AS Year,
count(x) AS Count
FROM mytable
GROUP BY year WITH ROLLUP
returns (as expected)
Year Count
---- -----
2015 3
2016 2
NULL 5
When I query for non-date columns (varchar, for example), I can deal with the NULL values by using IFNULL or COALESCE functions to replace NULL values with strings. However, when I apply the same logic to the above query, it does not seem to work.
SELECT
COALESCE(YEAR(date_time), 'moo') AS Year,
count(x) AS Count
FROM mytable
GROUP BY year WITH ROLLUP
or
SELECT
IFNULL(YEAR(date_time), 'moo') AS 'year',
count(x) AS Count
FROM mytable
GROUP BY year WITH ROLLUP
returns
Year Count
---- -----
2015 3
2016 2
NULL 5
instead of expected
Year Count
---- -----
2015 3
2016 2
moo 5
Any ideas, suggestions?
If you're trying to target the NULL's generated during the aggregate rollup calculations (as opposed to NULLs in your raw data), you're referring to the wrong NULLs. If so, this is probably what you are trying to do:
SELECT
IFNULL(m.year, 'moo')
FROM
(SELECT
YEAR(date_time) AS 'year'
FROM
mytable
GROUP BY
year
WITH ROLLUP) m
Here's the sqlfiddle.
Related
i have an orders table, and i need to fetch the orders record by month. but i have terms if there is no data in a month it should still show the data but forcing to zero like this:
what i have done is using my query:
select sum(total) as total_orders, DATE_FORMAT(created_at, "%M") as date
from orders
where is_active = 1
AND tenant_id = 2
AND created_at like '%2021%'
group by DATE_FORMAT(created_at, "%m")
but the result is only fetched the existed data:
can anyone here help me to create the exactly query?
Thank you so much
Whenever you're trying to use a value that doesn't exist in the table, one option is to use a reference; whether it's from a table or a query-generated value.
I'm guessing that in terms of date data, the column created_at in table orders may have a complete list all the 12 months in a year regardless of which year.
Let's assume that the table data for orders spans from 2019 to present date. With that you can simply create a 12 months reference table for a LEFT JOIN operation. So:
SELECT MONTHNAME(created_at) mnt FROM orders GROUP BY MONTHNAME(created_at);
You can append that into your query like:
SELECT IFNULL(SUM(total),0) as total_orders, mnt
from (SELECT MONTHNAME(created_at) mnt FROM orders GROUP BY MONTHNAME(created_at)) mn
LEFT JOIN orders o
ON mn.mnt=MONTHNAME(created_at)
AND is_active = 1
AND tenant_id = 2
AND created_at like '%2021%'
GROUP BY mnt;
Apart from adding the 12 months sub-query and a LEFT JOIN, there are 3 other changes from your original query:
IFNULL() is added to the SUM() operation in SELECT to return 0 if the value is non-existent.
All the WHERE conditions has been switched to ON since remaining it as WHERE will make the LEFT JOIN becoming a normal JOIN.
GROUP BY is using the sub-query generated month (mnt) value instead.
Taking consideration of table orders might not have the full 12 months, you can generate it from query. There are a lot of ways of doing it but here I'm only going to show the UNION method that works with most MySQL version.
SELECT MONTHNAME(CONCAT_WS('-',YEAR(NOW()),mnt,'01')) dt
FROM
(SELECT 1 AS mnt UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION
SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION
SELECT 9 UNION SELECT 10 UNION SELECT 11 UNION SELECT 12) mn
If you're using MariaDB version that supports SEQUENCE ENGINE, the same query above is much shorter:
SELECT MONTHNAME(CONCAT_WS('-',YEAR(NOW()),mnt,'01'))
FROM (SELECT seq AS mnt FROM seq_1_to_12) mn
I'm using MariaDB 10.5 in this demo fiddle however it seems like the month name ordering is based on the name value rather than the month itself so it looks un-ordered. It's in the correct order if it's in MySQL 8.0 fiddle though.
Thanks all for the answers & comments i really appreciate it.
i solved it by create table helper for static months then use union and aliasing, since i need the months in indonesia, i create case-when function too.
so, the query is like this:
SELECT total_orders,
(CASE date WHEN 01 THEN 'Januari'
WHEN 02 THEN 'Februari'
WHEN 03 THEN 'Maret'
WHEN 04 THEN 'April'
WHEN 05 THEN 'Mei'
WHEN 06 THEN 'Juni'
WHEN 07 THEN 'Juli'
WHEN 08 THEN 'Agustus'
WHEN 09 THEN 'September'
WHEN 10 THEN 'Oktober'
WHEN 11 THEN 'November'
WHEN 12 THEN 'Desember'
ELSE date END ) AS date
FROM (SELECT SUM(total) AS total_orders,
DATE_FORMAT(created_at, "%m") AS date
FROM orders
WHERE is_active = 1
AND tenant_id = 2
AND created_at like '%2021%'
GROUP BY DATE_FORMAT(created_at, "%m")
UNION
SELECT 0 AS total_orders,
code AS date
FROM quantum_default_months ) as Q
GROUP BY date
I still don't know if this query is fully correct or not, but I get my exact result.
cmiiw.
thanks all
I have this table below. I want to count and select the values within a month range.
Here's my query
select count(*) as c, monthname(file_date) as mn
from baguio_patrolcase
where monthname(file_date) between 'January' and 'July'
group by monthname(file_date)
order by file_date
What I want to achive is that it will also count and select the values from February an June. How will I do this?
When you convert the date to a month name, you are comparing strings. Here are two options to do what you want:
where month(file_date) between 2 and 6
where file_date >= '2015-02-01' and file_date < '2015-07-01'
The second is better, because it allows the engine to use an index on file_date (if available).
Also, between keeps the end values (it is inclusive). So, if you want February through June, then use those months, rather than 1 and 7.
What I have understood, you can use in clause:
select count(*) as c, monthname(file_date) as mn
from baguio_patrolcase
where monthname(file_date) in('January', 'July')
group by monthname(file_date)
order by file_date
Add the name of all the required months in the in clause.
You can see the SQLFiddle Demo here.
I have a database with multiple columns from which I have created this view where I have multiple rows similar to the ones shown below. The data is available for each day of the month from 2009 to 2010 and for all the month for the 5 names given. I have to get the 'Name' for which the occurrence of category 'Super' is more than 5 times each month and list them out separately for each month. The view contains data for all months together.
Name Dates Category
--------------------------------
PAT 2009-01-01 Super
YAT 2009-01-01 No
ROT 2009-01-01 No
SUP 2009-01-01 Super
ANT 2009-01-01 Super
I tried getting a count of the Name in MySQL using
SELECT `NAME`,`DATES`
FROM (
SELECT `NAME`, `CATEGORY`,MONTH(`DATES`)
FROM VIEW
GROUP BY `NAME`, `CATEGORY`,MONTH(`DATES`)
HAVING COUNT(`CATEGORY`)>5
) a
GROUP BY `NAME`
HAVING count(`CATEGORY`)>5;
But it does not return any rows.
You're trying to group your rows into months. The best way to do this is to start with an expression that will take any date and convert it to the first day of the month in which it occurs. That expression is.
DATE(DATE_FORMAT(dates, '%Y-%m-01'))
Next, you use this expression in a query with a GROUP BY clause.
SELECT NAME, CATEGORY,
DATE(DATE_FORMAT(DATES, '%Y-%m-01')) DATES
FROM VIEW
GROUP BY NAME, CATEGORY, DATE(DATE_FORMAT(DATES, '%Y-%m-01'))
HAVING COUNT(*) > 5
This will yield all the name / category / month combinations occurring more than five times.
I think that's what you want. But maybe you want all the monthly items listed in any month where the Super category appears more than five times for some name. To do that first we write a subquery to get a list of those dates:
SELECT DATE(DATE_FORMAT(DATES, '%Y-%m-01')) DATES
FROM VIEW
WHERE CATEGORY = 'Super'
GROUP BY NAME, DATE(DATE_FORMAT(DATES, '%Y-%m-01'))
HAVING COUNT(*) > 5
Then we write a main query to get all the data
SELECT DISTINCT
NAME, CATEGORY,
DATE(DATE_FORMAT(DATES, '%Y-%m-01')) DATES
FROM VIEW
WHERE DATE(DATE_FORMAT(DATES, '%Y-%m-01')) IN
(
SELECT DATE(DATE_FORMAT(DATES, '%Y-%m-01')) DATES
FROM VIEW
WHERE CATEGORY = 'Super'
GROUP BY NAME, DATE(DATE_FORMAT(DATES, '%Y-%m-01'))
HAVING COUNT(*) > 5
)
The trick to getting this sort of thing to work is choosing the right date-arithmetic expression to use in your GROUP BY clause. The functions like DAY(), MONTH(), and YEAR() are surprisingly difficult to use correctly, so I think you may find DATE(DATE_FORMAT(dates, '%Y-%m-01')) more reliable.
Using MySQL, I am counting the occurrence of several events (fields) over a time span of years. I then display this in columns by year. My query works perfect when grouped by year. I now want to add a final column which displays the aggregate of the years. How do I include the total of columns query?
Event 2008 2009 2010 2011 total
A 0 2 0 1 3
B 1 2 3 0 6
etc.
Here is the real query:
select
count(*) as total_docs,
YEAR(field_document_date_value) as doc_year,
field_document_facility_id_value as facility,
IF(count(IF(field_document_type_value ='LIC809',1, NULL)) >0,count(IF(field_document_type_value ='LIC809',1, NULL)),'-') as doc_type_LIC809,
IF(count(IF(field_document_type_value ='LIC9099',1, NULL)) >0,count(IF(field_document_type_value ='LIC9099',1, NULL)),'-') as doc_type_LIC9099,
IF(count(field_document_f1_value) >0,count(field_document_f1_value),'-') as substantial_compliance,
IF(count(field_document_f2_value) >0,count(field_document_f2_value),'-') as deficiencies_sited,
IF(count(field_document_f3_value) >0,count(field_document_f3_value),'-') as admin_outcome_809,
IF(count(field_document_f4_value) >0,count(field_document_f4_value),'-') as unfounded,
IF(count(field_document_f5_value) >0,count(field_document_f5_value),'-') as substantiated,
IF(count(field_document_f6_value) >0,count(field_document_f6_value),'-') as inconclusive,
IF(count(field_document_f7_value) >0,count(field_document_f7_value),'-') as further_investigation,
IF(count(field_document_f8_value) >0,count(field_document_f8_value),'-') as admin_outcome_9099,
IF(count(field_document_type_a_value) >0,count(field_document_type_a_value),'-') as penalty_type_a,
IF(count(field_document_type_b_value) >0,count(field_document_type_b_value),'-') as penalty_type_b,
IF(sum(field_document_civil_penalties_value) >0,CONCAT('$',sum(field_document_civil_penalties_value)),'-') as total_penalties,
IF(count(field_document_noncompliance_value) >0,count(field_document_noncompliance_value),'-') as total_noncompliance
from rcfe_content_type_facility_document
where YEAR(field_document_date_value) BETWEEN year(NOW()) -9 AND year(NOW())
and field_document_facility_id_value = :facility
group by doc_year
You can not GROUP row twice in a SELECT, so you can only count row in a year or in total. You can UNION two SELECT (one grouped by year, second not grouped - total) to overcome this limitation, but I think it is better to count total from year result in script if there is any.
Simplified example:
SELECT by_year.amount, years.date_year FROM
-- generating years pseudo table
(
SELECT 2008 AS date_year
UNION ALL SELECT 2009
UNION ALL SELECT 2010
UNION ALL SELECT 2011
) AS years
-- joining with yearly stat data
LEFT JOIN
(
SELECT SUM(value_field) AS amount, YEAR(date_field) AS date_year FROM data
GROUP BY YEAR(date_field)
) AS by_year USING(date_year)
-- appending total
UNION ALL SELECT SUM(value_field) AS amount, 'total' AS date_year FROM data
WITH ROLLUP is your friend:
http://dev.mysql.com/doc/refman/5.7/en/group-by-modifiers.html
Use your original query and simply add this to the last line:
GROUP BY doc_year WITH ROLLUP
That will add a final cumulative row to your query's result set.
(Using SQL Server 2008) I could easily get this to work if I built a view, and then a query on the view, but I want to perform this in one SQL query. I have a table that contains two columns (DeliveredDate (DateTime), Delivered (Varchar)). I am first converting DeliveredDate to Date only, and then grouping by Date. At the same time I am performing a Count on Delivered (column is either YES or NULL). Here is what I use to accomplish this portion:
SELECT CAST([DeliveredDate] As Date),
COUNT([Delivered])
FROM [TableName]
GROUP BY CAST([DeliveredDate] As Date)
ORDER BY CAST([DeliveredDate] As Date)
As an output, I get something like:
DeliveredDate | Delivered
2012-04-24 10
2012-04-25 500
2012-04-26 422
2012-04-27 33
What I'm looking for is something like this:
DeliveredDate | Delivered | RunningTotal
2012-04-24 10 10
2012-04-25 500 510
2012-04-26 422 932
2012-04-27 33 965
I've tried various examples I've seen out there, but none seem to match this scenario of performing a Count and a RunningTotal on said Count.
If you are using a product that implements ordered OVER clauses for SUM, you can do something like this:
select
cast(DeliveredDate as date) as DeliveredDate,
count(Delivered) as Delivered,
sum(count(Delivered)) over (
order by DeliveredDate
) as RunningTotal
from Orders
group by OrderDate
order by OrderDate;
Your expression count(Delivered) is a bit strange. Just to be sure it's what you want: it will count the number of rows on the particular date for which the value in the Delivered column is NOT NULL.
I'm not sure if a CTE counts as a view but this will work in SQL 2005+ which Does Not Support ordered OVER clauses for SUM,
WITH cte (DeliveredDate, Delivered)
AS (SELECT Cast([DeliveredDate] AS DATE) DeliveredDate,
Count([Delivered]) Delivered
FROM [TableName]
GROUP BY Cast([DeliveredDate] AS DATE))
SELECT d1.delivereddate,
d1.delivered,
Sum(d2.Delivered) RunningTotal
FROM cte d1
LEFT JOIN cte d2
ON d1.delivereddate >= d2.DeliveredDate
GROUP BY d1.delivereddate,
d1.delivered
ORDER BY d1.delivereddate