FarligOpptreden
Executive Member
*BUMP*
(In case the OP only saw my original post and not the edit for the sample...)
(In case the OP only saw my original post and not the edit for the sample...)
South Africa’s biggest forum. Discuss, discover, and connect with thousands of members.
*BUMP*
(In case the OP only saw my original post and not the edit for the sample...)
SELECT AccountNumber, COUNT(DISTINCT(PaymentOp)) FROM transactions
GROUP BY AccountNumber
_Hecate_ said:This gives the same results as mine. For each transaction I need to go back 30 mins and count the distinct number of paymentoptions. I'm not linking it back to reference each individual transactiondate, the code is referencing the whole date range
He needs to count the distinct number of payment options made, per account, within 30 minutes of each other. So, in other words, he needs all accounts where there were at least 2 different payment options made within a 30 minute period. This has got nothing to do with filtering it according to the different payment options made in the last 30 minutes, as most replies to this topic assume...Can you explain what you mean by this?
Seems to be on the right track but got some false positives![]()
Will play around with it.
Seems to be on the right track but got some false positives![]()
Will play around with it.
He needs to count the distinct number of payment options made, per account, within 30 minutes of each other. So, in other words, he needs all accounts where there were at least 2 different payment options made within a 30 minute period. This has got nothing to do with filtering it according to the different payment options made in the last 30 minutes, as most replies to this topic assume...
Now there's a good explanation!![]()
I just read an earlier post of yours - do you need to only go BACK 30 minutes for each transaction? The sample I posted allowed 30 minutes in either direction, so that might be some of the "false positives" you're getting...
Yes only 30 mins back. I changed it, still doesn't work.
Dammit! Hate that I can't figure this out and now have to like a typical girl ask a guy to help me! Grrrrr
Will let you guys know if they find a solution.
So you only need the transactions for the last 30 min? If so you should be able to utilize my statement somehow. What db are you using?
Sent from the MyBroadband iPhone App
DECLARE @Test TABLE (AccountNumber char, TransactionDateTime datetime, PaymentOp int)
INSERT INTO @Test (AccountNumber, TransactionDateTime, PaymentOp)
SELECT 'A', '2011/02/10 04:01', 1 UNION ALL
SELECT 'A', '2011/02/10 04:02', 2 UNION ALL
SELECT 'A', '2011/02/10 04:03', 3 UNION ALL
SELECT 'A', '2011/02/10 04:04', 3 UNION ALL
SELECT 'B', '2011/01/28 22:55', 111 UNION ALL
SELECT 'B', '2011/01/28 22:59', 222 UNION ALL
SELECT 'B', '2011/01/29 23:17', 333 UNION ALL
SELECT 'B', '2011/01/29 23:17', 444 UNION ALL
SELECT 'C', '2011/03/03 09:24', 101 UNION ALL
SELECT 'C', '2011/03/03 09:25', 101 UNION ALL
SELECT 'C', '2011/03/15 07:33', 102 UNION ALL
SELECT 'C', '2011/03/15 07:36', 102 UNION ALL
SELECT 'C', '2011/03/15 07:37', 102 UNION ALL
SELECT 'C', '2011/03/15 07:38', 102
;WITH NextMatch AS
(
SELECT
AccountNumber
, PaymentOp
, TransactionDateTime as TranTime
, dateadd(mi,-30,TransactionDateTime) as MinTranTime
FROM @Test
)
, Matches AS
(
SELECT
AccountNumber
, PaymentOp
, TranTime
, MinTranTime
, stuff
(
(
SELECT DISTINCT ',' + convert(varchar,NM.PaymentOp)
FROM NextMatch NM
WHERE
NM.AccountNumber = M.AccountNumber
AND NM.TranTime BETWEEN M.MinTranTime AND M.TranTime
FOR XML PATH('')
)
, 1
, 1
, ''
) AS DistinctPaymentOptions
FROM NextMatch M
)
SELECT
AccountNumber
, PaymentOp
, DistinctPaymentOptions
FROM Matches
It was soo simple I just needed to join the table to itself via an inner join on accountnumber and the transactiondatetime between transactiondatetime and dateadd(minute,-30, transactiondatetime) then have the accountnumber from table A and count the cards from table B
/facepalm!
Yes only 30 mins back. I changed it, still doesn't work.
Dammit! Hate that I can't figure this out and now have to like a typical girl ask a guy to help me! Grrrrr
Will let you guys know if they find a solution.