在单独的列中获取相同的表数据

时间:2015-01-02 07:43:00

标签: sql sql-server-2008 join subquery

我有一个这种格式的SQL表

ColumnID    Column1     Column2     Column3
-------------------------------------------
230         ABC         20          1
230         DEF         14.50       1
230         XYZ         10          1
230         DEF         09          0
230         ABC         30          0
230         MNO         16          0
230         OOP         07          1

我想基于Column3 = 1和Column3 = 0

的ColumnID输出如下
output_Column1      output_Column2      output_Column3      output_Column4
--------------------------------------------------------------------------
ABC                 20                  ABC                 30
DEF                 14.50               DEF                 09
OOP                 07                  MNO                 16
XYZ                 10                  NULL                 NULL

使用自我加入我得到了6条不正确的记录。如果Column3值与column3 = 1匹配且column3 = 0,则不必强制使用Column1值。

请帮忙!

4 个答案:

答案 0 :(得分:1)

试试这个:

SELECT MAX(CASE WHEN Column3 = 1 THEN Column1 ELSE NULL END) output_Column1,
       MAX(CASE WHEN Column3 = 1 THEN Column2 ELSE NULL END) output_Column2,
       MAX(CASE WHEN Column3 = 0 THEN Column1 ELSE NULL END) output_Column3,
       MAX(CASE WHEN Column3 = 0 THEN Column2 ELSE NULL END) output_Column4
FROM tablename
GROUP BY ColumnID, Column1;

答案 1 :(得分:0)

试试这个

SELECT t1.Column1 AS output_Column1,t1.Column2 
    AS output_Column2, t2.Column1 AS     
            output_Column3,t2.Column2 AS output_Column4 FROM mytable 
            as t1 LEFT OUTER JOIN mytable 
      as t2 ON t1.COLUMNID=t2.COLUMNID WHERE t1.COlumn3 =0 AND t1.Column3=3

或者

   SELECT t1.Column1 AS output_Column1,t1.Column2 
        AS output_Column2, t2.Column1 AS     
                output_Column3,t2.Column2 AS output_Column4 FROM mytable 
                as t1 LEFT OUTER JOIN mytable 
          as t2 ON t1.COLUMNID=t2.COLUMNID WHERE EXISTS
     (
       SELECT ColumnID FROM mytable WHERE COLUMNID=t1.COlumnID and Column3=0 AND COlumn3=3
     )

答案 2 :(得分:0)

好的,根据您的评论,我收集到了Column1值

  • 只存在一个Column3 = 0的记录,而没有Column3 = 1的记录
  • 只存在一个Column3 = 1的记录,而没有Column3 = 0的记录
  • 存在Column3 = 1的一条记录和一条Column3 = 0的记录

  • 对于Column1值,Column3 = 0永远不会存在多条记录。
  • 对于Column1值,Column3 = 1永远不会存在多条记录。

自连接表,一次为column3 = 0,一次为column3 = 1.使用完全外连接,以获取没有column3 = 0或1的条目的记录。

select
  m1.column1 as output_column1,
  m1.column2 as output_column2,
  m0.column1 as output_column3,
  m0.column2 as output_column4
from (select * from mytable where column3 = 0) m0
full outer join (select * mytable where column3 = 1) m1
on m0.column1 = m1.column1;

如果没有自我加入,您可以实现相同目标:

select
  max(case when column3 = 1 then column1 end) as output_column1,
  max(case when column3 = 1 then column2 end) as output_column2,
  max(case when column3 = 0 then column1 end) as output_column3,
  max(case when column3 = 0 then column2 end) as output_column4
from mytable
where column3 in (0,1)
group by column1;

答案 3 :(得分:0)

以下是对您的请求所做更改的单独答案,使其成为另一个问题。

SQL查询从关系数据库中选择相关数据。但是,在您的情况下,您选择不相关的数据。如果你将OOP与MNO结合并且单独留下XYZ或将XYZ与MNO耦合并且单独留下OOP则无关紧要。您加入记录虽然它们彼此无关。 (OOP和XYZ都与MNO无关。)

通常你会选择

output_Column1      output_Column2      output_Column3      output_Column4
ABC                 20                  ABC                 30
DEF                 14.50               DEF                 09
                                        MNO                 16
OOP                 07                                          
XYZ                 10                                          

但你希望它是

output_Column1      output_Column2      output_Column3      output_Column4
ABC                 20                  ABC                 30
DEF                 14.50               DEF                 09
OOP                 07                  MNO                 16
XYZ                 10                                          

以便保存行。您通常会在显示数据的应用程序中执行此操作,即使用Java,C#,PHP或其他任何内容来填充显示网格。

如果要通过加入不相关的记录在SQL中实现相同的目标,则必须创建关系。例如,将行号添加到所有column3 = 0个没有兄弟,其中column3 = 1的记录,并将行号添加到所有column3 = 1个没有兄弟,column3 = 0的记录。然后将第一个与第二个按行号相结合。

select
  m1.column1 as output_column1,
  m1.column2 as output_column2,
  m0.column1 as output_column3,
  m0.column2 as output_column4
from (select * from mytable where column3 = 0) m0
join (select * mytable where column3 = 1) m1 on m1.column1 = m0.column1
union all
select
  m1.column1 as output_column1,
  m1.column2 as output_column2,
  m0.column1 as output_column3,
  m0.column2 as output_column4
from 
(
  select
    row_number() over (order by column1) as joinkey,
    column1,
    column2
  from mytable 
  where column3 = 0
  and not exists
  (
    select *
    from mytable couple
    where couple.column1 = mytable.column1
    and couple.column3 = 1
  )
) m0
full outer join 
(
  select
    row_number() over (order by column1) as joinkey,
    column1,
    column2
  from mytable 
  where column3 = 1
  and not exists
  (
    select *
    from mytable couple
    where couple.column1 = mytable.column1
    and couple.column3 = 0
  )
) m1 on m1.joinkey = m0.joinkey;