简体   繁体   中英

Combining multiple rows into a single row SQL

I have a table like this.

|InvID| Client | Group | PricedDate | TotalFee | RepricedFee | CompanyFee|
|1    | A      | A.1   | 02-24-2020 | 100      | 80          | 8         |
|1    | A      | A.1   | 01-05-2020 | 100      | 75          | 1         |
|2    | A      | A.1   | 01-09-2020 | 100      | 60          | 1         |
|3    | B      | B.1   | 01-11-2020 | 150      | 95          | 10        |
|4    | B      | B.1   | 01-01-2020 | 100      | 55          | 11        |
|4    | B      | B.1   | 02-01-2020 | 100      | 90          | 10        |

I need to display a single row based on the latest PricedDate and Sum of Company Fee

|InvID| Client | Group | PricedDate | TotalFee | RepricedFee | CompanyFee|
|1    | A      | A.1   | 02-24-2020 | 100      | 80          | 9         |
|2    | A      | A.1   | 01-09-2020 | 100      | 60          | 1         |
|3    | B      | B.1   | 01-11-2020 | 150      | 95          | 10        |
|4    | B      | B.1   | 02-01-2020 | 100      | 90          | 21        |

just do aggregation

select invId,client,[group],max(priceddate),max(Totalfee),min(repricedFee),sum(companyfee)
from table 
group by invId,client,[group]

Try it like this:

select *
       , (select sum(CompanyFee) from my_table mt3 group by InvID) CompanyFee
from my_table mt1
where mt1.PricedDate = (select max(mt2.PricedDate) 
                        from my_table mt2
                        where mt2.InvID = mt1.InvID);

This part will make sure your data is from the row that has the largest PricedDate :

mt1.PricedDate = (select max(mt2.PricedDate) 
                  from my_table mt2
                  where mt2.InvID = mt1.InvID)

Also, if it is not enough to group by InvID only you can add other columns there.

Here is a demo

Try this,

declare @CompanyFee= select sum(CompanyFee) from table1
select InvID,Client,Group,PricedDate,TotalFee,RepricedFee,@CompanyFee from table1
       where priceddate=max(priceddate)

Try this.

select *
from my_table mt1
cross apply (
    select CompanyFee=sum(CompanyFee) from my_table mt3  where mt3.invid=mt1.invid
) as CompanyFeeTbl                    
where mt1.PricedDate = (select max(mt2.PricedDate) 
                        from my_table mt2
                        where mt2.InvID = mt1.InvID)

You can use window function :

select t.InvID, t.Client, t.Group, t.PricedDate, 
       t.TotalFee, t.RepricedFee, t.SumCompanyFee as CompanyFee
from(select t.*, sum(t.companyfee) over (partition by t.client, t.invId) as SumCompanyFee,
            row_number() over (partition by t.client, t.invId order by t.PricedDate desc) as seq
     from table t
     ) t
where seq = 1;

Is it the latest row per InvID you want? I would probably just get the maximum date and the sum in an aggregation query and then join that row:

select
  t.invid,
  t.client,
  t.group,
  t.priceddate,
  t.totalfee,
  t.repricedfee,
  agg.sum_fee as companyfee
from
(
  select invid, max(priceddate) as max_date, sum(companyfee) as sum_fee
  from mytable
  group by invid
) agg
join mytable t on t.invid = agg.invid and t.priceddate = agg.max_date
order by t.invid;

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