2

I am trying to get two columns, frequency and frequency - min(frequency) but all I see is zero for the second column. What could possibly be wrong?

SELECT
    frequency, frequency - min(frequency)
    FROM
    words
GROUP BY id
ORDER BY frequency;

enter image description here

4
  • 2
    Please show some sample data. The data you show seems fine: 1-1 = 0, 5-5 = 0. I'm sure that's not what you mean... Commented Dec 16, 2015 at 9:14
  • 2
    Are you using mysql or postgresql? Those are two very different databases. Commented Dec 16, 2015 at 9:14
  • 1
    I need minimum frequency of entire table. I guess it is considering min frequency of just that row Commented Dec 16, 2015 at 9:15
  • 1
    @Mureinik ... edited the tags.. using postgresql Commented Dec 16, 2015 at 9:16

4 Answers 4

8

Your query groups by the unique value of frequency. In each such group, the minimal frequency is just the frequency itself, so you always get 0 when subtracting the two. Instead, you could use the windowing version of min:

SELECT   frequency, frequency - MIN(frequency) OVER() AS diff
FROM     words
ORDER BY frequency
Sign up to request clarification or add additional context in comments.

Comments

2

Add a sub-query that returns the min frequency value:

SELECT
    frequency, frequency - (select min(frequency) from words)
    FROM
    words
ORDER BY frequency;

Edit:

Wrap it up in a derived table:

SELECT frequency, frequency - minfreq, frequency + minfreq
FROM words
    CROSS JOIN (select min(frequency) minfreq from words) dt
ORDER BY frequency

4 Comments

Ah... can we name the (select min(frequency) from words) and use it over and over again instead of writing the sub query?
SELECT frequency, frequency - minfreq, frequency + minfreq FROM words where minfreq = (select min(frequency) from words) ORDER BY frequency; Is this even possible?
SELECT frequency, frequency - minfreq, frequency + minfreq FROM words, (select min(frequency) minfreq from words) t ORDER BY frequency
Added alternative, like splash58 suggested.
1

Try it:

SELECT frequency, frequency-min_frequency
FROM ( 
SELECT frequency,  MIN(frequency) AS min_frequency 
FROM words
GROUP BY frequency
) as A 
ORDER BY frequency;

Comments

0

This is certainly a queer query. You group by ID, so you get one result record per ID. But ID suggests that this is the table's ID identifying records uniquely. So GROUP BY id doesn't change anything, you still get all records in your results. With one exception: min(frequency) now means the minimum frequency per group. As the "group" is one record, the minimum value is the the value itself of course. The non-aggregated frequency is also the records' frequency uniquely identified by ID. So your query can be re-written as:

SELECT
  frequency, frequency - frequency
FROM words
ORDER BY frequency;

I suppose you want to compare each record's frequency with the minimum frequency found in the table? You'd get this value in a subquery:

SELECT
  frequency, frequency - (select min(frequency) from words)
FROM words
ORDER BY frequency;

Or:

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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.