sql - How to select and group by month in linq? -
select name, m1 = sum(case when month(pay_date) = 1 1 else 0 end), m2 = sum(case when month(pay_date) = 2 1 else 0 end), m3 = sum(case when month(pay_date) = 3 1 else 0 end), m4 = sum(case when month(pay_date) = 4 1 else 0 end), m5 = sum(case when month(pay_date) = 5 1 else 0 end), m6 = sum(case when month(pay_date) = 6 1 else 0 end), m7 = sum(case when month(pay_date) = 7 1 else 0 end) tbl_post year(date) = 2015 group name
name m1 m2 m3 m4 m5 m6 m7 0 0 0 0 0 174 327 b 44071 50101 5501 569 65831 6998 69305 c 0 0 0 0 5 147 195 d 3438 6003 8640 8930 11598 13731 13368 e 0 1 3 2 3 3 3 f 2455 29084 993 6310 7561 19040 25722
try query
var sums = d in contex.tbl_post d.date.value.year=2015 group d new {d.name} g select new { name = g.key, m1 = g.sum(s => s.pay_date.value.month==1??1:0), m2 = g.sum(s => s.pay_date.value.month==2??1:0), m3 = g.sum(s => s.pay_date.value.month==3??1:0), m4 = g.sum(s => s.pay_date.value.month==4??1:0), m5 = g.sum(s => s.pay_date.value.month==5??1:0), m6 = g.sum(s => s.pay_date.value.month==6??1:0), m7 = g.sum(s => s.pay_date.value.month==7??1:0) };
Comments
Post a Comment