-1

Say a column is:

Price  
$3.00  
$3.00

The min and max would be equal. Is there a way to check for that in the WHERE clause?

halfer
  • 19,824
  • 17
  • 99
  • 186
Ricky Su
  • 295
  • 1
  • 7
  • 10
  • 1
    select case when max(price) = min(price) then 'dosomething' else 'dosomething' end as 'something' – Marco Bong Feb 24 '16 at 00:57
  • 1
    Ricky, how are you getting on with the below answers? They look helpful. It is customary here to vote on, accept, or reply to answers shortly after they come in. – halfer Feb 26 '16 at 12:10
  • With regret, downvoted (see above). – halfer Feb 29 '16 at 13:03

3 Answers3

1

There is a hundred years old HAVING for filtering by aggregates.

SELECT 1
FROM   YOUR_TABLE
HAVING MAX(PRICE) = MIN(PRICE)
Paul
  • 1,085
  • 12
  • 20
0
WITH t(price) AS ( VALUES
  (3.00),
  (3.00)
)
SELECT
  CASE
    WHEN max(price) = min(price) THEN TRUE
    ELSE FALSE
  END
FROM t;
Dmitry S
  • 4,990
  • 2
  • 24
  • 32
-1

This is a way of doing that in MS SQL, I guess that should help you for Postgres somehow:

IF EXISTS (SELECT NULL FROM Table HAVING MAX(Price) = MIN(Price))
    PRINT 'Equal'
ELSE
    PRINT 'Not equal'

Another way inspired in Dmitry's answer:

IF ((SELECT CASE WHEN MAX(Price) = MIN(Price) THEN 1 END FROM Table) = 1)
    PRINT 'Equal'
ELSE
    PRINT 'Not equal'
Andrew
  • 7,602
  • 2
  • 34
  • 42