当实例在多列上分组或分类时,如何找到平均值?

时间:2019-01-17 18:15:58

标签: c# sql sql-server entity-framework linq

有一个表“ Result”,其中包含以下数据:

ID   Title   Category  SubCategory  Value
1    Part-1  CatX      A            100
2    Part-1  CatX      B            0
3    Part-1  CatX      C            50
4    Part-1  CatY      A            100
5    Part-1  CatY      B            0
6    Part-1  CatY      C            100
7    Part-2  CatM      A            30
8    Part-2  CatM      B            10
9    Part-2  CatM      C            100
10   Part-2  CatN      A            50
11   Part-2  CatN      B            10
12   Part-2  CatN      C            80

这就是我要实现的目标:

Title   SubCategory   AvgValue
Part-1  A          100
Part-1  B          0
Part-1  C          75
Part-2  A          40
Part-2  B          10
Part-2  C          90

每个标题具有3个子类别,我需要显示每个标题的每个子类别的平均值。请帮忙。 答案可以是SQL Server查询或Linq,因为我可以在列表/数据表中获取结果表。

4 个答案:

答案 0 :(得分:4)

使用avg()聚合函数

  select Title,SubCategory ,avg(Value) 
  from table_name 
  group by Title,SubCategory 

答案 1 :(得分:3)

这可以工作

select Title, SubCategory, AVG(Value)
from Table1 
group by Title, subcategory

答案 2 :(得分:2)

这是解决方法:

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

namespace ConsoleApplication97
{
    class Program
    {
        static void Main(string[] args)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("ID", typeof(int));
            dt.Columns.Add("Title", typeof(string));
            dt.Columns.Add("Category", typeof(string));
            dt.Columns.Add("SubCategory", typeof(string));
            dt.Columns.Add("Value", typeof(int));

            dt.Rows.Add(new object[] {1, "Part-1", "CatX", "A", 100});
            dt.Rows.Add(new object[] {2, "Part-1", "CatX", "B", 0});
            dt.Rows.Add(new object[] {3, "Part-1", "CatX", "C", 50});
            dt.Rows.Add(new object[] {4, "Part-1", "CatY", "A", 100});
            dt.Rows.Add(new object[] {5, "Part-1", "CatY", "B", 0});
            dt.Rows.Add(new object[] {6, "Part-1", "CatY", "C", 100});
            dt.Rows.Add(new object[] {7, "Part-2", "CatM", "A", 30});
            dt.Rows.Add(new object[] {8, "Part-2", "CatM", "B", 10});
            dt.Rows.Add(new object[] {9, "Part-2", "CatM", "C", 100});
            dt.Rows.Add(new object[] {10, "Part-2", "CatN", "A", 50});
            dt.Rows.Add(new object[] {11, "Part-2", "CatN", "B", 10});
            dt.Rows.Add(new object[] {12, "Part-2", "CatN", "C", 80});

            var groups = dt.AsEnumerable().GroupBy(x => new { title = x.Field<string>("Title"), subcategory = x.Field<string>("SubCategory") }).ToList();

            var totals = groups.Select(x => new {title = x.Key.title, subCategory = x.Key.subcategory, average = x.Average(y => y.Field<int>("Value"))}).ToList();

        }
    }


}

答案 3 :(得分:2)

您还可以尝试如下所示的另一种方法。

Create Table Result(Id int, Title Varchar(10), Category Varchar(10), SubCategory Varchar(10), Value Int)
Insert Into Result Values(1, 'Part-1','CatX',      'A',            100),
(2    ,'Part-1','CatX',      'B',            0),
(3    ,'Part-1','CatX',      'C',            50),
(4    ,'Part-1','CatY',      'A',            100),
(5    ,'Part-1','CatY',      'B',            0),
(6    ,'Part-1','CatY',      'C',            100),
(7    ,'Part-2','CatM',      'A',            30),
(8    ,'Part-2','CatM',      'B',            10),
(9    ,'Part-2','CatM',      'C',            100),
(10   ,'Part-2','CatN',      'A',            50),
(11   ,'Part-2','CatN',      'B',            10),
(12   ,'Part-2','CatN',      'C',            80)

Select Title, SubCategory, AVG(Value) as Average from Result
Group By Title, SubCategory

Select Title, SubCategory, SUM(Value) / COUNT(*) as Average
From Result Group By Title, SubCategory

两种情况下的输出如下所示。

enter image description here

您可以找到实时演示Live Demo Here