3

假设我的选择语句的结果如下(我有 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 

我能做的是创建一个表变量并指定所有可能的列并将每个选择语句中的记录插入其中。但也许像 PIVOT 这样的更好的解决方案?
编辑

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

4

5 回答 5

5

怎么样,如果您需要它们在同一行中,这将获取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,这是一个带有UNPIVOTand的版本PIVOT

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

于 2012-09-12T20:37:03.727 回答
3

这将为您做到:

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

UNION ALL

SELECT
ID, '', '', field3, field4
FROM someothertable
于 2012-09-12T20:37:27.153 回答
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

我仍然不确定这样做的目的,但这应该可以提供您想要的输出。

于 2012-09-12T20:50:06.110 回答
0

你不应该需要旋转,你的结果已经很好了。

如果需要,您可以将所有 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

你真的不需要旋转,只需像你最初想的那样隔开你的列。您不会旋转以移动列,而是旋转以对分组数据执行聚合功能。

于 2012-09-12T20:35:41.027 回答
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
于 2012-09-12T20:43:56.553 回答