简体   繁体   中英

Using Group By to get Desired Result

I have a table "Table1" like below:

EffectiveDate  Client  Fund    RunDate     UserId 
2014-05-31       A     AGG    2014-06-03    user   
2014-03-31       A     AGG    2014-07-01    user 
2014-10-31       A     AGG    2014-11-04    user 
2014-09-30       A     EFA    2013-10-10    user 
2014-11-31       A     EFA    2014-01-15    user
2014-01-31       A     EFA    2014-02-03    user

I need to get result like if Effective Date is maximum for any given input date then it'll only return that record for a particular Fund.I am using query to get desired result like :

SELECT Max(tbl.effectivedate) AS EffectiveDate, 
       tbl.client, 
       tbl.fund, 
       tbl.rundate, 
       tbl.userid 
FROM   (SELECT effectivedate, 
               client, 
               fund, 
               rundate, 
               userid 
        FROM   Table1 
        WHERE  effectivedate < = '11/01/2014') AS tbl 
GROUP  BY tbl.client, 
          tbl.fund, 
          tbl.rundate, 
          tbl.userid 

But I am not getting the desired result. Please if anyone help me.

Desired Output :

 EffectiveDate  Client  Fund    RunDate     UserId 
  2014-10-31       A     AGG    2014-11-04    user 
  2014-09-30       A     EFA    2013-10-10    user 

Try this one:

SAMPLE DATA

create table #table1(
    EffectiveDate date,
    Client varchar(100),
    Fund varchar(100),
    RunDate date,
    UserId varchar(100)
)

insert into #table1
select '2014-05-31', 'A', 'AGG', '2014-06-03', 'user' union all 
select '2014-03-31', 'A', 'AGG', '2014-07-01', 'user' union all 
select '2014-10-31', 'A', 'AGG', '2014-11-04', 'user' union all 
select '2014-09-30', 'A', 'EFA', '2013-10-10', 'user' union all 
select '2014-11-30', 'A', 'EFA', '2014-01-15', 'user' union all 
select '2014-01-31', 'A', 'EFA', '2014-02-03', 'user'

CTE SOLUTION

;with cte as(
    select
        *,
        rn = row_number() over(partition by Fund order by EffectiveDate desc)
    from #table1
    where
        EffectiveDate <= '2014/11/01'
)
select
    EffectiveDate,
    Client,
    Fund,
    RunDate,
    UserId
from cte
where
    rn = 1

WITHOUT CTE AND ROW_NUMBER()

select
    t1.*
from #table1 t1
inner join (
    select
        EffectiveDate = MAX(EffectiveDate),
        Fund
    from #table1
    where
        EffectiveDate <= '2014/11/01'
    group by Fund
) t2
    on t2.fund = t1.fund
    and t2.EffectiveDate = t1.EffectiveDate

Another way to do it if you are interested. You were almost there with your original code (fiddle here: http://sqlfiddle.com/#!3/20926c/9 ):

SELECT effectivedate,
       client, 
       fund, 
       rundate, 
       userid 
FROM
(
    SELECT MAX(tbl.effectivedate) OVER (PARTITION BY tbl.client, tbl.fund) AS MaxEffDateByClientByFund,
           tbl.effectivedate,
           tbl.client, 
           tbl.fund, 
           tbl.rundate, 
           tbl.userid 
FROM       Table1 tbl
WHERE      effectivedate < = '20141101'
) tbl2
WHERE effectivedate = MaxEffDateByClientByFund;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM