62,072
社区成员
发帖
与我相关
我的任务
分享
select * from
(
select row_number() over(partition by country order by [sum] desc) as rowid,* from list
) as tmp
where rowid<2
SELECT [t1].[country], [t3].[productname] AS [pname], [t3].[sum] AS [total]
FROM (
SELECT [t0].[country]
FROM [List] AS [t0]
GROUP BY [t0].[country]
) AS [t1]
CROSS APPLY (
SELECT TOP (1) [t2].[productname], [t2].[sum]
FROM [List] AS [t2]
WHERE (([t1].[country] IS NULL) AND ([t2].[country] IS NULL)) OR (([t1].[country] IS NOT NULL) AND ([t2].[country] IS NOT NULL) AND ([t1].[country] = [t2].[country]))
ORDER BY [t2].[sum] DESC
) AS [t3]
ORDER BY [t3].[sum] DESC
from a in Lists
group a by a.Country into cqqq
from max in cqqq.OrderByDescending(x => x.Sum).Take(1)
select new
{
country = cqqq.Key,
pname = max.Productname,
total = max.Sum
}
var finResult=from a in groupResultQuery
group a by a.country into cqqq
let max=cqqq.OrderByDescending(x=>x.sum).First()
select new
{
country = cqqq.Key,
pname=max.productname,
total=max.sum
};
foreach (var f in finResult)
{
Write(f.country + "," + f.pname + "," + f.total);
}
List<Country> list = new List<Country>();
list.Add(new Country() { country = "USA", productname = "ipad", sum = 100000 });
list.Add(new Country() { country = "USA", productname = "mac air", sum = 200000 });
list.Add(new Country() { country = "CHINA", productname = "dell", sum = 100000 });
(from l in list group l by l.country into g select new { country = g.Key, productname = g.Max(x => x.productname), sum = g.Max(x => x.sum) }).ToList().ForEach(x => Console.WriteLine(x.country + "==" + x.productname + "==" + x.sum));