Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 566
  • Last Modified:

anyone care to explain the follow query? Im lost

Hi, Im a little bit confused from the following query and will be happy if someone would care to help
Im not even sure which table is it refering to and escpecilly what  all of
 SELECT 0 SeqValue  mean

Thank you/1
query =<<<EOB
SELECT
    timestamps.request_date, D.id, COUNT(SR.id) AS `numLeads`
FROM
(
    SELECT
        DATE_SUB(CURDATE(), INTERVAL SEQ.SeqValue DAY) AS request_date
    FROM
    (
        SELECT
            (HUNDREDS.SeqValue + TENS.SeqValue + ONES.SeqValue) SeqValue
        FROM
        (
            SELECT 0 SeqValue    UNION ALL
            SELECT 1 SeqValue    UNION ALL
            SELECT 2 SeqValue    UNION ALL
            SELECT 3 SeqValue    UNION ALL
            SELECT 4 SeqValue    UNION ALL
            SELECT 5 SeqValue    UNION ALL
            SELECT 6 SeqValue    UNION ALL
            SELECT 7 SeqValue    UNION ALL
            SELECT 8 SeqValue    UNION ALL
            SELECT 9 SeqValue
        ) ONES
        CROSS JOIN
        (
            SELECT 0  SeqValue    UNION ALL
            SELECT 10 SeqValue    UNION ALL
            SELECT 20 SeqValue    UNION ALL
            SELECT 30 SeqValue    UNION ALL
            SELECT 40 SeqValue    UNION ALL
            SELECT 50 SeqValue    UNION ALL
            SELECT 60 SeqValue    UNION ALL
            SELECT 70 SeqValue    UNION ALL
            SELECT 80 SeqValue    UNION ALL
            SELECT 90 SeqValue
        ) TENS
        CROSS JOIN
        (
            SELECT 0   SeqValue    UNION ALL
            SELECT 100 SeqValue    UNION ALL
            SELECT 200 SeqValue    UNION ALL
            SELECT 300 SeqValue
        ) HUNDREDS
    ) SEQ

    WHERE
        SEQ.SeqValue <= {$days}
    ORDER BY
        SEQ.SeqValue DESC
) timestamps

LEFT OUTER JOIN Domain D ON id='{$safeDomainId}'
LEFT OUTER JOIN ServiceRequests SR ON
    SR.timestamp >= timestamps.request_date AND
    SR.timestamp <= ADDDATE(timestamps.request_date, INTERVAL 1 DAY) AND
    SR.domain_id=D.id AND
    SR.email_address != 'leads@orininc.com' AND
    SR.email_address != 'leads@allyoucanmove.com'
GROUP BY timestamps.request_date, D.id
ORDER BY timestamps.request_date ASC, D.id ASC

Open in new window

0
Nura111
Asked:
Nura111
  • 4
  • 4
2 Solutions
 
Kevin CrossChief Technology OfficerCommented:
Hi, Nura111!

First, it looks like it is building up a numbers table. It is not pulling from a specific table, it is just utilizing the fact that you can use "SELECT 0 AS Alias" -- this is comparable to selecting "SELECT 0 AS Alias FROM DUAL;".

It is CROSS JOINing each set of numbers to expand the numbers set and ultimately it looks like convert the number to a date. So more appropriately, it is creating a dates table.

Hope that helps!
0
 
Nura111Author Commented:
Where does he create it to which db?
0
 
Kevin CrossChief Technology OfficerCommented:
By the way, the linked Article can be used to generate a util.numbers table in your MySQL server that you can reuse for purposes like this. The performance may be slightly better as it is indexed versus having to build a new sequence every time.

So to answer your question "where does he create it to which db?" It currently is a derived table and so not physically created to any db. I would recommend having a util db/schema for such things though.

It comes in handy when you are generating reports where you need all dates, but your data may have gaps. It looks like that is the scenario here. So using a table of numbers, the script is creating a table of dates and then LEFT OUTER JOINing those to your data. That way, you get NULL or 0 (in the case of COUNT) when missing data, but at least have a row. When there is a match on the date, then you get data.
0
Improve Your Query Performance Tuning

In this FREE six-day email course, you'll learn from Janis Griffin, Database Performance Evangelist. She'll teach 12 steps that you can use to optimize your queries as much as possible and see measurable results in your work. Get started today!

 
Nura111Author Commented:
Ok Ill try look deeper inot the article you linked because I have no idea what so ever on what is for and the purpose..
If you have any other good reference let me know
Thank you for the help
0
 
Kevin CrossChief Technology OfficerCommented:
This is for SQL Server, but the concept is the same.
Fun with MS SQL spt_values for delimited strings and virtual calendars
It is the inspiration for the comment in my article called Fun with Help Topics. :) see the section where mark is using the "numbers" table as a virtual calendar as that is what you are doing here.

i.e.,
FROM table_of_dates AS timestamps
LEFT OUTER JOIN Domain D ON id='{$safeDomainId}'
LEFT OUTER JOIN ServiceRequests SR ON
    SR.timestamp >= timestamps.request_date AND
    SR.timestamp <= ADDDATE(timestamps.request_date, INTERVAL 1 DAY)

See you are using the table_of_dates to ensure that a contiguous block of dates show up in the final results. If there is a match via the LEFT OUTER JOIN, then your aggregates or other data will show valid values. Otherwise, they will be NULL or 0 for a row. If you can have gaps in report, then this technique is not needed. It is useful when you need EVERY day in a range represented.

Hopefully my article helps you understand the concept of the numbers table a little better, but the more important thing -- or why I directed you there -- is that the scripts in the article are valid for actually creating a numbers table in your environment in its own schema called util.

After running it, add a 0 row to simulate above as it uses 0.
INSERT INTO util.numbers(n) VALUES(0);

Open in new window


You can then simplify the whole code down to this:
query =<<<EOB
SELECT
    timestamps.request_date, D.id, COUNT(SR.id) AS `numLeads`
FROM (
   SELECT DATE_SUB(CURDATE(), INTERVAL n DAY) AS request_date
        , DATE_SUB(CURDATE(), INTERVAL n-1 DAY) AS request_date_end
   FROM util.numbers 
   WHERE n <= {$days}
   ORDER BY n DESC
) AS timestamps
LEFT OUTER JOIN Domain D ON id='{$safeDomainId}'
LEFT OUTER JOIN ServiceRequests SR ON
    SR.timestamp >= timestamps.request_date AND
    SR.timestamp <= timestamps.request_date_end AND
    SR.domain_id=D.id AND
    SR.email_address != 'leads@orininc.com' AND
    SR.email_address != 'leads@allyoucanmove.com'
GROUP BY timestamps.request_date, D.id
ORDER BY timestamps.request_date ASC, D.id ASC
;

Open in new window

0
 
Nura111Author Commented:
Thank you!
0
 
Nura111Author Commented:
Hi mwvisa1:
the problem with  FROM util.numbers
is that Table 'util.numbers' doesn't exist
0
 
Kevin CrossChief Technology OfficerCommented:
Nura111, the first post you selected has a link to an article where I give some code to create the util.numbers table. *smile* It is handy to have one in your system, but I have in the comments how to do this on the fly also -- similar to Mark's use of spt_values in his article.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Improve Your Query Performance Tuning

In this FREE six-day email course, you'll learn from Janis Griffin, Database Performance Evangelist. She'll teach 12 steps that you can use to optimize your queries as much as possible and see measurable results in your work. Get started today!

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now