Outputting result as 2 separate columns So I created a procedure that calculates the average and median of a string array of numbers:

CREATE PROCEDURE dbo.spAvg_Median 
    @input nvarchar(20) 
AS
BEGIN
    declare @split table(id int primary key identity (1,1), number int)

    INSERT INTO @split SELECT value FROM string_split(@input, ',')

    SELECT Average FROM (
        SELECT SUM(number)/COUNT(number) as [Average]
        FROM @split
        ) AS T1
    UNION
    SELECT Median FROM (
        SELECT o.number AS [Median], rn=ROW_NUMBER() OVER (ORDER BY o.number), c.c
        FROM @split AS o
        CROSS APPLY (SELECT c=COUNT(*) FROM @split b) AS c
        ) AS x
        WHERE rn IN ((c + 1)/2, (c + 2)/2)
END
RETURN 0 
GO


However, when you run this query you get the following result: 

EXEC dbo.spAvg_Median'1,5,9,8,7'




What I want it to output is Average in one column and Median in another column next to it.

The UNION command is there so it doesn't output it as two different queries like this, since that is not what I want:
According to rule to find median(average if even number of values &amp; middle number if odd number of values). It can be done like this.

CREATE PROCEDURE dbo.spAvg_Median 
    @input nvarchar(20) 
AS
BEGIN
    declare @split table(id int primary key identity (1,1), number int)
    declare @count int

    INSERT INTO @split SELECT value FROM string_split(@input, ',')
    select @count=count(*) from @split

        SELECT AVG(A.number) as [Average],B.Median
            FROM @split A
        CROSS APPLY
                    (
                    SELECT Median FROM (
                    SELECT
                              o.number AS [Median]
                            , rn=ROW_NUMBER() OVER (ORDER BY o.number ASC)
                    FROM    @split AS o        
                    ) AS x
                    WHERE rn =ROUND(@count/2.0,0)
                    ) B
        GROUP BY B.Median
END
RETURN 0 
GO


Thanks!