2012-04-03 27 views
2

日付とグループに基づいて結果を分割する方法はありますか?

日付(月)とグループに基づくクエリの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

はそれを達成するために参加しているが、はい、それは可能です。 [Joins SQL](http://www.w3schools.com/sql/sql_join_inner.asp)を読んでください。 –

+0

はピボットが必要なように見えますが、アプリケーション側ではなくサーバ側で行うのはなぜですか? – Taryn

+0

ここで完全な答えを書く時間はありませんが、SQL Serverを使用している場合、解決策はDynamic SQL Pivotです。ここに素晴らしい例があります:http://stackoverflow.com/a/7182489/570191 –

答えて

1

グループを使用して、結果ピボットを作成するループを使用します。 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)

SO検索すると、あなたの答えの多くを提供しますダイナミックピボットも同様です。

1

次のLINQクエリは、目的の結果に近い結果を返します。

結果からテーブルを簡単に作成できるはずです。

LINQPadから
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 
      }; 

結果は、あなたが内部で作業する必要があります
enter image description here

関連する問題