一个sql查询中的多个Count语句

时间:2014-05-16 17:16:21

标签: sql performance count

表A由列LIKE区域,里程和项目编号组成 表B由项目编号,文件夹名称和文档名称组成 我需要一个结果集,我需要在每个项目下给出每个文档的计数以及它的区域和里程详细信息

我的查询如下:

SELECT PN.[ProjectNumber] AS 'Project ID' ,
       PN.[Number2]       AS 'Mileage'    ,
       PN.[Region]        AS  'Region'    ,
       ( SELECT COUNT(*) WHERE PN.[ProjectNumber] in ( SELECT [Project Site] COLLATE Latin1_General_CI_AI FROM [WSS_Content].[dbo].[_TPG_DocumentReport] DR where [Project Site] <> '_PSEP' aND DR.Stage LIKE '%Stage 1' And LeafName like '%'+ '[_]'+'162'+'[_]'+'%' ) ) AS 'WORKORDER' ,
       ( SELECT COUNT(*) WHERE PN.[ProjectNumber] in ( SELECT [Project Site] COLLATE Latin1_General_CI_AI FROM [WSS_Content].[dbo].[_TPG_DocumentReport] DR where [Project Site] <> '_PSEP' aND DR.Stage LIKE '%Stage 1' And LeafName like '%'+ '[_]'+'047'+'[_]'+'%' ) ) AS 'HighLevel' ,
       ( SELECT COUNT(*) WHERE PN.[ProjectNumber] in ( SELECT [Project Site] COLLATE Latin1_General_CI_AI FROM [WSS_Content].[dbo].[_TPG_DocumentReport] DR where [Project Site] <> '_PSEP' aND DR.Stage LIKE '%Stage 1' And LeafName like '%'+ '[_]'+'054'+'[_]'+'%' ) ) AS 'Study'     ,
       ( SELECT COUNT(*) WHERE PN.[ProjectNumber] in ( SELECT [Project Site] COLLATE Latin1_General_CI_AI FROM [WSS_Content].[dbo].[_TPG_DocumentReport] DR where [Project Site] <> '_PSEP' aND DR.Stage LIKE '%Stage 1' And LeafName like '%'+ '[_]'+'138'+'[_]'+'%' ) ) AS 'Decision'  ,
       ( SELECT COUNT(*) WHERE PN.[ProjectNumber] in ( SELECT [Project Site] COLLATE Latin1_General_CI_AI FROM [WSS_Content].[dbo].[_TPG_DocumentReport] DR where [Project Site] <> '_PSEP' aND DR.Stage LIKE '%Stage 1' And LeafName like '%'+ '[_]'+'213'+'[_]'+'%' ) ) AS 'Record'    ,
       ( SELECT COUNT(*) WHERE PN.[ProjectNumber] in ( SELECT [Project Site] COLLATE Latin1_General_CI_AI FROM [WSS_Content].[dbo].[_TPG_DocumentReport] DR where [Project Site] <> '_PSEP' aND DR.Stage LIKE '%Stage 1' And LeafName like '%'+ '[_]'+'164'+'[_]'+'%' ) ) AS 'Work'      ,
       ( SELECT COUNT(*) WHERE PN.[ProjectNumber] in ( SELECT [Project Site] COLLATE Latin1_General_CI_AI FROM [WSS_Content].[dbo].[_TPG_DocumentReport] DR where [Project Site] <> '_PSEP' aND DR.Stage LIKE '%Stage 1' And LeafName like '%'+ '[_]'+'052'+'[_]'+'%' ) ) AS 'Attribute'  
FROM [Projectserver_Reporting_TPG].[dbo].[_TPG_ProjectInformation] PN

我得到了预期的结果集但不确定如何更好地使查询具有更少的行数和更好的性能。这是我的结果集:

