添加当前年初至今/去年总计比较列

时间:2018-08-30 18:32:34

标签: sql sql-server totals

我们有两种记录类型:外部记录(acc)和内部记录(简称为记录)。

目前,我的查询返回的是(acc)个月的总计,以及从系统实施之日(开始)的(acc)个总计。

我们现在需要添加(累计)当前年初至今的总数(以“ /”正斜杠分隔),去年(累计)的总数。

内部记录的内容与查询中的[Last Month]和[Inception Counts of Records]相同。

添加(内部)当前年初至今的总数(以“ /”正斜杠分隔),去年(内部)的总数。

我认为我需要做的是添加更多子选择来获得所需的数字,但是我在理解该部分的结构以及随后沿已存在的列的使用方式方面遇到困难。

我包含的当前查询仅检索当前的每月总计,以及从两种记录类型(外部和内部)的开始日期起的总计。

我希望所有这些都是有道理的,因为我想寻求帮助,以便我完成此查询。任何对我的查询的示例或修改,或一些指导都将很棒。

查询-修改后的WHERE子句和AND条件,因为使用已过滤(在一定程度上)的样本数据-。

SELECT
    [RecordTypes].[Agency]                                                          [Agency]
,   [RecordTypes].[Record Type]                                                     [Record Type]
,   ISNULL([MonthlyRecords].[ACA], 0)                                               [ACA]
,   ISNULL([RecordTypes].[Inception Count ACA], 0)                                  [Inception Count ACA]
,   ISNULL([MonthlyRecords].[Count of Records], 0)                                  [Last Month]
,   [RecordTypes].[Inception Count of Records]                                      [Inception Count of Records]
,   ISNULL([RecordTypes].[Date of Last Record], '')                                 [Date of Last record]
,   ISNULL([RecordTypes].[Record Type Last Modified], '')                           [Date Record Type Last Modified]
,   IIF([RecordTypes].[ACA Enabled] IN ('VHAPP', 'VHSP', 'VHAI'), 'True', 'False')  [ACA Enabled]
FROM
    (SELECT
        CASE
            WHEN [R3].[R1_APP_TYPE_ALIAS] IN('Permitted Use Site Plan Review', 'Minor Alteration to Approved Conditional Use', 'Minor Alteration to Approved Planned Unit Development') THEN 'Zoning'
            WHEN [R3].[R1_APP_TYPE_ALIAS] IN ('Code Enforcement Mileage', 'Renewal Registration', 'Rental Property Emergency Contact', 'W&M License') THEN 'Building Inspection'
            WHEN [R3].[R1_PER_TYPE] IN ('Annexation', 'Condominium', 'Land Management', 'Land Use', 'Landmark', 'Subdivision') THEN 'Planning'          
            WHEN [R3].[R1_PER_GROUP]+'/'+[R3].[R1_PER_TYPE] = 'AMS/Equipment'               
                OR [R3].[R1_PER_GROUP]+'/'+[R3].[R1_PER_TYPE] = 'AMS/Vehicle'
                OR [R3].[R1_PER_GROUP]+'/'+[R3].[R1_PER_TYPE] = 'AMS/Facility'
                OR [R3].[R1_APP_TYPE_ALIAS] = 'As-Built Request'
                OR LEFT([R3].[R1_PER_TYPE], 11) = 'Engineering' THEN 'Engineering'
            WHEN [R3].[R1_APP_TYPE_ALIAS] = 'Edible Landscape Permit' THEN 'Mayor'                      
            WHEN [R3].[R1_PER_GROUP] = 'LICENSES' 
                AND [R3].[R1_PER_TYPE] = 'Building Inspection' THEN 'Building'
            WHEN [R3].[R1_PER_TYPE] = 'Traffic' THEN 'Traffic Engineering'
            WHEN LEFT([R3].[R1_PER_TYPE], '7') = 'Streets' THEN 'Streets'
            WHEN [R3].[R1_PER_GROUP] = 'AnimalServices' THEN 'Health'           
            ELSE [R3].[R1_PER_TYPE]
        END                             [Agency]
     ,  CASE
            WHEN [B1].[B1_PER_SUB_TYPE] NOT IN ('Landlord Registration', 'Foreclosure Registration')
                AND [R3].[R1_PER_TYPE] = 'Building Inspection'
                AND [R3].[R1_PER_GROUP] = 'Licenses' THEN 'License '+[R3].[R1_APP_TYPE_ALIAS]
            ELSE REPLACE([R3].[R1_APP_TYPE_ALIAS], '/NA', '')
        END                             [Record Type]
    ,   COUNT([B1].[B1_ALT_ID])         [Inception Count of Records]
    ,   COUNT([B1].[B1_ACCESS_BY_ACA])  [Inception Count ACA]
    ,   MAX([B1].[B1_FILE_DD])          [Date of Last Record]
    ,   MAX([R3].[REC_DATE])            [Record Type Last Modified]
    ,   MAX([R3].[R1_UDCODE3])          [ACA Enabled]
    FROM
        [R3APPTYP] [R3]
    LEFT OUTER JOIN [B1PERMIT] [B1]
        ON [R3].[SERV_PROV_CODE] =          [B1].[SERV_PROV_CODE]
            AND [R3].[R1_PER_GROUP] =       [B1].[B1_PER_GROUP]
            AND [R3].[R1_PER_TYPE] =        [B1].[B1_PER_TYPE]
            AND [R3].[R1_PER_SUB_TYPE] =    [B1].[B1_PER_SUB_TYPE]
            AND [R3].[R1_PER_CATEGORY] =    [B1].[B1_PER_CATEGORY]          
    WHERE 
        [R3].[SERV_PROV_CODE] = 'MISOULA'
        AND [R3].[REC_STATUS] = 'A'
    GROUP BY
        CASE
            WHEN [R3].[R1_APP_TYPE_ALIAS] IN('Permitted Use Site Plan Review', 'Minor Alteration to Approved Conditional Use', 'Minor Alteration to Approved Planned Unit Development') THEN 'Zoning'
            WHEN [R3].[R1_APP_TYPE_ALIAS] IN('Code Enforcement Mileage', 'Renewal Registration', 'Rental Property Emergency Contact', 'W&M License') THEN 'Building Inspection'
            WHEN [R3].[R1_PER_TYPE] IN('Annexation', 'Condominium', 'Land Management', 'Land Use', 'Landmark', 'Subdivision') THEN 'Planning'       
            WHEN [R3].[R1_PER_GROUP]+'/'+[R3].[R1_PER_TYPE] = 'AMS/Equipment'
                OR [R3].[R1_PER_GROUP]+'/'+[R3].[R1_PER_TYPE] = 'AMS/Vehicle'
                OR [R3].[R1_PER_GROUP]+'/'+[R3].[R1_PER_TYPE] = 'AMS/Facility'
                OR [R3].[R1_APP_TYPE_ALIAS] = 'As-Built Request'
                OR LEFT([R3].[R1_PER_TYPE], 11) = 'Engineering' THEN 'Engineering'
            WHEN [R3].[R1_APP_TYPE_ALIAS] = 'Edible Landscape Permit' THEN 'Mayor'      
            WHEN [R3].[R1_PER_GROUP] = 'LICENSES'
                AND [R3].[R1_PER_TYPE] = 'Building Inspection' THEN 'Building'
            WHEN [R3].[R1_PER_TYPE] = 'Traffic' THEN 'Traffic Engineering'
            WHEN LEFT([R3].[R1_PER_TYPE], '7') = 'Streets' THEN 'Streets'
            WHEN [R3].[R1_PER_GROUP] = 'AnimalServices' THEN 'Health'
            ELSE [R3].[R1_PER_TYPE]
        END
    ,   CASE
            WHEN [B1].[B1_PER_SUB_TYPE] NOT IN('Landlord Registration', 'Foreclosure Registration')
                AND [R3].[R1_PER_TYPE] = 'Building Inspection'
                AND [R3].[R1_PER_GROUP] = 'Licenses' THEN 'License '+[R3].[R1_APP_TYPE_ALIAS]
            ELSE REPLACE([R3].[R1_APP_TYPE_ALIAS], '/NA', '')
        END) 
            [RecordTypes]
LEFT OUTER JOIN
    (SELECT
        CASE
            WHEN [R3].[R1_APP_TYPE_ALIAS] IN('Permitted Use Site Plan Review', 'Minor Alteration to Approved Conditional Use', 'Minor Alteration to Approved Planned Unit Development') THEN 'Zoning'
            WHEN [R3].[R1_APP_TYPE_ALIAS] IN('Code Enforcement Mileage', 'Renewal Registration', 'Rental Property Emergency Contact', 'W&M License') THEN 'Building Inspection'         
            WHEN [R3].[R1_PER_TYPE] IN('Annexation', 'Condominium', 'Land Management', 'Land Use', 'Landmark', 'Subdivision') THEN 'Planning'           
            WHEN [R3].[R1_PER_GROUP]+'/'+[R3].[R1_PER_TYPE] = 'AMS/Equipment'               
                OR [R3].[R1_PER_GROUP]+'/'+[R3].[R1_PER_TYPE] = 'AMS/Vehicle'
                OR [R3].[R1_PER_GROUP]+'/'+[R3].[R1_PER_TYPE] = 'AMS/Facility'              
                OR [R3].[R1_APP_TYPE_ALIAS] = 'As-Built Request'
                OR LEFT([R3].[R1_PER_TYPE], 11) = 'Engineering' THEN 'Engineering'
            WHEN [R3].[R1_APP_TYPE_ALIAS] = 'Edible Landscape Permit' THEN 'Mayor'                               
            WHEN [R3].[R1_PER_GROUP] = 'LICENSES'
                AND [R3].[R1_PER_TYPE] = 'Building Inspection' THEN 'Building'
            WHEN [R3].[R1_PER_TYPE] = 'Traffic' THEN 'Traffic Engineering'
            WHEN LEFT([R3].[R1_PER_TYPE], '7') = 'Streets' THEN 'Streets'
            WHEN [R3].[R1_PER_GROUP] = 'AnimalServices' THEN 'Health'
            ELSE [R3].[R1_PER_TYPE]
        END [Agency]
     ,  CASE
            WHEN [B1].[B1_PER_SUB_TYPE] NOT IN ('Landlord Registration', 'Foreclosure Registration')
                AND [R3].[R1_PER_TYPE] = 'Building Inspection'
                AND [R3].[R1_PER_GROUP] = 'Licenses' THEN 'License '+[R3].[R1_APP_TYPE_ALIAS]
            ELSE REPLACE([R3].[R1_APP_TYPE_ALIAS], '/NA', '')
        END [Record Type]
     ,  COUNT([B1].[B1_ALT_ID]) [Count of Records]
     ,  COUNT([B1].[B1_ACCESS_BY_ACA]) [ACA]
     FROM
        [R3APPTYP] [R3]
     LEFT OUTER JOIN [B1PERMIT] [B1]
        ON [R3].[SERV_PROV_CODE] = [B1].[SERV_PROV_CODE]
            AND [R3].[R1_PER_GROUP] = [B1].[B1_PER_GROUP]
            AND [R3].[R1_PER_TYPE] = [B1].[B1_PER_TYPE]
            AND [R3].[R1_PER_SUB_TYPE] = [B1].[B1_PER_SUB_TYPE]
            AND [R3].[R1_PER_CATEGORY] = [B1].[B1_PER_CATEGORY]
            AND LEFT([B1_ALT_ID], 3) <> 'TMP'
            AND DATEPART([m], [B1].[B1_FILE_DD]) = DATEPART([m], DATEADD([m], -1, GETDATE()))
            AND DATEPART([yyyy], [B1].[B1_FILE_DD]) = DATEPART([yyyy], DATEADD([m], -1, GETDATE()))                   
     WHERE 
        [R3].[SERV_PROV_CODE] = 'MISOULA'
        AND [R3].[REC_STATUS] = 'A' 
     GROUP BY
        CASE
            WHEN [R3].[R1_APP_TYPE_ALIAS] IN('Permitted Use Site Plan Review', 'Minor Alteration to Approved Conditional Use', 'Minor Alteration to Approved Planned Unit Development') THEN 'Zoning'
            WHEN [R3].[R1_APP_TYPE_ALIAS] IN('Code Enforcement Mileage', 'Renewal Registration', 'Rental Property Emergency Contact', 'W&M License') THEN 'Building Inspection'         
            WHEN [R3].[R1_PER_TYPE] IN('Annexation', 'Condominium', 'Land Management', 'Land Use', 'Landmark', 'Subdivision') THEN 'Planning'           
            WHEN [R3].[R1_PER_GROUP]+'/'+[R3].[R1_PER_TYPE] = 'AMS/Equipment'
                OR [R3].[R1_PER_GROUP]+'/'+[R3].[R1_PER_TYPE] = 'AMS/Vehicle'
                OR [R3].[R1_PER_GROUP]+'/'+[R3].[R1_PER_TYPE] = 'AMS/Facility'
                OR [R3].[R1_APP_TYPE_ALIAS] = 'As-Built Request'
                OR LEFT([R3].[R1_PER_TYPE], 11) = 'Engineering' THEN 'Engineering'
            WHEN [R3].[R1_APP_TYPE_ALIAS] = 'Edible Landscape Permit' THEN 'Mayor'          
            WHEN [R3].[R1_PER_GROUP] = 'LICENSES'
                AND [R3].[R1_PER_TYPE] = 'Building Inspection' THEN 'Building'
            WHEN [R3].[R1_PER_TYPE] = 'Traffic' THEN 'Traffic Engineering'
            WHEN LEFT([R3].[R1_PER_TYPE], '7') = 'Streets' THEN 'Streets'
            WHEN [R3].[R1_PER_GROUP] = 'AnimalServices' THEN 'Health'
            ELSE [R3].[R1_PER_TYPE]
        END
     ,  CASE
            WHEN [B1].[B1_PER_SUB_TYPE] NOT IN('Landlord Registration', 'Foreclosure Registration')
                AND [R3].[R1_PER_TYPE] = 'Building Inspection'
                AND [R3].[R1_PER_GROUP] = 'Licenses' THEN 'License '+[R3].[R1_APP_TYPE_ALIAS]
            ELSE REPLACE([R3].[R1_APP_TYPE_ALIAS], '/NA', '')
        END) 
            [MonthlyRecords]     
    ON [RecordTypes].[Agency] = [MonthlyRecords].[Agency]   
        AND [RecordTypes].[Record Type] = [MonthlyRecords].[Record Type]
