C#基于日期(月)和组拆分查询结果

时间:2012-04-03 16:33:14

标签: c# .net sql dataset pivot

根据日期和群组,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


这是可以实现的吗?

3 个答案:

答案 0 :(得分:1)

使用group by和循环结果创建pivot。 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;}
}

答案 1 :(得分: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

答案 2 :(得分: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也会为动态支点提供很多答案。