Skip to content
Advertisement

How to query SQL by month & id to get SUM

I have the table data like:

enter image description here

And I want to display asp.net table like: enter image description here

Here is the code to get data by group month and shop code

var gridData = data
                    .GroupBy(
                        x => new {
                            Convert.ToDateTime(x.InspectedAt).Month,
                            Convert.ToDateTime(x.InspectedAt).Year,
                            x.ShopCode
                        }
                    )
                    .Select(x => new InspectionSearchData
                    {
                        month = x.Key.Month,
                        shopCode = x.Key.ShopCode,
                        total = (int)x.Sum(a => a.PerformCount)
                    })
                    .OrderBy(x => x.month)
                    .ToList();

But i don’t know exactly how to display data like image 2 in asp.net html table.
Can anyone advise on how to do that in asp.net?
Thanks in advance.

Advertisement

Answer

Create a pivot table :

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("shopcode", typeof(string));
            dt.Columns.Add("month", typeof(int));
            dt.Columns.Add("count", typeof(int));

            dt.Rows.Add(new object[] { "007", 4, 1 });
            dt.Rows.Add(new object[] { "007", 6, 2 });
            dt.Rows.Add(new object[] { "009", 7, 1 });
            dt.Rows.Add(new object[] { "010", 8, 1 });
            dt.Rows.Add(new object[] { "010", 5, 1 });
            dt.Rows.Add(new object[] { "011", 9, 5 });

            int minMonth = dt.AsEnumerable().Min(x => x.Field<int>("month"));
            int maxMonth = dt.AsEnumerable().Max(x => x.Field<int>("month"));

            DataTable pivotTable = new DataTable();
            pivotTable.Columns.Add("shopcode", typeof(string));
            for (int i = minMonth; i <= maxMonth; i++)
            {
                pivotTable.Columns.Add(i.ToString(), typeof(string));
            }

            var groups = dt.AsEnumerable().GroupBy(x => x.Field<string>("shopcode"));

            foreach(var group in groups)
            {
                DataRow newRow = pivotTable.Rows.Add();
                newRow["shopcode"] = group.Key;
                var months = group.GroupBy(x => x.Field<int>("month"));
                foreach(var month in months)
                {
                    newRow[month.Key.ToString()] = month.Sum(x => x.Field<int>("count"));
                }
                for(int i = minMonth; i <= maxMonth; i++)
                {
                    if(newRow[i.ToString()] == DBNull.Value)
                    {
                        newRow[i.ToString()] = 0;
                    }
                }
            }
        }
    }
}
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement