联合选择水平语句

时间:2012-09-12 20:29:21

标签: sql sql-server sql-server-2008-r2

让我说我的选择语句的结果如下(我有5个):

   Id  Animal AnimalId
    1   Dog     Dog1
    1   Cat     Cat57

   Id  Transport TransportId
    2   Car        Car100
    2   Plane      Plane500

我想得到如下结果:

   Id  Animal AnimalId    Transport    TransportId
    1   Dog     Dog1
    1   Cat     Cat57
    2                        Car          Car100
    2                       Plane         Plane500 

我可以做的是我可以创建一个tablevariable并指定所有可能的列并将每个select语句中的记录插入其中。但也许更好的解决方案,如PIVOT?
      的修改

查询:第1名:Select CategoryId as Id, Animal, AnimalId from Animal
第二名:Select CategoryId as Id, Transport, TransportId from Transport

5 个答案:

答案 0 :(得分:3)

如果你在同一行中需要它们,这会获得每行的row_number()并加入其中:

select a.id,
  a.aname,
  a.aid,
  t.tname,
  t.tid
from 
(
  select id, aname, aid, row_number() over(order by aid) rn
  from animal
) a
left join
(
  select id, tname, tid, row_number() over(order by tid) rn
  from transport
) t
  on a.rn = t.rn

请参阅SQL Fiddle with Demo

如果您在同一行中不需要它们,请使用UNION ALL

select id, aname, aid, 'Animal' tbl
from animal
union all
select id, tname, tid, 'Transport'
from transport

请参阅SQL Fiddle with Demo

编辑#1,这是一个UNPIVOTPIVOT

的版本
select an_id, [aname], [aid], [tname], [tid]
from
(
  select *, row_number() over(partition by col order by col) rn
  from animal
  unpivot
  (
    value
    for col in (aname, aid)
  ) u
  union all
  select *, row_number() over(partition by col order by col) rn
  from transport
  unpivot
  (
    value
    for col in (tname, tid)
  ) u
) x1
pivot
(
  min(value)
  for col in([aname], [aid], [tname], [tid])
) p
order by an_id

请参阅SQL Fiddle with Demo

答案 1 :(得分:3)

这可以帮到你:

SELECT
ID, field1, field2, '' as field3, '' as field4
FROM sometable

UNION ALL

SELECT
ID, '', '', field3, field4
FROM someothertable

答案 2 :(得分:0)

您不需要转动,结果已经很好了。

如果需要,您可以使用与第一个选择相同的格式UNION所有5个语句:ID / Category / CategoryID。然后你将获得一个长的结果集,其中所有5个集合都附加了3列宽。

这就是你想要的吗?或者您需要区分“类别”吗?


举个例子,试试:

Select CategoryId as Id, Animal, AnimalId from Animal
union all
Select CategoryId as Id, Transport, TransportId from Transport

如果需要,可以对列进行别名,如:

Select CategoryId as Id, Animal as category, AnimalId as categoryID from Animal
union all
Select CategoryId as Id, Transport, TransportId from Transport

你真的不需要转动,只是像你最初想的那样空出你的列。您没有透视移动列,您可以通过旋转来对分组数据执行聚合功能。

答案 3 :(得分:0)

create table Animal (
    Animal varchar(50)
    ,AnimalID varchar(50)
    )

create table Transport (
    Transport varchar(50)
    ,TransportID varchar(50)
    )

insert into Animal values ('Dog', 'Dog1')
insert into Animal values ('Cat', 'Cat57')

insert into Transport values ('Car', 'Car100')
insert into Transport values ('Plane', 'Plane500')


select  ID = 1
    ,A.Animal
    ,A.AnimalID
    ,Transport = ''
    ,TransportID = ''
from    Animal A

union

select  ID = 2
    ,Animal = ''
    ,AnimalID = ''
    ,T.Transport
    ,T.TransportID
from    Transport T

答案 4 :(得分:0)

要以您想要的格式获取它,请选择所需的值,然后为其他列选择null(或空字符串)。

SELECT
    CategoryId as Id, 
    Animal as 'Animal', 
    AnimalId as 'AnimalId',
    null as 'Transport',
    null as 'TransportId'
FROM Animal
UNION
SELECT 
    CategoryId as Id, 
    null as 'Animal',
    null as 'AnimalId',
    Transport as 'Transport',  
    TransportId as 'TransportId' 
FROM Transport

我仍然不确定这个目的,但这应该提供你想要的输出。