Performance 需要一些关于从相当复杂的源编写大型报告查询的最佳方法的建议吗

Performance 需要一些关于从相当复杂的源编写大型报告查询的最佳方法的建议吗,performance,views,multiple-databases,cross-join,Performance,Views,Multiple Databases,Cross Join,在搜索其他答案以查看是否已经提出了这个特定的问题(并且已经提出了其他风格的问题——只是没有涵盖我需要询问的所有问题)之后,我想提出以下场景,并就创建此报告查询的最有效方法征求建议。这是一篇冗长的帖子,不幸的是,我不允许发布任何T-SQL代码——我的雇主明确禁止这样做 这些年来我学到的一件事是,有更多的人知道的比我多得多——因此,在搜索并没有找到你需要的东西后,问问别人:) 所有表都有主键,如果有表链接到子表,则有外键。数据库和报表服务器是SQL 2008 R2。该服务器运行128GB RAM,是

在搜索其他答案以查看是否已经提出了这个特定的问题(并且已经提出了其他风格的问题——只是没有涵盖我需要询问的所有问题)之后,我想提出以下场景,并就创建此报告查询的最有效方法征求建议。这是一篇冗长的帖子,不幸的是,我不允许发布任何T-SQL代码——我的雇主明确禁止这样做

这些年来我学到的一件事是,有更多的人知道的比我多得多——因此,在搜索并没有找到你需要的东西后,问问别人:)

所有表都有主键,如果有表链接到子表,则有外键。数据库和报表服务器是SQL 2008 R2。该服务器运行128GB RAM,是一个4CPU四核超线程beastie

首先,我有一组包含位置、部分和区域的表。位置通过映射表链接到横断面-有多个位置链接到横断面,横断面通过映射表链接到区域,还有多个横断面链接到区域。这些表都位于数据库“A”中。我在一个单独的数据库“B”中创建了一个视图,它表示一个快速执行的漂亮结果集中的区域-部分-位置链接。我称之为LocationSectionArea视图

在数据库“B”中,数千名用户正在输入关于其工作的各种类别的每日统计数据。目前共有91个类别。每个类别将包含从3个到25个不等的子类别。随着通过管理界面添加或删除更多类别,类别和子类别的数量可以随时更改。我创建了一个表示这些链接的视图,并返回一个名为CategorySubCategoryFields视图的结果集

为每个类别/子类别链接收集的信息各不相同-有些需要8个不同的字段,有些需要3个,有些只需要1个。这些字段链接到相应的子类别。这同样包含在数据库“B”中

我已经构建了一个视图,可以很好地将这些数据汇集到这个数据库中,最终得到一个大的结果矩阵,以一种非常容易报告的方式呈现数据,并且视图的执行速度到目前为止似乎是可以接受的。我称之为UserStatsView。这包含在数据库“B”中

现在-输入数据的用户被分配到前面提到的位置之一。结果需要显示区段内的所有位置,区域内的所有区段,以及每个位置显示该类别的每个类别和子类别。然后需要将其链接到UserStatsView中的结果,这样您最终会得到一个相当大的结果矩阵,其中有许多0,其中没有结果,但它们是为报告目的而创建的

例如,报告必须允许用户选择一个部分,然后生成该部分中包含的所有位置的报告,以及用户为该位置输入的结果的计数-对于存在的每个类别子类别组合。它必须显示该部分和所有类别子类别的所有位置,无论是否有用户输入的实际结果。因此,在一些报告中可能有很多0

为此,我创建了一个结果视图,它将LocationSectionArea与CategorySubCategoryFields视图交叉连接。这创建了我想要的基本矩阵。然后,我将此矩阵左键连接到UserStatsView的结果,连接LocationId、CategoryId和SubCategoryId以插入用户对位置的结果。这些都是在数据库“B”中创建的

