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 |
|
egerencher
Starting Member
3 Posts |
Posted - 2012-09-19 : 18:26:55
|
| I work for a credit card company. Our database has a customer table and a transaction table. Fields in the customer table are SSN and CustomerKey. Fields in the transaction table are CustomerKey, transaction date (Transdate), and transaction amount (TransAmt).I need a query that can identify each ssn where the sum of any of their transaction amounts > 1000 within a two day period in 2012. If a ssn has transaction amounts > 1000 within a two day period, I need the query to return all the transactions for that ssn.Here is an example of the raw data in the Transaction Table:Trans#-----CustKey-----Date--------Amount1-----------12345----01/01/12--------$6002-----------12345----01/02/12--------$5003-----------67890----01/03/12--------$104-----------98765----04/01/12--------$6005-----------43210----04/02/12--------$6006-----------43210----04/03/12--------$1007-----------13579----04/02/12--------$6008-----------24568----04/03/12--------$100Here is an example of the raw data in the Customer Table:CustKey-----SSN12345------12345678967890------12345678998765------98765432143210------98765432113579------24680135724568------246801357Here are the results I need:Trans#------SSN---------Date---------Amount1--------123456789----01/01/12---------$6002--------123456789----01/02/12---------$5003--------123456789----01/03/12----------$104--------987654321----04/01/12---------$6005--------987654321----04/02/12---------$6006--------987654321----04/03/12---------$100As you can see in my results included all transactions for SSN 123456789 and 987654321, and excluded SSN 246801357.Here is what I tried. select c.ssn, t.*from TransDtl tjoin Customer c on t.CustomerKey = c.CustomerKeyjoin(select c.ssn, SUM(t.TransAmt) as Total from TransDtl t join Customer c on t.CustomerKey = c.CustomerKey where t.postdate between '1/4/2012' and '1/5/2012' group by c.ssn having SUM(t.transamt) > 1000) xon x.ssn = c.SSNwhere t.postdate between '1/4/2012' and '1/5/2012'The problem I am having is that I can only run my above query for 2 days out of the year. I want to run the query for the whole year, and identify any transaction in that year where the sum of the transactions exceeded 1000 in any given 2-day period; and then list all the transactions for the ssn. Thank you! |
|
|
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts |
Posted - 2012-09-19 : 18:54:30
|
If I understood the requirements correctly (i.e., the dates have to be consecutive, and any SSN that satisfies the two-consecutive-day requirement should be listed etc.), see if this query gives you what you are looking for.This code parses without any errors, but I have not tested the logic. If you post sample data in a form that someone can copy and paste to create the test tables and populate with data, you would get faster and better answers.;WITH cte AS( SELECT t.TransNum, c.SSN, t.Date, t.Amount FROM TransactionsTable t INNER JOIN CustomerTable c ON c.CustKey = t.CustKey)SELECT * FROM cteWHERE SSN IN( SELECT a.SSN FROM cte a INNER JOIN cte b ON a.SSN = b.SSN AND a.Date = DATEADD(dd,1,b.Date) AND a.Amount + b.Amount > 1000) |
 |
|
|
|
|
|
|
|