Author |
Topic |
zwheeler
Starting Member
25 Posts |
Posted - 2013-08-28 : 09:28:07
|
HiI table with 132 columns, the number of rows vary based on criteria. Each column represents a financial transaction code. So, for each column i need to get the Max Value, Min Value, Mean Value and Median Value. I have the formulas for each. I can do it by manually adding each column which is problematic. So i created a table with just the column names. I created a cursor (so i could loop through the column names one at a time, pass in the column name.thought process: if i could pass in each column name in a loop then i can pass in the name to my select into statement (add each row to another table that will contain all of my statistics) however, i keep running into error(s) because it does not recognize my column name and i am not sure if i need another cursor as well.See Code Below (You Help and Insight would be greatly appreciated)DECLARE @Column_Name_Metric varchar(50)DECLARE @Metric_Value decimal(15,2)declare @getMetricValue CURSORDECLARE @getName CURSORSET @getName = CURSOR FORSELECT column_name FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'temp_prt' order by column_nameOPEN @getNameFETCH NEXTFROM @getName INTO @Column_Name_MetricWHILE @@FETCH_STATUS = 0BEGINPRINT @Column_Name_Metric ----> this gets me all of the columns --Add to result set to table INSERT INTO stg_prt_statistics select @Column_Name_Metric,max(@Column_Name_Metric) as maxi,min(@Column_Name_Metric) as mini, avg(@Column_Name_Metric) as meani,( (SELECT MAX(@Column_Name_Metric) FROM (SELECT TOP 50 PERCENT @Column_Name_Metric FROM temp_prt ORDER BY @Column_Name_Metric) AS BottomHalf) + (SELECT MIN(@Column_Name_Metric) FROM (SELECT TOP 50 PERCENT @Column_Name_Metric FROM temp_prt ORDER BY @Column_Name_Metric DESC) AS TopHalf) ) / 2.0 AS Median from temp_prtFETCH NEXTFROM @getName INTO @Column_Name_MetricENDCLOSE @getNameDEALLOCATE @getName |
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2013-08-28 : 12:06:31
|
It sounds like you have an architectural issue. Having 132 columns to represent transaction codes, seems like a rather bad idea. That aside, can you provide some sample data and your expected output? Do you just need to min, mean and median for each column?Here are some links that can help you prepare your DDL, DML and expected output:http://www.sqlservercentral.com/articles/Best+Practices/61537/http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx |
|
|
zwheeler
Starting Member
25 Posts |
Posted - 2013-08-28 : 13:28:05
|
HiYes, i need to get the min,max,mean, and median for each column.Here is the ddl, for the data i will provide only about 7 columns (132 is a lot) and 4 rows of data--DDL[Code]CREATE TABLE [dbo].[temp_prt]( [mtr_unit_count] [numeric](17, 0) NULL, [pk_afs_id] [numeric](31, 21) NULL, [fk_afs_header_id] [numeric](30, 21) NULL, [fk_afs_project_id] [numeric](31, 21) NULL, [mtr_3247] [numeric](31, 21) NULL, [mtr_3248] [numeric](31, 21) NULL, [mtr_3249] [numeric](31, 21) NULL, [mtr_3250] [numeric](31, 21) NULL, [mtr_5000t] [numeric](31, 21) NULL, [mtr_5060n] [numeric](31, 21) NULL, [mtr_5060t] [numeric](31, 21) NULL, [mtr_5100t] [numeric](31, 21) NULL, [mtr_5120] [numeric](31, 21) NULL, [mtr_5121] [numeric](31, 21) NULL, [mtr_5140] [numeric](31, 21) NULL, [mtr_5152n] [numeric](31, 21) NULL, [mtr_5170] [numeric](31, 21) NULL, [mtr_5180] [numeric](31, 21) NULL, [mtr_5190] [numeric](31, 21) NULL, [mtr_5191] [numeric](31, 21) NULL, [mtr_5192] [numeric](31, 21) NULL, [mtr_5193] [numeric](31, 21) NULL, [mtr_5194] [numeric](31, 21) NULL, [mtr_5195] [numeric](31, 21) NULL, [mtr_5200T] [numeric](31, 21) NULL, [mtr_5220] [numeric](31, 21) NULL, [mtr_5240] [numeric](31, 21) NULL, [mtr_5250] [numeric](31, 21) NULL, [mtr_5270] [numeric](31, 21) NULL, [mtr_5290] [numeric](31, 21) NULL, [mtr_5300] [numeric](31, 21) NULL, [mtr_5400T] [numeric](31, 21) NULL, [mtr_5410] [numeric](31, 21) NULL, [mtr_5430] [numeric](31, 21) NULL, [mtr_5440] [numeric](31, 21) NULL, [mtr_5490] [numeric](31, 21) NULL, [mtr_5900T] [numeric](31, 21) NULL, [mtr_5910] [numeric](31, 21) NULL, [mtr_5920] [numeric](31, 21) NULL, [mtr_5945] [numeric](31, 21) NULL, [mtr_5960] [numeric](31, 21) NULL, [mtr_5970] [numeric](31, 21) NULL, [mtr_5990] [numeric](31, 21) NULL, [mtr_6000T] [numeric](31, 21) NULL, [mtr_6203] [numeric](31, 21) NULL, [mtr_6204] [numeric](31, 21) NULL, [mtr_6210] [numeric](31, 21) NULL, [mtr_6235] [numeric](31, 21) NULL, [mtr_6250] [numeric](31, 21) NULL, [mtr_6263T] [numeric](31, 21) NULL, [mtr_6310] [numeric](31, 21) NULL, [mtr_6311] [numeric](31, 21) NULL, [mtr_6312] [numeric](31, 21) NULL, [mtr_6320] [numeric](31, 21) NULL, [mtr_6330] [numeric](31, 21) NULL, [mtr_6331] [numeric](31, 21) NULL, [mtr_6340] [numeric](31, 21) NULL, [mtr_6350] [numeric](31, 21) NULL, [mtr_6351] [numeric](31, 21) NULL, [mtr_6370] [numeric](31, 21) NULL, [mtr_6390] [numeric](31, 21) NULL, [mtr_6420] [numeric](31, 21) NULL, [mtr_6450] [numeric](31, 21) NULL, [mtr_6451] [numeric](31, 21) NULL, [mtr_6452] [numeric](31, 21) NULL, [mtr_6453] [numeric](31, 21) NULL, [mtr_6400T] [numeric](31, 21) NULL, [mtr_6510] [numeric](31, 21) NULL, [mtr_6515] [numeric](31, 21) NULL, [mtr_6520] [numeric](31, 21) NULL, [mtr_6521] [numeric](31, 21) NULL, [mtr_6525] [numeric](31, 21) NULL, [mtr_6530] [numeric](31, 21) NULL, [mtr_6531] [numeric](31, 21) NULL, [mtr_6546] [numeric](31, 21) NULL, [mtr_6548] [numeric](31, 21) NULL, [mtr_6570] [numeric](31, 21) NULL, [mtr_6590] [numeric](31, 21) NULL, [mtr_6500T] [numeric](31, 21) NULL, [mtr_6710] [numeric](31, 21) NULL, [mtr_6711] [numeric](31, 21) NULL, [mtr_6720] [numeric](31, 21) NULL, [mtr_6721] [numeric](31, 21) NULL, [mtr_6722] [numeric](31, 21) NULL, [mtr_6723] [numeric](31, 21) NULL, [mtr_6790] [numeric](31, 21) NULL, [mtr_6700T] [numeric](31, 21) NULL, [mtr_6820] [numeric](31, 21) NULL, [mtr_6825] [numeric](31, 21) NULL, [mtr_6830] [numeric](31, 21) NULL, [mtr_6840] [numeric](31, 21) NULL, [mtr_6845] [numeric](31, 21) NULL, [mtr_6850] [numeric](31, 21) NULL, [mtr_6890] [numeric](31, 21) NULL, [mtr_6800T] [numeric](31, 21) NULL, [mtr_6900] [numeric](31, 21) NULL, [mtr_6600] [numeric](31, 21) NULL, [mtr_6610] [numeric](31, 21) NULL, [mtr_7105] [numeric](31, 21) NULL, [mtr_7110] [numeric](31, 21) NULL, [mtr_7115] [numeric](31, 21) NULL, [mtr_7120] [numeric](31, 21) NULL, [mtr_7130] [numeric](31, 21) NULL, [mtr_7140] [numeric](31, 21) NULL, [mtr_7141] [numeric](31, 21) NULL, [mtr_7142] [numeric](31, 21) NULL, [mtr_7190] [numeric](31, 21) NULL, [mtr_7100T] [numeric](31, 21) NULL, [mtr_S1000_010] [numeric](31, 21) NULL, [mtr_S1000_020] [numeric](31, 21) NULL, [mtr_S1000_030] [numeric](31, 21) NULL, [mtr_S1000_040] [numeric](31, 21) NULL, [mtr_fass_score] [numeric](3, 0) NULL, [mtr_grs_rent_unit] [numeric](31, 21) NULL, [mtr_vacancy_rate] [numeric](16, 9) NULL, [mtr_eff_rent_unit] [numeric](31, 21) NULL, [mtr_othr_inc_unit] [numeric](31, 21) NULL, [mtr_op_expenses_per_unit] [numeric](16, 6) NULL, [mtr_net_op_income_per_unit] [numeric](15, 8) NULL, [mtr_debt_service_coverage] [numeric](12, 5) NULL)[/Code]--- DML and sample datainsert into temp_prt (mtr_unit_count,pk_afs_id,8fk_afs_header_id,7fk_afs_project_id,6mtr_3247,5mtr_3248,4mtr_3250,3mtr_5000t,2mtr_5060n)values (131,80.3791348600508,34459.2875318066,2602938.9312977,-73.4637404580152,0,0,-73.4637404580152,466.039440203562)insert into temp_prt (mtr_unit_count,pk_afs_id,8fk_afs_header_id,7fk_afs_project_id,6mtr_3247,5mtr_3248,4mtr_3250,3mtr_5000t,2mtr_5060n)values (85,25.1019607843137,72715.6862745098,4207666.66666666,-57.8333333333333,0,0,-57.8333333333333,399.425490196078)insert into temp_prt (mtr_unit_count,pk_afs_id,8fk_afs_header_id,7fk_afs_project_id,6mtr_3247,5mtr_3248,4mtr_3250,3mtr_5000t,2mtr_5060n)values (173,113.623795761078,195168.59344894,4138641.6184971,-78.2129094412331,0,0,-78.2129094412331,401.440751445086)insert into temp_prt (mtr_unit_count,pk_afs_id,8fk_afs_header_id,7fk_afs_project_id,6mtr_3247,5mtr_3248,4mtr_3250,3mtr_5000t,2mtr_5060n)values (151,32.0358719646799,108261.589403973,4090960.26490066,-52.365894039735,0,0,-52.365894039735,463.893487858719) --Expected out put for just 1 column (although i need similar result for each column except mtr_countselect 'mtr_5120',max(mtr_5120) as maxi,min(mtr_5120) as mini, avg(mtr_5120) as meani,( (SELECT MAX(mtr_5120) FROM (SELECT TOP 50 PERCENT mtr_5120 FROM temp_prt ORDER BY mtr_5120) AS BottomHalf) + (SELECT MIN(mtr_5120) FROM (SELECT TOP 50 PERCENT mtr_5120 FROM temp_prt ORDER BY mtr_5120 DESC) AS TopHalf) ) / 2.0 AS Medianfrom temp_prtproducesmetric_name maxi mini meani medianmtr_5120 357.43 338.01 347.11 346.50 |
|
|
zwheeler
Starting Member
25 Posts |
Posted - 2013-08-29 : 18:24:08
|
Thanks here is my solution to the problems--define my outer loopDECLARE @getName CURSOR, @columnname varchar(35),@sql nvarchar(2000);SET @getName = CURSOR FOR SELECT column_name FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'temp_prt' order by column_nameOPEN @getNameFETCH NEXTFROM @getName INTO @columnnameWHILE @@FETCH_STATUS = 0BEGIN--this is my inner loopDECLARE @getMetric CURSOR, @column_name varchar(35), @metric_min decimal(12,3), @metric_max decimal(12,3), @metric_avg_mean decimal(12,3), @metric_median decimal(12,3);select @sql= N'SET @getMetric = CURSOR FOR select ' + N' + min(' + @columnname + N' ),' + N' + max(' + @columnname + N' ),' + N' + avg(' + @columnname + N' ),' + N'((SELECT MAX(' + @columnname + N') FROM (SELECT TOP 50 PERCENT ' + @columnname + ' FROM temp_prt ORDER BY ' + @columnname + N') AS BottomHalf) + (SELECT MIN(' + @columnname + ') FROM (SELECT TOP 50 PERCENT ' + @columnname + ' FROM temp_prt ORDER BY ' + @columnname + N' DESC) AS TopHalf)) / 2.0' + N' from temp_prt ' + N';OPEN @getMetric';exec sp_executesql @sql,N'@getMetric cursor output',@getMetric outputFETCH NEXTFROM @getMetric INTO @metric_min,@metric_max,@metric_avg_mean,@metric_medianWHILE @@FETCH_STATUS = 0BEGIN insert into prt_metric_test (tag_column_name,mtr_min,mtr_max,mtr_avg,mtr_median) values (@columnname,@metric_min,@metric_max,@metric_avg_mean,@metric_median)FETCH NEXTFROM @getMetric INTO @metric_min,@metric_max,@metric_avg_mean,@metric_medianENDCLOSE @getMetricDEALLOCATE @getMetric--Close my outer loopFETCH NEXTFROM @getName INTO @columnnameENDCLOSE @getNameDEALLOCATE @getNameselect * from prt_metric_testwhich producespk_id tag_column_name mtr_min mtr_max mtr_median mtr_avg1 fk_afs_header_id 34459.288 195168.593 90488.638 102651.2892 fk_afs_project_id 2602938.931 4207666.667 4114800.942 3760051.8703 mtr_3247 -78.213 -52.366 -65.649 -65.4694 mtr_3248 0.000 0.000 0.000 0.0005 mtr_3249 0.000 0.000 0.000 0.0006 mtr_3250 -78.213 -52.366 -65.649 -65.4697 mtr_5000t 399.425 466.039 432.667 432.7008 mtr_5060n -121.260 -60.362 -75.816 -83.3139 mtr_5060t -65.211 37.793 -3.221 -8.46510 mtr_5100t 338.242 357.428 346.497 347.16611 mtr_5120 338.013 357.428 346.497 347.10912 mtr_5121 0.000 0.000 0.000 0.00013 mtr_5140 0.000 0.000 0.000 0.00014 mtr_5152n 324.812 349.811 340.591 338.95115 mtr_5170 0.000 0.229 0.000 0.05716 mtr_5180 0.000 0.000 0.000 0.00017 mtr_5190 0.000 0.000 0.000 0.00018 mtr_5191 0.000 0.000 0.000 0.00019 mtr_5192 0.000 0.000 0.000 0.00020 mtr_5193 0.000 0.000 0.000 0.00021 mtr_5194 0.000 0.000 0.000 0.00022 mtr_5195 0.000 0.000 0.000 0.00023 mtr_5200T 1.926 18.156 6.389 8.21524 mtr_5220 0.000 0.000 0.000 0.00025 mtr_5240 0.000 0.000 0.000 0.00026 mtr_5250 0.000 0.000 0.000 0.00027 mtr_5270 0.000 0.000 0.000 0.00028 mtr_5290 1.926 18.156 6.389 8.21529 mtr_5300 0.000 0.000 0.000 0.00030 mtr_5400T 0.011 0.189 0.043 0.07231 mtr_5410 0.000 0.000 0.000 0.00032 mtr_5430 0.000 0.000 0.000 0.00033 mtr_5440 0.005 0.044 0.020 0.02234 mtr_5490 0.004 0.176 0.008 0.04935 mtr_5900T 49.567 132.947 96.097 93.67736 mtr_5910 0.000 0.774 0.105 0.24637 mtr_5920 2.592 3.421 3.121 3.06438 mtr_5945 0.000 0.000 0.000 0.00039 mtr_5960 0.000 0.000 0.000 0.00040 mtr_5970 0.000 0.000 0.000 0.00041 mtr_5990 46.095 130.196 92.590 90.36842 mtr_6000T 363.648 489.763 455.624 441.16543 mtr_6203 0.000 0.000 0.000 0.00044 mtr_6204 0.000 0.000 0.000 0.00045 mtr_6210 0.000 0.368 0.024 0.10446 mtr_6235 0.000 0.000 0.000 0.00047 mtr_6250 0.000 0.000 0.000 0.00048 mtr_6263T 91.278 185.724 119.805 129.15349 mtr_6310 0.000 0.000 0.000 0.00050 mtr_6311 21.765 61.617 27.361 34.52651 mtr_6312 0.000 0.000 0.000 0.00052 mtr_6320 16.240 17.490 17.030 16.94753 mtr_6330 31.801 59.924 35.690 40.77654 mtr_6331 0.000 0.000 0.000 0.00055 mtr_6340 1.571 21.807 13.896 12.79256 mtr_6350 3.217 6.546 3.925 4.40357 mtr_6351 6.638 15.166 11.122 11.01258 mtr_6370 3.175 19.018 6.088 8.59259 mtr_6390 0.000 0.000 0.000 0.00060 mtr_6400T 36.717 99.637 59.272 63.72461 mtr_6420 0.000 0.000 0.000 0.00062 mtr_6450 3.474 54.379 20.901 24.91463 mtr_6451 13.543 22.822 16.435 17.30964 mtr_6452 0.000 18.807 7.131 8.26765 mtr_6453 10.420 15.263 13.627 13.23466 mtr_6500T 44.809 70.276 57.780 57.66167 mtr_6510 0.000 0.000 0.000 0.00068 mtr_6515 0.000 0.000 0.000 0.00069 mtr_6520 0.000 0.000 0.000 0.00070 mtr_6521 0.000 0.000 0.000 0.00071 mtr_6525 18.156 39.175 22.736 25.70172 mtr_6530 3.150 5.818 3.525 4.00473 mtr_6531 0.000 0.000 0.000 0.00074 mtr_6546 0.000 0.000 0.000 0.00075 mtr_6548 0.000 0.000 0.000 0.00076 mtr_6570 0.000 0.000 0.000 0.00077 mtr_6590 16.178 42.700 26.473 27.95678 mtr_6600 41.903 108.695 59.817 67.55879 mtr_6610 6.738 7.793 7.316 7.29180 mtr_6700T 16.715 29.354 18.246 20.64081 mtr_6710 0.460 2.070 1.614 1.44082 mtr_6711 2.236 4.308 2.837 3.05483 mtr_6720 5.372 6.959 6.052 6.10984 mtr_6721 0.000 0.000 0.000 0.00085 mtr_6722 1.695 3.450 2.090 2.33186 mtr_6723 1.761 9.349 2.486 4.02187 mtr_6790 2.304 4.724 3.857 3.68588 mtr_6800T 148.294 201.648 165.002 169.98689 mtr_6820 113.447 138.041 123.775 124.76090 mtr_6825 0.000 0.000 0.000 0.00091 mtr_6830 22.203 42.094 25.806 28.97792 mtr_6840 0.000 0.000 0.000 0.00093 mtr_6845 0.000 0.000 0.000 0.00094 mtr_6850 8.748 16.282 11.161 11.83895 mtr_6890 2.534 5.590 4.761 4.41296 mtr_6900 0.000 0.000 0.000 0.00097 mtr_7100T -63.426 0.045 -3.998 -17.84498 mtr_7105 0.000 63.426 4.008 17.86199 mtr_7110 0.000 0.000 0.000 0.000100 mtr_7115 0.000 0.000 0.000 0.000101 mtr_7120 0.000 0.000 0.000 0.000102 mtr_7130 0.000 0.000 0.000 0.000103 mtr_7140 0.000 0.000 0.000 0.000104 mtr_7141 0.000 0.000 0.000 0.000105 mtr_7142 0.000 0.000 0.000 0.000106 mtr_7190 0.000 0.045 0.010 0.016107 mtr_debt_service_coverage 0.492 1.249 0.925 0.898108 mtr_eff_rent_unit 324.812 349.811 340.591 338.951109 mtr_fass_score NULL NULL NULL NULL110 mtr_grs_rent_unit 338.242 357.428 346.497 347.166111 mtr_net_op_income_per_unit NULL NULL NULL NULL112 mtr_op_expenses_per_unit NULL NULL NULL NULL113 mtr_othr_inc_unit 49.615 132.958 96.211 93.749114 mtr_S1000_010 13.299 18.454 15.035 15.455115 mtr_S1000_020 1.511 8.333 7.225 6.073116 mtr_S1000_030 2.926 19.760 14.509 12.926117 mtr_S1000_040 0.000 0.000 0.000 0.000118 mtr_unit_count 85.000 173.000 141.000 135.000119 mtr_vacancy_rate 0.006 0.053 0.018 0.024120 pk_afs_id 25.102 113.624 56.208 62.785 |
|
|
|
|
|