ORDER BY
    [RecordTypes].[Agency]
,   [RecordTypes].[Record Type];

表定义-B1PERMIT

CREATE TABLE [dbo].[B1PERMIT](
    [SERV_PROV_CODE] [varchar](15) NOT NULL,
    [B1_PER_GROUP] [varchar](30) NOT NULL,
    [B1_PER_TYPE] [varchar](30) NOT NULL,
    [B1_PER_SUB_TYPE] [varchar](30) NOT NULL,
    [B1_PER_CATEGORY] [varchar](30) NOT NULL,
    [B1_FILE_DD] [datetime] NULL,
    [B1_ALT_ID] [varchar](30) NULL,
    [B1_ACCESS_BY_ACA] [varchar](1) NULL
) ON [PRIMARY]

表定义-R3APPTYP

CREATE TABLE [dbo].[R3APPTYP](
    [SERV_PROV_CODE] [varchar](15) NOT NULL,
    [R1_PER_GROUP] [varchar](30) NOT NULL,
    [R1_PER_TYPE] [varchar](30) NOT NULL,
    [R1_PER_SUB_TYPE] [varchar](30) NOT NULL,
    [R1_PER_CATEGORY] [varchar](30) NOT NULL,
    [REC_DATE] [datetime] NOT NULL,
    [R1_UDCODE3] [varchar](12) NULL,
    [REC_STATUS] [varchar](1) NULL,
    [R1_APP_TYPE_ALIAS] [varchar](255) NULL
) ON [PRIMARY]

