SQL分组后选择多个最大行值

时间:2019-06-19 02:17:07

标签: c# sql linq

我有一个表,其中每个JunctionlistID重复很多次。每行中每个ID的前面都有一个JunctionlistID。 我想为ID最新的每个JunctionlistID选择整行。此表中共有5列,我希望在选择该行时选择所有列。

    ID | MonitoringString| JunctionListId | area_id| CompanyProfileId
    1  | 1006410001D0    | 267            | 910064 | 7
    2  | 1206420001D0    | 268            | 910065 | 7
    3  | 1306440001D0    | 267            | 910064 | 7
    4  | 1506450001D0    | 268            | 910065 | 7
    5  | 1606470001D0    | 267            | 910064 | 7
    6  | 1806480001D0    | 268            | 910065 | 7
    7  | 1006420001D0    | 267            | 910064 | 7
    8  | 1006470001D0    | 268            | 910065 | 7
    9  | 1006490001D0    | 267            | 910064 | 7
   10  | 1006430001D0    | 268            | 910065 | 7
   11  | 1006460001D0    | 285            | 910066 | 8
   12  | 1006438001D0    | 268            | 910067 | 8

答案应该是

    ID | MonitoringString| JunctionListId | area_id| CompanyProfileId       
    9  | 1006490001D0    | 267            | 910064 | 7
   10  | 1006430001D0    | 268            | 910065 | 7

我尝试如下查询-

    Select ID,MonitoringString,JunctionListId,area_id,CompanyProfileId from tblMonitoring where CompanyProfileId=7

我在linq和SQL中也需要相同的查询,如果有人知道,请给我适当的解决方案。

谢谢

5 个答案:

答案 0 :(得分:0)

如果我的理解正确,您只需要基于u128b mul_mod(u128b a, u128b b, u128b m) { if (m == 0) return a * b; u128b r = {0, 0}; while(a > 0) { if (a & 1) if ((r += b) > m) r %= m; a >>= 1; if ((b <<= 1) > m) b %= m; } return r; } //https://stackoverflow.com/questions/20111827/various-questions-about-rsa-encryption/20114154#20114154 u128b pow_mod(u128b base, u128b expon, u128b mod) { u128b remainder = {0, 1}; while (expon > 0) { if (expon & 1) remainder = mul_mod(r, base, mod); base = mul_mod(base, base, mod); expon >>= 1; } return remainder; } 的每个公司和联结ID的最新记录。您可以使用id

row_number()

答案 1 :(得分:0)

选择MAX(ID)ID, Max(MonitoringString)MonitoringString, Max(JunctionListID)JunctionListID, 最大值(area_id)area_id, 最大值(CompanyProfileId)CompanyProfileId 来自MonitorsList 其中CompanyProfileId = 7 JunctionListID

分组

答案 2 :(得分:0)

https://dotnetfiddle.net/oiRkzO

using System;
using System.Data.Entity;
using System.Linq;
using System.Collections.Generic;


public class Program
{
    public static void Main()
    {

            List<Item> items = new List<Item>()
            {
                new Item() { ID = 1, MonitoringString = "1006410001D0", JunctionListId = 267, area_id = 910064 , CompanyProfileId = 7},
                new Item() { ID = 2, MonitoringString = "1206420001D0", JunctionListId = 268, area_id = 910065 , CompanyProfileId = 7},
                new Item() { ID = 3, MonitoringString = "1306440001D0", JunctionListId = 267, area_id = 910064 , CompanyProfileId = 7},
                new Item() { ID = 4, MonitoringString = "1506450001D0", JunctionListId = 268, area_id = 910065 , CompanyProfileId = 7},
                new Item() { ID = 5, MonitoringString = "1606470001D0", JunctionListId = 267, area_id = 910064 , CompanyProfileId = 7},
                new Item() { ID = 6, MonitoringString = "1806480001D0", JunctionListId = 268, area_id = 910065 , CompanyProfileId = 7},
                new Item() { ID = 7, MonitoringString = "1006420001D0", JunctionListId = 267, area_id = 910064 , CompanyProfileId = 7},
                new Item() { ID = 8, MonitoringString = "1006470001D0", JunctionListId = 268, area_id = 910065 , CompanyProfileId = 7},
                new Item() { ID = 9, MonitoringString = "1006490001D0", JunctionListId = 267, area_id = 910064 , CompanyProfileId = 7},
                new Item() { ID = 10, MonitoringString = "1006430001D0", JunctionListId = 268, area_id = 910065 , CompanyProfileId = 7},
                new Item() { ID = 11, MonitoringString = "1006460001D0", JunctionListId = 285, area_id = 910066 , CompanyProfileId = 8},
                new Item() { ID = 12, MonitoringString = "1006438001D0", JunctionListId = 268, area_id = 910067 , CompanyProfileId = 8},

            };

        var result = items.GroupBy(item => item.JunctionListId).Select(g => g.FirstOrDefault(gx => gx.ID == g.Max(x => x.ID))).ToList();
        var resultCmp7 = items.Where(item => item.CompanyProfileId == 7).GroupBy(item => item.JunctionListId).Select(g => g.FirstOrDefault(gx => gx.ID == g.Max(x => x.ID))).ToList();

        foreach (var item in result)
        {
            Console.WriteLine(string.Format("{0},{1},{2},{3}",item.ID, item.MonitoringString, item.JunctionListId,item.area_id, item.CompanyProfileId));
        }

        Console.WriteLine();   

        foreach (var item in resultCmp7)
        {
            Console.WriteLine(string.Format("{0},{1},{2},{3}",item.ID, item.MonitoringString, item.JunctionListId,item.area_id, item.CompanyProfileId));
        }

        Console.ReadLine();
    }

    class Item
    {
        public int ID { get; set; }
        public string MonitoringString { get; set; }
        public int JunctionListId { get; set; }
        public int area_id { get; set; }
        public int CompanyProfileId { get; set; }
    }
}

答案 3 :(得分:0)

尝试一下,

从tblMonitoring WHERE CompanyProfileID ='7'GROUP BY CompanyProfileId;中选择COUNT(ID),MonitoringString,JunctionListId,are_id,CompanyProfileId;

答案 4 :(得分:0)

对于linq,您可以执行以下操作。 (看起来您也想按companyprofileid进行过滤)

var result = items.Where(x=>x.CompanyProfileId==7)
.GroupBy(x=>x.JunctionListId)
.Select(x=>x.ToList()
            .OrderByDescending(c=>c.ID)
            .ThenBy(c=>c.JunctionListId)
            .First());