现在,只要在最终where子句中提供足够的过滤器以减少记录数量,这一切都可以正常工作。正如你可以想象的那样,如果他们试图运行一个结果集更大的报告,它开始变得非常缓慢。(即,覆盖所有地点6个月的日期范围超过30秒)

我的部分问题是,我相信位置信息是从数据库“A”链接进来的,构建的视图包含在数据库“B”中,以及此报告所需的所有其他信息的所有视图表和记录

另一部分是我相信创建的非常大和复杂的交叉连接,然后链接到用户结果的视图。交叉联接是一个笛卡尔结果,在与UserStatsView结果集联接之前没有索引或相关数据

我认为第三个问题是,数据正在被“创造”来填补所有空白,以便产生结果结构,然后将其输入SSR。我已经看到表假脱机和散列连接占用了大量的执行时间,以及从数据库中查找位置、节和区域。这些都是通过查询执行计划显示的

我想问的是,根据上述标准,是否有人知道是否有更好的方法生成此结果集。我是否完全错过了一个非常简单和快速的方法,这是很有可能的:)。如果有人能建议我应该研究什么,我会很高兴地去做——只是不确定在这个阶段应该研究什么

干杯

更新:以下是定义两个数据库的表结构,。很明显,在真实的表格中有更多的内容,但这是它们如何结合在一起的关键。请原谅这么大的帖子-我已经删除了除了关键字段和一些数据字段以外的所有内容,以便您可以看到表的结构。表名和字段名已更改为通用名称
**Location Table**
LocationId BIGINT PK
LocationName Varchar(100)


**Section Table**
SectionId Bigint PK
SectionName varchar(100)



**Area Table**
AreaId Bigint PK
AreaName varchar(100)



**LocationSectionMap Table**
LocSecId Bigint PK
LocationId Bigint FK Index to Location Table
SectionId BigInt FK Index to Section Table




**SectionAreaMap Table**
SecAreaId Bigint PK 
SectionId Bigint FK Index to Section Table
AreaId Bigint FK Index to Area Table
**Categories Table** 
CategoryId Bigint PK
categoryName varchar(100)



**SubCategories Table**
SubCategoryId Bigint
CategoryId Bigint FK to Categories Table 
SubCategoryType Int
FieldTypeId Int (1, 2, 3 or 4)


**UserStats Table**
UserStatId bigint PK
UserId Bigint
StartDate DateTime
EndDate DateTime
LocationId Bigint --> this is the location ID in Location table in database A
SectionId Bigint --> this is the Section ID in Section Table in database A
AreaId Bigint --> this is the Area ID of the Area Table in database A



**FieldType1 Table**
FieldType1Id bigint PK
UserStatId  Bigint FK to UserStats Table
SubCategoryId Bigint FK to Subcategories Table
Value1 int
Value2 int





**FieldType2 Table**
FieldType2Id bigint PK
UserStatId  Bigint FK to UserStats Table
SubCategoryId Bigint FK to Subcategories Table
Value1 int 
Value2 int 
Value4 int
Value5 int
Value6 int
Value7 int
Value8 int
Value9 int
Value10 int


**FieldType3 Table**
FieldType3Id bigint PK
UserStatId  Bigint FK to UserStats Table
SubCategoryId Bigint FK to Subcategories Table
Value1 int
Value2 int
Value11 int
Value12 int
Value13 int
Value14 int
Value15 int
Value16 int


**FieldType4 Table**
FieldType4Id bigint PK
UserStatId  Bigint FK to UserStats Table
SubCategoryId Bigint FK to Subcategories Table
CombinedValue1And2 int



