仅合并四个索引视图

时间:2014-11-27 09:12:38

标签: sql sql-server

我正在使用带有最新服务包的SQL Server 2012 Express和Developer Edition。

我创建了这三个索引视图:

CREATE VIEW dbo.RequestedCodesStatistics
WITH SCHEMABINDING
AS
    SELECT CODE_LEVEL, COUNT_BIG(*) as CODES_REQUESTED
    FROM dbo.Codes
    GROUP BY CODE_LEVEL
go

CREATE UNIQUE CLUSTERED INDEX IX_Requested_Statistics ON dbo.RequestedCodesStatistics (CODE_LEVEL)
go
CREATE VIEW dbo.PrintedCodesStatistics
WITH SCHEMABINDING
AS
    SELECT LEVEL, COUNT_BIG(*) as CODES_PRINTED
    FROM dbo.Codes
    WHERE   (FLAG = 1) OR
            (FLAG = 0) OR
            (FLAG = 20) OR
            (FLAG = 120)
    GROUP BY LEVEL
go

CREATE UNIQUE CLUSTERED INDEX IX_Printed_Statistics ON dbo.PrintedCodesStatistics (LEVEL)
go

CREATE VIEW dbo.ReadCodesStatistics
WITH SCHEMABINDING
AS
    SELECT LEVEL, COUNT_BIG(*) as CODES_READ
    FROM dbo.Codes
    WHERE   (FLAG = 0) OR
            (FLAG = 20) OR
            (FLAG = 120)
    GROUP BY LEVEL
go

CREATE UNIQUE CLUSTERED INDEX IX_Read_Statistics ON dbo.ReadCodesStatistics (LEVEL)
go

CREATE VIEW dbo.DroppedCodesStatistics
WITH SCHEMABINDING
AS
    SELECT LEVEL, COUNT_BIG(*) as CODES_DROPPED
    FROM dbo.Codes
    WHERE   (FLAG = 11) OR
            (FLAG = 30) OR
            (FLAG = 31) OR
            (FLAG = 130)
    GROUP BY LEVEL
go

CREATE UNIQUE CLUSTERED INDEX IX_Dropped_Statistics ON dbo.DroppedCodesStatistics (LEVEL)
go

如您所见,我只能从CODES表中获取统计信息,具体取决于其FLAG列值。

如何使用五列创建一个视图:CODES_REQUESTEDLEVELCODES_PRINTEDCODES_READCODES_DROPPED

3 个答案:

答案 0 :(得分:1)

您可以使用“过滤后的聚合技巧”:

SUM(CASE WHEN [CONDITION] THEN 1 ELSE 0 END) AS MyFilteredCount

根据您喜欢的任何条件进行过滤。

答案 1 :(得分:0)

你正在寻找这个吗?

With RequestedCodesStatistics as 
(   
    SELECT CODE_LEVEL Level, COUNT_BIG(*) as CODES_REQUESTED
    FROM dbo.Codes
    GROUP BY CODE_LEVEL
),
PrintedCodesStatistics as 
( 
    SELECT LEVEL, COUNT_BIG(*) as CODES_PRINTED
    FROM dbo.Codes
    WHERE   (FLAG = 1) OR
            (FLAG = 0) OR
            (FLAG = 20) OR
            (FLAG = 120)
    GROUP BY LEVEL
),
ReadCodesStatistics as 
(
    SELECT LEVEL, COUNT_BIG(*) as CODES_READ
    FROM dbo.Codes
    WHERE   (FLAG = 0) OR
            (FLAG = 20) OR
            (FLAG = 120)
    GROUP BY LEVEL
),
DroppedCodesStatistics as 
(
    SELECT LEVEL, COUNT_BIG(*) as CODES_DROPPED
    FROM dbo.Codes
    WHERE   (FLAG = 11) OR
            (FLAG = 30) OR
            (FLAG = 31) OR
            (FLAG = 130)
    GROUP BY LEVEL
)
select  a.Level ,    CODES_REQUESTED,CODES_PRINTED, CODES_READ , CODES_DROPPED
  from RequestedCodesStatistics a 
  left join PrintedCodesStatistics b
    on a.level = b.level 
  left join ReadCodesStatistics c
    on a.level = c.level 
  left join DroppedCodesStatistics d
    on a.level = c.level 

答案 2 :(得分:0)

另一种可能性是将您当前的观点用作来源:

select 
    coalesce(RCS.LEVEL, PCS.LEVEL, ...) as LEVEL,
    RCS.CODES_REQUESTED, PCS.CODES_PRINTED, ...
from 
    dbo.RequestedCodesStatistics RCS with(noexpand)
    full join dbo.PrintedCodesStatistics PCS with(noexpand) on PCS.LEVEL = RCS.LEVEL
    ...