Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Efficient way to perform calculations on large datasets

$
0
0

Hi, I am using SQL Server 2008 R2, Members table has more than 10 million records and I am working on summarizing all this data for reporting. I need to calculate Sum of AccruedHours, Sum of Amount for each member in the Calendar year for Code "EEALP", Sum of Amount for each member in the Calendar year for Code "EEVAP" & Sum of Amount for each member in the Calendar year for Code "EEPHL". I was doing this using join for each code and it was taking forever to return results. Could you please let me know better way of writing this. Please find sample data below.

DECLARE @Membership TABLE (Id INT, [MemberId] INT, WorkPeriod INT, Code VARCHAR(15), [AccruedHours] DECIMAL(10,4), Amount MONEY) 
INSERT INTO @Membership
SELECT 1, 234576, 201301, 'EEALP', 21.4, 132.45
UNION ALL
SELECT 2, 234576, 201301, 'EESIL', 7.5, 42.3
UNION ALL
SELECT 3, 234576, 201301, 'EEVAP', 13.2, 24.6
UNION ALL
SELECT 4, 234576, 201301, 'EEPHL', 9.0, 18.0
UNION ALL
SELECT 5, 234576, 201301, 'EESIL', 12.5, 22.5
UNION ALL
SELECT 6, 234576, 201301, 'EEUPH', 2.3, 3.13
UNION ALL
SELECT 7, 234576, 201302, 'EEALP', 11.1, 89.13
UNION ALL
SELECT 8, 234576, 201302, 'EESIL', 3.7, 5.3
UNION ALL
SELECT 9, 234576, 201302, 'EEVAP', 4.9, 12.65
UNION ALL
SELECT 10, 234576, 201302, 'EEPHL', 4.1, 12.32
UNION ALL
SELECT 11, 234576, 201302, 'EESIL', 9.7, 19.14
UNION ALL
SELECT 12, 234576, 201302, 'EEUPH', 14.3, 17.29
UNION ALL
SELECT 13, 234576, 201303, 'EEALP', 5.6, 45.14
UNION ALL
SELECT 14, 234576, 201303, 'EESIL', 19.3, 22.19
UNION ALL
SELECT 15, 234576, 201303, 'EEVAP', 4.9, 7.8
UNION ALL
SELECT 16, 234576, 201303, 'EEPHL', 8.7, 22.17
UNION ALL
SELECT 17, 234576, 201303, 'EESIL', 16.3, 32.92
UNION ALL
SELECT 18, 234576, 201303, 'EEUPH', 21.3, 31.28
UNION ALL
SELECT 19, 234576, 201304, 'EEALP', 17.8, 21.78
UNION ALL
SELECT 20, 234576, 201304, 'EESIL', 6.9, 8.83
UNION ALL
SELECT 21, 234576, 201304, 'EEVAP', 8.3, 16.73
UNION ALL
SELECT 22, 234576, 201304, 'EEPHL', 7.1, 15.69
UNION ALL
SELECT 23, 234576, 201304, 'EESIL', 12.9, 15.25
UNION ALL
SELECT 24, 234576, 201304, 'EEUPH', 17.1, 7.64
UNION ALL
SELECT 25, 498723, 201301, 'EEALP', 27.1, 97.37
UNION ALL
SELECT 26, 498723, 201301, 'EESIL', 12.3, 31.3
UNION ALL
SELECT 27, 498723, 201301, 'EEVAP', 13.2, 29.6
UNION ALL
SELECT 28, 498723, 201301, 'EEPHL', 9.7, 17.19
UNION ALL
SELECT 29, 498723, 201301, 'EESIL', 14.15, 16.45
UNION ALL
SELECT 30, 498723, 201301, 'EEUPH', 13.4, 13.0
UNION ALL
SELECT 31, 498723, 201302, 'EEALP', 13.7, 45.13
UNION ALL
SELECT 32, 498723, 201302, 'EESIL', 5.9, 6.2
UNION ALL
SELECT 33, 498723, 201302, 'EEVAP', 8.9, 17.65
UNION ALL
SELECT 34, 498723, 201302, 'EEPHL', 8.2, 19.87
UNION ALL
SELECT 35, 498723, 201302, 'EESIL', 12.9, 23.85
UNION ALL
SELECT 36, 498723, 201302, 'EEUPH', 18.4, 19.32
UNION ALL
SELECT 37, 498723, 201303, 'EEALP', 15.8, 32.26
UNION ALL
SELECT 38, 498723, 201303, 'EESIL', 21.2, 24.1
UNION ALL
SELECT 39, 498723, 201303, 'EEVAP', 13.3, 12.9
UNION ALL
SELECT 40, 498723, 201303, 'EEPHL', 7.8, 13.92
UNION ALL
SELECT 41, 498723, 201303, 'EESIL', 14.4, 28.92
UNION ALL
SELECT 42, 498723, 201303, 'EEUPH', 14.6, 29.78
UNION ALL
SELECT 43, 498723, 201304, 'EEALP', 27.8, 39.64
UNION ALL
SELECT 44, 498723, 201304, 'EESIL', 16.3, 18.39
UNION ALL
SELECT 45, 498723, 201304, 'EEVAP', 17.3, 34.73
UNION ALL
SELECT 46, 498723, 201304, 'EEPHL', 14.1, 29.69
UNION ALL
SELECT 47, 498723, 201304, 'EESIL', 2.9, 5.4
UNION ALL
SELECT 48, 498723, 201304, 'EEUPH', 6.1, 3.43
UNION ALL
SELECT 49, 616897, 201301, 'EEALP', 17.3, 56.1
UNION ALL
SELECT 50, 616897, 201301, 'EESIL', 12.7, 24.3
UNION ALL
SELECT 51, 616897, 201301, 'EEVAP', 19.2, 38.6
UNION ALL
SELECT 52, 616897, 201301, 'EEPHL', 17.2, 27.32
UNION ALL
SELECT 53, 616897, 201301, 'EESIL', 9.5, 18.6
UNION ALL
SELECT 54, 616897, 201301, 'EEUPH', 21.3, 43.13
UNION ALL
SELECT 55, 616897, 201302, 'EEALP', 13.1, 54.13
UNION ALL
SELECT 56, 616897, 201302, 'EESIL', 13.2, 15.3
UNION ALL
SELECT 57, 616897, 201302, 'EEVAP', 14.7, 13.75
UNION ALL
SELECT 58, 616897, 201302, 'EEPHL', 8.1, 10.19
UNION ALL
SELECT 59, 616897, 201302, 'EESIL', 13.6, 24.19
UNION ALL
SELECT 60, 616897, 201302, 'EEUPH', 12.3, 14.33
UNION ALL
SELECT 61, 616897, 201303, 'EEALP', 6.0, 37.14
UNION ALL
SELECT 62, 616897, 201303, 'EESIL', 14.2, 27.29
UNION ALL
SELECT 63, 616897, 201303, 'EEVAP', 5.9, 8.8
UNION ALL
SELECT 64, 616897, 201303, 'EEPHL', 6.8, 17.11
UNION ALL
SELECT 65, 616897, 201303, 'EESIL', 15.1, 29.8
UNION ALL
SELECT 66, 616897, 201303, 'EEUPH', 19.3, 24.28
UNION ALL
SELECT 67, 616897, 201304, 'EEALP', 10.8, 29.78
UNION ALL
SELECT 68, 616897, 201304, 'EESIL', 8.9, 13.83
UNION ALL
SELECT 69, 616897, 201304, 'EEVAP', 7.3, 19.73
UNION ALL
SELECT 70, 616897, 201304, 'EEPHL', 6.1, 12.69
UNION ALL
SELECT 71, 616897, 201304, 'EESIL', 18.9, 17.25
UNION ALL
SELECT 72, 616897, 201304, 'EEUPH', 11.1, 17.23
UNION ALL
SELECT 73, 723411, 201301, 'EEALP', 17.3, 56.1
UNION ALL
SELECT 74, 723411, 201301, 'EESIL', 12.7, 24.3
UNION ALL
SELECT 75, 723411, 201301, 'EESIL', 9.5, 18.6
UNION ALL
SELECT 76, 723411, 201301, 'EEUPH', 21.3, 43.13
UNION ALL
SELECT 77, 723411, 201302, 'EEALP', 13.1, 54.13
UNION ALL
SELECT 78, 723411, 201302, 'EESIL', 13.2, 15.3
UNION ALL
SELECT 79, 723411, 201302, 'EESIL', 13.6, 24.19
UNION ALL
SELECT 80, 723411, 201302, 'EEUPH', 12.3, 14.33
UNION ALL
SELECT 81, 723411, 201303, 'EEALP', 6.0, 37.14
UNION ALL
SELECT 82, 723411, 201303, 'EESIL', 14.2, 27.29
UNION ALL
SELECT 83, 723411, 201303, 'EESIL', 15.1, 29.8
UNION ALL
SELECT 84, 723411, 201303, 'EEUPH', 19.3, 24.28
UNION ALL
SELECT 85, 723411, 201304, 'EEALP', 10.8, 29.78
UNION ALL
SELECT 86, 723411, 201304, 'EESIL', 8.9, 13.83
UNION ALL
SELECT 87, 723411, 201304, 'EESIL', 18.9, 17.25
UNION ALL
SELECT 88, 723411, 201304, 'EEUPH', 11.1, 17.23
SELECT * FROM @Membership
/*
--Resultset
MemberId [Year] ALP_AccruedHours ALP_Amount    VAP_Amount      PHL_Amount
234576    2013  55.9		     288.5	      61.78		68.18
498723    2013  84.4		     214.4	      94.88		80.67
616897	   2013	  47.2		     177.15         80.88		67.31
723411	   2013  47.2		     177.15          0		 0	
*/
Thanks in advance..............

Ione



Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>