If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Query Shorthand

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-25-07, 15:57
clbardwe@gpi.com clbardwe@gpi.com is offline
Registered User
 
Join Date: Jan 2007
Posts: 6
Query Shorthand

I'm probably only going to hear that it can't be done, but I'm going to ask anyway...

I have a query that reads (among other things) a date/time field. The date/time field is localized to east coast. I have another table where the timezone offset is kept (for stores in central, mountain, and pacific zones). I know what the calculation is to get the local time for a store. Unfortunately my query uses the date/time a lot, so I would have to use that calculation a lot. What I would like is to ba able to perform the calculation for each record and use the result in the rest of the query.

Something like, instead of this:
Code:
     ,SUM(CASE WHEN DATE_FORMAT(DATE_ADD(TransDt, INTERVAL tblStores.TimeZone HOUR), '%H') = '07' THEN 1 ELSE 0 END) AS Hour7
Id like to do something like this:
Code:
     ,DATE_ADD(TransDt, INTERVAL tblStores.TimeZone HOUR) AS LocalDate
     ,SUM(CASE WHEN DATE_FORMAT(LocalDate, '%H') = '07' THEN 1 ELSE 0 END) AS Hour7
Basically using the calculation of the date/time to create a value AS LocalDate and then using that in subsequent calculations (CASE statements).

The above won't work, however. I'm getting an "Unknown column 'LocalDate' in 'field list'" error.

Is there any other way of doing this?

I am running MySQL 4.1 and I have no authority to create functions (or anything else).

My current query:
Code:
 SELECT 
     DAYNAME(TransDt) AS 'Day'
     ,SUM(CASE WHEN DATE_FORMAT(TransDt, '%H') = '07' THEN 1 ELSE 0 END) AS Hour7
     ,SUM(CASE WHEN DATE_FORMAT(TransDt, '%H') = '08' THEN 1 ELSE 0 END) AS Hour8
    #DATE_FORMAT's '09' through '19' trimmed out for length
     ,SUM(CASE WHEN DATE_FORMAT(TransDt, '%H') = '20' THEN 1 ELSE 0 END) AS Hour20
     ,SUM(CASE WHEN DATE_FORMAT(TransDt, '%H') = '21' THEN 1 ELSE 0 END) AS Hour21
     ,SUM(CASE WHEN (DATE_FORMAT(TransDt, '%H') < '07') OR (DATE_FORMAT(TransDt, '%H') > '21') THEN 1 ELSE 0 END) AS HourOther
     ,COUNT(InvNum) AS DayTotal
    ,1 AS Position1
FROM 
    tblTransactions
        LEFT JOIN tblStores
            ON tblTransactions.StoreNum = tblStores.StoreNum 
WHERE 
    (StoreNum = 123)
    AND (Date(TransDt) >= '2006-12-04') 
    AND (Date(TransDt) <= '2006-12-31')
GROUP BY 
     DAYNAME(TransDt);
Reply With Quote
  #2 (permalink)  
Old 01-25-07, 17:02
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
just shove the calculation down into a subquery
Code:
select foo
     , bar
     , foo - x as p
     , bar + x as q
     , x*x as xsquared
  from (
       select foo
            , bar
            , some_immensely_complex_expression as x
         from daTable
       ) as dt
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On