样本数据-B1PERMIT

MISSOULA    Permitting  Engineering Erosion Control NA  2018-07-16 00:00:00.000 ENG100-2017-04471   NULL
MISSOULA    Permitting  Engineering Sewer Plug  NA  2017-01-12 00:00:00.000 ENGSRP-2016-04904   NULL
MISSOULA    Permitting  Parks   DiscGolf    NA  2017-01-01 00:00:00.000 PRKDGP-2017-00001   Y
MISSOULA    Permitting  Parks   DiscGolf    NA  2017-01-01 00:00:00.000 PRKDGP-2017-00003   Y
MISSOULA    Permitting  Parks   DiscGolf    NA  2017-01-01 00:00:00.000 PRKDGP-2017-00004   Y
MISSOULA    Permitting  Parks   DiscGolf    NA  2017-01-01 00:00:00.000 PRKDGP-2017-00007   Y
MISSOULA    Permitting  Parks   DiscGolf    NA  2017-01-01 00:00:00.000 PRKDGP-2017-00009   Y
MISSOULA    Permitting  Parks   DiscGolf    NA  2017-01-01 00:00:00.000 PRKDGP-2017-00012   Y
MISSOULA    Permitting  Parks   DiscGolf    NA  2017-01-01 00:00:00.000 PRKDGP-2017-00014   Y
MISSOULA    Permitting  Parks   DiscGolf    NA  2017-01-02 00:00:00.000 PRKDGP-2017-00017   Y
MISSOULA    Permitting  Parks   DiscGolf    NA  2017-01-02 00:00:00.000 PRKDGP-2017-00021   Y
MISSOULA    Permitting  Parks   DiscGolf    NA  2017-01-02 00:00:00.000 PRKDGP-2017-00022   Y
MISSOULA    Permitting  Parks   DiscGolf    NA  2017-01-02 00:00:00.000 PRKDGP-2017-00024   Y
MISSOULA    Permitting  Parks   DiscGolf    NA  2017-01-02 00:00:00.000 PRKDGP-2017-00028   Y
MISSOULA    Permitting  Parks   DiscGolf    NA  2017-01-02 00:00:00.000 PRKDGP-2017-00030   Y
MISSOULA    Permitting  Parks   DiscGolf    NA  2017-01-03 00:00:00.000 PRKDGP-2017-00032   Y
MISSOULA    Permitting  Parks   DiscGolf    NA  2018-01-01 00:00:00.000 PRKDGP-2018-00015   Y
MISSOULA    Permitting  Parks   DiscGolf    NA  2018-01-02 00:00:00.000 PRKDGP-2018-00019   Y
MISSOULA    Permitting  Parks   DiscGolf    NA  2018-01-04 00:00:00.000 PRKDGP-2018-00156   Y
MISSOULA    Permitting  Parks   DiscGolf    NA  2018-01-05 00:00:00.000 PRKDGP-2018-00173   NULL
MISSOULA    Permitting  Parks   DiscGolf    NA  2018-01-05 00:00:00.000 PRKDGP-2018-00202   NULL
MISSOULA    Permitting  Parks   DiscGolf    NA  2018-01-06 00:00:00.000 PRKDGP-2018-00224   Y
MISSOULA    Permitting  Parks   DiscGolf    NA  2018-01-07 00:00:00.000 PRKDGP-2018-00235   Y
MISSOULA    Permitting  Parks   DiscGolf    NA  2018-01-07 00:00:00.000 PRKDGP-2018-00238   Y
MISSOULA    Permitting  Parks   DiscGolf    NA  2018-01-07 00:00:00.000 PRKDGP-2018-00241   Y
MISSOULA    Permitting  Parks   DiscGolf    NA  2018-01-07 00:00:00.000 PRKDGP-2018-00244   Y
MISSOULA    Permitting  Parks   DiscGolf    NA  2018-01-08 00:00:00.000 PRKDGP-2018-00257   NULL
MISSOULA    Permitting  Parks   DiscGolf    NA  2018-01-08 00:00:00.000 PRKDGP-2018-00258   NULL
MISSOULA    Permitting  Parks   DiscGolf    NA  2018-01-08 00:00:00.000 PRKDGP-2018-00259   NULL
MISSOULA    Permitting  Parks   DiscGolf    NA  2018-01-08 00:00:00.000 PRKDGP-2018-00260   NULL

