Get MAX and MIN in SQL string

;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 solved my previous problem with the above request. I created a new question because I have a new problem. How to get maximum and maximum speed per line?

Below is the result obtained 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  |

I want to achieve 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 in the second result, is that he removed the highest and lowest point / rate from the line and received the average value of the remaining bet / point. AHH, MK and DIL are not the only Voters, there are 14 of them, I just made the first 3 to make them short and clear.

+4
source share
1 answer

I believe that you are looking at something like the following (although I use case aggregation, not a code).

, , , , "" ( , 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;

: PIVOT, , , , . , .

+3

Source: https://habr.com/ru/post/1673370/


All Articles