如何獲得我有的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]
預先感謝您。
將一個不相關的子查詢添加到選擇列表中。 – jarlh