【问题标题】:SQL Query to join the same table based on conditionSQL查询根据条件加入同一张表
【发布时间】:2015-04-06 02:01:29
【问题描述】:

谁能帮我写一个查询以获得以下结果。

表格是这样的:

    Table1
    ------------
    Col1    Col2
    ===     ====
    A       Error
    B       Success
    C       Success
    D       Success

这是查询的结果(使用Join语句后):

     Success    Error
     --------   ------
     B          A
     C          A
     D          A

预期结果:

     Success    Error
     B          A
     C          0
     D          0

Col2 中的值可能会有所不同 - 如下所述。

    Col1    Col2
    ----    -----
    A       Error
    B       Success
    C       Success
    D       Failed
    E       Unknown
    F       Success
    G       Error

以及预期的结果:

    Success Failed  Error   Unknown
    ------- ------  -----   -------
    B       D       A       E   
    C       0       G       0
    F       0       0       0

我试图将第 2 列中的值作为新表中的标题,并将第 1 列中的值作为新表中的行值。

这是我的 SQL 代码:

Select tb1.Col1 as Success, tb2.Col1 as Error 
from ( Select * from table1 where Col2 = 'Success') tb1 
Join ( Select * from table1 where Col2 = 'Error') tb2 
on tb1.Col1 is not NUll 

【问题讨论】:

  • 能否请您显示您目前拥有的SQL。
  • 我对 SQL 查询相当陌生。我写了一个简单的查询。尝试在线搜索可能的选项,但我没有找到任何选项。 Select tb1.Col1 as Success, tb2.Col1 as Error from (Select * from table1 where Col2 = 'Success') tb1 Join (Select * from table1 where Col2 = 'Error') tb2 on tb1.Col1 is not NUll
  • 仍然不是很清楚你想要达到什么目的......我猜你的数据模型设计有问题。
  • @Anoop 你想要一些不合理的东西。单个表行(或查询结果行)中的值是指同一事物。您似乎想要多个彼此无关的独立列表。 (当然可以写这样的 SQL,只是没有任何意义,因此你不应该这样做。也许你首先描述你想要达到的最终结果。)
  • @tomalak 是的,即使我有一段时间也这么认为。我认为将值重新排列到新表中会很容易以供以后使用。我正在使用此查询进行进一步处理。多次运行查询似乎会影响性能,然后我想到了这个选项。

标签: sql-server join


【解决方案1】:

您只想获取彼此相邻的项目列表,而它们之间没有任何实际关系?像这样的 row_number 应该可以工作:

select
  max(case when col2 = 'Success' then col1 else null end) as Success,
  max(case when col2 = 'Failed' then col1 else null end) as Failed,
  max(case when col2 = 'Error' then col1 else null end) as Error,
  max(case when col2 = 'Unknown' then col1 else null end) as Unknown
from (
select
  col2,
  col1,
  row_number() over (partition by col2 order by col1) as rn
from
  data
) TMP
group by RN

SQL 小提琴:http://sqlfiddle.com/#!6/8c641/4

【讨论】:

  • 这仅适用于 Success、Failed、Error 和 Unknown 是唯一可能的值。如果可能有其他人,您需要使用 COALESCE 和动态 SQL 来解决问题。
【解决方案2】:

我认为@JamesZ 的答案是正确的,但是如果 OP 需要 Col2 的动态值的结果,我建议使用此代码:

Declare @query nvarchar(max) = ''

select @query = @query + ', isnull(max(case when col2 = ''' + Col2 + ''' then col1 else null end), 0) as ' + Col2
from (select distinct Col2 from temptable) dt

set @query = 'select ' + substring(@query, 2, len(@query)) +
   ' from (select col2, col1, row_number() over (partition by col2 order by col1) as rn from temptable) TMP group by RN'

exec(@query)

结果的列现在等待各种Col2:

Error | Failed | Success | Unknown
------+--------+---------+-------------
A     | D      | B       | E
G     | 0      | C       | 0
0     | 0      | F       | 0

【讨论】:

    【解决方案3】:

    这是标准的支点任务:

    Create table t(code char(1), status nvarchar(10))
    
    Insert into t values
    ( 'A',       'Error'),
    (  'B',       'Success'),
    (  'C',       'Success'),
    (  'D',       'Failed'),
    ( 'E',       'Unknown'),
    (   'F',       'Success'),
    ( 'G',       'Error')
    
    ;with cte as (Select *, row_number() over(partition by status order by code) c from t)
     Select success, failed, error, unknown from cte
     Pivot(max(code) for status in([error],[failed],[unknown],[success])) p
    

    小提琴:http://sqlfiddle.com/#!6/06e8c/12

    【讨论】:

      猜你喜欢
      • 2012-03-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多