**SubCategoryAssociations Table**
SubCategoryAssociationId Int PK
ReportOfSubCategoryId bigint FK to Subcategories Table
IncludeValuesFromSubcategoryId Bigint FK to Subcategories Table
SELECT     A.AreaId, A.AreaName, C.SectionId, C.SectionName, E.LocationId, E.LocationName
FROM         DatabaseA.dbo.tblAreas AS A INNER JOIN
                      DatabaseA.dbo.tblAreaSections AS B ON A.AreaId = B.AreaId INNER JOIN
                      DatabaseA.dbo.tblSections AS C ON B.SectionId = C.SectionId INNER JOIN
                      DatabaseA.dbo.tblSectionLocations AS D ON C.SectionId = D.SectionId INNER JOIN
                      DatabaseA.dbo.tblLocations AS E ON D.LocationId = E.LocationId
SELECT     TOP (100) PERCENT dbo.tblCategories.CategoryId, 
          dbo.tblCategories.CategoryName, 
                      dbo.tblSubCategory.SubCategoryId, 
                      dbo.tblSubCategory.SubCategoryname, 
              dbo.tblSubCategory.SubCategoryTypeId
FROM         dbo.tblCategories INNER JOIN
                      dbo.tblSubCategory ON dbo.tblCategories.CategoryId = dbo.tblSubCategory.CategoryId
ORDER BY dbo.tblCategories.CategoryName, dbo.tblSubCategory.SubCategoryname
SELECT     dbo.vwCategoryAndSubCategory.CategoryId, dbo.vwCategoryAndSubCategory.CategoryName, 
                      dbo.vwCategoryAndSubCategory.CategoryPlacementOrder, dbo.vwCategoryAndSubCategory.IsStandardDaybookEntryCategory, 
                      dbo.vwCategoryAndSubCategory.SubCategoryId, dbo.vwCategoryAndSubCategory.SubCategoryname, dbo.vwCategoryAndSubCategory.SubCategoryTypeId, 
                      dbo.vwCategoryAndSubCategory.SubCategoryPlacementOrder, dbo.vwAreasSectionsAndLocations.AreaId, dbo.vwAreasSectionsAndLocations.AreaName, 
                      dbo.vwAreasSectionsAndLocations.SectionId, dbo.vwAreasSectionsAndLocations.SectionName, dbo.vwAreasSectionsAndLocations.LocationId, 
                      dbo.vwAreasSectionsAndLocations.LocationName
FROM         dbo.vwCategoryAndSubCategory CROSS JOIN
                      dbo.vwAreasSectionsAndLocations
