Group and count data based on a calculated field in mysql?

Gorrister

I have 2 tables in mysql that record the dates a certain event happens.

  • Table 1: (id, session_id, date)
  • Table 2: (id, session_date)

Table1 is the main table where I only write either session_id (in that case the date is fetched from Table2's matching record) or date. The field that I'm not filling, stays NULL.

Now, I want to count how many times that event has happened per month/year and, if possible, only do that procedure after a specific date (e.g. 01-01-2013).

Here's what I've done so far, I've created another date field (called date_final) that holds the non-NULL value per row, so I'm trying to group my data based on that field.

SELECT table1.date AS date1, 
       table2.session_date AS date2, 
       @date_final := CONCAT_WS('', table1.date, table2.session_date) AS date_final,
       MONTHNAME(@date_final) AS `month`,
       YEAR(@date_final) AS `year`   
FROM table1
LEFT JOIN table2 ON table2.id = table1.session_id
GROUP BY YEAR(@date_final), MONTH(@date_final)

Obviously this has not worked very well, hence I'm here. MySQL returns to me only some values per month/year and that's not good. It gets even worse when I add the COUNT(*) clause. I'm just a newbie in mysql and I guess I'm getting into deep waters here :P

##Sample data for Table1##
#id  session_id  date#
1            1  NULL
2         NULL  2013-01-04
3            2  NULL
4         NULL  2013-01-20
5         NULL  2013-02-22
6            3  NULL

##Sample data for Table2##
#id  session_date#
1       2013-01-02
2       2013-01-10
3       2013-03-02

Expected results? Something like:

2013  January  3
2013  February 2
2013  March    1
Joachim Isaksson

Removing the session variable since the rules of using them reliably aren't straight forward, what you seem to be looking for is a LEFT JOIN with COALESCE, something like;

SELECT 
  YEAR(COALESCE(table1.date, table2.session_date)) year,
  MONTHNAME(COALESCE(table1.date, table2.session_date)) month,
  COUNT(*) cnt
FROM table1
LEFT JOIN table2 ON table1.session_id = table2.id
GROUP BY year, month
ORDER BY year, MONTH(COALESCE(table1.date, table2.session_date))

An SQLfiddle to test with.

Note that here table1.date takes precedence over table2.session_date, if you want it the other way you'll have to reverse the order in the COALESCEs.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Calculated field based on sum and count of other field

From Dev

Mysql Group by Year and Count field

From Dev

How can I group mysql results based on a calculated colum?

From Dev

Mysql subquery for group and count records by specific field

From Dev

MySQL add count field based on date

From Dev

MySQL add count field based on date

From Dev

Group MySQL data based on year

From Dev

Limiting GROUP BY based on COUNT() values in mySQL

From Dev

Filter data based on result set of group and count

From Dev

(MySQL) Group by field and select both COUNT(field) and number of grouped rows

From Dev

SSRS : calculated field based on other field and condition

From Dev

MySQL calculated field between dates

From Dev

MongoDB - Count documents based on if a field has a particular value in group stage

From Dev

SSRS Count Record Based on Field Value (Status) in a Group

From Dev

SELECT COUNT() GROUP BY Set of data in IN() mysql

From Dev

how can I count mysql data with group by

From Dev

SELECT COUNT() GROUP BY Set of data in IN() mysql

From Dev

ParentFieldValue and COUNT MVL in calculated field: Siebel

From Dev

mysql count and then group by that count

From Dev

Count MySql Data based on drop box value

From Dev

Get the count of data based on id in mysql result

From Dev

Group by embedded field and count

From Dev

Count group by field if not null

From Dev

Group/count by part of a field

From Dev

Tableau: creating calculated field based on cell values

From Dev

Calculated Field Based on Corresponding Row Month

From Dev

Doing a count on a field in a mysql table based on a condition evaluated from a join

From Dev

MySQL multiple count based on two column with multiple GROUP BY in single table

From Dev

count number of rows in a data frame in R based on group

Related Related

  1. 1

    Calculated field based on sum and count of other field

  2. 2

    Mysql Group by Year and Count field

  3. 3

    How can I group mysql results based on a calculated colum?

  4. 4

    Mysql subquery for group and count records by specific field

  5. 5

    MySQL add count field based on date

  6. 6

    MySQL add count field based on date

  7. 7

    Group MySQL data based on year

  8. 8

    Limiting GROUP BY based on COUNT() values in mySQL

  9. 9

    Filter data based on result set of group and count

  10. 10

    (MySQL) Group by field and select both COUNT(field) and number of grouped rows

  11. 11

    SSRS : calculated field based on other field and condition

  12. 12

    MySQL calculated field between dates

  13. 13

    MongoDB - Count documents based on if a field has a particular value in group stage

  14. 14

    SSRS Count Record Based on Field Value (Status) in a Group

  15. 15

    SELECT COUNT() GROUP BY Set of data in IN() mysql

  16. 16

    how can I count mysql data with group by

  17. 17

    SELECT COUNT() GROUP BY Set of data in IN() mysql

  18. 18

    ParentFieldValue and COUNT MVL in calculated field: Siebel

  19. 19

    mysql count and then group by that count

  20. 20

    Count MySql Data based on drop box value

  21. 21

    Get the count of data based on id in mysql result

  22. 22

    Group by embedded field and count

  23. 23

    Count group by field if not null

  24. 24

    Group/count by part of a field

  25. 25

    Tableau: creating calculated field based on cell values

  26. 26

    Calculated Field Based on Corresponding Row Month

  27. 27

    Doing a count on a field in a mysql table based on a condition evaluated from a join

  28. 28

    MySQL multiple count based on two column with multiple GROUP BY in single table

  29. 29

    count number of rows in a data frame in R based on group

HotTag

Archive