And this update query I repeating for remaining columns. How could I do that in single query?
Performance implications aside for a moment, the easiest way to accomplish this in a single query** is by creating a User-Defined Aggregate (UDA) via SQLCLR. This is what I described in my answer to your related question:
How can I pass column to function in sql?How can I pass column to function in sql?
If you had such a function, you could do the following:
INSERT INTO RESULT(Col1, Col2, Col3, Col4, Col5)
SELECT SUM(ListingCol1) AS [Col1],
SUM(ListingCol2) AS [Col2],
dbo.AggMedian(ListingCol3) AS [Col3],
dbo.AggMedian(ListingCol4) AS [Col4],
dbo.AggMedian(ListingCol5) AS [Col5]
FROM LISTING;
With that said, performance is still something that needs to be considered. The approach shown above does not work in all situations. If the LISTING
table has millions of rows (at least millions per each grouping), then this might not work. But if each grouping has 5000 rows, or maybe even 10,000 or something along those lines, and the process doesn't run multiple times per minute, then you should be fine. Of course, as with anything, it should be tested against your actual data to determine if there is a performance issue or not.
** By "easiest way in a single query", I am assuming that the desired method is one that can be easily applied in multiple situations, especially ad hoc queries.