SELECT      dbo.tblUserStats.UserStatId, 
    dbo.tblUserStats.UserId, 
    dbo.tblUserStats.LocationId, 
        dbo.tblUserStats.SectionId, 
    dbo.tblUserStats.AreaId,                       
            dbo.tblUserStats.StartDate, 
    dbo.tblUserStats.EndDate, 
    dbo.tblFieldType1.CategoryId, 
    dbo.tblFieldType1.SubCategoryId, 
            1 AS FieldTypeId, 
    (dbo.tblFieldType1.Value1 + ISNULL
                      ((SELECT     SUM(A.Value1)
                          FROM         dbo.tblFieldType1 A
                          WHERE     A.SubCategoryId IN
                                                    (SELECT DISTINCT IncludeValuesFromSubCategoryId
                                                      FROM          dbo.tblSubCategoryAssociations
                                                      WHERE      ReportOnSubCategoryId = A.SubCategoryId) AND dbo.tblFieldType1.UserStatId = A.UserStatId), 0) 
                  + ISNULL
                      ((SELECT     SUM(A.Value1)
                          FROM         dbo.tblFieldType2 A
                          WHERE     A.SubCategoryId IN
                                                    (SELECT DISTINCT IncludeValuesFromSubCategoryId
                                                      FROM          dbo.tblSubCategoryAssociations
                                                      WHERE      ReportOnSubCategoryId = A.SubCategoryId) AND dbo.tblFieldType1.UserStatId = A.UserStatId), 0) 
                  + ISNULL
                      ((SELECT     SUM(A.Value1)
                          FROM         dbo.tblFieldType3 A
                          WHERE     A.SubCategoryId IN
                                                    (SELECT DISTINCT IncludeValuesFromSubCategoryId
                                                      FROM          dbo.tblSubCategoryAssociations
                                                      WHERE      ReportOnSubCategoryId = A.SubCategoryId) AND dbo.tblFieldType1.UserStatId = A.UserStatId), 0)) 
                  AS Value1, (dbo.tblFieldType1.Value2 + ISNULL
                      ((SELECT     SUM(A.Value2)
                          FROM         dbo.tblFieldType1 A
                          WHERE     A.SubCategoryId IN
                                                    (SELECT DISTINCT IncludeValuesFromSubCategoryId
                                                      FROM          dbo.tblSubCategoryAssociations
                                                      WHERE      ReportOnSubCategoryId = A.SubCategoryId) AND dbo.tblFieldType1.UserStatId = A.UserStatId), 0) 
                  + ISNULL
                      ((SELECT     SUM(A.Value2)
                          FROM         dbo.tblFieldType2 A
                          WHERE     A.SubCategoryId IN
                                                    (SELECT DISTINCT IncludeValuesFromSubCategoryId
                                                      FROM          dbo.tblSubCategoryAssociations
                                                      WHERE      ReportOnSubCategoryId = A.SubCategoryId) AND dbo.tblFieldType1.UserStatId = A.UserStatId), 0) 
                  + ISNULL
                      ((SELECT     SUM(A.Value2)
                          FROM         dbo.tblFieldType3 A
                          WHERE     A.SubCategoryId IN
                                                    (SELECT DISTINCT IncludeValuesFromSubCategoryId
                                                      FROM          dbo.tblSubCategoryAssociations
                                                      WHERE      ReportOnSubCategoryId = A.SubCategoryId) AND dbo.tblFieldType1.UserStatId = A.UserStatId), 0)) 
                  AS Value2, Value1 + Value2 AS CombinedValue1And2, 0 AS Value3, 0 AS Value4, 
                  0 AS Value5, 0 AS Value6, 0 AS Value7, 0 AS Value8, 
                  0 AS Value9, 0 AS Value10, 0 AS Value11, 0 AS Value12, 0 AS Value13, 
                  0 AS Value14, 0 AS Value15, 0 AS Value16
FROM         dbo.tblUserStats INNER JOIN
                  dbo.tblFieldType1 ON dbo.tblUserStats.UserStatId = dbo.tblFieldType1.UserStatId 

UNION ALL

