Use Aggregate Function in UNION ALL result set

How can I use aggregate Functions in UNION ALL Resultset

FOR EXAMPLE

Result Set Would Be

When I tried to obtain MAX(A) it returns 3. I want 6.

When I tried to obtain MAX(B) it returns 4. I want 7.

Other than Max(), Can I obtain another aggregate function which user defined?

For example:

(SELECT TOP 1 A WHERE B=5)

Try this way:


If the column A is varchar (You said that in the comment below) try this way:

With TOP 1