简体   繁体   English

SQL Over Partition通过Over Partition By

[英]SQL Over Partition By Over Partition By

I have the following code and it works fine when I am referencing one ' leadno ' as it returns the highest sum of individual products 我有以下代码,当我引用一个“ leadno ”时,它可以正常工作,因为它返回单个产品的最高总和

select t.*
from (Select t2.leadno, t1.quoteno, t1.cn_ref, sum(t1.qty/100) as 't_qty',
             ROW_NUMBER() Over (Partition By t1.cn_ref order by sum(qty/100) desc) as RN 
      From dba.quotelne t1 INNER JOIN
           dba.quotehdr t2
           ON t1.quoteno = t2.quoteno
      Where leadno = 31665 and t1.statusflag = 'A' and t2.statusflag = 'A' 
      Group By t2.leadno, t1.quoteno, t1.cn_ref
     ) t
where rn = 1

But when I add more that one leadno it returns doesn't return all the quoteno it only returns the values of the highest sum(qty/100) , so Is there a way to run a ROW_NUMBER() Over (Partition By in a ROW_NUMBER() Over (Partition By) 但是,当我添加更多的是一个leadno返回不返回所有quoteno它只返回最高值sum(qty/100)那么,有没有一种方式来运行一个ROW_NUMBER() Over (Partition ByROW_NUMBER() Over (Partition By)

I hope this makes sense, but if not I will try to explain, a little further, each 'lead' may have one or more 'quotes' against it. 我希望这是有道理的,但如果不能,我将作进一步解释,每个“线索”可能都有一个或多个“引号”。 Each 'quote' will have a number of products listed. 每个“报价”都会列出许多产品。 These may be duplicated (hence why I am using sum(qty/100) What I need to happen is all leadno to be displayed, the unique cn_ref and the highest quantity RN = 1 against the leadno 这些可以被复制(所以为什么我使用的sum(qty/100)我需要发生的是所有leadno要显示,独特的cn_ref和最高数量RN = 1leadno

I am not 100% sure but I think you need to put the leadno in the Partition to accomplish what you need: 我不确定100%,但是我认为您需要将Leadno放入“分区”中才能完成所需的操作:

select t.*
    from (Select t2.leadno, t1.quoteno, t1.cn_ref, sum(t1.qty/100) as 't_qty',
         ROW_NUMBER() Over (Partition By t2.leadno, t1.cn_ref order by sum(qty/100) desc) as RN 
      From dba.quotelne t1 INNER JOIN
           dba.quotehdr t2
       ON t1.quoteno = t2.quoteno
      Where leadno = 31665 and t1.statusflag = 'A' and t2.statusflag = 'A' 
      Group By t2.leadno, t1.quoteno, t1.cn_ref
     ) t
where rn = 1

Beside that, you shouldn't devide the qty in the order by, it doens't change the order because you devide every value by 100, but the engine has to devide every record before it can create the summary 除此之外,您不应该按顺序来分配数量,它不会更改顺序,因为您将每个值都按100进行分配,但是引擎必须对每个记录进行分类,然后才能创建摘要

Why your need to use ROW_NUMBER() OVER (PARTITION BY)? 为什么需要使用ROW_NUMBER()OVER(PARTITION BY)?

If I understand your needs, you want the the follow values: leadno, cn_ref, max(sum(qty/100)) 如果我了解您的需求,则需要以下值:Leadno,cn_ref,max(sum(qty / 100))

So, try this query: 因此,请尝试以下查询:

Select t2.leadno, t1.cn_ref, max(sum(t1.qty/100)) as 't_qty',
  From dba.quotelne t1 INNER JOIN
       dba.quotehdr t2
       ON t1.quoteno = t2.quoteno
  Where leadno = 31665 and t1.statusflag = 'A' and t2.statusflag = 'A' 
  Group By t2.leadno

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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