Project ID          Mileage Region          WORKORDER HighLevel Study Decision Record Work Attribute
------------------- ------- --------------- --------- --------- ----- -------- ------ ---- ---------
1024                NULL    Transmission        0         0       0       0       0     0      0
41-05-A             NULL    Inland              0         0       0       0       0     0      0
35-351              NULL    Northern            0         0       0       0       0     0      0
404                 9.00    Transmission        1         1       1       1       1     1      1
36-6588             NULL    Northern            0         0       0       0       0     0      0
Project Excellence  NULL    NULL                0         0       0       0       0     0      0
49-28               2.39    SDG&E               1         1       1       1       1     1      1
37-18-K             2.00    Pacific Coast       1         1       1       1       0     1      1
42-57               NULL    Orange Coast        0         0       0       0       0     0      0
1003LT2             NULL    Transmission        0         0       0       0       0     0      0
247 ID403-T1        NULL    Transmission        0         0       0       0       0     0      0

1 个答案:

答案 0 :(得分:0)

我设置了一些表来测试它,使用虚假和演示数据的混合:

CREATE TABLE [_TPG_ProjectInformation] (
      ProjectNumber VARCHAR(20)
    , Number2 NUMERIC(4,2)
    , Region VARCHAR(15)
);

INSERT INTO [_TPG_ProjectInformation] (ProjectNumber,Number2,Region) VALUES ('1024',NULL,'Transmission');
INSERT INTO [_TPG_ProjectInformation] (ProjectNumber,Number2,Region) VALUES ('41-05-A',NULL,'Inland');
INSERT INTO [_TPG_ProjectInformation] (ProjectNumber,Number2,Region) VALUES ('35-351',NULL,'Northern');
INSERT INTO [_TPG_ProjectInformation] (ProjectNumber,Number2,Region) VALUES ('404',9.00,'Transmission');
INSERT INTO [_TPG_ProjectInformation] (ProjectNumber,Number2,Region) VALUES ('36-6588',NULL,'Northern');
INSERT INTO [_TPG_ProjectInformation] (ProjectNumber,Number2,Region) VALUES ('Project Excellence',NULL,'NULL');
INSERT INTO [_TPG_ProjectInformation] (ProjectNumber,Number2,Region) VALUES ('49-28',2.39,'SDG&E');
INSERT INTO [_TPG_ProjectInformation] (ProjectNumber,Number2,Region) VALUES ('37-18-K',2.00,'Pacific Coast');
INSERT INTO [_TPG_ProjectInformation] (ProjectNumber,Number2,Region) VALUES ('42-57',NULL,'Orange Coast');
INSERT INTO [_TPG_ProjectInformation] (ProjectNumber,Number2,Region) VALUES ('1003LT2',NULL,'Transmission');
INSERT INTO [_TPG_ProjectInformation] (ProjectNumber,Number2,Region) VALUES ('247 ID403-T1',NULL,'Transmission');

CREATE TABLE [_TPG_DocumentReport] (
      [Project Site] VARCHAR(20)
    , [Stage] VARCHAR(20)
    , [LeafName] VARCHAR(20)
);

