【问题标题】:How to select distinct columns and return non null values如何选择不同的列并返回非空值
【发布时间】:2014-07-08 14:21:23
【问题描述】:

我有许多表与一个主表有关系。 我希望能够加入这些表,同时从每一行中选择不同的值,并且只返回非空值这可能吗?

例如,

 ColA    ColB    ColC
 Black   3       Car
 Red     18      60

这是我目前的查询:

SELECT DISTINCT FormTitle, FormSection FROM Table1 c
FULL OUTER JOIN Table2 l
   ON c.FormSectionID = l.FormSectionID
FULL OUTER JOIN Table3 f
   ON c.FormID = f.FormID

我希望 FormTitle 列具有所有不同的非空值,并且 FormSection 列等具有相同的值。

这是我添加 WHERE FormTitle IS NOT NULL AND FormSection IS NOT NULL 时的结果

FormTitle                                   FormSection
Activities                                  Activities
Annual Program Data Demographic             Demographics
Annual Program Data Screening and Outreach  Other Diseases (Not Eligible for RECA)
Annual Program Data Screening and Outreach  Outreach/Education Encounters
Annual Program Data Screening and Outreach  Program Activities by Type
Annual Program Data Screening and Outreach  RECA-Eligible Diseases by Exposure Category

【问题讨论】:

    标签: sql sql-server join distinct


    【解决方案1】:

    处理 NULL 时,请使用 IS NULL 或 IS NOT NULL

     SELECT DISTINCT FormTitle, FormSection 
     FROM Table1 c
     FULL OUTER JOIN Table2 l
       ON c.FormSectionID = l.FormSectionID 
     FULL OUTER JOIN Table3 f
      ON c.FormID = f.FormID
     WHERE FormTitle IS NOT NULL 
       AND FormSection IS NOT NULL
    

    【讨论】:

    • 我刚刚运行了这个,一些 FormTitles 仍然有重复值
    • 所以没有办法确保列也不同?
    • 不同的列是什么意思?您不在乎来自不同行的值是否在同一行中返回?
    • 你真的要展示你“想要”的东西。如果你有 2 列可以有不同 nbr 的不同值,那么你所要求的是不可能的......
    • @TMcKeown 谢谢我不知道
    【解决方案2】:

    这是一个奇怪的请求,但可以做到。在这样的事情上的表现不会很壮观,但在一个小数据集上它应该不是什么大问题。我使用您发布的示例来提供帮助。如果您可以像这样以可消耗的格式发布 ddl 和 sample,那将非常有帮助。

    if OBJECT_ID('tempdb..#Something') is not null
        drop table #Something
    
    create table #Something
    (
        Title varchar(50),
        FormSection varchar(50)
    )
    
    insert #Something
    select 'Activities', 'Activities' union all
    select 'Annual Program Data Demographic', 'Demographics' union all
    select 'Annual Program Data Screening and Outreach', 'Other Diseases (Not Eligible for RECA)' union all
    select 'Annual Program Data Screening and Outreach', 'Outreach/Education Encounters' union all
    select 'Annual Program Data Screening and Outreach', 'Program Activities by Type' union all
    select 'Annual Program Data Screening and Outreach', 'RECA-Eligible Diseases by Exposure Category';
    
    with Titles as
    (
        select Title, ROW_NUMBER() over (order by (select null)) as RowNum
        from #Something
        group by Title
    )
    , FormSections as
    (
        select FormSection, ROW_NUMBER() over (order by (select null)) as RowNum
        from #Something
        group by FormSection
    )
    
    select FormSection, Title
    from
    (
        select FormSection, Title
        from FormSections fs
        left join Titles t on fs.RowNum = t.RowNum
    
        union all
    
        select FormSection, Title
        from Titles t
        left join FormSections fs on fs.RowNum = t.RowNum
    ) x
    group by FormSection, Title
    

    【讨论】:

      猜你喜欢
      • 2021-08-30
      • 1970-01-01
      • 1970-01-01
      • 2013-02-11
      • 1970-01-01
      • 1970-01-01
      • 2021-09-18
      • 2017-10-01
      • 1970-01-01
      相关资源
      最近更新 更多