2

I have a table Item and I want to get the minimum price of the Item for the particular id

Table Item:

Id    Price1                Price2           Price3
1       10                    20                30
2       20                    30                40

According to the above example, the minimum price for id-1 is 10 and for id-2 is 20. I simply just want to get the minimum value from the three column for particular id.

Remember: I can't create the cases as, any column can be null. Thanks in advance.

2
  • Why can't you use case? Just put corresponding conditions like Price1 is not null; Price2 is not null etc Commented Sep 9, 2013 at 6:44
  • 1
    It's generally a sign of a poor database design if multiple columns are storing the same "kind" of data, such that it makes sense to compare their values. Commented Sep 9, 2013 at 6:44

5 Answers 5

2

One approach could be like this:

SELECT Id, MIN(Price) FROM (
   SELECT Id, Price1 As Price FROM Table1
   UNION ALL
   SELECT Id, Price2 As Price FROM Table1
   UNION ALL
   SELECT Id, Price3 As Price FROM Table1
) As AllValues
GROUP BY Id

This works even if there are null values. Here is the working demo.

Sign up to request clarification or add additional context in comments.

7 Comments

I am using sql server 2008, is this approach works fine even in sql server 2005?
@garvitgupta yes it should work without any issues in sql server 2005 too.
When I run this query, it throws error of Invalid column name id, really strange, don't know why. I am typing the coulmn name exactly correct.
Here you give the link of working demo, I copied the "Create" statement query and try to build a schema on my local, and again it throw an error of "Incorrect syntax near 'auto_increment", it looks really strange.
@garvitgupta I have updated that link of working demo, please try again. Update link is: sqlfiddle.com/#!3/ce957/1
|
1

Two similar solutions, using APPLY operator:

SELECT t.Id,
       MIN(m.Price)
FROM   
    tableX AS t
  CROSS APPLY
    ( SELECT Price = Price1 UNION
      SELECT Price2 UNION
      SELECT Price3 
    ) AS m
GROUP BY t.Id ;



SELECT t.Id,
       x.Price
FROM   
    tableX AS t
  OUTER APPLY
    ( SELECT TOP (1) Price
      FROM 
       ( SELECT Price1 UNION
         SELECT Price2 UNION  
         SELECT Price3
       ) AS m (Price)
       WHERE Price IS NOT NULL
       ORDER BY Price ASC
    ) x ;

Tested at SQL-Fiddle

Comments

0

Please try:

Select Id,
       Case When Price1 < Price2 And Price1 < Price3 Then Price1
            When Price2 < Price1 And Price2 < Price3 Then Price2 
            Else Price3
            End As TheMin
From   
    YourTable

OR

select 
    Id, 
    MIN(Price) TheMin
FROM
(
    select Id, Price1 Price From YourTable
    union all
    select Id, Price2 Price From YourTable
    union all
    select Id, Price3 Price From YourTable
)x group by Id

2 Comments

I already mentioned that I can't use cases, as any of the three columns can be null, so to handle null we again have to make many cases. Thats why I am ignoring this logic
What should be the result if any of the column value is NULL?
0

Take maximum value for NULL:

Select Id,
       Case When ISNULL(Price1,MaxPrice) < ISNULL(Price2,MaxPrice)  And ISNULL(Price1,MaxPrice)  < ISNULL(Price3,MaxPrice)  Then ISNULL(Price1,MaxPrice)
            When ISNULL(Price2,MaxPrice)  < ISNULL(Price1,MaxPrice)  And ISNULL(Price2,MaxPrice)  < ISNULL(Price3,MaxPrice)  Then ISNULL(Price2,MaxPrice)  
            Else ISNULL(Price3,MaxPrice) 
            End As TheMin
From   
    YourTable

When MaxPrice could be calculated or just max int...

Comments

0

take a look at this post. My though is that you could invert the table column & row and simply use min() to get the minimum.

Select MYSQL rows but rows into columns and column into rows

(All credit to Anax in the linked post)

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.