10

I want select countries with maximum value of 'Value' for a 'grpid'. Also already selected 'Country' should not be considered for other 'grpid' while checking the maximum. ( ie Country or grpid should not be repeated in the result )

SQL Fiddle

Result:

Country    grpid        Value           Row_number

US        49707        604456458         1
GB        5086         497654945         4 
CA        909          353500201         10
JP        231          198291290         15
3
  • social.msdn.microsoft.com/Forums/en-US/… Commented Apr 1, 2014 at 12:21
  • in result Country or grpid should not be repeated... in your result 'GB' and 'US' is repeating. 1) select countries with maximum value of 'Value' for a 'grpid' 2) 'grpid' & country should not be repeated. 3) we need to take next maximum value if 'grpid' not there a for highest value. Commented Apr 1, 2014 at 12:27
  • Why didn't you include grpid '50147' Commented Apr 1, 2014 at 12:35

3 Answers 3

7

try this query instead,

  WITH OrderedOrders AS
  (
     SELECT country,grpid,value,ROW_NUMBER() OVER(PARTITION BY country ORDER BY   country,value DESC) AS 'RowNumber' 
     FROM test1
  ) 
 select * from  OrderedOrders
 where RowNumber =1
Sign up to request clarification or add additional context in comments.

1 Comment

Worked perfectly for me.
6

I believe this is what you're looking for:

SQL Fiddle

;with cte as 
(
  select 
      country,
      max(value) as MaxVal,
      min(row_number) as MinRow
  from test1
  group by Country
)
select 
  c.country,
  t.grpid,
  c.MaxVal,
  c.MinRow
from cte c
join test1 t
  on t.country = c.country 
  and t.value = c.MaxVal
  and t.row_number = c.MinRow
order by country, grpid

1 Comment

simple self-join by showing how to use with keyword is useful and link renewed. +1
0

Can you please try out this query

select 
    country,
    value,
    grpid,
    count(*) 
from test1 
group by  
    country,
    value,
    grpid 
order by 
    country,
    value desc

3 Comments

thanks, this wont work output Result: Country grpid Value Row_number US 49707 604456458 1 GB 5086 497654945 4 CA 909 353500201 10 JP 231 198291290 15 in result Country or grpid should not be repeated... in your result 'GB' and 'US' is repeating. 1) select countries with maximum value of 'Value' for a 'grpid' 2) 'grpid' & country should not be repeated. 3) we need to take next maximum value if 'grpid' not there a for highest value.
in result Country or grpid should not be repeated... in your result 'GB' and 'US' is repeating. 1) select countries with maximum value of 'Value' for a 'grpid' 2) 'grpid' & country should not be repeated. 3) we need to take next maximum value if 'grpid' not there a for highest value.
can you pls remove the country from the group by and select list and try?

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.