0

I Don't Know Why My Previous Topic Was Marked!!!???

This Is What I've Done So Far. I Can't Think Of Anything Else.

enter image description here

So Many Things I've Done To Reach To This Point Which Is Illustrated In My Problem Section. Which For Simplification I Made Name A,B,C,... .I'm Not Giving You My Homework.

My Problem:

enter image description here

2
  • 1
    Oops, I cant copy the Query Text! :( Feb 8, 2014 at 12:44
  • That Query Is Just For People Who Say This Is My HomeWork. Forget It. My Problem Starts From "My Problem" Section! Feb 8, 2014 at 12:47

2 Answers 2

1

Here is a SQL for SqlServer 2008 to solve your problem.

with data as (select name, count(*) as occurrence, sum(value) as sumvalue from mytab group by name)
select * from data where
  occurrence=(select max(occurrence) from data)
  and sumvalue=(select max(sumvalue) from data data2 where data2.occurrence=data.occurrence)

In data the needed values are collected (sum and count). Now we select the rows with max occurrence and filtering out the rows with the max value.

http://sqlfiddle.com/#!3/56b00/4

1
  • Clever Answer - God Help You - Thank You Very Very Much Feb 8, 2014 at 13:13
0
SELECT [Name], [Value]
FROM (
    SELECT *, RANK() OVER(ORDER BY [Count] DESC, Value DESC) [rn]
    FROM (
        SELECT [Name], SUM(Value) [Value], COUNT(1) [Count]
        FROM MyTable
        GROUP BY Name
    ) t
) t
WHERE rn = 1

SQL Fiddle

or more simply

SELECT TOP 1 WITH TIES [Name], [Value]
FROM (
    SELECT [Name], SUM(Value) [Value], COUNT(1) [Count]
    FROM MyTable
    GROUP BY Name
) t
ORDER BY [Count] DESC, Value DESC

SQL Fiddle

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

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

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