简体   繁体   English

SQL查询选择记录最大

[英]SQL query select record with Max

I have these records below : 我在下面有这些记录:

CustomerID | Name | Store | Quantity  
1          | Elie |   HO  |    16    
1          | Elie |   S1  |    4  

I would like to filter customers by taking only their max quantity? 我想仅通过提取最大数量来过滤客户吗? I tried it with Max, but the problem I cannot render all the fields with it. 我用Max尝试过,但问题是我无法使用它渲染所有字段。 If I add main.store in the first line, the second row shows. 如果我在第一行中添加main.store,则会显示第二行。 Is there any solution? 有什么解决办法吗?

Select main.CUSTOMER_ID, main.Name
from
(
    Select Name = cus.FIRST_NAME + ' ' + cus.LAST_NAME, 
           Store = cs.NAME
           ,Transaction_Number = count(ts.TRANSACTION_SUMMARY_ID) 
           ,cus.CUSTOMER_ID
    from TRANSACTION_SUMMARY ts
    inner join dbo.CUSTOMER cus 
        on ts.CUSTOMER_ID = cus.CUSTOMER_ID
    inner join dbo.CORPORATE_STORE cs 
        on ts.CORPORATE_STORE_ID = cs.CORPORATE_STORE_ID
    Group by cus.CUSTOMER_ID
        ,cus.FIRST_NAME
        ,cus.LAST_NAME
        ,cs.Name
) as main
Group by CUSTOMER_ID
    ,main.Name
order by main.CUSTOMER_ID

This is a good use of window functions: 这是窗口函数的好用法:

with t as (
      Select Name = cus.FIRST_NAME + ' ' + cus.LAST_NAME, 
             Store = cs.NAME,  
             Transaction_Number = count(ts.TRANSACTION_SUMMARY_ID) , cus.CUSTOMER_ID
      from TRANSACTION_SUMMARY ts
      inner join dbo.CUSTOMER cus on ts.CUSTOMER_ID = cus.CUSTOMER_ID
      inner join dbo.CORPORATE_STORE cs on ts.CORPORATE_STORE_ID = cs.CORPORATE_STORE_ID
      Group by cus.CUSTOMER_ID, cus.FIRST_NAME, cus.LAST_NAME, cs.Name
     )
select name, store, Transaction_Number, CUSTOMER_ID
from (select t.*,
             row_number() over (partition by customer_id order by transaction_number desc) as seqnum
      from t
     ) t
where seqnum = 1;

You can actually dispense with the subquery. 您实际上可以免除子查询。 However, using window functions with aggregations looks funny at first: 但是,将窗口函数与聚合一起使用时乍看起来很有趣:

with t as (
      Select Name = cus.FIRST_NAME + ' ' + cus.LAST_NAME, 
             Store = cs.NAME,  
             Transaction_Number = count(ts.TRANSACTION_SUMMARY_ID) , cus.CUSTOMER_ID,
             row_number() over (partition by cus.CUSTOMER_ID
                                order by count(ts.TRANSACTION_SUMMARY_ID) desc
                               ) as seqnum
      from TRANSACTION_SUMMARY ts
      inner join dbo.CUSTOMER cus on ts.CUSTOMER_ID = cus.CUSTOMER_ID
      inner join dbo.CORPORATE_STORE cs on ts.CORPORATE_STORE_ID = cs.CORPORATE_STORE_ID
      Group by cus.CUSTOMER_ID, cus.FIRST_NAME, cus.LAST_NAME, cs.Name
     )
select name, store, Transaction_Number, CUSTOMER_ID
from t
where seqnum = 1;

Please try: 请试试:

select * From tbl a
where a.Quantity=
    (select MAX(b.Quantity) from tbl b where a.CustomerID=b.CustomerID)

what you want is 你想要的是

select customer_id, max( quantity ) 
from main
group by customer_id

then you can use this to join to itself if you want 那么你可以使用它来加入自己

select * 
from main 
, (
select customer_id, max( quantity ) qty
from main
group by customer_id
) m
where main.customer_id = m.customer_id
and main.quantity = m.qty

Obviously, name has no business being in this table, but you included it, so I did too... 显然,此表中没有name ,但您将其包括在内,所以我也这样做了。

SELECT x.* 
  FROM my_table x 
  JOIN 
     ( SELECT customerid
            , name
            , MAX(quantity) max_quantity 
         FROM my_table 
        GROUP 
           BY customerid
            , name
     ) y 
    ON y.customerid = x.customerid 
   AND y.name = x.name 
   AND y.max_quantity = x.quantity;

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

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