English 中文(简体)
如何在最后4行数据上履行综合功能?
原标题:How to perform aggregate function on last 4 rows of data?

我从下面的模型上找到了一张桌子

public class WeeklyNums
{
   public int FranchiseId { get; set; }
   public DateTime WeekEnding { get; set; }
   public decimal Sales { get; set; }
}

我需要第四列计算本周和前三个星期的最小值。 所以产出会像这样。

1   7-Jan   $1  
1   14-Jan  $2  
1   21-Jan  $3  
1   28-Jan  $4  **1**  
1   4-Feb   $4  **2**  
1   11-Feb  $6  **3**  
1   18-Feb  $4  **4**  
1   25-Feb  $8  **4**  
1   3-Mar   $7  **4**  

我甚至不知道从何说起,即使帮助在SQL解决问题,也会有所帮助。

特克斯! 特克斯! 特克斯! 特克斯!

最佳回答

我知道我为时已晚,

var result = from w1 in db.Table
             from w2 in db.Table.Where(x => x.WeekEnding >= w1.WeekEnding.AddDays(-28))
             select new
             {
                 FranchiseId = w1.FranchiseId,
                 WeekEnding = w1.WeekEnding,
                 Sales = w1.Sales,
                 SalesMin = w2.Min(x => x.Sales)
             };
问题回答

考虑使用 outer 应用 :

select  yt1.*
,       hist.four_week_min
from    YourTable yt1
outer apply
        (
        select  min(col1) as four_week_min
        from    YourTable yt2
        where   yt2.dt between dateadd(wk, -3, yt1.dt) and yt1.dt
        ) hist

例如,SQL Fiddle

var runningMins =   from weekNum in data
                    select new
                               {
                                   FranchiseId = weekNum.FranchiseId,
                                   WeekEnding = weekNum.WeekEnding,
                                   Sales = weekNum.Sales,
                                   LastThreeWeeks = data.OrderByDescending( x => x.WeekEnding )
                                        .Where( x => x.WeekEnding <= weekNum.WeekEnding )
                                        .Take( 4 )
                                        .Min( x => x.Sales )
                               };

SQL 查询将返回当前和前三次的最小值, 不论日期是否完全相隔三周 :

With RnkItems As 
  (
    Select DateVal, Sales
      , Row_Number() Over ( Order By DateVal ) As Rnk
    From SourceData
    )
Select *
  , (
    Select Min(Sales)
    From  RnkItems As R1
    Where R1.Rnk Between R.Rnk - 3 And R.Rnk
    )
From RnkItems R
Order By 1

SQL 中文本





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签