简体   繁体   English

如何从LINQ实现SQL Pivot语句

[英]How can I achieve SQL Pivot statement from LINQ

I am looking to achieve below SQL statement from LINQ. 我希望从LINQ实现下面的SQL语句。 I am not sure whether is it possible? 我不确定是否可能? Can someone advice me on this? 有人可以给我建议吗?

SELECT *
FROM (
    SELECT CONVERT(VARCHAR, (DATEADD(WEEK, DATEDIFF(WEEK, 0, S.SampleDrawn), 0)), 101) [Date], [Range] =
        CASE 
            WHEN ProbBacteremia >= 0 AND ProbBacteremia < 0.50 THEN 'Low'
            WHEN ProbBacteremia >= 0.50 AND ProbBacteremia < 0.75 THEN 'Med' 
            ELSE 'High'
        END
    FROM Result.Calculation C INNER JOIN Data.SampleSet S ON C.SampleSetID = S.ID  WHERE  S.SampleDrawn >= DATEADD(WEEK,-1,GETDATE())) o
    PIVOT
    (
        COUNT(o.[Range])
        FOR [Range] IN (
        [Low], [Med], [High])
    ) pt
    ORDER BY [Date]

Result of the above query will be as below 以上查询的结果如下

Date        Low Med High
09/04/2017  370 174 175
09/11/2017  764 352 389
09/18/2017  759 384 360
09/25/2017  765 385 404
10/02/2017  115 48  56

Note that, above date has grouped by week. 请注意,以上日期已按周分组。 Ie. 就是 09/04 , 09/11, 09/18 etc. I did lot of research but i found only to group by Week Number. 09/04,09/11,09/18等。我做了很多研究,但发现只按周数分组。

This is as far as i could come up with LINQ which will return me the below result set. 这是我所能想到的LINQ,它将返回以下结果集。

data = (from a in context.Calculations
                             where a.SampleSet.SampleDrawn >= dtStart && (isDeptFilter || a.SampleSet.Department == location)
                             group a by new { Text = RangeProvider(a.ProbBacteremia * 100, riskCats), Date = a.SampleSet.SampleDrawn.Date } into groupedData
                             orderby groupedData.Key.Date ascending
                             select new { Value = groupedData.Count(), Text = groupedData.Key.Text, Date = groupedData.Key.Date.ToShortDateString() }).ToList();

public static string RangeProvider(int value)
        {
            if (value > 0 && value <= 25)
            { return "Low"; }
            if (value > 25 && value <= 75)
            { return "Medium"; }
            if (value > 75 && value <= 90)
            { return "High"; }
            else
            { return "Very High"; }
        }

Result dataset of the obver LINQ is 观察者LINQ的结果数据集为

Date        Text Value
09/04/2017  Low  65
09/04/2017  Med  80
09/04/2017  High 40
09/05/2017  Low  30
10/05/2017  Med  50
10/05/2017  High 44

Hope this explains what I'm trying to achieve. 希望这能解释我要达到的目标。 Please can someone help me with this? 请有人可以帮我吗?

作为一种变通办法,我使用了Entity Framework Core的“ FromSQL ”方法来执行我的存储过程,该过程将处理所有GROUP BY。

you can use this. 你可以用这个

data = (from a in context.Calculations
             where a.SampleSet.SampleDrawn >= dtStart && (isDeptFilter || a.SampleSet.Department == location)
             group a by new { Text = RangeProvider(a.ProbBacteremia * 100, riskCats), Date = a.SampleSet.SampleDrawn.Date } into groupedData
             orderby groupedData.Key.Date ascending
             select new { 
                 Date = groupedData.Key.Date.ToShortDateString() ,
                 Low = ( groupedData.Key.Text =="Low" )?groupedData.Count() : 0,
                 Medium = ( groupedData.Key.Text =="Medium" )?groupedData.Count() : 0,
                 High = ( groupedData.Key.Text =="High" )?groupedData.Count() : 0,
                 VeryHigh = ( groupedData.Key.Text =="Very High" )?groupedData.Count() : 0
             }).ToList();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM