2012-04-03 39 views
2

基於日期和分組的C#拆分結果有沒有辦法?

C#根據日期(月)和分組拆分結果

數據:

 
Group | Create Date | Qty 
A  | 2012-03-01 | 5 
A  | 2012-02-01 | 1 
A  | 2012-02-01 | 3 
B  | 2012-02-01 | 4 
A  | 2012-01-01 | 1 

數據網格將顯示:

 
     Total | 2012/01 | 2012/02 | 2012|03 
Group A: 10 | 1  | 4  | 5 
GROUP B: 4  | 0  | 4  | 0 


這是不是可以實現?

+0

你需要對內部工作連接來實現這一目標,但是是有可能。作爲閱讀材料[加入SQL](http://www.w3schools.com/sql/sql_join_inner.asp)。 – 2012-04-03 16:36:50

+0

看起來像你想要的一個樞紐,爲什麼你不在服務器端而不是應用端做到這一點? – Taryn 2012-04-03 16:37:08

+0

沒時間在這裏寫一個完整的答案,但如果您使用SQL Server,解決方案是通過動態SQL數據透視表。這是一個很好的例子:http://stackoverflow.com/a/7182489/570191 – 2012-04-03 16:39:41

回答

1

使用group by和循環結果創建數據透視表。 PS:使用LinqPad查看輸出。

void Main() 
{ 
    var data = new List<Entry> { 
     new Entry { Group = "A", Date = DateTime.Parse("2012-03-01"), Qty = 5 }, 
     new Entry { Group = "A", Date = DateTime.Parse("2012-02-01"), Qty = 1 }, 
     new Entry { Group = "A", Date = DateTime.Parse("2012-02-01"), Qty = 3 }, 
     new Entry { Group = "B", Date = DateTime.Parse("2012-02-01"), Qty = 4 }, 
     new Entry { Group = "A", Date = DateTime.Parse("2012-01-01"), Qty = 1 } 
    }; 
    data.GroupBy(d => new { d.Group, d.Date }).Dump(); 
} 

class Entry 
{ 
    public string Group { get; set; } 
    public DateTime Date { get; set; } 
    public int Qty { get; set; } 
} 

// Define other methods and classes here 
class Button 
{ 
    public Color BackColor 
    { get; set;} 
} 
0

您可以使用帶有靜態/動態數據透視的PIVOT來做到這一點。下面是一個靜態的旋轉的例子:

create table #temp 
(
    [group] varchar(1), 
    createdate smalldatetime, 
    qty int 
) 

insert into #temp values ('A', '3/1/2012', 5) 
insert into #temp values ('A', '2/1/2012', 1) 
insert into #temp values ('A', '2/1/2012', 3) 
insert into #temp values ('B', '2/1/2012', 4) 
insert into #temp values ('A', '1/1/2012', 1) 

select [group] 
    , total 
    , isnull([1/1/2012], 0) as '2012/1' 
    , isnull([2/1/2012], 0) as '2012/2' 
    , isnull([3/1/2012], 0) as '2012/3' 
from 
(

    select t1.[group], t1.createdate, t1.qty, t2.total 
    from #temp t1 
    left join 
    (
     select [group], sum(qty) as Total 
     from #temp 
     group by [group] 
    ) t2 
     on t1.[group] = t2.[group] 
) x 
pivot 
(
    sum(qty) 
    for createdate in ([3/1/2012], [2/1/2012], [1/1/2012]) 
) p 
order by [group] 

drop table #temp 

一個動態的支點有很多的資源:

Pivots with Dynamic Columns in SQL Server 2005

Using SQL Server 2005/2008 Pivot on Unknown Number of Columns (Dynamic Pivot)

搜索做會給你很多答案在動態樞軸上也是如此。

1

以下LINQ查詢將返回一個接近您想要的結果。

您應該可以很容易地從結果中創建表格。

var data = new [] { 
    new { Group = "A", Date = DateTime.Parse("2012-03-01"), Qty = 5 }, 
    new { Group = "A", Date = DateTime.Parse("2012-02-01"), Qty = 1 }, 
    new { Group = "A", Date = DateTime.Parse("2012-02-01"), Qty = 3 }, 
    new { Group = "B", Date = DateTime.Parse("2012-02-01"), Qty = 4 }, 
    new { Group = "A", Date = DateTime.Parse("2012-01-01"), Qty = 1 } 
}; 

var result = from item in (
       from d in data 
       group d by d.Group into EntryGroup 
       select new { 
        EntryGroup.Key, 
        YearAndMonthGroups = 
         from e in EntryGroup 
         let yearAndMonth = e.Date.ToString("yyyy-MM") 
         group e by yearAndMonth into monthGroup 
         orderby monthGroup.Key 
         select new { 
          YearAndMonth = monthGroup.Key, 
          SubTotal = monthGroup.Sum(w => w.Qty) 
         }    
       }) 
      select new { 
       Group = item.Key, 
       Total = item.YearAndMonthGroups.Sum(s => s.SubTotal), 
       YearAndMonthGroups = item.YearAndMonthGroups 
      }; 

結果從LINQPad是
enter image description here