Solved

anyone care to explain the follow query? Im lost

Posted on 2011-09-13
8
520 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 59

Accepted Solution

by:
Kevin Cross earned 500 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 59

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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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 59

Assisted Solution

by:Kevin Cross
Kevin Cross earned 500 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 59

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
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…

756 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