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.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 datetime sequence multiple iterations

Author  Topic 

Movp69
Starting Member

10 Posts

Posted - 2009-01-29 : 09:28:12
I'm new to SQL so excuse my naivety : )

I have a datetime formatted column for transactions, by numerous customeIDs.

I am trying to cluster together all date times which are BETWEEN 1 AND 10 and leave all the others as stand alones.

However, I cannot simply do a JOIN e.g.
SELECT *
FROM table1 a, table1 b
JOIN table1 b ON a.customerid=b.customerid
WHERE a.transactionday=b.transactionday BETWEEN 1 AND 10

because there are up to 310 separate transactiondays (though filtering out the ones which happen on the same day is necessary)

So, i need some kind of iteration which could be possibly infinite and which could calculate all the possible combinations of datetime differences and cluster the ones that are 1-10. i.e. there may be 4 transactiondays for 1 customerid, so the code would need to check differences between 1 and 2, 1 and 3, 1 and 4, 2 and 3, 2 and 4, 3 and 4 etc...

any smart ideas?
one thing we did was create a sequence column which numbers in order the transactiondays under 1 customer i.e. 1,2, 3, 4.... and then starts at 1 again for the next customerid. thought maybe this would help....?!


thanks ever so much

Mo

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-01-29 : 09:32:30
show some sample data and explain what you want with output.

ALso this is script library. in future post this in transact sql forum in 2000 or 2005
Go to Top of Page

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2009-01-29 : 09:34:35
moved from script library.

___________________________________________________________________________
Causing trouble since 1980
Blog: http://weblogs.sqlteam.com/mladenp
Speed up SSMS development: www.ssmstoolspack.com <- version 1.1 out!
Go to Top of Page

Movp69
Starting Member

10 Posts

Posted - 2009-01-29 : 09:54:09
ID CustomerID TRANSACTIONDAY SEQ

1 I100000999 2007-07-23 00:00:00.000 1
2 I100000999 2007-08-08 00:00:00.000 2
3 I100000999 2008-05-17 00:00:00.000 3
4 I100000999 2008-06-03 00:00:00.000 4
5 I100000999 2008-06-18 00:00:00.000 5
6 I100000999 2008-07-04 00:00:00.000 6
7 I100000997 2007-12-02 00:00:00.000 1

I want to group all transactiondays that fall within 9 days of each other.

Thanks
Go to Top of Page

tonymorell10
Yak Posting Veteran

90 Posts

Posted - 2009-01-29 : 11:33:40
I'm not sure exactly what you're expecting the output to look like. Here's a stab at it. I modified the data you provided so that it would return some results.


DECLARE @tv_Transactions TABLE
(
ID int,
CustomerID varchar(25),
TransactionDay datetime,
Sequence int
);

INSERT @tv_Transactions (ID, CustomerID, TransactionDay, Sequence)
SELECT 1, 'I100000999', '2007-07-23 00:00:00.000', 1 UNION ALL
SELECT 2, 'I100000999', '2007-07-25 00:00:00.000', 2 UNION ALL
SELECT 3, 'I100000999', '2008-05-17 00:00:00.000', 3 UNION ALL
SELECT 4, 'I100000999', '2008-05-25 00:00:00.000', 4 UNION ALL
SELECT 5, 'I100000999', '2008-06-01 00:00:00.000', 5 UNION ALL
SELECT 6, 'I100000999', '2008-07-04 00:00:00.000', 6 UNION ALL
SELECT 7, 'I100000997', '2007-12-02 00:00:00.000', 1 UNION ALL
SELECT 8, 'I100000997', '2007-12-11 00:00:00.000', 2;


WITH TranDays (ID, CustomerID, TransactionDay, NextID, NextTransactionDay, Days) AS
(
SELECT a.ID, a.CustomerID, a.TransactionDay,
b.id as NextID, b.transactionday as NextTransactionDay,
DATEDIFF(dd, a.transactionday, b.transactionday) as Days
FROM @tv_Transactions a
LEFT JOIN @tv_Transactions b
ON a.customerid = b.customerid AND b.transactionday > a.transactionday
)

SELECT ID, CustomerID, TransactionDay
FROM TranDays
WHERE Days BETWEEN 1 AND 10
UNION
SELECT NextID AS ID, CustomerID, NextTransactionDay AS TransactionDay
FROM TranDays
WHERE Days BETWEEN 1 AND 10;
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-01-29 : 12:20:08
do you mean this?

SELECT CustomerID,
(DATEDIFF(dd,TransactionDay,Start)/9 + 1) AS Group,
MIN(TransactionDay) AS GroupStart
MAX(TransactionDay) AS GroupEnd
FROM
(
SELECT ID,
CustomerID,
TransactionDay,
Sequence,
MIN(TransactionDay) OVER(PARTITION BY CustomerID) AS Start
FROm YourTable
)t
GROUP BY CustomerID,DATEDIFF(dd,TransactionDay,Start)/9
Go to Top of Page
   

- Advertisement -