Im working on a query that is meant to retrive sales by hour, which it does. However, in the used database table all the timestamps are UTC +1
, also for sales made in a country with UTC +2
.
So what I'm trying to achive is a result that can be used by local business units (a parameter is set depending on who is looking at a report will determine the which country/store to display). So when it's sales in a UTC +2
country the datestamp needs to be modified with +1. I'm thinking this can be done in the group by, perhaps by using a DATEADD
together with a condition that checks the county name. For example, when the country is 'Greece' (column exists in the database), use a DATEADD
to add 1 hour to the timestamp.
Is this a possible solution and if so, how is it done?
This is the GROUP BY
im using at the moment:
SELECT
DATEPART(hour, sales.OrderDate) AS Hour,
SUM(CASE WHEN FORMAT(sales.OrderDate, 'yyyy-MM-dd') = Cast(GETDATE() AS date) THEN sales.SALES * (1 + sales.vv / 100) END) AS SALES,
COUNT(DISTINCT (CASE WHEN FORMAT(sales.OrderDate, 'yyyy-MM-dd') = Cast(GETDATE() AS date) THEN sales.OID END) ) AS CUSTOMERS,
MAX(CASE WHEN FORMAT(sv.Date_Time, 'yyyy-MM-dd') = Cast(GETDATE() AS date) THEN sv.CC END) AS VISITORS
FROM
[DW].[Tot_Sales] AS sales
LEFT JOIN [DW].[SV_24M] AS sv ON dateadd(hour, datediff(hour, 0, sales.[OrderDate]), 0) = dateadd(HOUR, 0, sv.Date_Time)
AND sales.SID = sv.SID
WHERE
sales.SID = @Store
AND FORMAT(sales.OrderDate, 'yyyy-MM-dd') > DATEADD(year, - 1, Cast(GETDATE() AS date))
GROUP BY
sales.SID,
DATEPART(hour, sales.OrderDate)
ORDER BY
DATEPART(hour, sales.OrderDate)
It is in the columns SALES
and CUSTOMERS
this needs to be applied, and they are from the table named sales
. This is how the result looks:
The issue is that the sales and customers occuring at hour 9 actually occured hour 10 in UCT +2
. Visitors data arrives in local time ( UCT +2
in this case), and therefore it's a mismatch between Visitors and Customers.
Sample data set:
If you know the countries then you could make use of AT TIME ZONE in the query to set the appropriate local time.
Refer to this
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.