我的记录如下
Code Name Customer Price Measure Type Retail Price Measure Type
D035 ALU 8.00 F 8.00 F
D035 ALU 9.00 F NULL NULL
我希望从以上记录中选择最高价格,我该怎么做?预期记录
Code Name Customer Price Measure Type Retail Price Measure Type
D035 ALU 9.00 F 8.00 F
我尝试按度量类型选择没有组的最高价格,并按度量类型将所选结果加入,但似乎不是预期的。
答案 0 :(得分:1)
ANSI SQL方法是order by
和fetch first 1 row only
:
select t.*
from t
order by t.retail_price desc nulls last
fetch first 1 row only;
并非所有数据库都支持这些构造,但您通常可以执行类似的操作。
答案 1 :(得分:1)
我想说。您的零售价格是十进制值然后是NULL。在十进制或数字数据类型中无法插入NULL。所以我假设你有一个varchar数据类型。
此外,您还使用了两个具有相同名称的列[测量类型]。为了更好的理解,我改变了名字。
Select *
From [YourTableName]
GROUP By [Code]
,[Name],[Customer Price], [Measure Type], [Retail Price], [Measrue Type]
HAVING MAX([Customer Price])=(Select TOP 1 MAX([Customer Price]) From [YourTableName])
因为你没有任何主键。所以,我们不能加入JOIN。因此,这是您的问题的解决方案。