1
select * 
from monthStatistics ms 
    where ms.beloneMonth = 
         (
            select max(ms2.beloneMonth) 
            from monthStatistics ms2 
            where ms.materialDictionaryId = ms2.materialDictionaryId 
            and ms.locationId = ms2.locationId 
            and ms.price=ms2.price
          )

样本数据

id  beloneMonth       materialDictinaryId    price  acount
1   2013/7            1                      100    200
2   2013/7            2                      100    200
3   2013/8            1                      100    200
4   2013/8            1                      200    200

结果:

id  beloneMonth       materialDictinaryId    price  acount
2   2013/7            2                      100    200
3   2013/8            1                      100    200
4   2013/8            1                      200    200

分组并获得最大月份行。

4

1 回答 1

1

以下将返回monthStatistics其中具有max值的行beloneMonth

monthStatistics
    .GroupBy(x=>x.id)
    .SelectMany
    (
        x=>
        x.Where
        (
            z=>
            z.beloneMonth == x.Max(y=>y.beloneMonth)
        )
    );
于 2013-10-11T07:28:01.787 回答