2016-08-25 34 views
4

我有以下T-SQL表,數據和查詢。如何從T-SQL轉換分區的總和

create table sampledata 
(
    name nvarchar(50), 
    sampletime datetime, 
    samplevalue decimal 
); 

insert into sampledata (name, sampletime, samplevalue) values 
('ABC1235', cast('2016/01/01 10:00:00 AM' as datetime), 50.00), 
('ABC1235', cast('2016/01/01 10:05:00 AM' as datetime), 50.00), 
('ABC1235', cast('2016/01/01 10:10:00 AM' as datetime), 50.00), 
('ABC1235', cast('2016/01/01 10:15:00 AM' as datetime), null), 
('ABC1235', cast('2016/01/01 10:20:00 AM' as datetime), null), 
('ABC1235', cast('2016/01/01 10:25:00 AM' as datetime), null), 
('ABC1235', cast('2016/01/01 10:30:00 AM' as datetime), 50.00), 
('ABC1235', cast('2016/01/01 10:35:00 AM' as datetime), 50.00), 
('ABC1235', cast('2016/01/01 10:40:00 AM' as datetime), 50.00), 
('ABC1235', cast('2016/01/01 10:45:00 AM' as datetime), 50.00), 
('ABC1235', cast('2016/01/01 10:50:00 AM' as datetime), 50.00), 
('ABC1235', cast('2016/01/01 10:55:00 AM' as datetime), 50.00), 
('ABC1235', cast('2016/01/01 11:00:00 AM' as datetime), 50.00), 
('ABC1235', cast('2016/01/01 11:05:00 AM' as datetime), null), 
('ABC1235', cast('2016/01/01 11:10:00 AM' as datetime), null), 
('ABC1235', cast('2016/01/01 11:15:00 AM' as datetime), 50.00), 
('ABC1235', cast('2016/01/01 11:20:00 AM' as datetime), 50.00), 
('ABC1235', cast('2016/01/01 11:25:00 AM' as datetime), 50.00), 
('ABC1235', cast('2016/01/01 11:30:00 AM' as datetime), null), 
('ABC1235', cast('2016/01/01 11:35:00 AM' as datetime), 50.00), 
('ABC1235', cast('2016/01/01 11:40:00 AM' as datetime), 50.00), 
('ABC1235', cast('2016/01/01 11:45:00 AM' as datetime), 50.00), 
('ABC1235', cast('2016/01/01 11:50:00 AM' as datetime), null), 
('ABC1235', cast('2016/01/01 11:55:00 AM' as datetime), null), 
('ABC1235', cast('2016/01/01 12:00:00 PM' as datetime), 50.00), 
('ABC1235', cast('2016/01/01 12:05:00 PM' as datetime), null), 
('ABC1235', cast('2016/01/01 12:10:00 PM' as datetime), null), 
('ABC1235', cast('2016/01/01 12:15:00 PM' as datetime), null), 
('ABC1235', cast('2016/01/01 12:20:00 PM' as datetime), null), 
('ABC1235', cast('2016/01/01 12:25:00 PM' as datetime), null), 
('ABC1235', cast('2016/01/01 12:30:00 PM' as datetime), null) 
('ZYA4567', cast('2016/01/01 10:00:00 AM' as datetime), 50.00), 
('ZYA4567', cast('2016/01/01 10:05:00 AM' as datetime), 50.00), 
('ZYA4567', cast('2016/01/01 10:10:00 AM' as datetime), 50.00), 
('ZYA4567', cast('2016/01/01 10:15:00 AM' as datetime), null), 
('ZYA4567', cast('2016/01/01 10:20:00 AM' as datetime), null), 
('ZYA4567', cast('2016/01/01 10:25:00 AM' as datetime), null), 
('ZYA4567', cast('2016/01/01 10:30:00 AM' as datetime), null), 
('ZYA4567', cast('2016/01/01 10:35:00 AM' as datetime), 50.00), 
('ZYA4567', cast('2016/01/01 10:40:00 AM' as datetime), 50.00), 
('ZYA4567', cast('2016/01/01 10:45:00 AM' as datetime), 50.00), 
('ZYA4567', cast('2016/01/01 10:50:00 AM' as datetime), 50.00), 
('ZYA4567', cast('2016/01/01 10:55:00 AM' as datetime), 50.00), 
('ZYA4567', cast('2016/01/01 11:00:00 AM' as datetime), 50.00), 
('ZYA4567', cast('2016/01/01 11:05:00 AM' as datetime), null), 
('ZYA4567', cast('2016/01/01 11:10:00 AM' as datetime), null), 
('ZYA4567', cast('2016/01/01 11:15:00 AM' as datetime), 50.00), 
('ZYA4567', cast('2016/01/01 11:20:00 AM' as datetime), 50.00), 
('ZYA4567', cast('2016/01/01 11:25:00 AM' as datetime), 50.00), 
('ZYA4567', cast('2016/01/01 11:30:00 AM' as datetime), null), 
('ZYA4567', cast('2016/01/01 11:35:00 AM' as datetime), 50.00), 
('ZYA4567', cast('2016/01/01 11:40:00 AM' as datetime), 50.00), 
('ZYA4567', cast('2016/01/01 11:45:00 AM' as datetime), 50.00), 
('ZYA4567', cast('2016/01/01 11:50:00 AM' as datetime), null), 
('ZYA4567', cast('2016/01/01 11:55:00 AM' as datetime), null), 
('ZYA4567', cast('2016/01/01 12:00:00 PM' as datetime), 50.00), 
('ZYA4567', cast('2016/01/01 12:05:00 PM' as datetime), null), 
('ZYA4567', cast('2016/01/01 12:10:00 PM' as datetime), null), 
('ZYA4567', cast('2016/01/01 12:15:00 PM' as datetime), null), 
('ZYA4567', cast('2016/01/01 12:20:00 PM' as datetime), null), 
('ZYA4567', cast('2016/01/01 12:25:00 PM' as datetime), 40.00), 
('ZYA4567', cast('2016/01/01 12:30:00 PM' as datetime), 50.00) 

