1

I have table which contains three columns Work, Cost, Duration. I need to get the maximum occurred values in all three columns. If two values occurred same times, then return the maximum value from that two. Please see the sample data & result below.

Work    Cost      Duration
 5       2        6
 5       8        7
 6       8        7
 2       2        2
 6       2        6

I need to get the result as

Work    Cost    Duration
 6       2         7

I tried with the following query, But it is returning the value for one column, that too it is returning the count for all the values

select Duration, count(*) as "DurationCount" from SimulationResult
  group by Duration
  order by count(*) desc,Duration desc
4

2 回答 2

1

你可以做类似的事情

select * from
(select top 1 Work from SimulationResult
  group by Work
  order by count(*) desc, Work desc),

(select top 1 Cost from SimulationResult
  group by Cost 
  order by count(*) desc, Cost desc),

(select top 1 Duration from SimulationResult
  group by Duration
  order by count(*) desc, Duration desc)
于 2013-07-25T09:25:16.980 回答
0

尝试以下操作:

select max(t1.a), max(t2.b), max(t3.c)
from
(select a from (
select a, count(a) counta
from #tab
group by a) tempa
having counta = max(counta)) t1,
(select b from (
select b, count(b) countb
from #tab
group by b) tempb
having countb = max(countb)) t2,
(select c from (
select c, count(c) countc
from #tab
group by c) tempc
having countc = max(countc)) t3
于 2013-07-25T10:09:38.913 回答