tsql - SQL Server 2008R2 - How to show difference between two rows of a calculated column in a group? -


i'm quite new sql server , having trouble coming solution this. query aggregates total numbers of leavers , achievers area , ethnicity, dividing achievers leavers give success rate. want show difference between success rate white british , bme within each area grouping. plan use value highlight areas difference between ethnicities greater (for example) 5%.

the data source query excel sheet of ~20,000 rows

select  [assdir name] area,  ethnicity2 ethnicity,  sum([leavers overall]) leavers,   sum([ach overall]) achievers,  sum([ach overall]) / nullif (sum([leavers overall]), 0) * 100 success  sheet1$  group [assdir name], ethnicity2 order [assdir name],  case when [ethnicity2]='white british' '1' else [ethnicity2] end 

it produces first 5 columns - want able add difference column:

    area    ethnicity       leavers achievers   success      difference     ====================================================================             abc     white british   325     253         77.84615385  3.234927235     abc     bme             111     90          81.08108108  3.234927235     abc     not provided    7       5           71.42857143  3.234927235     def     white british   291     196         67.35395189  -13.11666375     def     bme             59      32          54.23728814  -13.11666375     def     not provided    1       1           100          -13.11666375     ghi     white british   684     578         84.50292398  4.487901711     ghi     bme             109     97          88.99082569  4.487901711     ghi     not provided    2       2           100          4.487901711 

it's getting work within groups that's causing headache - areas can't see way adapt works when area grouped column:

select   ethnicity2 ethnicity,  sum([leavers overall]) leavers,  sum([completed overall]) completers,  sum([ach overall]) achievers,  sum([ach overall]) / nullif (sum([leavers overall]), 0) * 100 success, (select          sum([ach overall]) / nullif (sum([leavers overall]), 0) * 100          sheet1$          ethnicity2 = 'bme'         group  ethnicity2) - (select          sum([ach overall]) / nullif (sum([leavers overall]), 0) * 100          sheet1$          ethnicity2 = 'white british'         group  ethnicity2) diff  sheet1$ group  ethnicity2  

sorry length, there's elegant solution i'm missing hours of googling/searching has drawn blank.

quick hack without fancy:

with q (     select      [assdir name] area,      ethnicity2 ethnicity,      sum([leavers overall]) leavers,       sum([ach overall]) achievers,      sum([ach overall]) / nullif (sum([leavers overall]), 0) * 100 success     sheet1$     group [assdir name], ethnicity2 ) select     area, ethnicity, leavers, achievers, success,     (select success q q2 q2.area = q.area , q2.ethnicity = 'bme') -     (select success q q2 q2.area = q.area , q2.ethnicity = 'white british') difference q order area,  case when ethnicity ='white british' '1' else ethnicity end 

your approach have worked if had correlated inside query outside area.


Comments

Popular posts from this blog

1111. appearing after print sequence - php -

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

Ruby on Rails, ActiveRecord, Postgres, UTF-8 and ASCII-8BIT encodings -