August 24, 2017 at 10:18 am
I know you can use the MAX(v) FROM (VALUES () AS VALUE(v)) query to select the max value from a series of columns. How can this be applied with a GROUP BY to select the max of those max values?
For example,
CREATE TABLE #Temp
(
ID INT IDENTITY PRIMARY KEY,
GroupVal INT,
Val2 INT,
Val3 INT,
Val4 INT
)
That gives me the max value from each row, of the three columns val1, val2, and val3. But, I can't surround that with a MAX and GROUP BY GroupVal, since I'm aggregating an aggregate.
I know I can do something like this:
CREATE TABLE #Temp
(
ID INT IDENTITY PRIMARY KEY,
GroupVal INT,
Val2 INT,
Val3 INT,
Val4 INT
)
But that's hideously ugly, and gets incredibly complex when the number of columns increases.
Any suggestions?
August 24, 2017 at 10:25 am
CREATE TABLE #Temp
(
ID INT IDENTITY PRIMARY KEY,
GroupVal INT,
Val2 INT,
Val3 INT,
Val4 INT
)
August 25, 2017 at 6:44 am
Have you ever used UNION or UNION ALL ? Try the following, which isn't all that bad in terms of how you'll have to expand it as the number of columns grows. Eventually, you might want to go to dynamic SQL for it, but that's not really necessary.CREATE TABLE #Temp (
ID INT IDENTITY PRIMARY KEY,
GroupVal INT,
Val2 INT,
Val3 INT,
Val4 INT
);
INSERT INTO #Temp (GroupVal, Val2, Val3, Val4)
VALUES (1, 2, 3, 4),
(1, 3, 4, 7),
(1, 4, 2, 1),
(2, 1, 3, 5),
(2, 4, 1, 3),
(3, 1, 2, 3);
SELECT X.GroupVal, MAX(X.Num) AS MaxValue
FROM (
SELECT GroupVal, Val2 AS Num
FROM #Temp
UNION ALL
SELECT GroupVal, Val3 AS Num
FROM #Temp
UNION ALL
SELECT GroupVal, Val4 AS Num
FROM #Temp
) AS X
GROUP BY X.GroupVal
ORDER BY X.GroupVal;
DROP TABLE #Temp;
Steve (aka sgmunson) π π π
Rent Servers for Income (picks and shovels strategy)
August 25, 2017 at 9:38 am
kramaswamy - Thursday, August 24, 2017 10:25 AMHm. Can't find any way to delete the question, so I guess I'll just answer it with the obvious solution that I didn't think of before writing the question πCREATE TABLE #Temp
(
ID INT IDENTITY PRIMARY KEY,
GroupVal INT,
Val2 INT,
Val3 INT,
Val4 INT
)INSERT INTO #Temp (GroupVal, Val2, Val3, Val4) VALUES (1, 2, 3, 4), (1, 3, 4, 7), (1, 4, 2, 1), (2, 1, 3, 5), (2, 4, 1, 3), (3, 1, 2, 3)SELECT
GroupVal,
MAX(MaxVal)
FROM
(
SELECT
GroupVal,
(
SELECT MAX(v)
FROM (VALUES (Val2), (Val3), (Val4)) AS VALUE(v)
) AS MaxVal
FROM #Temp
) r
GROUP BY GroupValDROP TABLE #Temp
-- Simpler and more intuitive using APPLY
SELECT
GroupVal,
MAX(x.MaxVal)
FROM #Temp
CROSS APPLY (SELECT MaxVal = MAX(v) FROM (VALUES (Val2), (Val3), (Val4)) AS VALUE(v)) x
GROUP BY GroupVal
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
August 25, 2017 at 11:03 am
CTE and cross apply looks good option
with CTE as
(
SELECT
GroupVal,
(
SELECT MAX(v)
FROM (VALUES (Val2), (Val3), (Val4)) AS VALUE(v)
) val
FROM #Temp
)
select GroupVal, max(val) from cte
group by GroupVal
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply