1

What I want to do is select all the IDs that have the most matches and list them in order of the most hits

TABLE - SUNDAY

ID | 8AM | 9AM | 10AM | 11AM
A  |  0  |  1  |  0   |  0 
B  |  0  |  0  |  1   |  1 
C  |  0  |  0  |  0   |  1

TABLE - MONDAY

ID | 8AM | 9AM | 10AM | 11AM
A | 0 | 0 | 1 | 1 
B | 0 | 1 | 0 | 0 
C | 0 | 0 | 0 | 1

TABLE - TUESDAY

ID | 8AM | 9AM | 10AM | 11AM
A | 0 | 1 | 0 | 0
B | 0 | 1 | 0 | 0
C | 0 | 0 | 0 | 1

Ex: I want to find all IDs that have Sunday at 9am and 11am, monday at 10am and 11am and tuesday at 9am and then order them by the most hits

I would get the following returned.

  • A 4 Hits
  • B 2 hits
  • C 2 hits

1 Answer 1

0

First, let's get a query to massage the data into something a bit easier to work with:

SELECT ID, 0 AS dow, 8 as hr, `8AM` AS hits
FROM SUNDAY
UNION ALL
SELECT ID, 0 AS dow, 9 as hr, `9AM` AS hits
FROM SUNDAY
UNION ALL
SELECT ID, 0 AS dow, 10 as hr, `10AM` AS hits
FROM SUNDAY
UNION ALL
SELECT ID, 0 AS dow, 11 as hr, `11AM` AS hits
FROM SUNDAY
UNION ALL
SELECT ID, 1 AS dow, 8 as hr, `8AM` AS hits
FROM MONDAY
UNION ALL
SELECT ID, 1 AS dow, 9 as hr, `9AM` AS hits
FROM MONDAY
UNION ALL
SELECT ID, 1 AS dow, 10 as hr, `10AM` AS hits
FROM MONDAY
UNION ALL
SELECT ID, 1 AS dow, 11 as hr, `11AM` AS hits
FROM MONDAY
UNION ALL
SELECT ID, 2 AS dow, 8 as hr, `8AM` AS hits
FROM TUESDAY
UNION ALL
SELECT ID, 2 AS dow, 9 as hr, `9AM` AS hits
FROM TUESDAY
UNION ALL
SELECT ID, 2 AS dow, 10 as hr, `10AM` AS hits
FROM TUESDAY
UNION ALL
SELECT ID, 2 AS dow, 11 as hr, `11AM` AS hits
FROM TUESDAY

SQL Fiddle example of the data this returns

Then you can just select from this (derived) table:

SELECT ID, SUM(hits) AS hits
FROM
(
    --above query, either as a view or derived table
    --or permanently changed
) hitsTable
WHERE (dow = 0 AND hr IN (9,11))
OR (dow = 1 AND hr IN (10,11))
OR (dow = 2 AND hr = 9)
GROUP BY ID
ORDER BY SUM(hits) DESC

SQL Fiddle example

Note the simplicity of the WHERE clause here. If you have control over your schema I would recommend permanently changing the way data is stored into this format. It will make your life much easier. If you can't, I suggest at least making a view that combines all the tables in this fashion, so you can easily query against them.


Can you do it with your current schema? Sure, and it may seem "shorter" or "simpler" but it's not as pretty if you are building these queries dynamically:

SELECT ID, SUM(hits) AS hits
FROM
(
    SELECT ID, `9AM` + `11AM` AS hits
    FROM SUNDAY
    UNION ALL
    SELECT ID, `10AM` + `11AM` AS hits
    FROM MONDAY
    UNION ALL
    SELECT ID, `9AM` AS hits
    FROM TUESDAY
) x
GROUP BY ID
ORDER BY SUM(hits) DESC
4
  • I actually do have control and just started to put this together. I was thinking of putting it all in 1 table but didn't know how many columns I could/should use. Obviously, this is for an entire week and the total columns would be 106. btw, thanks for the speedy response. Mar 21, 2013 at 1:37
  • @user2138263 That's kind of what I figured. This way, you only have four columns to ever worry about and you can aggregate very easily. You can actually get it down to two columns if you store ID and the time of each hit as a timestamp, but there may be a benefit to adding the hour/dow columns - and indexing them - if you plan on doing a lot of these queries.
    – lc.
    Mar 21, 2013 at 1:41
  • The second version is working. Question is, it is merely looking for any data in the column or is it looking for a 1 or a 0? Mar 21, 2013 at 2:26
  • @user2138263 Following your sample data, it's adding up the values
    – lc.
    Mar 21, 2013 at 10:36

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.