简体   繁体   English

如何查询具有第v.2列之一相同值的行中具有最高列值的行

[英]How to query for rows that have highest column value among rows that have same value for one of the columns v.2

Still a journeyman to SQL here. 仍然是SQL的熟练手。 I have a temp table named #everything which has the following data. 我有一个名为#everything的临时表,该表具有以下数据。 (headers abbreviated). (标题缩写)。 The temp table has many more columns than this. 临时表中的列比这多得多。

hh_key sub_id tier
   1    1234   T1
   2    2345   T2
   3    4567   T4
   3    4568   T3

I want to return the rows with the 'best' tier. 我想返回具有“最佳”层的行。 In my case best has the lowest number after the 'T'. 就我而言,最好的数字在“ T”之后最低。 So here are my desired results: 所以这是我想要的结果:

hh_key sub_id tier
   1    1234   T1
   2    2345   T2
   3    4568   T3

I know that this exact issue was asked and resolved here: previous question but the syntax provided in the answer does not work for me. 我知道这个确切的问题已在此处提出并解决: 上一个问题,但答案中提供的语法对我不起作用。 All records are returned, not just the ones with the desired values. 返回所有记录,而不仅仅是具有所需值的记录。

This would be my version of the previous answer and it does not return a subset of values. 这将是我上一个答案的版本,并且不会返回值的子集。 Everything is returned. 一切都返回了。

Select household_key, sm_subscription_id ,mytier
from #everything
where Mytier is not null
and Mytier IN (select MIN(mytier) from #everything group by household_key)
order by household_key

Could the fact that I have more than those 3 columns requested from the table be a factor? 原因是我表中所请求的列多于那三列吗?

You can use ROW_NUMER() : 您可以使用ROW_NUMER()

SUBSTRING(tier, 2, LEN(tier) - 1) will extract the numbers after 'T' . SUBSTRING(tier, 2, LEN(tier) - 1)将提取'T'之后的数字。 You can use the extracted string and CAST it to INT and use it in the ORDER BY clause of your ROW_NUMBER . 您可以使用提取的字符串并将其CASTINT ,然后在ROW_NUMBERORDER BY子句中使用它。

SQL Fiddle SQL小提琴

WITH Cte AS(
    SELECT *,
        rn = ROW_NUMBER() OVER(
            PARTITION BY hh_key
            ORDER BY CAST(SUBSTRING(tier, 2, LEN(tier) - 1) AS INT)
        )
    FROM #everything
)
SELECT
    hh_key, sub_id, tier
FROM Cte
WHERE rn = 1
DROP TABLE #everything

暂无
暂无

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

相关问题 SQL查询仅返回特定列中具有最高值的行 - SQL query to only return the rows that have the highest value in a certain column 如果两个或更多列具有相同的值,如何获取具有最高值的行 - How to get rows with highest value, if two or more Columns have same value 如何编辑此mySQL查询以不选择2列中具有相同值的行? - How to edit this mySQL query to not select rows that have same value in 2 columns? 在与另一列具有相同值的行中选择具有最高列值的行 - Selecting row with the highest column value among rows with the same value of another column 查找具有相同列值而不是另一列值的行 - Find rows that have one column value same but not the other 如何识别mysql中相同列中具有相同值的行? 而且我还必须向这些行添加条件 - How to identify rows that have the same value in same columns in mysql? and also i have to add condition to those rows 在 MySQL 中的列上查找具有相同值的行 - Find rows that have the same value on a column in MySQL 可以从可能具有相同值但被翻转的两行中按最高日期时间分组的MYSQL查询 - MYSQL query that can group by the highest datetime from two rows that might have the same value but flip flopped MySQL选择两列中唯一的行,其中一列中的值最高 - MySQL select unique rows in two columns with the highest value in one column 如何选择不同的行,其中一列可能具有许多相同的值,但所有第二列均具有相同的值? - How do I select distinct rows where a column may have a number of the same values but all their 2nd columns have the same value?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM