Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

anyone care to explain the follow query? Im lost

Posted on 2011-09-13
8
Medium Priority
?
561 Views
Last Modified: 2012-05-12
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
Comment
Question by:Nura111
  • 4
  • 4
8 Comments
 
LVL 60

Accepted Solution

by:
Kevin Cross earned 2000 total points
ID: 36533350
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
 

Author Comment

by:Nura111
ID: 36533354
Where does he create it to which db?
0
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 36533381
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:Nura111
ID: 36533395
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
 
LVL 60

Assisted Solution

by:Kevin Cross
Kevin Cross earned 2000 total points
ID: 36533527
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
 

Author Comment

by:Nura111
ID: 36537586
Thank you!
0
 

Author Comment

by:Nura111
ID: 37410053
Hi mwvisa1:
the problem with  FROM util.numbers
is that Table 'util.numbers' doesn't exist
0
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 37412472
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
In this article, we’ll look at how to deploy ProxySQL.
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

885 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question