C#根据日期(月)和组分割查询结果
在C#中是否有基于日期和组分割结果的方法C#根据日期(月)和组分割查询结果,c#,.net,sql,dataset,pivot,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 组|创建日期|数量 A | 2012-03-01 | 5 A | 2012-02-01 | 1 A | 2012-02-01 | 3 B | 201
数据:
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 组|创建日期|数量 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 总计| 2012/01 | 2012/02 | 2012 | 03 A组:10 | 1 | 4 | 5 B组:4 | 0 | 4 | 0
这是可以实现的吗?使用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;}
}
void Main()
{
var数据=新列表{
新条目{Group=“A”,Date=DateTime.Parse(“2012-03-01”),Qty=5},
新条目{Group=“A”,Date=DateTime.Parse(“2012-02-01”),Qty=1},
新条目{Group=“A”,Date=DateTime.Parse(“2012-02-01”),Qty=3},
新条目{Group=“B”,Date=DateTime.Parse(“2012-02-01”),Qty=4},
新条目{Group=“A”,Date=DateTime.Parse(“2012-01-01”),Qty=1}
};
GroupBy(d=>new{d.Group,d.Date}).Dump();
}
班级报名
{
公共字符串组{get;set;}
公共日期时间日期{get;set;}
公共整数数量{get;set;}
}
//在此处定义其他方法和类
类按钮
{
公共色底色
{get;set;}
}
您可以使用带静态/动态轴的任意一个轴来执行此操作。以下是静态轴的示例:
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
对于动态轴心,有很多资源:
搜索SO也将为您提供许多关于动态枢轴的答案。下面的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的结果是您需要处理内部联接才能实现这一点,但这是可能的。作为阅读材料。看起来您想要一个pivot,为什么不在服务器端而不是应用程序端这样做呢?这里没有时间写完整的答案,但是如果您使用SQL server,解决方案是通过动态SQL pivot。下面是一个很好的例子: