简体   繁体   English

如何基于SQL Server的另一列中的最高价值为每个客户获得一行

[英]How to get a single row for each customer based on highest value in another column in SQL Server

I have a table that contains some customer information: 我有一个包含一些客户信息的表:

--------------------------------------------
|CustID  CustType  OrderType  CountofOrders|
|------------------------------------------|
|  1        C         DD          23       |
|  2       IC         DE          10       |
|  2       IC         DR           7       |
|  2       IC         DS           7       |
|  3        C         DR          14       |
|  3        C         DS          19       |
|  4       IC         DS           5       |
|  4       IC         DR           5       |
|  4       IC         DE           3       |
|  5       IC         DR           7       |
|  5       IC         DS           7       |
|  5       IC         DE           7       |
--------------------------------------------

I want to pull all records for Customers when CustType is not 'IC', regardless of how many rows they have. 当CustType不是'IC'时,我想为客户拉出所有记录,无论他们有多少行。

So for instance, CustID 1 would return 1 row while CustID 3 would return 2 rows. 因此,例如,CustID 1将返回1行,而CustID 3将返回2行。 When the CustType is 'IC', I only want to pull a single row for that customer based on the highest CountofOrders. 当CustType为'IC'时,我只想根据最高CountofOrders为该客户拉一行。

So for CustID 2, it should pull the record with OrderType 'DE' and CountofOrders as 10. 因此,对于CustID 2,它应提取OrderType为'DE'且CountofOrders为10的记录。

If the 'IC' customer has the same count for the various OrderTypes, then I want to get the row where the OrderType is 'DE'. 如果“ IC”客户对各种OrderType的计数相同,那么我想获得OrderType为“ DE”的行。 If the OrderType is not 'DE', then I want to get the row with the OrderType as 'DS'. 如果OrderType不是'DE',那么我想获得OrderType为'DS'的行。 In short, priority should be given to OrderType 'DE', followed by 'DS', 'DR' and 'DD' 简而言之,应优先考虑OrderType'DE',然后是'DS','DR'和'DD'

So my final output should look something like this: 所以我的最终输出应如下所示:

-------------------------------------------
|CustID  CustType  OrderType  CountofOrders|
-------------------------------------------|
|  1        C         DD           23      |
|  2       IC         DE           10      |
|  3        C         DR           14      |
|  3        C         DS           19      |
|  4       IC         DS            5      |
|  5       IC         DE            7      |
--------------------------------------------

I was able to use the following query to get the highest for when the CountofOrders is not the same, but I'm having a hard time figuring out how to get a row based on the OrderType when the CountofOrders is the same between the different OrderTypes. 当CountofOrders不相同时,我可以使用以下查询获得最高的价格,但是当CountofOrders在不同OrderTypes之间相同时,我很难解决如何基于OrderType获取行。 。

SELECT 
    a.CustID, a.CustType, a.OrderType, a.CountofOrders
FROM 
    CustInfo a 
WHERE 
    a.CountofOrders = (SELECT MAX(CountofOrders) 
                       FROM CustInfo b 
                       WHERE a.CustID = b.CustID)

Any help would be greatly appreciated. 任何帮助将不胜感激。 Thanks. 谢谢。

You can do this using ROW_NUMBER pretty easily. 您可以使用ROW_NUMBER轻松完成此操作。 This returns the results you stated you wanted from your sample data. 这将返回样本数据中您想要的结果。

DECLARE @Customer table
(
    CustID int
    , CustType varchar(10)
    , OrderType char(2)
    , CountofOrders int
)

INSERT @Customer
(
    CustID
    , CustType
    , OrderType
    , CountofOrders
)
VALUES
(1, 'C', 'DD', 23)
, (2, 'IC', 'DE', 10)
, (2, 'IC', 'DR', 7)
, (2, 'IC', 'DS', 7)
, (3, 'C', 'DR', 14)
, (3, 'C', 'DS', 19)
, (4, 'IC', 'DS', 5)
, (4, 'IC', 'DR', 5)
, (4, 'IC', 'DE', 3)
, (5, 'IC', 'DR', 7)
, (5, 'IC', 'DS', 7)
, (5, 'IC', 'DE', 7)

SELECT x.CustID
    , x.CustType
    , x.OrderType
    , x.CountofOrders
FROM
(
    SELECT *
         , ROW_NUMBER() over(partition by CustID order by CountOfOrders desc
            , case OrderType 
                when 'DE' then 1
                when 'DS' then 2
                when 'DR' then 3
                when 'DD' then 4
                else 5
            end) as RowNum
    FROM @Customer
) x
WHERE x.CustType <> 'IC'
    OR x.RowNum = 1
ORDER BY CustID
    , CountofOrders

I think your requirement cannot be done in query level because the order is based on priority. 我认为您的要求无法在查询级别完成,因为该顺序基于优先级。 I mean you have to use something like stored procedure or do the ordering process in your application 我的意思是您必须使用存储过程之类的东西或在应用程序中执行订购过程

暂无
暂无

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

相关问题 获取每个不同列A具有另一列B的最大值的行 - Get the row of each different column A with highest value for another column B 如何基于另一列的值获取单行值? - How to get single row of values based on value of another column? 如何基于另一个SQL Server表的单列中的更改添加一个SQL Server表的行和时间戳 - How to add a row and timestamp one SQL Server table based on a change in a single column of another SQL Server table Select 行使用 group by 并且在每个组中获取基于另一个列值的最高值的列值 - Select rows using group by and in each group get column values based on highest of another column value SQL Server - 只为字段的每个不同值获取一行 - SQL Server - Only get a single row for each distinct value of a field SQL Server 为每个日期最高的学生获取一行 - SQL Server Get one row for each student with highest date 如何使用另一列获取值并获取 MySQL 或 SQL 中每行之间的差异? - How to get the value using another column and get difference between each row in MySQL or SQL? 如何使用SQL Server在一行中每周为每个用户的每周结束日期获取前2条记录 - How to get top 2 records based on a week end date for each user for each week in a single row using SQL Server 始终获取SQL服务器中每个用户的最高值 - Get always the highest value of each user in SQL Server 如何使用另一列作为乘数而不是行数 MySQL 从表中获取最高出现值 - How to get highest occuring value from table using another column as a multiplier instead of row count MySQL
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM