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 |
|
amarundo
Starting Member
9 Posts |
Posted - 2012-01-04 : 15:49:28
|
| I'm pretty new to SQL, but not to programming in general.Here's what I need:Table A: customerid, customernameTable B: customerid, calldatetime, callresultcodeTable B will have many entries for every customerid, for everytime we called, each one with its own calldatetime and callresultcode.I want to have first, a list of just the most recent calls to each customerid in Table B, and second, an aggregate of callresultcode, so it will say for callresultcode = 'Signed Up' 25 records, for callresultcode = 'Go fly a kite' 100 records, and so on. I'm only interested in the last callresultcode, because previous ones will have things like 'No Answer', 'Not home', etc. And we don't have that last result in Table A.Thanks |
|
|
pduffin
Yak Posting Veteran
68 Posts |
Posted - 2012-01-04 : 17:43:26
|
| create table #tmpCustomer (customerid int, customername char(50))create table #tmpCustomerCall (customerid int, calldatetime datetime, callresultcode int)insert into #tmpCustomer values (1,'bob')insert into #tmpCustomer values (2,'jeff')insert into #tmpCustomerCall values (1,'jan 01,11',100)insert into #tmpCustomerCall values (1,'jan 05,11',101)insert into #tmpCustomerCall values (1,'feb 05,11',101)insert into #tmpCustomerCall values (2,'feb 01,11',100)insert into #tmpCustomerCall values (2,'mar 05,11',101)insert into #tmpCustomerCall values (2,'apr 05,11',103) select cu.customerid,customername,calldatetime,callresultcode from #tmpcustomer cu join #tmpcustomercall ca on ca.customerid = cu.customerid where ca.calldatetime = ( select max(calldatetime) calldatetime from #tmpcustomercall cc where cc.customerid = cu.customerid )select callresultcode,count(*)from( select cu.customerid,customername,calldatetime,callresultcode from #tmpcustomer cu join #tmpcustomercall ca on ca.customerid = cu.customerid where ca.calldatetime = ( select max(calldatetime) calldatetime from #tmpcustomercall cc where cc.customerid = cu.customerid )) agroup by callresultcodeLikes to run, hates the runs! |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-01-05 : 10:24:14
|
sounds like thisSELECT t.customername,b.calldatetime,b.callresultcode,t2.CntFROM tableA tINNER JOIN tableB bON b.customerid = t.customeridINNER JOIN (SELECT customerid,MAX(calldatetime) AS Latest FROM TableB GROUP BY customerid )t1ON t1.customerid = b.customeridAND t1.Latest = b.calldatetimeINNER JOIN (SELECT customerid,callresultcode,COUNT(*) AS Cnt FROM TableB GROUP BY customerid,callresultcode )t2ON t2.customerid = t1.customeridAND t2.callresultcode = b.callresultcode ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
amarundo
Starting Member
9 Posts |
Posted - 2012-02-05 : 12:48:49
|
| Hi Guys,Thanks so much. My projects changed and I wasn't able to test this.I may try it tonight.Thanks again for your help. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-02-05 : 12:51:22
|
ok...no problem ..let me know how you got on.. ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|
|