0

I have a mysql query that sometimes results in missing values. For my dashboard I'd like to fill those values, but would prefer to avoid build dummy tables if I can.

query:

SELECT COUNT(Comms_Timestamp) as call_count,DAYOFWEEK(Comms_Timestamp) as bucket 
FROM tblTest GROUP BY bucket;

results in

+------------+--------+
| call_count | bucket |
+------------+--------+
|          4 |      1 |
|          7 |      2 |
|          7 |      3 |
|          1 |      5 |
|          6 |      6 |
|          1 |      7 |
+------------+--------+

In the above example you can see bucket 4 is missing. I consider the method where the join is to a select union array, however since both fields are aggregates, I'm not sure how to go about it.

test data is

+---------------------+
| Comms_Timestamp     |
+---------------------+
| 2018-12-24 06:04:05 |
| 2018-12-24 12:18:39 |
| 2018-12-21 04:24:31 |
| 2018-12-21 08:32:44 |
| 2018-12-30 01:41:06 |
| 2018-12-30 01:53:00 |
| 2018-12-30 01:53:39 |
| 2018-12-30 02:00:01 |
| 2018-12-17 15:55:03 |
| 2018-12-17 16:04:12 |
| 2018-12-17 16:05:41 |
| 2018-12-17 16:07:43 |
| 2018-12-17 16:10:25 |
| 2018-12-18 14:03:22 |
| 2018-12-18 14:03:29 |
| 2018-12-18 14:10:19 |
| 2018-12-18 14:10:29 |
| 2018-12-18 14:10:31 |
| 2018-12-18 14:10:47 |
| 2018-12-18 14:10:55 |
| 2018-12-20 08:21:07 |
| 2018-12-28 11:03:59 |
| 2018-12-28 12:06:40 |
| 2018-12-28 12:15:01 |
| 2018-12-28 14:29:24 |
| 2019-01-05 13:33:43 |
+---------------------+
4
  • Can u post few more things like the structure of the two tables with some data in it? This much info is inadequate to suggest properly. Commented Feb 8, 2019 at 5:52
  • this might help you link Commented Feb 8, 2019 at 6:00
  • @RaviGaudani I'm specifically trying to avoid building empty tables.. Commented Feb 8, 2019 at 7:24
  • @BhushanShinde done Commented Feb 8, 2019 at 7:38

3 Answers 3

1

Since you are using mysql and don't have access to the seq_ option, here is an alternative way:

SELECT A.x AS bucket, IF(ISNULL(COUNT(t2.Comms_Timestamp)), 0, COUNT(t2.Comms_Timestamp)) AS call_count FROM
(select 1 x union select 2 union select 3 union select 4 union select 5 union select 6 union select 7) AS A
LEFT JOIN tblTest AS t2 ON DAYOFWEEK(t2.Comms_Timestamp) = A.x
GROUP BY bucket
ORDER BY bucket;

It may not be the prettiest option but will do what you need.

Here is a db fiddel link: db<>fiddle

Sign up to request clarification or add additional context in comments.

Comments

0

If you are using MariaDB there is their Sequence Storage Engine

There is no create needed for this table, however the maximum value must be known.

select version();
| version()                                   |
| :------------------------------------------ |
| 10.3.11-MariaDB-1:10.3.11+maria~stretch-log |
create table bob (a int)
insert into bob values (4),(2)
select * from seq_1_to_5
| seq |
| --: |
|   1 |
|   2 |
|   3 |
|   4 |
|   5 |
SELECT s.seq, bob.a
FROM seq_1_to_5 s 
LEFT JOIN bob 
  ON bob.a = s.seq
ORDER BY s.seq
seq |    a
--: | ---:
  1 | null
  2 |    2
  3 | null
  4 |    4
  5 | null

db<>fiddle here

1 Comment

unfortunately I'm running mysql for this one
0

You can use IFNULL() function in MYSQL:

SELECT IFNULL(COUNT(C.Comms_Timestamp),0) as call_count,IFNULL(DAYOFWEEK(C.Comms_Timestamp),0) as bucket 
FROM tblCommunication as C LEFT JOIN tblCareTeam as CT on C.id_Case = CT.id_Case 
GROUP BY CT.id_Site,bucket 
HAVING CT.id_Site=8;

1 Comment

Sorry that defintly doesn't do anything

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.