1

I want to add an extra column, where the max values of each group (ID) will appear.
Here how the table looks like:

select ID, VALUE from mytable

ID VALUE
1 4
1 1
1 7
2 2
2 5
3 7
3 3

Here is the result I want to get:

ID VALUE max_values
1 4 7
1 1 7
1 7 7
2 2 5
2 5 5
3 7 7
3 3 7

Thank you for your help in advance!

1
  • 1
    What DBMS are you using?
    – GarethD
    Oct 7, 2014 at 6:59

4 Answers 4

7

Your previous questions indicate that you are using SQL Server, in which case you can use window functions:

SELECT  ID, 
        Value,
        MaxValue = MAX(Value) OVER(PARTITION BY ID)
FROM    mytable;

Based on your comment on another answer about first summing value, you may need to use a subquery to actually get this:

SELECT  ID, 
        Date,
        Value,
        MaxValue = MAX(Value) OVER(PARTITION BY ID)
FROM    (   SELECT  ID, Date, Value = SUM(Value) 
            FROM    mytable
            GROUP BY ID, Date
        ) AS t;
2
  • Thank you, you'vebeen more than helpful
    – GGSandro
    Oct 7, 2014 at 7:17
  • 1
    @user3531117 Better than say thank you in comment, mark it as accepted by clicking check sign under the number. Oct 7, 2014 at 7:31
2

There is no need to use GROUP BY in subselect.

select ID, VALUE, 
  (select MAX(VALUE) from mytable where ID = t.ID) as MaxValue
from mytable t
3
  • Don't you have the same value for all rows then?
    – Philipp M
    Oct 7, 2014 at 6:58
  • select ID, Date, sum (Value) from mytable Date is month period and I want query to sum the values for each month. Then add a column that will reflect the max value for each ID. The problem is I don't want the query to execute the sum function twice that slows the process greatly
    – GGSandro
    Oct 7, 2014 at 7:11
  • 1
    Don't add the comment to all answers, just edit your question please.
    – Philipp M
    Oct 7, 2014 at 7:12
0

Use this query.

SELECT ID
    ,value
    ,(
        SELECT MAX(VALUE)
        FROM GetMaxValue gmv
        WHERE gmv.ID = gmv1.ID
        GROUP BY ID
        ) as max_value
FROM GetMaxValue gmv1
ORDER BY ID
3
  • In my case I use a heavy sum function, and executing another table my not be that suitable. I wondered if there's a more easier way..
    – GGSandro
    Oct 7, 2014 at 6:57
  • 1
    put your complete question, what are you looking for?
    – Mukund
    Oct 7, 2014 at 7:01
  • select ID, Date, sum (Value) from mytable Date is month period and I want query to sum the values for each month. Then add a column that will reflect the max value for each ID. The problem is I don't want the query to execute the sum function twice that slows the process greatly
    – GGSandro
    Oct 7, 2014 at 7:06
0

Try it with a sub select and group by, then grab the MAX of this group:

select 
  ID, 
  VALUE, 
  (select MAX(VALUE) 
   from mytable 
   group by ID 
   having ID = t.ID
  ) as max_values
from mytable t

Edit:

I built a SQL fiddle, which shows that my solution works, but also VDohnal is correct and doesn't need the group by, so I'll upvote his answer.

1
  • The subselect should be enclosed in parenthesis. Oct 7, 2014 at 6:52

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.