样本数据-R3APPTYP

    MISSOULA    Licenses    Civil Rights    AA Plan Exempt  2017-04-26 15:56:27.303 VHAPP   A   AA Plan Exemption (14 or fewer)
MISSOULA    Licenses    Civil Rights    AA Plan Exempt  2017-04-26 15:56:27.303 VHAPP   A   AA Plan Exemption (14 or fewer)
MISSOULA    Licenses    Civil Rights    AA Plan Exempt  2017-04-26 15:56:27.303 VHAPP   A   AA Plan Exemption (14 or fewer)
MISSOULA    Licenses    Civil Rights    AA Plan Exempt  2017-04-26 15:56:27.303 VHAPP   A   AA Plan Exemption (14 or fewer)
MISSOULA    Licenses    Civil Rights    AA Plan Exempt  2017-04-26 15:56:27.303 VHAPP   A   AA Plan Exemption (14 or fewer)
MISSOULA    Licenses    Civil Rights    AA Plan Exempt  2017-04-26 15:56:27.303 VHAPP   A   AA Plan Exemption (14 or fewer)
MISSOULA    Licenses    Civil Rights    AA Plan Exempt  2017-04-26 15:56:27.303 VHAPP   A   AA Plan Exemption (14 or fewer)
MISSOULA    Licenses    Civil Rights    AA Plan Exempt  2017-04-26 15:56:27.303 VHAPP   A   AA Plan Exemption (14 or fewer)
MISSOULA    Licenses    Civil Rights    AA Plan Exempt  2017-04-26 15:56:27.303 VHAPP   A   AA Plan Exemption (14 or fewer)
MISSOULA    Licenses    Civil Rights    AA Plan Exempt  2017-04-26 15:56:27.303 VHAPP   A   AA Plan Exemption (14 or fewer)
MISSOULA    Licenses    Civil Rights    AA Plan Exempt  2017-04-26 15:56:27.303 VHAPP   A   AA Plan Exemption (14 or fewer)
MISSOULA    Licenses    Civil Rights    AA Plan Exempt  2017-04-26 15:56:27.303 VHAPP   A   AA Plan Exemption (14 or fewer)
MISSOULA    Licenses    Civil Rights    AA Plan Exempt  2017-04-26 15:56:27.303 VHAPP   A   AA Plan Exemption (14 or fewer)
MISSOULA    Licenses    Civil Rights    AA Plan Exempt  2017-04-26 15:56:27.303 VHAPP   A   AA Plan Exemption (14 or fewer)
MISSOULA    Licenses    Civil Rights    AA Plan Exempt  2017-04-26 15:56:27.303 VHAPP   A   AA Plan Exemption (14 or fewer)
MISSOULA    _Enforcement    Building Inspection Housing Rental Prop Emergency Contacts  2018-02-26 16:36:45.987 NA  A   _Enforcement/Building Inspection/Housing/Rental Prop Emergency Contacts
MISSOULA    Land    Landmark    NA  NA  2018-04-18 09:13:50.787 VHAPP   A   Certificate of Appropriateness Application
MISSOULA    Land    Landmark    NA  NA  2018-04-18 09:13:50.787 VHAPP   A   Certificate of Appropriateness Application
MISSOULA    Land    Landmark    NA  NA  2018-04-18 09:13:50.787 VHAPP   A   Certificate of Appropriateness Application
MISSOULA    Land    Landmark    NA  NA  2018-04-18 09:13:50.787 VHAPP   A   Certificate of Appropriateness Application
MISSOULA    Land    Landmark    NA  NA  2018-04-18 09:13:50.787 VHAPP   A   Certificate of Appropriateness Application
MISSOULA    Land    Landmark    NA  NA  2018-04-18 09:13:50.787 VHAPP   A   Certificate of Appropriateness Application
MISSOULA    Land    Landmark    NA  NA  2018-04-18 09:13:50.787 VHAPP   A   Certificate of Appropriateness Application
MISSOULA    Land    Landmark    NA  NA  2018-04-18 09:13:50.787 VHAPP   A   Certificate of Appropriateness Application
MISSOULA    Land    Landmark    NA  NA  2018-04-18 09:13:50.787 VHAPP   A   Certificate of Appropriateness Application
MISSOULA    Land    Landmark    NA  NA  2018-04-18 09:13:50.787 VHAPP   A   Certificate of Appropriateness Application
MISSOULA    Land    Landmark    NA  NA  2018-04-18 09:13:50.787 VHAPP   A   Certificate of Appropriateness Application
MISSOULA    Land    Landmark    NA  NA  2018-04-18 09:13:50.787 VHAPP   A   Certificate of Appropriateness Application
MISSOULA    Land    Landmark    NA  NA  2018-04-18 09:13:50.787 VHAPP   A   Certificate of Appropriateness Application
MISSOULA    Land    Landmark    NA  NA  2018-04-18 09:13:50.787 VHAPP   A   Certificate of Appropriateness Application

