简体   繁体   中英

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. If I add main.store in the first line, the second row shows. 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...

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;

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