You have 2 possibilities to do that
CREATE TABLE #TempListings ( ListingId int, Price money, UnitTypeId int, BedroomsAvailable int ) INSERT INTO #TempListings VALUES(1, 1000, 1, 1) INSERT INTO #TempListings VALUES(2, 2000, 1, 1) INSERT INTO #TempListings VALUES(3, 3000, 1, 1) INSERT INTO #TempListings VALUES(4, 1000, 1, 2) INSERT INTO #TempListings VALUES(5, 2000, 1, 2) INSERT INTO #TempListings VALUES(6, 3000, 1, 2) INSERT INTO #TempListings VALUES(7, 1000, 2, 1) INSERT INTO #TempListings VALUES(8, 2000, 2, 1) INSERT INTO #TempListings VALUES(9, 3000, 2, 1) INSERT INTO #TempListings VALUES(10, 1000, 2, 2) INSERT INTO #TempListings VALUES(11, 2000, 2, 2) INSERT INTO #TempListings VALUES(12, 3000, 2, 2) GO
SELECT BedroomsAvailable ,COUNT(listingid) AS Count ,MIN(price) AS MinPrice ,MAX(price) AS MaxPrice ,AVG(price) AS AveragePrice ,STDEV(price) as StandardDeviation ,PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY UnitTypeId) OVER (PARTITION BY BedroomsAvailable) AS MedianCont ,PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY UnitTypeId) OVER () AS MedianCont ,UnitTypeId FROM #TempListings GROUP BY BedroomsAvailable, UnitTypeId ORDER BY UnitTypeId GO
BedroomsAvailable | Count | MinPrice | MaxPrice | AveragePrice | StandardDeviation | MedianCont | MedianCont | UnitTypeId ----------------: | ----: | --------: | --------: | -----------: | ----------------: | ---------: | ---------: | ---------: 1 | 3 | 1000.0000 | 3000.0000 | 2000.0000 | 1000 | 1.5 | 1.5 | 1 2 | 3 | 1000.0000 | 3000.0000 | 2000.0000 | 1000 | 1.5 | 1.5 | 1 2 | 3 | 1000.0000 | 3000.0000 | 2000.0000 | 1000 | 1.5 | 1.5 | 2 1 | 3 | 1000.0000 | 3000.0000 | 2000.0000 | 1000 | 1.5 | 1.5 | 2
db<>fiddle here