| Author |
Topic |
|
jjmusicpro
Yak Posting Veteran
79 Posts |
Posted - 2012-01-05 : 17:02:59
|
| I have a query like this below -select u.username as [account name], ap.applicationname as [applicationdate], ud.lastupdatefrom users u, userapplicationdata udinner join applications ap on ud.applicationid = ap.applicationidorder by [account name]asc, [application name] ascThis query will bring back multiple records of somsone so for example.Joe Jones, Google, 2012-01-01 19:432 Joe Jones, Google, 2012-01-02 19:432 Joe Jones, Google, 2012-01-05 19:432 Joe Jones, Google, 2012-01-08 19:432 Joe Jones, Google 2, 2012-01-08 19:432 Joe Jones, Google 2, 2012-01-06 19:432 Frank Jones, Google, 2012-01-08 19:432 Frank Jones, Google, 2012-01-09 19:432 I would like for it to look at each "set" of users records of a particlular application, and find the one with the latest date, and didsplay that only, so the result would be like.Joe Jones, Google, 2012-01-08 19:432 Joe Jones, Google 2, 2012-01-08 19:432 Frank Jones, Google, 2012-01-09 19:432 any ideas? |
|
|
pduffin
Yak Posting Veteran
68 Posts |
Posted - 2012-01-05 : 17:17:55
|
| This is something I wrote for a previous post to show how to do this, I think this will work for you too!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,callresultcodefrom #tmpcustomer cujoin #tmpcustomercall ca on ca.customerid = cu.customeridwhere ca.calldatetime =(select max(calldatetime) calldatetimefrom #tmpcustomercall ccwhere cc.customerid = cu.customerid)Likes to run, hates the runs! |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-01-06 : 13:51:43
|
| [code]select [account name], [applicationdate], lastupdatefrom(select u.username as [account name], ap.applicationname as [applicationdate], ud.lastupdate,row_number() over (partition by u.username , ap.applicationname order by ud.lastupdate desc) as Rnfrom users u, userapplicationdata udinner join applications ap on ud.applicationid = ap.applicationid)twhere rn=1order by [account name]asc, [application name] asc[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-01-06 : 13:52:40
|
| just noticed in your query you dont have any relationship specified between users and userapplicationdata tables so its effectively doing a cross join. is this intentional?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|