0

我的调查数据存储在 SQL Server 数据库中,我正在使用 tableau 对其进行分析。Tableaus 的一个怪癖是它需要特定格式的数据,如此链接所示

Tableau 数据样式

他们使用联合的方法有效,但是我的数据分布在 100 多个问题中,其中一些有 10 个选项。UNION这会创建一个几乎无法编辑的巨大 SQL语句。

我的计划是对需要更改的每个问题执行联合,然后与不需要修改的其他数据进行内部连接,但是连接只返回第一个选择语句字段。

这是工会声明供参考

 select
    [Survey].[SurveyId] AS [SurveyId],
    [HumanCapital_Issues].[DisclosureResponsibility_Importance] AS  [importance],
    'Disclosure Responsibility' as [HCI_Importance]
FROM [Survey].[Survey] [Survey] 
INNER JOIN [Survey].[HumanCapital_Issues] [HumanCapital_Issues] ON ([survey].[SurveyId] = [HumanCapital_Issues].[SurveyId])

union

select
    [Survey].[SurveyId] AS [SurveyId],
    [HumanCapital_Issues].[DevelopingSkills_Importance] AS  [importance],
    'Developing skills' as [HCI_Importance]
FROM [Survey].[Survey] [Survey]
INNER JOIN [Survey].[HumanCapital_Issues] [HumanCapital_Issues] ON ([Survey].[SurveyId] = [HumanCapital_Issues].[SurveyId])


SELECT [Survey].[SurveyId] AS [Survey_SurveyId],
    [Survey].[Year] AS [Year],
    [Survey].[FinancialInstitutionCode] AS [FinancialInstitutionCode],
    [Survey].[DeletionCode] AS [DeletionCode],
    [Survey].[SalaryOnly] AS [SalaryOnly],
    [FinancialInstitution].[FinancialInstitutionCode] AS [FinancialInstitution_FinancialInstitutionCode],
    [FinancialInstitution].[Name] AS [Name],
    [FinancialInstitution].[FDIC] AS [FDIC],
    [FinancialInstitution].[Address_Street1] AS [Address_Street1],
    [FinancialInstitution].[Address_Street2] AS [Address_Street2],
    [FinancialInstitution].[Address_City] AS [Address_City],
    [FinancialInstitution].[Address_State] AS [Address_State],
    [FinancialInstitution].[Address_Zip] AS [Address_Zip],
    [FinancialInstitution].[Address_County] AS [Address_County],
    [FinancialInstitution].[ParentCompany] AS [ParentCompany],
    [FinancialInstitution].[FDICBHC] AS [FDICBHC],
    [FinancialInstitution].[OwnershipStructureCode] AS [OwnershipStructureCode],
    [FinancialInstitution].[Employees_FullTimeEquivalent] AS [Employees_FullTimeEquivalent],
    [FinancialInstitution].[Employees_FullTime] AS [Employees_FullTime],
    [FinancialInstitution].[Employees_PartTime] AS [Employees_PartTime],
    [FinancialInstitution].[Branches_FullService] AS [Branches_FullService],
    [FinancialInstitution].[Branches_InStore] AS [Branches_InStore],
    [FinancialInstitution].[Brances_LimitedService] AS [Brances_LimitedService],
    [FinancialInstitution].[Headquarters_Population] AS [Headquarters_Population],
    [FinancialInstitution].[LargestCity_Population] AS [LargestCity_Population],
    [FinancialInstitution].[Assets] AS [Assets],
    [FinancialInstitution].[Deposits] AS [Deposits],
    [FinancialInstitution].[TARP_Participated] AS [TARP_Participated],
    [FinancialInstitution].[TARP_CausedConcern] AS [TARP_CausedConcern],
    [FinancialInstitution].[TARP_RequiredChanges] AS [TARP_RequiredChanges],
    [FinancialInstitution].[RegionCode] AS [RegionCode],
    [HumanCapital_Issues].[SurveyId] AS [HumanCapital_Issues_SurveyId],
    [HumanCapital_Issues].[DisclosureResponsibility_Concern] AS [DisclosureResponsibility_Concern],
    [HumanCapital_Issues].[DevelopingSkills_Concern] AS [DevelopingSkills_Concern],
    [HumanCapital_Issues].[MotivatingPerformance_Concern] AS [MotivatingPerformance_Concern],
    [HumanCapital_Issues].[Reorganizations_Concern] AS [Reorganizations_Concern],
    [HumanCapital_Issues].[Hiring_Concern] AS [Hiring_Concern],
    [HumanCapital_Issues].[Retention_Concern] AS [Retention_Concern],
    [HumanCapital_Issues].[ContainingCosts_Concern] AS [ContainingCosts_Concern],
    [HumanCapital_Issues].[ManagingRetirements_Concern] AS [ManagingRetirements_Concern],
    [HumanCapital_Issues].[SalesCulture_Concern] AS [SalesCulture_Concern],
    [HumanCapital_Issues].[DevelopingLeaders_Concern] AS [DevelopingLeaders_Concern],
    [HumanCapital_Issues].[Other_Description] AS [Other_Description],
    [HumanCapital_Issues].[Other_Concern] AS [Other_Concern]
FROM 
    [Survey].[Survey] [Survey]
INNER JOIN 
    [Profile].[FinancialInstitution] [FinancialInstitution] ON ([Survey].[SurveyId] = [FinancialInstitution].[FinancialInstitutionCode])
INNER JOIN 
    [Survey].[HumanCapital_Issues] [HumanCapital_Issues] ON ([Survey].[SurveyId] = [HumanCapital_Issues].[SurveyId])

有没有一种“简单的方法”来执行联合,然后将它与另一个选择语句连接起来?

4

1 回答 1

0

如果我最终理解了这个问题,你想加入获取大部分数据的单个查询和获取特定部分的联合查询。希望 SurveyID 是连接元素。将现有查询用作派生表:

select *
  from
  (
     { union query here }
  ) q1
 inner join
  (
     { query with all the data here }
  ) q2
    on q1.SurveyID = q2.SurveyID
于 2012-08-03T22:17:17.357 回答