1 个答案:

答案 0 :(得分:1)

我认为您可以这样做(没有多个子查询):

WITH
  BaseData ([Agency], [Record Type], [B1_ALT_ID], [B1_ACCESS_BY_ACA], [B1_FILE_DD], [REC_DATE], [R1_UDCODE3], ThisYear, LastYear, LastMonth) AS (
    SELECT
        CASE
            WHEN [R3].[R1_APP_TYPE_ALIAS] IN('Permitted Use Site Plan Review', 'Minor Alteration to Approved Conditional Use', 'Minor Alteration to Approved Planned Unit Development') THEN 'Zoning'
            WHEN [R3].[R1_APP_TYPE_ALIAS] IN ('Code Enforcement Mileage', 'Renewal Registration', 'Rental Property Emergency Contact', 'W&M License') THEN 'Building Inspection'
            WHEN [R3].[R1_PER_TYPE] IN ('Annexation', 'Condominium', 'Land Management', 'Land Use', 'Landmark', 'Subdivision') THEN 'Planning'          
            WHEN [R3].[R1_PER_GROUP]+'/'+[R3].[R1_PER_TYPE] = 'AMS/Equipment'               
              OR [R3].[R1_PER_GROUP]+'/'+[R3].[R1_PER_TYPE] = 'AMS/Vehicle'
              OR [R3].[R1_PER_GROUP]+'/'+[R3].[R1_PER_TYPE] = 'AMS/Facility'
              OR [R3].[R1_APP_TYPE_ALIAS] = 'As-Built Request'
              OR LEFT([R3].[R1_PER_TYPE], 11) = 'Engineering' THEN 'Engineering'
            WHEN [R3].[R1_APP_TYPE_ALIAS] = 'Edible Landscape Permit' THEN 'Mayor'                      
            WHEN [R3].[R1_PER_GROUP] = 'LICENSES' 
                AND [R3].[R1_PER_TYPE] = 'Building Inspection' THEN 'Building'
            WHEN [R3].[R1_PER_TYPE] = 'Traffic' THEN 'Traffic Engineering'
            WHEN LEFT([R3].[R1_PER_TYPE], '7') = 'Streets' THEN 'Streets'
            WHEN [R3].[R1_PER_GROUP] = 'AnimalServices' THEN 'Health'           
            ELSE [R3].[R1_PER_TYPE]
        END [Agency]
     ,  CASE
            WHEN [B1].[B1_PER_SUB_TYPE] NOT IN ('Landlord Registration', 'Foreclosure Registration')
             AND [R3].[R1_PER_TYPE] = 'Building Inspection'
             AND [R3].[R1_PER_GROUP] = 'Licenses' THEN 'License '+[R3].[R1_APP_TYPE_ALIAS]
            ELSE REPLACE([R3].[R1_APP_TYPE_ALIAS], '/NA', '')
        END [Record Type]
    ,   [B1].[B1_ALT_ID]
    ,   [B1].[B1_ACCESS_BY_ACA]
    ,   [B1].[B1_FILE_DD]
    ,   [R3].[REC_DATE]
    ,   [R3].[R1_UDCODE3]
    ,   CASE
          WHEN [B1].[B1_FILE_DD] >= DATEADD(year, DATEDIFF(year, 0, GETDATE()), 0)
          THEN 1 ELSE 0
        END
    ,   CASE
          WHEN [B1].[B1_FILE_DD] >= DATEADD(year, DATEDIFF(year, 0, GETDATE())-1, 0)
           AND [B1].[B1_FILE_DD] < DATEADD(year, DATEDIFF(year, 0, GETDATE()), 0)
          THEN 1 ELSE 0
        END
    , CASE
        WHEN [B1].[B1_FILE_DD] >= DATEADD(m, DATEDIFF(m, 0, GETDATE())-1, 0)
         AND [B1].[B1_FILE_DD] < DATEADD(m, DATEDIFF(m, 0, GETDATE()), 0)
        THEN 1 ELSE 0
      END

    FROM [R3APPTYP] [R3]
      LEFT OUTER JOIN [B1PERMIT] [B1]
        ON [R3].[SERV_PROV_CODE] =        [B1].[SERV_PROV_CODE]
          AND [R3].[R1_PER_GROUP] =       [B1].[B1_PER_GROUP]
          AND [R3].[R1_PER_TYPE] =        [B1].[B1_PER_TYPE]
          AND [R3].[R1_PER_SUB_TYPE] =    [B1].[B1_PER_SUB_TYPE]
          AND [R3].[R1_PER_CATEGORY] =    [B1].[B1_PER_CATEGORY]          

    WHERE [R3].[SERV_PROV_CODE] = 'MISOULA'
      AND [R3].[REC_STATUS] = 'A'
  )