SELECT      dbo.tblUserStats.UserStatId, 
    dbo.tblUserStats.UserId, 
    dbo.tblUserStats.LocationId, 
            dbo.tblUserStats.SectionId, 
    dbo.tblUserStats.AreaId, 
            dbo.tblUserStats.StartDate, 
    dbo.tblUserStats.EndDate, 
    dbo.tblFieldType2.CategoryId, 
    dbo.tblFieldType2.SubCategoryId, 
            2 AS FieldTypeId, 
    dbo.tblFieldType2.Value1 + ISNULL
                      ((SELECT     SUM(Value1) AS Expr1
                          FROM         dbo.tblFieldType1 AS A
                          WHERE     (FieldId IN
                                                    (SELECT DISTINCT IncludeValuesFromSubCategoryId
                                                      FROM          dbo.tblSubCategoryAssociations
                                                      WHERE      (ReportOnSubCategoryId = A.SubCategoryId))) AND (dbo.tblFieldType2.UserStatId = UserStatId)), 0) 
                  + ISNULL
                      ((SELECT     SUM(Value1) AS Expr1
                          FROM         dbo.tblFieldType2 AS A
                          WHERE     (FieldId IN
                                                    (SELECT DISTINCT IncludeValuesFromSubCategoryId
                                                      FROM          dbo.tblSubCategoryAssociations
                                                      WHERE      (ReportOnSubCategoryId = A.SubCategoryId))) AND (dbo.tblFieldType2.UserStatId = UserStatId)), 0) 
                  + ISNULL
                      ((SELECT     SUM(Value1) AS Expr1
                          FROM         dbo.tblFieldType3 AS A
                          WHERE     (FieldId IN
                                                    (SELECT DISTINCT IncludeValuesFromSubCategoryId
                                                      FROM          dbo.tblSubCategoryAssociations
                                                      WHERE      (ReportOnSubCategoryId = A.SubCategoryId))) AND (dbo.tblFieldType2.UserStatId = UserStatId)), 0) 
                  AS Value1, dbo.tblFieldType2.Value2 + ISNULL
                      ((SELECT     SUM(Value2) AS Expr1
                          FROM         dbo.tblFieldType1 AS A
                          WHERE     (FieldId IN
                                                    (SELECT DISTINCT IncludeValuesFromSubCategoryId
                                                      FROM          dbo.tblSubCategoryAssociations
                                                      WHERE      (ReportOnSubCategoryId = A.SubCategoryId))) AND (dbo.tblFieldType2.UserStatId = UserStatId)), 0) 
                  + ISNULL
                      ((SELECT     SUM(Value2) AS Expr1
                          FROM         dbo.tblFieldType2 AS A
                          WHERE     (FieldId IN
                                                    (SELECT DISTINCT IncludeValuesFromSubCategoryId
                                                      FROM          dbo.tblSubCategoryAssociations
                                                      WHERE      (ReportOnSubCategoryId = A.SubCategoryId))) AND (dbo.tblFieldType2.UserStatId = UserStatId)), 0) 
                  + ISNULL
                      ((SELECT     SUM(Value2) AS Expr1
                          FROM         dbo.tblFieldType3 AS A
                          WHERE     (FieldId IN
                                                    (SELECT DISTINCT IncludeValuesFromSubCategoryId
                                                      FROM          dbo.tblSubCategoryAssociations
                                                      WHERE      (ReportOnSubCategoryId = A.SubCategoryId))) AND (dbo.tblFieldType2.UserStatId = UserStatId)), 0) 
                  AS Value2, dbo.tblFieldType2.Value1 + dbo.tblFieldType2.Value2 AS CombinedValue1And2, 
                  ISNULL(dbo.tblFieldType2.Value3, 0), ISNULL(dbo.tblFieldType2.Value4, 0), 
                  ISNULL(dbo.tblFieldType2.Value5, 0), ISNULL(dbo.tblFieldType2.Value6, 0), 
                  ISNULL(dbo.tblFieldType2.Value7, 0), ISNULL(dbo.tblFieldType2.Value8, 0), 
                  ISNULL(dbo.tblFieldType2.Value9, 0), ISNULL(dbo.tblFieldType2.Value10, 0), 
                  0 AS Value11, 0 AS Value12, 0 AS Value13, 0 AS Value14, 0 AS Value15, 
                  0 AS Value16
FROM         dbo.tblUserStats INNER JOIN
                  dbo.tblFieldType2 ON dbo.tblUserStats.UserStatId = dbo.tblFieldType2.UserStatId 
