sql - Merge four indexed views in only one -


i'm working sql server 2012 express , developer edition latest service pack.

i have created 3 indexed views:

create view dbo.requestedcodesstatistics schemabinding     select code_level, count_big(*) codes_requested     dbo.codes     group code_level go  create unique clustered index ix_requested_statistics on dbo.requestedcodesstatistics (code_level) go create view dbo.printedcodesstatistics schemabinding     select level, count_big(*) codes_printed     dbo.codes       (flag = 1) or             (flag = 0) or             (flag = 20) or             (flag = 120)     group level go  create unique clustered index ix_printed_statistics on dbo.printedcodesstatistics (level) go  create view dbo.readcodesstatistics schemabinding     select level, count_big(*) codes_read     dbo.codes       (flag = 0) or             (flag = 20) or             (flag = 120)     group level go  create unique clustered index ix_read_statistics on dbo.readcodesstatistics (level) go  create view dbo.droppedcodesstatistics schemabinding     select level, count_big(*) codes_dropped     dbo.codes       (flag = 11) or             (flag = 30) or             (flag = 31) or             (flag = 130)     group level go  create unique clustered index ix_dropped_statistics on dbo.droppedcodesstatistics (level) go 

as can see, i'm getting statistics codes table depending on flag column value.

how can create 1 view 5 columns: codes_requested, level, codes_printed, codes_read , codes_dropped?

you can use "filtered aggregation trick":

sum(case when [condition] 1 else 0 end) myfilteredcount 

filter on condition like.


Comments

Popular posts from this blog

javascript - Any ideas when Firefox is likely to implement lengthAdjust and textLength? -

matlab - "Contour not rendered for non-finite ZData" -

delphi - Indy UDP Read Contents of Adata -