【问题标题】:C# split results of a query based on Date(Month) and Group基于日期(月)和组的 C# 拆分查询结果
【发布时间】:2012-04-03 16:33:14
【问题描述】:

在 C# 中有没有一种方法可以根据日期和组拆分结果?

数据:

集团 |创建日期 |数量 一个 | 2012-03-01 | 5 一个 | 2012-02-01 | 1 一个 | 2012-02-01 | 3 乙| 2012-02-01 | 4 一个 | 2012-01-01 | 1

数据网格将显示:

总计 | 2012/01 | 2012/02 | 2012|03 A组:10 | 1 | 4 | 5 B组:4 | 0 | 4 | 0


这是可以实现的吗?

【问题讨论】:

  • 您需要使用内部 JOins 来实现这一点,但这是可能的。 Joins SQL 作为阅读材料。
  • 看起来你想要一个支点,你为什么不在服务器端而不是应用端做呢?
  • 没有时间在这里写完整的答案,但如果您使用的是 SQL Server,解决方案是通过 Dynamic SQL Pivot。这是一个很好的例子:stackoverflow.com/a/7182489/570191

标签: c# .net sql dataset pivot


【解决方案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;}
}

【讨论】:

    【解决方案2】:

    以下 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 的结果是

    【讨论】:

      【解决方案3】:

      您可以使用 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 也会为您提供很多关于动态枢轴的答案。

      【讨论】:

        猜你喜欢
        • 2019-06-04
        • 1970-01-01
        • 2014-04-27
        • 1970-01-01
        • 2016-05-16
        • 2013-03-16
        • 2011-10-07
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多