INSERT INTO [_TPG_DocumentReport] ([Project Site],[Stage],[LeafName]) VALUES ('404','Some Stage 1','This_Code_162_B');
INSERT INTO [_TPG_DocumentReport] ([Project Site],[Stage],[LeafName]) VALUES ('404','This Stage 1','That_Code_047_R');
INSERT INTO [_TPG_DocumentReport] ([Project Site],[Stage],[LeafName]) VALUES ('404','That Stage 1','Some_Code_054_E');
INSERT INTO [_TPG_DocumentReport] ([Project Site],[Stage],[LeafName]) VALUES ('404','Miss Stage 1','This_Code_138_C');
INSERT INTO [_TPG_DocumentReport] ([Project Site],[Stage],[LeafName]) VALUES ('404','Wood Stage 1','In_Code_213_E');
INSERT INTO [_TPG_DocumentReport] ([Project Site],[Stage],[LeafName]) VALUES ('404','Step Stage 1','Out_Code_164_C');
INSERT INTO [_TPG_DocumentReport] ([Project Site],[Stage],[LeafName]) VALUES ('404','Done Stage 1','Build_Code_052_C');
INSERT INTO [_TPG_DocumentReport] ([Project Site],[Stage],[LeafName]) VALUES ('49-28','Some Stage 1','This_Code_162_B');
INSERT INTO [_TPG_DocumentReport] ([Project Site],[Stage],[LeafName]) VALUES ('49-28','This Stage 1','That_Code_047_R');
INSERT INTO [_TPG_DocumentReport] ([Project Site],[Stage],[LeafName]) VALUES ('49-28','That Stage 1','Some_Code_054_E');
INSERT INTO [_TPG_DocumentReport] ([Project Site],[Stage],[LeafName]) VALUES ('49-28','Miss Stage 1','This_Code_138_C');
INSERT INTO [_TPG_DocumentReport] ([Project Site],[Stage],[LeafName]) VALUES ('49-28','Wood Stage 1','In_Code_213_E');
INSERT INTO [_TPG_DocumentReport] ([Project Site],[Stage],[LeafName]) VALUES ('49-28','Step Stage 1','Out_Code_164_C');
INSERT INTO [_TPG_DocumentReport] ([Project Site],[Stage],[LeafName]) VALUES ('49-28','Done Stage 1','Build_Code_052_C');
INSERT INTO [_TPG_DocumentReport] ([Project Site],[Stage],[LeafName]) VALUES ('37-18-K','Some Stage 1','This_Code_162_B');
INSERT INTO [_TPG_DocumentReport] ([Project Site],[Stage],[LeafName]) VALUES ('37-18-K','This Stage 1','That_Code_047_R');
INSERT INTO [_TPG_DocumentReport] ([Project Site],[Stage],[LeafName]) VALUES ('37-18-K','That Stage 1','Some_Code_054_E');
INSERT INTO [_TPG_DocumentReport] ([Project Site],[Stage],[LeafName]) VALUES ('37-18-K','Miss Stage 1','This_Code_138_C');
INSERT INTO [_TPG_DocumentReport] ([Project Site],[Stage],[LeafName]) VALUES ('37-18-K','Step Stage 1','Out_Code_164_C');
INSERT INTO [_TPG_DocumentReport] ([Project Site],[Stage],[LeafName]) VALUES ('37-18-K','Done Stage 1','Build_Code_052_C');

然后我把它变成了一个PIVOT查询来减少表扫描的数量(虽然在你的情况下可能是索引搜索):

SELECT
      [Project ID]
    , [Mileage]
    , [Region]
    , [Stage]
    , ISNULL([162],0) AS 'WORKORDER'
    , ISNULL([047],0) AS 'HighLevel'
    , ISNULL([054],0) AS 'Study'    
    , ISNULL([138],0) AS 'Decision' 
    , ISNULL([213],0) AS 'Record'   
    , ISNULL([164],0) AS 'Work'     
    , ISNULL([052],0) AS 'Attribute'
FROM (
    SELECT
        PN.[ProjectNumber] AS 'Project ID' ,
        PN.[Number2]       AS 'Mileage'    ,
        PN.[Region]        AS 'Region'     ,
        DR.[Stage]         AS 'Stage'      ,
        DR.[LeafCode]      AS 'LeafCode'
    FROM [_TPG_ProjectInformation] PN
    LEFT JOIN (
        SELECT [Project Site], [Stage], substring(leafname,patindex('%_[0-9][0-9][0-9]_%',leafname)+1,3) AS LeafCode
        FROM [_TPG_DocumentReport]
        ) DR ON PN.[ProjectNumber] = DR.[Project Site] COLLATE Latin1_General_CI_AI
    ) Z
    --Note, no further specifying of [Project Site] needed, thus no filter for '_PSEP'.
        PIVOT (
            COUNT(LeafCode)
            FOR LeafCode IN
                ([162],[047],[054],[138],[213],[164],[052])
        ) P

而不是[_TPG_DocumentReport]上的7个表扫描,计划只得到1.虽然在查询计划中计算成本更高,但总体来说这个成本只有一半。我希望它有所帮助。

编辑:已更新,以包含每条评论的阶段。