Current month each day stats per user row

kaidoj

I have 23 users in group 75(user_group).

Then I have table users_participated where row is set only if user participated ( sometimes it exists but is 0 ( not participate ) ).

Now I need to show each day for every user this month did he participate or not.

I can do it with php loop. But I was wondering if there is a way to do it only with single mysql query.

   //output html table should look like y = yes n = no 1,2,3,5 are days in month
   user | 1 | 2 | 3 | 4 | 5.. 
   user1| y | n | y | n | n
   user2| n | y | y | n | y

   // user table
   id | user   | group
   1  | user1  |  75
   2  | user2  |  75
   3  | user3  |  75
   4  | user4  |  68

   //participate table
   user_id | participated | date_added
      1    |  1           | 2014-03-29 11:03:00
      2    |  0           | 2014-03-29 11:03:00

//sometimes it can be 0 AND sometimes if its null its not even in this table if u know what i mean.

How can I do this ?

krokodilko

A simple crosstab query can do that:

SELECT u.user,
       max( if(day(p.date_added) = 1, p.participated, 0 ) ) day1,
       max( if(day(p.date_added) = 2, p.participated, 0 ) ) day2,
       ..............
       ..............
       ..............
       max( if(day(p.date_added) = 28, p.participated, 0 ) ) day28,
       max( if(day(p.date_added) = 29, p.participated, 0 ) ) day29,
       max( if(day(p.date_added) = 30, p.participated, 0 ) ) day30,
       max( if(day(p.date_added) = 31, p.participated, 0 ) ) day31
FROM user u
JOIN participate p
ON u.id = p.user_id
WHERE p.date_added >= '2014-03-01'
  AND p.date_added < '2014-04-01'
GROUP BY u.id

demo: http://sqlfiddle.com/#!9/cd31f/3

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Check if the current date is last day of the month

From Java

first and last day of the current month in swift

From Dev

javascript get current day and month issue

From Dev

Get first day of current and next month in VBSCRIPT

From Dev

How to find current day no. in month in php

From Dev

How to get last day of current month and last day of next month

From Dev

Getting first and last day of the current month

From Dev

Bootstrap Datepicker preselect first day of the current month

From Dev

Is it possible to compare day + month(not year) against current day + month in python?

From Dev

First business day of the current month - SQL Server

From Dev

Show data in current day, week, month and year

From Dev

Count data per day in a specific month in Postgresql

From Dev

Select one row per day for each value

From Dev

How to get last day of current month?

From Dev

MySQL sales per Day, Week and Month

From Dev

Assign total value of month to each day of month

From Dev

Get first day of current and next month in VBSCRIPT

From Dev

PHP table of current month with date on each row

From Dev

javascript get current day and month issue

From Dev

(Wordpress) User Registered On Current Month

From Dev

Get value for each day in month

From Dev

beforeShowDay don't disabled day in current month

From Dev

how to calculate the last day of the current month?

From Dev

How can I get the number of logins each day in the current month using awk?

From Dev

Change image for each day of month

From Dev

How to make a sql server query that converts one row with From Date To Date columns to multiple rows for each day or each month

From Dev

In a table containing rows of date ranges, from each row, generate one row per day containing hours of utilization

From Dev

Finding Average per day, week and month

From Dev

Select count for each day in a month

Related Related

  1. 1

    Check if the current date is last day of the month

  2. 2

    first and last day of the current month in swift

  3. 3

    javascript get current day and month issue

  4. 4

    Get first day of current and next month in VBSCRIPT

  5. 5

    How to find current day no. in month in php

  6. 6

    How to get last day of current month and last day of next month

  7. 7

    Getting first and last day of the current month

  8. 8

    Bootstrap Datepicker preselect first day of the current month

  9. 9

    Is it possible to compare day + month(not year) against current day + month in python?

  10. 10

    First business day of the current month - SQL Server

  11. 11

    Show data in current day, week, month and year

  12. 12

    Count data per day in a specific month in Postgresql

  13. 13

    Select one row per day for each value

  14. 14

    How to get last day of current month?

  15. 15

    MySQL sales per Day, Week and Month

  16. 16

    Assign total value of month to each day of month

  17. 17

    Get first day of current and next month in VBSCRIPT

  18. 18

    PHP table of current month with date on each row

  19. 19

    javascript get current day and month issue

  20. 20

    (Wordpress) User Registered On Current Month

  21. 21

    Get value for each day in month

  22. 22

    beforeShowDay don't disabled day in current month

  23. 23

    how to calculate the last day of the current month?

  24. 24

    How can I get the number of logins each day in the current month using awk?

  25. 25

    Change image for each day of month

  26. 26

    How to make a sql server query that converts one row with From Date To Date columns to multiple rows for each day or each month

  27. 27

    In a table containing rows of date ranges, from each row, generate one row per day containing hours of utilization

  28. 28

    Finding Average per day, week and month

  29. 29

    Select count for each day in a month

HotTag

Archive