【问题标题】:Top 2 offers with sum of all offers排名前 2 位的报价和所有报价的总和
【发布时间】:2015-10-18 03:20:20
【问题描述】:

有人可以告诉我如何获得如下结果。

使用 rank

我也希望得到“total_offer”,它应该是“offer1”和“offer2”的总和。当没有 offer2(例如:taurus)时,'total offer' 应该是 'offer1' 和 'null' for 'offer2'

输入:

customer    make    zipcode offer notes  
mark        focus   101     250   cash  
mark        focus   101     2500  appreciation cash  
mark        focus   101     1000  cash  
mark        focus   101     1500  cash offer  

henry       520i    21405   500  cash offer  
henry       520i    21405   100  cash  
henry       520i    21405   750  appreciation cash  
henry       520i    21405   100  cash  

mark        taurus  48360   250    appreciation cash  

mark        mustang 730     500  cash  
mark        mustang 730     1000  Cash offer  
mark        mustang 730     1250  appreciation cash  

期望的输出:

| CUSTOMER | MAKE    | ZIPCODE | TOP_OFFER1 | notes1 | TOP_OFFER2 | notes2 | Total_offer |  
| henry | 520i | 21405 | 750  | appreciation cash | 500 | cash offer | 1250    
| mark  | focus   | 101  2500 | appreciation cash | 1500 | cash offer | 4000  
| mark | mustang | 730 | 1250 | appreciation cash | 1000 | cash offer | 2250    
| mark  | taurus  | 48360 | 250 | appreciation cash | NULL       | 250 |   

谢谢

PS:

下面的链接告诉我需要执行dense_rank 才能获得前2 名的优惠。 (Using a pl-sql procedure or cursor to select top 3 rank)

【问题讨论】:

    标签: sql oracle stored-procedures oracle11g cursor


    【解决方案1】:
     with x as 
     (select row_number() over(partition by customer,make order by offer desc) rn,
      customer, make, zipcode, offer from tablename)
     , y as (select customer, make, zipcode, offer from x where rn <=2)
     , z as (select customer, make, zipcode, 
             case when rn = 1 then offer else 0 end as offer_1, 
             case when rn = 2 then offer else 0 end as offer_2 
             from y)
      select customer, make, zipcode, offer_1, offer_2, offer_1+offer_2 total_offer
      from z
    

    这利用递归 cte 来完成您的任务。

    【讨论】:

      【解决方案2】:

      您最好使用ROW_NUMBER 而不是DENSE_RANK(可能返回两行以上)加上LEAD 来查找第二高的值

      select customer, make, zipcode,
          TOP_OFFER1, 
          TOP_OFFER2, 
          TOP_OFFER1 + coalesce(TOP_OFFER2,0) as Total_offer
      from
       ( 
         select customer, make, zipcode, 
            offer as TOP_OFFER1, -- max offer
            lead(offer) over (partition by customer, make, zipped
                              order by offer desc) as TOP_OFFER2, -- 2nd highest offer
            row_number() over (partition by customer, make, zipped
                              order by offer desc) as rn
        from tab
       ) dt
      where rn = 1 -- only the row with the highest offer
      

      【讨论】:

      • 嘿dnoeth,需要在row_number()函数之后包含(
      • @Sandeep:谢谢,已修复。
      • 你能告诉我如何使用notes1和notes 2吗?谢谢
      • @pavan:只需复制计算并将offer更改为notes
      【解决方案3】:

      试试这个代码...

      WITH subqueryfactoring AS
        (SELECT customer,
          make ,
          zipcode ,
          offer,
          lead(offer) over (partition BY customer, make , zipcode order by offer DESC) SecondLeadValue,
          row_number() over (partition BY customer, make , zipcode order by offer DESC ) rownumber
        FROM abc1
        )
      SELECT customer,
        make ,
        zipcode,
        offer "Top Offer1 ",
        SecondLeadValue "Top offer 2",
        offer + COALESCE(SecondLeadValue,0) "Total Offer"
      FROM subqueryfactoring
      WHERE rownumber<2;
      

      【讨论】:

        猜你喜欢
        • 2012-03-12
        • 1970-01-01
        • 2020-09-04
        • 1970-01-01
        • 1970-01-01
        • 2018-12-24
        • 1970-01-01
        • 1970-01-01
        • 2011-12-07
        相关资源
        最近更新 更多