SELECT
    [Agency]
,   [Record Type]
,   ISNULL(COUNT(CASE LastMonth WHEN 1 THEN [B1_ACCESS_BY_ACA] ELSE NULL END), 0)   [ACA]
,   ISNULL(COUNT(CASE LastYear WHEN 1 THEN [B1_ACCESS_BY_ACA] ELSE NULL END), 0)    [Last Year ACA]
,   ISNULL(COUNT(CASE ThisYear WHEN 1 THEN [B1_ACCESS_BY_ACA] ELSE NULL END), 0)    [YTD ACA]
,   ISNULL(COUNT([B1_ACCESS_BY_ACA]), 0)                                            [Inception Count ACA]
,   ISNULL(COUNT(CASE LastMonth WHEN 1 THEN [B1_ALT_ID] ELSE NULL END), 0)          [Last Month]
,   ISNULL(COUNT(CASE LastYear WHEN 1 THEN [B1_ALT_ID] ELSE NULL END), 0)           [Last Year Count of Records]
,   ISNULL(COUNT(CASE ThisYear WHEN 1 THEN [B1_ALT_ID] ELSE NULL END), 0)           [YTD Count of Records]
,   ISNULL(COUNT([B1_ALT_ID]), 0)                                                   [Inception Count of Records]
,   ISNULL(MAX([B1_FILE_DD]), '')                                                   [Date of Last record]
,   ISNULL(MAX([REC_DATE]), '')                                                     [Date Record Type Last Modified]
,   IIF(MAX([R1_UDCODE3]) IN ('VHAPP', 'VHSP', 'VHAI'), 'True', 'False')            [ACA Enabled]

