简体   繁体   中英

SQL select multiple max rows value after group by

I have a table where each JunctionlistID is repeated many times. there is a JunctionlistID in front of each id in each row. I want to select entire row for each JunctionlistID where ID is latest. There are total 5 columns in this table i want all columns to get selected when i select that row.

    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

The Answer should be

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

I try the query as below -

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

I need the query same as well in linq and SQL both, If anybody know please give me the proper solution.

Thanks

If I understand correctly, you just want the last records for each company and junction id, based on the id . You can use row_number() :

Select m.*
from (select m.*, 
             row_number() over (partition by CompanyProfileId, JunctionListId order by id desc) as seqnum
      from tblMonitoring m
     ) m
where CompanyProfileId = 7 and seqnum = 1;

从MonitorsList 选择MAX(ID)ID,Max(MonitoringString)MonitoringString,Max(JunctionListID)JunctionListID,Max(area_id)area_id,Max(CompanyProfileId)CompanyProfileId, 其中CompanyProfileId = 7 Group by JunctionListID

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; }
    }
}

Try this one,

SELECT COUNT(ID), MonitoringString, JunctionListId, are_id, CompanyProfileId FROM tblMonitoring WHERE CompanyProfileID = '7' GROUP BY CompanyProfileId;

For linq, you could do the following. (Looks like you also want to filter by 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());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM