简体   繁体   English

连续获取MAX和MIN SQL

[英]Get MAX and MIN in a row SQL

;WITH CTE AS
(
    SELECT * FROM
    (
        SELECT CandidateID, t_Candidate.Name, ISNULL(CAST(AVG(Rate) AS DECIMAL(12,2)),0) AS Rate, t_Ambassadors.Name AS CN
        FROM t_Vote INNER JOIN t_Candidate 
        ON t_Vote.CandidateID = t_Candidate.ID
        INNER JOIN t_Ambassadors 
        ON t_Vote.AmbassadorID = t_Ambassadors.ID
        GROUP BY Rate, CandidateID, t_Candidate.Name, t_Ambassadors.Name
    )MySrc
    PIVOT
    (
        AVG(Rate)
        FOR CN IN ([Jean],[Anna],[Felicia])
    )AS nSrc

)SELECT CandidateID, Name, CAST([Jean] AS DECIMAL(12,2)) AS AHH ,CAST([Anna] AS DECIMAL(12,2)) AS MK,CAST([Felicia] AS DECIMAL(12,2)) AS DIL, CAST(([Jean] + [Anna] + [Felicia])/3 AS DECIMAL(12,2)) AS Total
FROM CTE
GROUP BY Cte.CandidateID, cte.Name, cte.[Jean], cte.[Anna], cte.[Felicia]

I have solved my previous problem with the above query. 我用上面的查询解决了我以前的问题。 I created a new question because I have new problem. 我创建了一个新问题,因为我遇到了新问题。 How do I get the MAX and MIN rate in a row? 如何连续获得MAX和MIN速率?

The following is the result I get from the above query: 以下是我从上面的查询得到的结果:

| CandidateID | Name |  AHH  |  MK  | DIL  | Total |
|-------------|------|-------|------|------|-------|
|     CID1    | Jay  | 7.00  | 3.00 | 3.00 | 4.33  |
|     CID2    | Mia  | 2.00  | 9.00 | 7.00 | 6.00  |

What I want to achieve is this: 我想要实现的是:

| CandidateID | Name |  AHH  |  MK  | DIL  | Total |
|-------------|------|-------|------|------|-------|
|     CID1    | Jay  | 7.00  | 3.00 | 3.00 | 3.00  |
|     CID2    | Mia  | 2.00  | 9.00 | 7.00 | 7.00  |

So what happened on the 2nd result is that, it removed the Highest and Lowest score/rate from the row and Get the average of remaining rate/score. 所以在第二个结果发生的事情是,它从行中删除了最高和最低得分/率并得到剩余率/得分的平均值。 AHH, MK and DIL are not the only Voters, there are 14 of them, I just took the 3 first to make it short and clearer. AHH,MK和DIL并不是唯一的选民,其中有14个,我只是先拿出3个选民,让它更短更清晰。

I believe you're looking by something like the following (though I'm using case aggregation rather than a pivot). 我相信你正在寻找类似下面的东西(虽然我使用的是案例聚合而不是支点)。

Essentially, it does the same thing your query does except that it uses a row number to figure out the highest and lowest and exclude them from the final "total" (in the case of a tie, it'll just select one of them, but you can use RANK() instead of row_number() if you don't want to include tied highest/lowest in the average): 从本质上讲,它会对您的查询执行相同的操作,除了它使用行号来计算最高和最低值并将它们从最终的“总数”中排除(在平局的情况下,它只选择其中一个,但如果您不想在平均值中包含最高/最低值,则可以使用RANK()而不是row_number():

WITH CTE AS
(
    SELECT CandidateID,
           Name,
           CN,
           Rate,
           Lowest = ROW_NUMBER() OVER (PARTITION BY CandidateID, Name ORDER BY Rate),
           Highest = ROW_NUMBER() OVER (PARTITION BY CandidateID, Name ORDER BY Rate DESC)
    FROM
    (
        SELECT CandidateID,
               t_Candidate.Name,
               CN = t_Ambassadors.Name,
               Rate = ISNULL(CAST(AVG(Rate) AS DECIMAL(12,2)),0)
        FROM t_Vote
        JOIN t_Candidate
            ON t_Vote.CandidateID = t_Candidate.ID
        JOIN t_Ambassadors
            ON t_Vote.AmbassadorID = t_Ambassadors.ID
        GROUP BY CandidateID, t_Candidate.Name, t_Ambassadors.Name
    ) AS T
)
SELECT CandidateID,
       Name,
       AHH = MAX(CASE WHEN CN = 'Jean' THEN Rate END),
       MK = MAX(CASE WHEN CN = 'Anna' THEN Rate END),
       DIL = MAX(CASE WHEN CN = 'Felicia' THEN Rate END), -- and so on and so forth for each CN
       Total = AVG(CASE WHEN Lowest != 1 AND Highest != 1 THEN Rate END)
FROM CTE
GROUP BY CandidateID, Name;

EDIT: It is possible to do this using PIVOT, but unless I'm mistaken, it becomes a matter of working out the average of the ones that aren't highest and lowest before pivoting, which becomes a bit more convoluted. 编辑:有可能使用PIVOT做到这一点,但除非我弄错了,否则在转动之前计算出不是最高和最低的平均值的问题变得有点复杂。 It's all around easier to use case aggregation, IMO. 使用案例聚合IMO更容易。

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

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