SQL查询选择记录与Max

时间:2014-07-21 11:21:49

标签: sql sql-server

我在下面有这些记录:

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

我想通过仅采用最大数量来过滤客户? 我用Max尝试过,但问题是我无法用它渲染所有字段。如果我在第一行添加main.store,则显示第二行。 有没有解决方案?

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

4 个答案:

答案 0 :(得分:4)

这很好地利用了窗口函数:

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;

您实际上可以免除子查询。但是,首先使用具有聚合的窗口函数看起来很有趣:

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;

答案 1 :(得分:2)

请尝试:

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

答案 2 :(得分:1)

你想要的是

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

然后你可以使用它来加入自己,如果你想

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

答案 3 :(得分:0)

显然,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;