我需要显示一个历史,我们所有的Products,我们已售出按天,周,月和年。这些数据将被发送到google charts,以显示结果的折线图。
所以如果我有一个名为Products的表,它看起来是这样的:
产品
ProductID INT
DateCreated DATETIMEOFFSET用户要求查看ProductID 1的历史记录。我如何检索它?例如输出。
图表1(日期与销售数量)
Monday 1st Dec: 0
Tuesday 2nd Dec: 3
Wed 3rd Dec: 1图表2(周数与销售量)
Week 49 2008: 21
Week 50 2008: 11
Week 51 2008: 45
Week 52 2008: 0Graph3 (月数与销售量)
Dec 08: 102
Jan 09: 23我不确定“白天”是否能做到……或者其他任何东西。
干杯:)
更新1:实现了部分功能...
在花了一段时间后,我得到了第一个工作...但仍然需要其他两个方面的帮助,并使其成为一个查询的一部分...
from p in Products
where p.ProductId == 69
group p.DateCreated by p.DateCreated.Date into grouping
orderby grouping.Key
select new { Date = grouping.Key, Count = grouping.Count() }发布于 2009-03-27 00:11:01
var data = from p in ctx.Products
where p.ProductID == *productId*
group p by p.DateCreated.DayOfWeek into groupedProducts
select new { DayOfWeek = groupedProducts.Key, Count = groupedProcuts.Count() };没有测试,我想这对你来说可能已经足够了。
每年都要这样做:
var data = from p in ctx.Products
where p.ProductID == *productId*
group p by n.CreateDate.Year into gn
select new {
Count = from a in gn
group a by a.CreateDate.DayOfYear into aa
select new { Count = aa.Count(), Key = new DateTime(gn.Key , 1, 1).AddDays(aa.Key) }
};(很抱歉变量名:P)
发布于 2009-03-26 23:55:01
我不知道如何使用LINQ表达式做到这一点,但您可以使用PIVOT运算符编写存储过程。参见Using PIVOT and UNPIVOT。
发布于 2009-03-27 00:28:23
使用DateTime.Date、DateTime.Month和DateTime.Year。但是,LINQ- to -SQL中存在一个错误,有时会将它们转换为无效的SQL。作为一种变通办法,你可以根据SqlMethods.DateDiffDay、SqlMethods.DateDiffMonth和SqlMethods.DateDiffYear进行分组,比如'1980-01-01‘。
周的部分并不是那么容易。每周的开始取决于Sql Server设置,因此它不可靠。或者,您可以使用SqlMethods.DateDiffDay从您的周开始的某个日期开始,并将日差除以7。如果您需要给定年份的周数,则应计算小于或等于年初的最近一周开始日的日差值。
https://stackoverflow.com/questions/687968
复制相似问题