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 |
offspring22
Starting Member
38 Posts |
Posted - 2012-02-10 : 17:03:23
|
Hello,We have a table that has 25,000 or so records of sold properties over the last 20 years or so. Each property has a unique number we call a LINC number, and a solddate.I have a request come my way here that needs me to find the average time it takes for sold property to be sold again, or it's turnover time. Some may have only sold once, some may have sold 10 times - in that case I would need it to include the turn over between each sale.My data looks like:linc SoldDate0028497410 2002-06-07 00:00:00.0000028497428 2003-06-17 00:00:00.0000028498475 2002-07-29 00:00:00.0000028498475 2003-02-26 00:00:00.0000028498483 2002-06-15 00:00:00.0000028498491 2008-12-01 00:00:00.0000028498491 2010-11-14 00:00:00.000Any tips on how to achieve this? |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2012-02-10 : 17:25:03
|
This, maybe?DECLARE @Sample TABLE ( LINC CHAR(10) NOT NULL, SoldDate DATETIME NOT NULL )INSERT @SampleSELECT '0028497410', '20020607' UNION ALLSELECT '0028497428', '20030617' UNION ALLSELECT '0028498475', '20020729' UNION ALLSELECT '0028498475', '20030226' UNION ALLSELECT '0028498483', '20020615' UNION ALLSELECT '0028498491', '20081201' UNION ALLSELECT '0028498491', '20101114'-- SwePesoSELECT LINC, 1E * DATEDIFF(DAY, MIN(SoldDate), MAX(SoldDate)) / (COUNT(*) - 1) AS TurnoverFROM @SampleGROUP BY LINCHAVING COUNT(*) > 1ORDER BY LINC N 56°04'39.26"E 12°55'05.63" |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2012-02-10 : 17:28:46
|
What about the LINC's that has been sold only once?-- SwePeso 2SELECT LINC, CASE WHEN COUNT(*) = 1 THEN DATEDIFF(DAY, MIN(SoldDate), GETDATE()) ELSE 1E * DATEDIFF(DAY, MIN(SoldDate), MAX(SoldDate)) / (COUNT(*) - 1) END AS TurnoverFROM @SampleGROUP BY LINCORDER BY LINC-- SwePeso 3SELECT LINC, CASE WHEN COUNT(*) = 1 THEN NULL ELSE 1E * DATEDIFF(DAY, MIN(SoldDate), MAX(SoldDate)) / (COUNT(*) - 1) END AS TurnoverFROM @SampleGROUP BY LINCORDER BY LINC N 56°04'39.26"E 12°55'05.63" |
 |
|
robvolk
Most Valuable Yak
15732 Posts |
Posted - 2012-02-10 : 17:31:04
|
[code];WITH CTE(linc, SoldDate, rn) AS ( SELECT linc, SoldDate, ROW_NUMBER() OVER (PARTITION BY linc ORDER BY SoldDate) FROM myTable)SELECT a.linc, AVG(DATEDIFF(DAY,a.SoldDate,b.SoldDate)) daysFROM CTE AINNER JOIN CTE B ON A.linc=B.linc AND A.rn=B.rn-1GROUP BY a.linc[/code] |
 |
|
offspring22
Starting Member
38 Posts |
Posted - 2012-02-10 : 18:00:08
|
Thanks SwePeso - that seems to have done it for me. One question though - what exactly does the 1E * your code do? Haven't the 1E * doesn't seem to make a difference. |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2012-02-10 : 18:05:18
|
Rob, if you have 4 dates (in chronological order; a, b, c, d) the average of them is written(b - a) + (c - b) + (d - c)--------------------------- 3 which is what you have written in your code.But if we expand the formula, we find that(b - a) + (c - b) + (d - c) b - a + c - b + d - c d - a--------------------------- = --------------------- = ----- 3 3 3 So there is no need to self-join and calculate the intermediate steps. N 56°04'39.26"E 12°55'05.63" |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2012-02-10 : 18:07:38
|
quote: Originally posted by offspring22 Thanks SwePeso - that seems to have done it for me. One question though - what exactly does the 1E * your code do? Haven't the 1E * doesn't seem to make a difference.
Yes it does, if the turnover contains decimals, such as 212.92 days or something.With the 1E you can get a decimal number back, otherwise the result is an integer division and returns 212.So it comes down to what precision you want for the result. N 56°04'39.26"E 12°55'05.63" |
 |
|
|
|
|
|
|