Solved

anyone care to explain the follow query? Im lost

Posted on 2011-09-13
8
497 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 59

Accepted Solution

by:
Kevin Cross earned 500 total points
Comment Utility
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
Comment Utility
Where does he create it to which db?
0
 
LVL 59

Expert Comment

by:Kevin Cross
Comment Utility
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
 

Author Comment

by:Nura111
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 59

Assisted Solution

by:Kevin Cross
Kevin Cross earned 500 total points
Comment Utility
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
Comment Utility
Thank you!
0
 

Author Comment

by:Nura111
Comment Utility
Hi mwvisa1:
the problem with  FROM util.numbers
is that Table 'util.numbers' doesn't exist
0
 
LVL 59

Expert Comment

by:Kevin Cross
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
SQL Command Tool comes with APEX under SQL Workshop. It helps us to make changes on the database directly using a graphical user interface. This helps us writing any SQL/ PLSQL queries and execute it on the database and we can create any database ob…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now