UNION ALL
SELECT      dbo.tblUserStats.UserStatId, 
    dbo.tblUserStats.UserId, 
    dbo.tblUserStats.LocationId, 
    dbo.tblUserStats.SectionId, 
    dbo.tblUserStats.AreaId, 
            dbo.tblUserStats.StartDate, 
    dbo.tblUserStats.EndDate, 
    dbo.tblFieldType3.CategoryId, 
    dbo.tblFieldType3.SubCategoryId, 
            3 AS FieldTypeId, 
    dbo.tblFieldType3.Value1 + ISNULL
                      ((SELECT     SUM(Value1) AS Expr1
                          FROM         dbo.tblFieldType1 AS A
                          WHERE     (FieldId IN
                                                    (SELECT DISTINCT IncludeValuesFromSubCategoryId
                                                      FROM          dbo.tblSubCategoryAssociations
                                                      WHERE      (ReportOnSubCategoryId = A.SubCategoryId))) AND (dbo.tblFieldType3.UserStatId = UserStatId)), 0) 
                  + ISNULL
                      ((SELECT     SUM(Value1) AS Expr1
                          FROM         dbo.tblFieldType2 AS A
                          WHERE     (FieldId IN
                                                    (SELECT DISTINCT IncludeValuesFromSubCategoryId
                                                      FROM          dbo.tblSubCategoryAssociations
                                                      WHERE      (ReportOnSubCategoryId = A.SubCategoryId))) AND (dbo.tblFieldType3.UserStatId = UserStatId)), 0) 
                  + ISNULL
                      ((SELECT     SUM(Value1) AS Expr1
                          FROM         dbo.tblFieldType3 AS A
                          WHERE     (FieldId IN
                                                    (SELECT DISTINCT IncludeValuesFromSubCategoryId
                                                      FROM          dbo.tblSubCategoryAssociations
                                                      WHERE      (ReportOnSubCategoryId = A.SubCategoryId))) AND (dbo.tblFieldType3.UserStatId = UserStatId)), 0) 
                  AS Value1, dbo.tblFieldType3.Value2 + ISNULL
                      ((SELECT     SUM(Value2) AS Expr1
                          FROM         dbo.tblFieldType1 AS A
                          WHERE     (FieldId IN
                                                    (SELECT DISTINCT IncludeValuesFromSubCategoryId
                                                      FROM          dbo.tblSubCategoryAssociations
                                                      WHERE      (ReportOnSubCategoryId = A.SubCategoryId))) AND (dbo.tblFieldType3.UserStatId = UserStatId)), 0) 
                  + ISNULL
                      ((SELECT     SUM(Value2) AS Expr1
                          FROM         dbo.tblFieldType2 AS A
                          WHERE     (FieldId IN
                                                    (SELECT DISTINCT IncludeValuesFromSubCategoryId
                                                      FROM          dbo.tblSubCategoryAssociations
                                                      WHERE      (ReportOnSubCategoryId = A.SubCategoryId))) AND (dbo.tblFieldType3.UserStatId = UserStatId)), 0) 
                  + ISNULL
                      ((SELECT     SUM(Value2) AS Expr1
                          FROM         dbo.tblFieldType3 AS A
                          WHERE     (FieldId IN
                                                    (SELECT DISTINCT IncludeValuesFromSubCategoryId
                                                      FROM          dbo.tblSubCategoryAssociations
                                                      WHERE      (ReportOnSubCategoryId = A.SubCategoryId))) AND (dbo.tblFieldType3.UserStatId = UserStatId)), 0) 
                  AS Value2, dbo.tblFieldType3.Value1 + dbo.tblFieldType3.Value2 AS CombinedValue1And2, 0 AS Value3, 
                  0 AS Value4, 0 AS Value5, 0 AS Value6, 0 AS Value7, 
                  0 AS Value8, 0 AS Value9, 0 AS Value10, 
                  ISNULL(dbo.tblFieldType3.Value11, 0), ISNULL(dbo.tblFieldType3.Value12, 0), 
                  ISNULL(dbo.tblFieldType3.Value13, 0), ISNULL(dbo.tblFieldType3.Value14, 0), 
                  ISNULL(dbo.tblFieldType3.Value15, 0), ISNULL(dbo.tblFieldType3.Value16, 0)
FROM         dbo.tblUserStats INNER JOIN
                  dbo.tblFieldType3 ON dbo.tblUserStats.UserStatId = dbo.tblFieldType3.UserStatId 
