sql - Percentage of marks in various mark ranges -



sql - Percentage of marks in various mark ranges -

i have table looks this

for each assessment code create query returns percentage of marks in mark ranges... example

fileyear assessmentcode markrange markpercentage 2014 11cpssdd 0-10 5.88 2014 11cpssdd 11-20 0 2014 11cpssdd 21-30 0 2014 11cpssdd 31-40 0 2014 11cpssdd 41-50 0 2014 11cpssdd 51-60 0 2014 11cpssdd 61-70 0 2014 11cpssdd 71-80 29.4 2014 11cpssdd 81-90 52.9 2014 11cpssdd 91-100 11.7

not sure how @ , wondering if able help? in advance help.

you can calculating sum of each range , percentage of sum whole sum. total working example:

set nocount on go declare @datasource table ( [fileyear] char(4) ,[assessmentcode] char(8) ,[studentid] int ,[mark] tinyint ) insert @datasource ([fileyear], [assessmentcode], [studentid], [mark]) values ('2014', '11cpssdd', '34323', '75') ,('2014', '11cpssdd', '74666', '38') ,('2014', '11cpssdd', '87664', '34') ,('2014', '11cpssdd', '87576', '66') ,('2014', '11cpssdd', '23455', '87') ,('2014', '11cpssdd', '87654', '75') ,('2014', '11cpssdd', '98776', '75') ,('2014', '11cpssdd', '34543', '55') ,('2014', '11ecoeco', '87687', '89') ,('2014', '11ecoeco', '56466', '77') ,('2014', '11ecoeco', '34544', '45') ,('2014', '11ecoeco', '95554', '23') ,('2014', '11ecoeco', '22322', '56') ,('2014', '11ecoeco', '76557', '66') select [fileyear] ,[assessmentcode] ,[markpercentage] ,[markrange] * 100 /[all] [markrange] ( select [fileyear] ,[assessmentcode] ,sum(iif([mark] between 0 , 10, [mark], 0)) ,sum(iif([mark] between 11 , 20, [mark], 0)) ,sum(iif([mark] between 21 , 30, [mark], 0)) ,sum(iif([mark] between 31 , 40, [mark], 0)) ,sum(iif([mark] between 41 , 50, [mark], 0)) ,sum(iif([mark] between 51 , 60, [mark], 0)) ,sum(iif([mark] between 61 , 70, [mark], 0)) ,sum(iif([mark] between 71 , 80, [mark], 0)) ,sum(iif([mark] between 81 , 90, [mark], 0)) ,sum(iif([mark] between 91 , 100, [mark], 0)) ,sum([mark]) @datasource grouping [fileyear] ,[assessmentcode] ) datasource ([fileyear], [assessmentcode], [0-10], [11-20], [21-30], [31-40], [41-50], [51-60], [61-70], [71-80], [81-90], [91-100], [all]) unpivot ( [markrange] [markpercentage] in ([0-10], [11-20], [21-30], [31-40], [41-50], [51-60], [61-70], [71-80], [81-90], [91-100]) )pvt order [fileyear] ,[assessmentcode] ,[markpercentage] set nocount off go

check solution. pay attending filtering specific assesmentcode. also, if need result in decimal format, cast [markrange] before division.

set nocount on go declare @datasource table ( [fileyear] char(4) ,[assessmentcode] char(8) ,[studentid] int ,[mark] tinyint ) insert @datasource ([fileyear], [assessmentcode], [studentid], [mark]) values ('2014', '11cpssdd', '34323', '93') ,('2014', '11cpssdd', '74666', '93') ,('2014', '11cpssdd', '87664', '90') ,('2014', '11cpssdd', '87576', '90') ,('2014', '11cpssdd', '23455', '89') ,('2014', '11cpssdd', '87654', '86') ,('2014', '11cpssdd', '98776', '84') ,('2014', '11cpssdd', '34543', '84') ,('2014', '11cpssdd', '87687', '84') ,('2014', '11cpssdd', '56466', '83') ,('2014', '11cpssdd', '34544', '82') ,('2014', '11cpssdd', '95554', '80') ,('2014', '11cpssdd', '22322', '79') ,('2014', '11cpssdd', '76557', '78') ,('2014', '11cpssdd', '76557', '77') ,('2014', '11cpssdd', '76557', '76') ,('2014', '11cpssdd', '76557', '70') select [fileyear] ,[assessmentcode] ,[markpercentage] ,[markrange] * 100 /[all] [markrange] ( select [fileyear] ,[assessmentcode] ,sum(iif([mark] between 0 , 10, 1, 0)) ,sum(iif([mark] between 11 , 20, 1, 0)) ,sum(iif([mark] between 21 , 30, 1, 0)) ,sum(iif([mark] between 31 , 40, 1, 0)) ,sum(iif([mark] between 41 , 50, 1, 0)) ,sum(iif([mark] between 51 , 60, 1, 0)) ,sum(iif([mark] between 61 , 70, 1, 0)) ,sum(iif([mark] between 71 , 80, 1, 0)) ,sum(iif([mark] between 81 , 90, 1, 0)) ,sum(iif([mark] between 91 , 100, 1, 0)) ,count([mark]) @datasource [assessmentcode] = '11cpssdd' grouping [fileyear] ,[assessmentcode] ) datasource ([fileyear], [assessmentcode], [0-10], [11-20], [21-30], [31-40], [41-50], [51-60], [61-70], [71-80], [81-90], [91-100], [all]) unpivot ( [markrange] [markpercentage] in ([0-10], [11-20], [21-30], [31-40], [41-50], [51-60], [61-70], [71-80], [81-90], [91-100]) )pvt order [fileyear] ,[assessmentcode] ,[markpercentage] set nocount off go

sql tsql percentage

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -