| Author |
Topic |
|
samuelclay
Yak Posting Veteran
71 Posts |
Posted - 2007-09-06 : 13:59:40
|
| What I'm looking for is a way to find the date when customers pass a threshold for transactions processed.Simplified, I have two tables, a customer table and a transaction per day table.-- Company info, including the threshold value to compare the running totals toCREATE TABLE #Company( [CompanyID] int NOT NULL, [Name] [varchar](150) NOT NULL, [Threshold] [int] NOT NULL)-- List of total transaction per day for each companyCREATE TABLE #Transactions( [TransactionID] [bigint] NOT NULL, [CompanyID] [int] NOT NULL, [Date] [smalldatetime] NOT NULL, [Transactions] [int] NOT NULL,)here's some test data:insert into #Company( CompanyID, [Name], Threshold)SELECT 1,'Alpha Dog School',15UNION SELECT 2, 'Bee Hive Supplies',8UNION SELECT 3, 'Clive''s Clarinets',55insert into #Transactions(TransactionID, CompanyID, Date, Transactions)SELECT 1,1,'1/1/2007 12:00:00 AM',2UNION SELECT 2,2,'1/1/2007 12:00:00 AM',1UNION SELECT 3,3,'1/1/2007 12:00:00 AM',8UNION SELECT 4,2,'1/2/2007 12:00:00 AM',1UNION SELECT 5,1,'1/2/2007 12:00:00 AM',1UNION SELECT 6,3,'1/2/2007 12:00:00 AM',15UNION SELECT 7,1,'1/3/2007 12:00:00 AM',6UNION SELECT 8,2,'1/3/2007 12:00:00 AM',9UNION SELECT 9,3,'1/3/2007 12:00:00 AM',12UNION SELECT 10,1,'1/4/2007 12:00:00 AM',3UNION SELECT 11,2,'1/4/2007 12:00:00 AM',5UNION SELECT 12,3,'1/4/2007 12:00:00 AM',11UNION SELECT 13,1,'1/5/2007 12:00:00 AM',4UNION SELECT 14,2,'1/5/2007 12:00:00 AM',4UNION SELECT 15,3,'1/5/2007 12:00:00 AM',15UNION SELECT 16,1,'1/6/2007 12:00:00 AM',4UNION SELECT 17,2,'1/6/2007 12:00:00 AM',5UNION SELECT 18,3,'1/6/2007 12:00:00 AM',19UNION SELECT 19,1,'1/7/2007 12:00:00 AM',8UNION SELECT 20,2,'1/7/2007 12:00:00 AM',9a quick query:select c.*,t.Date, t.Transactionsfrom #Company c inner join #Transactions t on c.CompanyID = t.CompanyIDwhere c.CompanyID = 1order by Dateshows : CompanyID Name Threshold Date Transactions1 Alpha Dog School 15 2007-01-01 00:00:00 21 Alpha Dog School 15 2007-01-02 00:00:00 11 Alpha Dog School 15 2007-01-03 00:00:00 61 Alpha Dog School 15 2007-01-04 00:00:00 31 Alpha Dog School 15 2007-01-05 00:00:00 41 Alpha Dog School 15 2007-01-06 00:00:00 41 Alpha Dog School 15 2007-01-07 00:00:00 8and adding the transactions, we see that they passed the threshold of 15 on 2007-01-05.What I'd like would each company and the date they crossed the threshold value in the company table. So for this test data, it would look like : (assuming I can add properly this morning)CompanyID CompanyName Date1 Alpha Dog School 2007-01-05 00:00:002 Bee Hive Supplies 2007-01-03 00:00:003 Clive's Clarinets 2007-01-05 00:00:00I would like to do this without a cursor, but I keep going around in circles trying to get the set logic straight. I have read some of the posts here about running totals, but didn't see a way to then pull a specific date from the list...Anyway, I hope someone with a clearer head this morning could see the quick and easy query I am missing.Samuel ClaySQL/ASP Developersamuel_clay@asp-dudes.com |
|
|
jen
Master Smack Fu Yak Hacker
4110 Posts |
Posted - 2007-09-06 : 15:42:28
|
| don't you just need to do a group by with max(date) in your query?i mean use your query as source select company,max(date)from (query)where threshold=5group by companyanyways... i haven't tested this so ...--------------------keeping it simple... |
 |
|
|
samuelclay
Yak Posting Veteran
71 Posts |
Posted - 2007-09-06 : 16:36:53
|
quote: Originally posted by jen don't you just need to do a group by with max(date) in your query?i mean use your query as source select company,max(date)from (query)where threshold=5group by companyanyways... i haven't tested this so ...--------------------keeping it simple...
not positive where you were going with this, but I don't want the max date... there could be three years worth of data, and they passed their threshold on day 5.. I want that date. Also the threshold is different for each company.A problem (the problem?) I'm having using a derived table, is that the transaction table has hundreds of thousand rows (a record for every customer, for every day). So the sub query that calculates the running total is taking too long.I have a partial solution (at least for my little example) using a udf. Going to test it on the actual data and see how it performs.Thanks for looking guys... if anyone sees a clever (effecient) way to do this without a udf, I'd be happy to try it out...Samuel ClaySQL/ASP Developersamuel_clay@asp-dudes.com |
 |
|
|
saad.ahnad@gmail.com
Starting Member
15 Posts |
Posted - 2007-09-06 : 19:57:03
|
| select c.CompanyID, c.[Name], c.Threshold, min(t.Date) threshold_datefrom #Company cleft outer join (select t1.TransactionID, t1.CompanyID, t1.Date, t1.Transactions, sum(t2.Transactions) t2from #Transactions t1 join #Transactions t2 on t1.CompanyID = t2.CompanyId and t2.Date <= t1.Dategroup by t1.TransactionID, t1.CompanyID, t1.Date, t1.Transactions) ton t.CompanyID = c.CompanyIDand t.t2 >= c.Thresholdgroup by c.CompanyID, c.[Name], c.Threshold--Saad Ahmadsaad.ahmad@gmail.com |
 |
|
|
|
|
|