Reputation: 317
I have some historical data from various sources that I want to compile down to a line graph.
Here is a sample SQL fiddle that I am working with https://www.db-fiddle.com/f/oprEgt1gEz55tY5xumU6Jz/1
However, there are various case scenarios I need to handle. In the below scenarios, I always have 3 sources(A, B, C). B and C are always bringing in values but A is the run that runs into issues.
The user asked for data from January 1 to January 5.
I am trying to do this in MySQL. I got a simple query using GROUP BY and HAVING however that is not enough to satisfy the use cases above. I was about writing a CTE with this or using PARTITION BY but once again stuck with not being able to satisfy all the conditions.
Upvotes: 0
Views: 67
Reputation: 515
Why don't you skip the HAVING
so that you will get all the data for Jan 3rd as well though there is no data in Source A on Jan 2.
SELECT sample_date, COUNT(source_data_id) AS NUMBER_OF_SOURCES,
SUM(value) AS TOTAL_VALUE
FROM sample_history_data
WHERE SOURCE_DATA_ID IN ('A','B','C')
GROUP BY sample_date
ORDER BY sample_date ASC;
sample_date NUMBER_OF_SOURCES TOTAL_VALUE
2001-01-01 3 30
2001-01-02 3 30
2001-01-03 2 20
2001-01-04 3 30
2001-01-05 3 30
Upvotes: 0