【问题标题】:Emulating SQL Server 2012 Lag Window function with LINQ使用 LINQ 模拟 SQL Server 2012 滞后窗口函数
【发布时间】:2014-02-13 19:58:44
【问题描述】:

我有一些示例数据,如下所示:(编辑:我没有 SQL Server 2012)

create table #base
(pat_id int
,admission_date date
,discharge_date date
)
go
insert into #base
values
(1, '2007-01-04',   '2007-01-04'),
(1, '2007-01-10',   '2007-01-10'),
(1, '2007-01-11',   '2007-01-11'),
(1, '2007-01-18',   '2007-01-18'),
(1, '2007-01-24',   '2007-01-24'),
(1, '2008-01-25',   '2008-01-26'),
(2, '2007-02-01',   '2007-02-01'),
(2, '2007-02-06',   '2007-02-06'),
(2, '2007-02-07',   '2007-02-07'),
(2, '2007-02-08',   '2007-02-08')

这是我希望用 LINQ 模拟的 SQL 查询

;with cte 
as
(
    select   pat_id
            ,admission_date
            ,discharge_date
            ,ROW_NUMBER() over(partition by pat_id order by admission_date asc) as rn
    from #base
)
select   firstCte.pat_id
        ,firstCte.discharge_date as firstAdmitReference
        ,secondCte.admission_date as dischargeReference
        ,case when DATEDIFF(DAY,firstCte.discharge_date,secondCte.admission_date) <= 30 then 1 else 0 end Readmit
from cte as firstCte
inner join cte as secondCte
      on firstCte.pat_id = secondCte.pat_id
where firstCte.rn = secondCte.rn -1 

该查询的结果如下所示:

create table #readmit_data
( pat_id int
 ,admission_date date
 ,discharge_date date
 ,within_x_days int
 )
insert into #readmit_data(pat_id,admission_date,discharge_date,within_x_days)
values

(1, '2007-01-04',   '2007-01-10',   1),
(1, '2007-01-10',   '2007-01-11',   1),
(1, '2007-01-11',   '2007-01-18',   1),
(1, '2007-01-18',   '2007-01-24',   1),
(1, '2007-01-24',   '2008-01-25',   0),
(2, '2007-02-01',   '2007-02-06',   1),
(2, '2007-02-06',   '2007-02-07',   1),
(2, '2007-02-07',   '2007-02-08',   1)

在这个结果集中,数据的基本格式是

patient ID dischargeDate nextVisitAdmitDate

within_x_days 列中的 1 表示患者在最后一次出院 30 天后再次入院。理想情况下,30 将是用户输入的变量。

LINQ 中有什么类型的构造可以做到这一点?

【问题讨论】:

  • 您想在 LINQ to SQL 查询中使用 #Temp 表吗?做不到。
  • 您的代码中的 #base#temp 是同一个表吗?
  • @thepirat000 #base 和 #temp 相同。我实际上并没有尝试查询临时表,我希望其他人能够快速可视化问题,所以我添加了 SQL 脚本。

标签: c# sql sql-server linq


【解决方案1】:

LinqToSql 没有 lag/lead/row_number 的类比函数。

您还不能这样做,最简单的方法是将 SQL 发送到机器,然后在返回时将结果集映射到您的对象。

【讨论】:

    【解决方案2】:

    Window To LINQ 是 .NET 的扩展函数库

    【讨论】:

      【解决方案3】:

      LinqTo2Db支持Window-FunctionsLagLead

      from p in db.Parent
          join c in db.Child on p.ParentID equals c.ParentID 
          select new
          {
              Lag = Sql.Ext
                  .Lag(p.Value1, Sql.Nulls.None)
                  .Over()
                  .PartitionBy(p.Value1, c.ChildID)
                  .OrderBy(p.Value1)
                  .ThenBy(c.ChildID)
                  .ThenBy(c.ParentID)
                  .ToValue(),
      
              Lead = Sql.Ext
                  .Lead(p.Value1, Sql.Nulls.None)
                  .Over()
                  .PartitionBy(p.Value1, c.ChildID)
                  .OrderByDesc(p.Value1)
                  .ThenBy(c.ChildID)
                  .ThenByDesc(c.ParentID)
                  .ToValue(),
          }
      

      在我的情况下,Sql.Nulls.IgnoreSql.Nulls.Respect 不适用于 SQL Server

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-06-20
        • 2017-04-30
        • 2015-02-16
        • 1970-01-01
        • 2022-01-22
        • 2018-01-03
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多