MySQL - Count all rows for each day of week but for each sender

AdRock

I have this query which outputs the count of rows for each day and if there are no rows, it outputs 0.

I now have an additional field I want to add into the query called 'Sender'. I need the query to do exactly the same but for each of the senders.

How can I perform a query so each Sender gets each day of the week with the values?

SELECT DAYNAME(DATE_SUB(CURDATE(), INTERVAL Days.n DAY)) AS `day`,
       COUNT(r.List_Date) AS `total`
FROM (SELECT 1 as n UNION ALL SELECT 2 as n UNION ALL 
      SELECT 3 as n UNION ALL SELECT 4 as n UNION ALL
      SELECT 5 as n UNION ALL SELECT 6 as n UNION ALL
      SELECT 7 as n
     ) Days LEFT JOIN
     returns r
     ON r.List_Date >= DATE_SUB(CURDATE(), INTERVAL Days.n DAY)
GROUP BY Days.n
ORDER BY Days.n DESC
Gordon Linoff

You need a cross join to get all the rows (each sender and each day of the week). Then use the left join:

SELECT s.sender, DAYNAME(DATE_SUB(CURDATE(), INTERVAL Days.n DAY)) AS `day`,
       COUNT(r.List_Date) AS `total`
FROM (SELECT 1 as n UNION ALL SELECT 2 as n UNION ALL 
      SELECT 3 as n UNION ALL SELECT 4 as n UNION ALL
      SELECT 5 as n UNION ALL SELECT 6 as n UNION ALL
      SELECT 7 as n
     ) Days CROSS JOIN
     (SELECT DISTINCT sender FROM returns) s LEFT JOIN
     returns r
     ON r.List_Date >= DATE_SUB(CURDATE(), INTERVAL Days.n DAY) and
        r.sender = s.sender
GROUP BY s.sender, Days.n
ORDER BY s.sender, Days.n DESC;

This uses the returns table to get the appropriate senders. If you have another table, you can use that instead.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Count occurrences for each day of week

From Dev

How can I count rows matching criteria for each week in MySQL?

From Dev

MySQL - count users for each day and count them all

From Dev

Count occurrence for each day of the week between two dates

From Dev

MySql - count from beginning until each day

From Dev

MySQL COUNT by Day of Week

From Dev

MySQL COUNT by Day of Week

From Dev

Mysql - Get count of unique distinct ip for each hour, each day

From Dev

Count unique number of days between 2 dates where each day will have multiple rows in MySQL

From Dev

Getting the count of rows in each category in a MySQL table

From Dev

Getting the count of rows in each category in a MySQL table

From Dev

MySQL - Count number of rows in each group

From Dev

SQL count for each day

From Dev

PHP loop (for each day of a week and each hour)

From Dev

Select all rows from start of the year, month, week and day in MySQL?

From Dev

R - count total number of rows in every 15 minutes for each day

From Dev

Return just last day from all available rows for each month

From Dev

All categories for each week and group

From Dev

Select count for each day in a month

From Dev

Array of times for each day of the week in iOS

From Dev

Determine which day of week is each date of the month

From Dev

Listing events in table for each day of the week

From Dev

xts - how to subset on each day of the week

From Dev

Determine which day of week is each date of the month

From Dev

js select hours for each day of the week

From Dev

Listing events in table for each day of the week

From Dev

PHP generate timestamp for each day of the week

From Dev

xts - how to subset on each day of the week

From Dev

Grouping by day then count result for each day

Related Related

  1. 1

    Count occurrences for each day of week

  2. 2

    How can I count rows matching criteria for each week in MySQL?

  3. 3

    MySQL - count users for each day and count them all

  4. 4

    Count occurrence for each day of the week between two dates

  5. 5

    MySql - count from beginning until each day

  6. 6

    MySQL COUNT by Day of Week

  7. 7

    MySQL COUNT by Day of Week

  8. 8

    Mysql - Get count of unique distinct ip for each hour, each day

  9. 9

    Count unique number of days between 2 dates where each day will have multiple rows in MySQL

  10. 10

    Getting the count of rows in each category in a MySQL table

  11. 11

    Getting the count of rows in each category in a MySQL table

  12. 12

    MySQL - Count number of rows in each group

  13. 13

    SQL count for each day

  14. 14

    PHP loop (for each day of a week and each hour)

  15. 15

    Select all rows from start of the year, month, week and day in MySQL?

  16. 16

    R - count total number of rows in every 15 minutes for each day

  17. 17

    Return just last day from all available rows for each month

  18. 18

    All categories for each week and group

  19. 19

    Select count for each day in a month

  20. 20

    Array of times for each day of the week in iOS

  21. 21

    Determine which day of week is each date of the month

  22. 22

    Listing events in table for each day of the week

  23. 23

    xts - how to subset on each day of the week

  24. 24

    Determine which day of week is each date of the month

  25. 25

    js select hours for each day of the week

  26. 26

    Listing events in table for each day of the week

  27. 27

    PHP generate timestamp for each day of the week

  28. 28

    xts - how to subset on each day of the week

  29. 29

    Grouping by day then count result for each day

HotTag

Archive