问题:我需要在表格的某一行显示一张数据中最高的数据的名称,价格和酒精含量。
尝试解决方案:
SELECT MAX(wineSugar_gl) FROM WINE;
其中WINE
是表名,wineSugar_gl
是行名。
尝试解决方案的输出:Attempted solution output
问题:为了显示所需的其他数据,我会在此代码中添加什么内容?
其他信息:以下是我需要显示的行名称。
wineName
winePrice
wineAlchohal
答案 0 :(得分:1)
您可以尝试这样
SELECT wineName, winePrice, wineAlchohal, MAX(wineSugar_gl) FROM WINE group by wineName, winePrice, wineAlchohal
答案 1 :(得分:0)
如果你想得到一些聚合值(比如MAX()),和你希望其他字段来自同一行,一种非常流行和有效的方法是使用row_number()
函数,如下所示:
SELECT wineName, winePrice, wineAlchohal, wineSugar_gl
FROM (
SELECT wineName, winePrice, wineAlchohal, wineSugar_gl,
row_number() over (order by wineSugar_gl desc) as seqnum
FROM WINE
) x
where seqnum = 1
--since we ordered by wineSugar_gl descending, seqnum 1 is equivalent
--to max(wineSugar_gl), and you can also get other fields from that row