Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
callawayx14
Yak Posting Veteran
73 Posts |
Posted - 2010-09-16 : 12:54:01
|
HelloI'm trying to figure out how to get the DateDiff for each of the groups below (by color). I would need to get the datediff for each group and then select the max DateDiff for the ticket. The data appears like the below exampleticket | ID | Type | Date1234 | 1 | 0 | 09/03/101234 | 2 | 1 | 09/04/101234 | 3 | 0 | 09/07/101234 | 4 | 1 | 09/10/101234 | 5 | 0 | 09/11/101234 | 6 | 1 | 09/16/10I appreciate any help providedThank you in advance  |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-09-16 : 13:10:38
|
[code]SELECT t.*,DATEDIFF(dd,t1.Date,t.Date) AS DaysFROM Table tCROSS APPLY (SELECT TOP 1 Date FROM Table WHERE ticket=t.ticket AND ID < t.ID AND Type=0 ORDER BY ID DESC)t1WHERE t.Type=1[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
callawayx14
Yak Posting Veteran
73 Posts |
Posted - 2010-09-16 : 13:52:31
|
Thank you very much!! Success :) |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-09-17 : 12:03:27
|
welcome------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
jcelko
Esteemed SQL Purist
547 Posts |
Posted - 2010-09-17 : 12:17:48
|
The data model for histories is to have a start and end date in the table. What you have is an example of a design flaw called attribute splitting.--CELKO--Books in Celko Series for Morgan-Kaufmann PublishingAnalytics and OLAP in SQLData and Databases: Concepts in Practice Data, Measurements and Standards in SQLSQL for SmartiesSQL Programming Style SQL Puzzles and Answers Thinking in SetsTrees and Hierarchies in SQL |
 |
|
|
|
|