UNION ALL
SELECT     dbo.tblUserStats.UserStatId, 
    dbo.tblUserStats.UserId, 
    dbo.tblUserStats.LocationId, 
            dbo.tblUserStats.SectionId, 
    dbo.tblUserStats.AreaId, 
            dbo.tblUserStats.StartDate, 
    dbo.tblUserStats.EndDate, 
    dbo.tblFieldType4.CategoryId, 
    dbo.tblFieldType4.SubCategoryId, 
            4 AS FieldTypeId, 
    0 AS Value1, 0 AS Value2, dbo.tblFieldType4.CombinedValue1And2 + (ISNULL
                      ((SELECT     SUM(A.Value1)
                          FROM         dbo.tblFieldType1 A
                          WHERE     A.SubCategoryId IN
                                                    (SELECT DISTINCT IncludeValuesFromSubCategoryId
                                                      FROM          dbo.tblSubCategoryAssociations
                                                      WHERE      ReportOnSubCategoryId = A.SubCategoryId) AND dbo.tblFieldType4.UserStatId = A.UserStatId), 0) 
                  + ISNULL
                      ((SELECT     SUM(A.Value1)
                          FROM         dbo.tblFieldType2 A
                          WHERE     A.SubCategoryId IN
                                                    (SELECT DISTINCT IncludeValuesFromSubCategoryId
                                                      FROM          dbo.tblSubCategoryAssociations
                                                      WHERE      ReportOnSubCategoryId = A.SubCategoryId) AND dbo.tblFieldType4.UserStatId = A.UserStatId), 0) 
                  + ISNULL
                      ((SELECT     SUM(A.Value1)
                          FROM         dbo.tblFieldType3 A
                          WHERE     A.SubCategoryId IN
                                                    (SELECT DISTINCT IncludeValuesFromSubCategoryId
                                                      FROM          dbo.tblSubCategoryAssociations
                                                      WHERE      ReportOnSubCategoryId = A.SubCategoryId) AND dbo.tblFieldType4.UserStatId = A.UserStatId), 0)) 
                  + (ISNULL
                      ((SELECT     SUM(A.Value2)
                          FROM         dbo.tblFieldType1 A
                          WHERE     A.SubCategoryId IN
                                                    (SELECT DISTINCT IncludeValuesFromSubCategoryId
                                                      FROM          dbo.tblSubCategoryAssociations
                                                      WHERE      ReportOnSubCategoryId = A.SubCategoryId) AND dbo.tblFieldType4.UserStatId = A.UserStatId), 0) 
                  + ISNULL
                      ((SELECT     SUM(A.Value2)
                          FROM         dbo.tblFieldType2 A
                          WHERE     A.SubCategoryId IN
                                                    (SELECT DISTINCT IncludeValuesFromSubCategoryId
                                                      FROM          dbo.tblSubCategoryAssociations
                                                      WHERE      ReportOnSubCategoryId = A.SubCategoryId) AND dbo.tblFieldType4.UserStatId = A.UserStatId), 0) 
                  + ISNULL
                      ((SELECT     SUM(A.Value2)
                          FROM         dbo.tblFieldType3 A
                          WHERE     A.SubCategoryId IN
                                                    (SELECT DISTINCT IncludeValuesFromSubCategoryId
                                                      FROM          dbo.tblSubCategoryAssociations
                                                      WHERE      ReportOnSubCategoryId = A.SubCategoryId) AND dbo.tblFieldType4.UserStatId = A.UserStatId), 0)) 
                  AS CombinedValue1And2, 0 AS Value3, 0 AS Value4, 0 AS Value5, 0 AS Value6, 
                  0 AS Value7, 0 AS Value8, 0 AS Value9, 0 AS Value10, 
                  0 AS Value11, 0 AS Value12, 0 AS Value13, 0 AS Value14, 0 AS Value15, 
                  0 AS Value16
FROM         dbo.tblUserStats INNER JOIN
                  dbo.tblFieldType4 ON dbo.tblUserStats.UserStatId = dbo.tblFieldType4.UserStatId 
