我正在尝试获取两列frequency
和frequency - min(frequency)
但我看到的第二列为零。什么可能是错的?
SELECT
frequency, frequency - min(frequency)
FROM
words
GROUP BY id
ORDER BY frequency;
答案 0 :(得分:8)
您的查询组按频率的唯一值。在每个这样的组中,最小频率只是频率本身,因此在减去两者时总是得到0
。相反,您可以使用min
的窗口版本:
SELECT frequency, frequency - MIN(frequency) OVER() AS diff
FROM words
ORDER BY frequency
答案 1 :(得分:2)
添加一个返回最小频率值的子查询:
SELECT
frequency, frequency - (select min(frequency) from words)
FROM
words
ORDER BY frequency;
修改强>
将其包装在派生表中:
SELECT frequency, frequency - minfreq, frequency + minfreq
FROM words
CROSS JOIN (select min(frequency) minfreq from words) dt
ORDER BY frequency
答案 2 :(得分:1)
试一试:
SELECT frequency, frequency-min_frequency
FROM (
SELECT frequency, MIN(frequency) AS min_frequency
FROM words
GROUP BY frequency
) as A
ORDER BY frequency;
答案 3 :(得分:0)
这肯定是一个奇怪的查询。您按ID分组,因此每个ID可以获得一个结果记录。但ID表明这是唯一标识记录的表格ID。因此GROUP BY id
不会改变任何内容,您仍会获得结果中的所有记录。除了一个例外:min(frequency)
现在表示每组的最低频率。由于“组”是一个记录,最小值当然是值本身。非聚合frequency
也是由ID唯一标识的记录频率。因此,您的查询可以重写为:
SELECT
frequency, frequency - frequency
FROM words
ORDER BY frequency;
我想您想要将每个记录的频率与表中的最低频率进行比较?您将在子查询中获得此值:
SELECT
frequency, frequency - (select min(frequency) from words)
FROM words
ORDER BY frequency;
或者:
SELECT
w.frequency, w.frequency - m.min_frequncy
FROM words w
CROSS JOIN (select min(frequency) as min_frequncy from words) m
ORDER BY frequency;