我有一个表格,我想要并排显示两次,其中表格按照不同的标准进行过滤。所以这是粗略的 (这不是真正的数据)表格现在看起来如何。
Name |1st|2nd|3rd
-------------------
Mark |1 | 2 | 3
Mark |2 | 4 | 6
Mark |3 | 6 | 8
Tim |6 | 2 | 4
Tim |10 | 11| 1
我希望结果看起来像这样:
Name1|1st|2nd|3rd Name2|1st|2nd|3rd
-------------------------------------
Mark | 1 | 2 | 3 | Tim | 6 | 2 | 4
Mark | 2 | 4 | 6 | Tim | 10| 11| 1
Mark | 3 | 6 | 8 |
所以我可以看到Mark和Tim的结果并排。
当我进行完全外部连接时,我得到它以便我看到表的第一个版本的所有结果,第二个版本为null,结果表中反之低。这是预期的,但我想知道如何尽可能地展示它我想要的方式。使用t-sql。
答案 0 :(得分:0)
尝试使用union。
Union避免显示重复数据,但可以将其移动到您要求的列。
如果您可以发布完整查询,我可以更准确地演示。
答案 1 :(得分:0)
--creating table
with myTable as (
select 'mark' as Name , 1 as [1st] , 2 as [2nd] , 3 as [3rd]
union
select 'mark' as Name , 2 as [1st] , 4 as [2nd] , 6 as [3rd]
union
select 'mark' as Name , 3 as [1st] , 6 as [2nd] , 8 as [3rd]
union
select 'Tim' as Name , 6 as [1st] , 2 as [2nd] , 4 as [3rd]
union
select 'Tim' as Name , 10 as [1st] , 11 as [2nd] , 1 as [3rd]
)
--query
select * from (
select ROW_NUMBER()over (order by a.name) as Row , * from myTable as a
where Name= 'Mark' )x
left join (
select ROW_NUMBER()over (order by a.name) as Row , * from myTable as a
where Name= 'Tim' )y
on x.Row = y.Row
以下是结果:
Row Name 1st 2nd 3rd Row Name 1st 2nd 3rd
-------------------- ---- ----------- ----------- ----------- -------------------- ---- ----------- ----------- -----------
1 mark 1 2 3 1 Tim 6 2 4
2 mark 2 4 6 2 Tim 10 11 1
3 mark 3 6 8 NULL NULL NULL NULL NULL