SELECT     TOP (100) PERCENT dbo.vwAreaSectionLocationCategorySubCategory.categoryId, 
        dbo.vwAreaSectionLocationCategorySubCategory.categoryName, 
                  dbo.vwAreaSectionLocationCategorySubCategory.SubCategoryId, 
                  dbo.vwAreaSectionLocationCategorySubCategory.SubCategoryName, 
        dbo.vwAreaSectionLocationCategorySubCategory.FieldTypeId, 
                  dbo.vwAreaSectionLocationCategorySubCategory.AreaId, 
                  dbo.vwAreaSectionLocationCategorySubCategory.AreaName, 
        dbo.vwAreaSectionLocationCategorySubCategory.SectionId, 
                  dbo.vwAreaSectionLocationCategorySubCategory.SectionName, 
        dbo.vwAreaSectionLocationCategorySubCategory.LocationId, 
                  dbo.vwAreaSectionLocationCategorySubCategory.LocationName, 
        ISNULL(dbo.vwUserStatsResults.UserStatsId, 0) AS UserStatsId, 
                  ISNULL(dbo.vwUserStatsResults.UserId, 0) AS UserId, 
        ISNULL(dbo.vwUserStatsResults.LocationId, 0) AS LocationId, 
        ISNULL(dbo.vwUserStatsResults.SectionId, 0) AS SectionId, 
        ISNULL(dbo.vwUserStatsResults.AreaId, 0) AS AreaId, 
                    ISNULL(dbo.vwUserStatsResults.StartDate, N'2000-01-01') AS StartDate, 
        ISNULL(dbo.vwUserStatsResults.EndDate, N'2000-01-01') AS EndDate, 
                    ISNULL(dbo.vwUserStatsResults.Value1, 0) AS Value1, 
        ISNULL(dbo.vwUserStatsResults.Value2, 0) AS Value2, 
        ISNULL(dbo.vwUserStatsResults.CombinedValue1And2, 0) AS CombinedValue1And2, 
        ISNULL(dbo.vwUserStatsResults.Value3, 0) AS Value3, 
        ISNULL(dbo.vwUserStatsResults.Value4, 0) AS Value4, 
                    ISNULL(dbo.vwUserStatsResults.Value5, 0) AS Value5, 
        ISNULL(dbo.vwUserStatsResults.Value6, 0) AS Value6, 
        ISNULL(dbo.vwUserStatsResults.Value7, 0) AS Value7, 
                    ISNULL(dbo.vwUserStatsResults.Value8, 0) AS Value8, 
        ISNULL(dbo.vwUserStatsResults.Value9, 0) AS Value9, 
        ISNULL(dbo.vwUserStatsResults.Value10, 0) AS Value10, 
                    ISNULL(dbo.vwUserStatsResults.Value11, 0) AS Value11, 
        ISNULL(dbo.vwUserStatsResults.Value12, 0) AS Value12, 
                    ISNULL(dbo.vwUserStatsResults.Value13, 0) AS Value13,   
        ISNULL(dbo.vwUserStatsResults.Value14, 0) AS Value14, 
        ISNULL(dbo.vwUserStatsResults.Value15, 0) AS Value15, 
                    ISNULL(dbo.vwUserStatsResults.Value16, 0) AS Value16
FROM         dbo.vwAreaSectionLocationCategorySubCategory LEFT OUTER JOIN
                  dbo.vwUserStatsResults ON dbo.vwAreaSectionLocationCategorySubCategory.LocationId = dbo.vwUserStatsResults.LocationId AND 
                  dbo.vwAreaSectionLocationCategorySubCategory.SubCategoryId = dbo.vwUserStatsResults.SubCategoryId AND 
                  dbo.vwAreaSectionLocationCategorySubCategory.FieldTypeId = dbo.vwUserStatsResults.FieldTypeId AND 
                  dbo.vwAreaSectionLocationCategorySubCategory.categoryId = dbo.vwUserStatsResults.categoryId