2017-02-20 143 views
1

如何獲得我有的SUM範圍的MAX值?SQL Server SUM then MAX

電流輸出:

Size | Format | ColB | Value 
Large | PCT | Tmp1 | 23.05 
Large | PCT | Tmp3 | 109.00 
Large | PCT | Tmp50 | 3.22 
Large | PCT | Tmp109 | 987.23 
Large | PCT | Tmp58 | 455.30 

預期輸出:

Size | Format | ColB | Value | Max_Value 
Large | PCT | Tmp1 | 23.05 | 987.23 
Large | PCT | Tmp3 | 109.00 | 987.23 
Large | PCT | Tmp50 | 3.22 | 987.23 
Large | PCT | Tmp109 | 987.23 | 987.23 
Large | PCT | Tmp58 | 455.30 | 987.23 

讓我知道這是可能的。
低於當前代碼:

USE DB1 

DECLARE @StartDate DATE 
DECLARE @EndDate DATE 
DECLARE @ColA VARCHAR(10) 
DECLARE @ColB VARCHAR(20) 
DECLARE @ColC VARCHAR(15) 
DECLARE @ColD VARCHAR(15) 
DECLARE @ColE VARCHAR(15) 
DECLARE @ColF VARCHAR(15) 
DECLARE @ColG VARCHAR(15) 

BEGIN 
SET @StartDate = '01/01/2017' 
SET @EndDate = '01/14/2017' 
SET @ColA = '%%' 
SET @ColB = '%%' 
SET @ColC = '%%' 
SET @ColD = '%%' 
SET @ColE = '%%' 
SET @ColF = '%%' 
SET @ColG = '%%' 
END 

SELECT 
    'Large' AS [Size] 
    ,'PCT' AS [Format] 
    ,[ColB] 
    ,CAST((SUM([Numerator])/SUM([Denominator]))*100 AS NUMERIC(12,2)) AS [Value] 
FROM [Table1] WITH(NOLOCK) 
WHERE [Date] BETWEEN @StartDate AND @EndDate 
AND [ColA] LIKE @ColA 
AND [ColB] LIKE @ColB 
AND [ColC] LIKE @ColC 
AND [ColD] LIKE @ColD 
AND [ColE] LIKE @ColE 
AND [ColF] LIKE @ColF 
AND [ColG] LIKE @ColG 
GROUP BY [ColB] 

預先感謝您。

+0

將一個不相關的子查詢添加到選擇列表中。 – jarlh

回答

0

您可以使用窗口函數像這樣:

SELECT 
    'Large' AS [Size] 
    ,'PCT' AS [Format] 
    ,[ColB] 
    ,CAST((SUM([Numerator])/SUM([Denominator]))*100 AS NUMERIC(12,2)) AS [Value] 
    ,cast(max(sum([Numerator])/sum([Denominator])) over(partition by null)) as numeric(12,2)) *100 as [MaxValue] 
FROM [Table1] WITH(NOLOCK) 
WHERE [Date] BETWEEN @StartDate AND @EndDate 
AND [ColA] LIKE @ColA 
AND [ColB] LIKE @ColB 
AND [ColC] LIKE @ColC 
AND [ColD] LIKE @ColD 
AND [ColE] LIKE @ColE 
AND [ColF] LIKE @ColF 
AND [ColG] LIKE @ColG 
GROUP BY [ColB] 

我認爲它應該在你的情況下工作。

+0

這按預期工作。謝謝。其他問題,如果該行的[Value]與[MaxValue]匹配,我如何創建一個新列以返回'Yes'?這只是爲了更容易的可見性,如果這不可行,那很好。再次感謝您的幫助。 – black31cat

+0

您可以在外部查詢中結束此查詢並在此處執行檢查: 'select a。*,iif(a.Value = a.MaxValue,'YES','NO')as IfMatches from(SELECT' Large'AS [Size] ,'PCT'AS [Format] ,[ColB] ... ... GROUP BY [ColB])as a' – plamenbv

1

使用您的SUM的子查詢,然後使用MAX函數來獲取此信息。

SELECT 'Large' AS [Size], 'PCT' AS [Format], [ColB], 
CAST((SUM([Numerator])/SUM([Denominator]))*100 AS NUMERIC(12,2)) AS [Value], 
    (SELECT MAX(SUBValue) 
    FROM (SELECT [ColB], CAST((SUM([Numerator])/SUM([Denominator]))*100 AS NUMERIC(12,2)) AS [SUBValue] 
     FROM [Table1] WITH(NOLOCK) 
     WHERE [Date] BETWEEN @StartDate AND @EndDate 
     AND [ColA] LIKE @ColA 
     AND [ColB] LIKE @ColB 
     AND [ColC] LIKE @ColC 
     AND [ColD] LIKE @ColD 
     AND [ColE] LIKE @ColE 
     AND [ColF] LIKE @ColF 
     AND [ColG] LIKE @ColG 
     GROUP BY [ColB])) AS Max_Value 
FROM [Table1] WITH(NOLOCK) 
WHERE [Date] BETWEEN @StartDate AND @EndDate 
AND [ColA] LIKE @ColA 
AND [ColB] LIKE @ColB 
AND [ColC] LIKE @ColC 
AND [ColD] LIKE @ColD 
AND [ColE] LIKE @ColE 
AND [ColF] LIKE @ColF 
AND [ColG] LIKE @ColG 
GROUP BY [ColB]