简体   繁体   English

对于SQL一列中的相同ID,如何为另一列中值较大的行编写Case语句?

[英]For the same ID in one column of SQL, how do I write a Case statement for the row with the greater value in the another column?

In SQL, for the same set of ID, I want to create the column Action with New where the Contract Number is the largest and Old for the smaller values.在SQL中,对于同一组ID,我想创建NewAction列,其中Contract Number是最大的, Old是较小的值。

For example, for ID = 123 , the largest value in Contract Number is 10, so that row will be marked as New and the others as Old .例如,对于ID = 123Contract Number中的最大值为 10,因此该行将标记为New ,其他行标记为Old The only columns in the SQL database are ID and Contract Number , so I'm assuming I should use a CASE WHEN statement to create the Action column, but not sure how to formulate it SQL 数据库中唯一的列是IDContract Number ,所以我假设我应该使用CASE WHEN语句来创建Action列,但不确定如何制定它

Sample table:样品表:

ID ID Contract Number合同号码
123 123 10 10
123 123 5 5个
123 123 3 3个
456 456 3 3个
456 456 2 2个

Expected output:预计 output:

ID ID Contract Number合同号码 Action行动
123 123 10 10 Keep保持
123 123 5 5个 Old老的
123 123 3 3个 Old老的
456 456 3 3个 Keep保持
456 456 2 2个 Old老的

You could use a case expression together with a windowed aggregate:您可以将 case 表达式与窗口聚合一起使用:

select *, 
  case when 
    contractnumber = Max(ContractNumber) over(partition by Id) then 'Keep'
  else 'Old' end Action
from sampledata;

I like using Common Table Expressions (CTEs) for this我喜欢为此使用公用表表达式 (CTE)

  --get the max value 
  ;with CN as (select max(ContractNumber) as MaxContractNumber, ID
  FROM [SampleDb].[dbo].[SampleDataforSO]
  group by ID,ContractNumber)
  --loop through and assign the relevant action item for the max value
  select s.ID, s.ContractNumber,
  case
  when ContractNumber < max(MaxContractNumber) then 'Old'
  when ContractNumber = max(MaxContractNumber) then 'Keep'
  end as 'Action'
  from CN
  join [SampleDb].[dbo].[SampleDataforSO] s
  on s.ID = cn.ID
  group by s.id, ContractNumber

output: output: 输出

暂无
暂无

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

相关问题 当另一行具有相同的 id 但列中的值不同时,如何在 SQL 中选择一行? - How do I select one row in SQL when another row has same id but a different value in a column? 对于同一列中的相同 ID,如何为未来日期编写 CASE 语句? - For the same ID in one column, how do I write a CASE statement for the future dates? 如何将一列的值复制到同一行的另一列? - How do I copy the value of one column into another in the same row? 需要一个 SQL 选择语句来返回在一个列中具有相同 id 而在另一列中具有不同值的行 - Need a SQL select statement to return rows that have the same id in one column and distinct value in another column 当一列中的多个值分配给另一列中的单个值时,如何编写一个复制行的程序? - How do I write a program that duplicates a row when multiple values in one column are assigned to a single value in another? 如何编写SQL语句从同一表中的另一列更新表中的列? - How can I write a SQL statement to update a column in a table from another column in the same table? 检索具有相同 ID 的记录,其中一个值在列中的出现次数大于另一个值的出现次数 - Retrieving records with the same ID where the number of occurrences of one value in a column is greater than occurrences of another value 如何在T / SQL中按一列分组并检索具有另一列最小值的行? - How do you group by one column and retrieve a row with the minimum value of another column in T/SQL? 如果2行具有相同的ID,则选择其他列值较大的一个 - If 2 rows have the same ID select one with the greater other column value 简单的SQL查询,如何检查列值是否以与另一列值相同的5个字母开头 - Simple SQL query, how do I check if a column value starts with the same 5 letters as another column value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM