Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase ««12

Difference between two date/times and flag each hour in between Expand / Collapse
Author
Message
Posted Friday, January 03, 2014 8:01 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Thursday, April 17, 2014 9:33 AM
Points: 6,754, Visits: 12,854
cs_source (1/3/2014)
Thank you dwain.c, i'm working through the query now to under stand it (pretty complicated :)

hi Chris.m below is the sample data as well as my expected output (sorry i should have provided this earlier)

...


Thanks! Yes that's great. It makes things so much easier.


“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Post #1527555
Posted Friday, January 03, 2014 8:27 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Thursday, April 17, 2014 9:33 AM
Points: 6,754, Visits: 12,854
;WITH 
[4] AS (SELECT n = 0 FROM (VALUES (0), (0), (0), (0)) d (n)),
[16] AS (SELECT n = 0 FROM [4] a, [4] b),
[256] AS (SELECT n = 0 FROM [16] a, [16] b),
[65536] AS (SELECT n = 0 FROM [256] a, [256] b),
iTally AS (SELECT n = ROW_NUMBER() OVER(ORDER BY (SELECT NULL))-1 FROM [65536] a, [16] b)

SELECT a.visit_date, a.vin, a.make, x.Hour_in_service
FROM #sampleA a
CROSS APPLY (
SELECT TOP (1 + DATEDIFF(hour, a.enter_datetime, a.depart_datetime))
Hour_in_service = DATEPART(HOUR,DATEADD(hour,n,a.enter_datetime))
FROM iTally t
) x



“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Post #1527565
Posted Friday, January 03, 2014 8:59 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Friday, January 17, 2014 9:48 AM
Points: 29, Visits: 109
okay, wow well thank you all for your queries, i learnt something new with each one, the last query outputs the data in the format i'm exactly looking for :) so thank you,

2 quick questions:

1 - if i want the visit date reflected with the date the hours fall under is that another query where i update the table that this data will go into?

2 - breaking down the query so i understand it more, whats going on is:

creating 4 cte tables
populating them with ascending integers
select the data and cross apply
its the second half of the query that i'm trying to wrap my head around:

SELECT TOP (1 + DATEDIFF(hour, a.enter_datetime, a.depart_datetime))
-- This gets the difference in hours for each enter/depart time and top 1 signals ?? when i remove the 1 i lose a record when i add the 1 i gain a record

Hour_in_service = DATEPART(HOUR,DATEADD(hour,n,a.enter_datetime))
-- a little confused here how n plays a role with the enter time
FROM iTally t
Post #1527592
Posted Friday, January 03, 2014 9:59 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Thursday, April 17, 2014 9:33 AM
Points: 6,754, Visits: 12,854
cs_source (1/3/2014)
okay, wow well thank you all for your queries, i learnt something new with each one, the last query outputs the data in the format i'm exactly looking for :) so thank you,


You're welcome


2 quick questions:

1 - if i want the visit date reflected with the date the hours fall under is that another query where i update the table that this data will go into?


Easiest way to answer this is if you can post it as expected output. A picture tells etc etc


2 - breaking down the query so i understand it more, whats going on is:

creating 4 cte tables

CTE's aren't tables. They're more or less views built on the fly, kinda like macro's.

populating them with ascending integers
select the data and cross apply
its the second half of the query that i'm trying to wrap my head around:

SELECT TOP (1 + DATEDIFF(hour, a.enter_datetime, a.depart_datetime))
-- This gets the difference in hours for each enter/depart time and top 1 signals ?? when i remove the 1 i lose a record when i add the 1 i gain a record

Hour_in_service = DATEPART(HOUR,DATEADD(hour,n,a.enter_datetime))
-- a little confused here how n plays a role with the enter time
FROM iTally t


Have a look at Jeff's Tally Tables article, currently on the hotlist.

Edited to fix tags


“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Post #1527634
Posted Friday, January 17, 2014 9:29 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Friday, January 17, 2014 9:48 AM
Points: 29, Visits: 109
hi again,

i dont mean to bump this thread up again but the output was great for the use and additional scope has been added. To use a graphing tool that we have (to display the data) i need to get the data into a single column based on which column has a value entered

example

hour_detail| hour1| hour2|hour3| hour4| hour5
5 | 0| 0| 0| 0| 5
2 | 0| 2| 0| 0| 0
3 | 0| 0| 3| 0| 0
1 | 1| 0| 0| 0| 0
4 | 0| 0| 0| 4| 0

I thought about create a massive case statement but i dont think that was correct, i also look at Coalesce but i think have too man columns to preform this as well as i have 0's and not nulls ? I have 23 columns across.

what is some advice on how i can proceed to do this?
Post #1532133
Posted Friday, January 17, 2014 9:47 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Thursday, April 17, 2014 9:33 AM
Points: 6,754, Visits: 12,854
This looks like the output from a CROSSTAB query. If so, you would normally aggregate across whatever the partition is and use MAX() on each column value. Post your query if you are unsure.

“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Post #1532146
« Prev Topic | Next Topic »

Add to briefcase ««12

Permissions Expand / Collapse