I have defined function
with table value parameter
which returns
median
as per values passed to it. Function
defined as:
CREATE FUNCTION [dbo].[fn_GetMedian](@List TypeMedian READONLY)
RETURNS INT
AS
BEGIN
<function body>
END
And Table Type
TypeMedian
definition as:
CREATE TYPE [dbo].[TypeMedian] AS TABLE(
[VALUE] [int] NULL
)
Now I have a table
Listing
with filled values in it as and a table
RESULT
to be filled according to table
Listing
:
Tables structure
as
LISTING(ListingCol1,ListingCol2,ListingCol3,ListingCol4,ListingCol5)
RESULT(Col1,Col2,Col3,Col4,Col5)
Listing table has more that 1000 rows of data.
All column
from both tables
are of type int
.
Now I want to fill columns
of RESULT table
and that column
could be calculate as:
Col1 = SUM(ListingCol1)
Col2 = SUM(ListingCol2)
Col3 = dbo.fn_GetMedian(ListingCol3)
Col4 = dbo.fn_GetMedian(ListingCol5ListingCol4)
Col5 = dbo.fn_GetMedain(ListingCol5)
And I'm doing so as:
INSERT INTO RESULT(Col1)
SELECT SUM(ListingCol1)
Update RESULT
SET Col2 = SUM(ListingCol2)
DECLARE @tbl_Median TypeMedian
INSERT INTO @tbl_Median
SELECT ListingCol3
FROM Listing
UPDATE RESULT
SET Col3 = dbo.fn_GetMedian(@tbl_Median)
-- For next column
DELETE FROM @tbl_Median
INSERT INTO @tbl_Median
SELECT ListingCol4
FROM Listing
UPDATE RESULT
SET Col4 = dbo.fn_GetMedian(@tbl_Median);
--And this update query I repeating for remaining columns. How could I do that in single query?