查詢:

select 
    name, 
    sampletime, 
    samplevalue, 
sum(case when samplevalue is null then 0 else 1 end) 
over(partition by name order by sampletime) 
* case when samplevalue is null then 1 else 0 end as block 
from sampledata 

我需要將其轉換成LINQ。

我想我會打破它分爲兩個步驟(我的實際查詢更多涉及包含join和where子句比這個例子)

var list = (from s in db.sampledata).ToList(); 

現在我不知道該怎麼辦

sum(case when samplevalue is null then 0 else 1 end) 
over(partition by name order by sampletime) 
* case when samplevalue is null then 1 else 0 end as block 

回答

2

你不能使用LinQToSQL來完成,因爲Linq沒有直接轉換爲SQL over partition構造。

但你可以做到這一點在客戶端,我建議你這樣說:

int i = 0; //we need this for closure 
var list = db.sampledata 
    .OrderBy(x => x.name) 
    .ThenBy(x => x.sampletime) //order your data like partition function do 
    .ToList() //get all from DB to server memory 
    .Select(x => 
    { 
     int block = 0; //logic of your block calculation 
     if (x.samplevalue.HasValue) 
      i++; 
     else 
      block = i; 
     //return your full collection 
     return new { 
         name = x.name, 
         sampletime = x.sampletime, 
         samplevalue = x.samplevalue, 
         block = block 
        }; 
    }); 

正如你可以看到排序和selectiong邏輯將在DB的一面,但計算領域block將LINQ的一面。

可能intrest你是存儲過程存儲功能來包裝你的T-SQL查詢,並將其映射到你的LinQToSQL DataContext,所以你可以把它的另一種方式