FROM BaseData
GROUP BY [Agency],  [Record Type]
ORDER BY [Agency],  [Record Type];

如果您确实需要“上个月”部分的TMP条件,则可以轻松地将其包含在相应的CASE表达式中。

合并新字段(已添加)

要合并新字段并使它们成为字符串,为简单起见,我建议使用另一个CTE。在第一个CTE(BaseData)的右括号后面添加逗号,并用第二个CTE和最终的SELECT语句替换SELECT语句,如下所示:

  Results AS (
    SELECT
        [Agency]
    ,   [Record Type]
    ,   ISNULL(COUNT(CASE LastMonth WHEN 1 THEN [B1_ACCESS_BY_ACA] ELSE NULL END), 0)   [ACA]
    ,   ISNULL(COUNT(CASE LastYear WHEN 1 THEN [B1_ACCESS_BY_ACA] ELSE NULL END), 0)    [Last Year ACA]
    ,   ISNULL(COUNT(CASE ThisYear WHEN 1 THEN [B1_ACCESS_BY_ACA] ELSE NULL END), 0)    [YTD ACA]
    ,   ISNULL(COUNT([B1_ACCESS_BY_ACA]), 0)                                            [Inception Count ACA]
    ,   ISNULL(COUNT(CASE LastMonth WHEN 1 THEN [B1_ALT_ID] ELSE NULL END), 0)          [Last Month]
    ,   ISNULL(COUNT(CASE LastYear WHEN 1 THEN [B1_ALT_ID] ELSE NULL END), 0)           [Last Year Count of Records]
    ,   ISNULL(COUNT(CASE ThisYear WHEN 1 THEN [B1_ALT_ID] ELSE NULL END), 0)           [YTD Count of Records]
    ,   ISNULL(COUNT([B1_ALT_ID]), 0)                                                   [Inception Count of Records]
    ,   ISNULL(MAX([B1_FILE_DD]), '')                                                   [Date of Last record]
    ,   ISNULL(MAX([REC_DATE]), '')                                                     [Date Record Type Last Modified]
    ,   IIF(MAX([R1_UDCODE3]) IN ('VHAPP', 'VHSP', 'VHAI'), 'True', 'False')            [ACA Enabled]

    FROM BaseData
    GROUP BY [Agency],  [Record Type]
  )

SELECT
     [Agency]
  ,  [Record Type]
  ,  [ACA]
  ,  [Inception Count ACA]
  ,  CASE [YTD ACA]
       WHEN 0 THEN 'NA' ELSE CONVERT(nvarchar(50), [YTD ACA])
     END 
     + N'/' + 
     CASE [Last Year ACA]
       WHEN 0 THEN 'NA' ELSE CONVERT(nvarchar(50), [Last Year ACA])
     END [YTD/LY ACA]
  ,  [Last Month]
  ,  [Inception Count of Records]
  ,  CASE [YTD Count of Records]
       WHEN 0 THEN 'NA' ELSE CONVERT(nvarchar(50), [YTD Count of Records])
     END 
     + N'/' + 
     CASE [Last Year Count of Records]
       WHEN 0 THEN 'NA' ELSE CONVERT(nvarchar(50), [Last Year Count of Records])
     END [YTD/LY of Records]
  ,  [Date of Last record]
  ,  [Date Record Type Last Modified]
  ,  [ACA Enabled]

FROM Results
ORDER BY [Agency],  [Record Type];