簡體   English   中英

SQL在分組后選擇多個最大行值

[英]SQL select multiple max rows value after group by

我有一個表,其中每個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中同樣的查詢,如果有人知道請給我正確的解決方案。

謝謝

如果我理解正確的話,你只是想為每個公司和結ID中的最后記錄的基礎上, id 你可以使用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; }
    }
}

試試這個,

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

對於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());

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM