在SQL Server的不同列中选择一列数据

在SQL Server的不同列中选择一列数据,sql,sql-server,Sql,Sql Server,我必须匹配两个表,表1和表2,对于表1的一行,我在表2中有两行或更多行,我想在一行中显示它们 现在我有以下几点: Select a.[ID], b.[Description] From table1 a, table2 b Where a.[ID] = b.[ID]; 输出: [ID] | [Description] -----+-------------- 1 | Fee 1 | Domestic Fee 2 | Fee 2 | Inte

我必须匹配两个表,表1和表2,对于表1的一行,我在表2中有两行或更多行,我想在一行中显示它们

现在我有以下几点:

Select
    a.[ID], b.[Description]
From 
    table1 a, table2 b 
Where 
    a.[ID] = b.[ID];
输出:

[ID] | [Description]
-----+--------------
1    | Fee
1    | Domestic Fee
2    | Fee
2    | International Fee 
我想得到以下结果

[ID] | [Description1] | [Description2]
-----+----------------+---------------
1    | Fee            | Domestic Fee
2    | Fee            | International Fee

提前感谢您:

如果您有多个描述,那么您可以使用Pivot,如下所示:

Select
    a.[ID],a.[Description], b.[Description]
From 
    table1 a left outer join table2 b 
on
    a.[ID] = b.[ID];``
Select * from (
    Select *, RowN = Concat('Description', Row_Number() over (partition by Id order by Id)) from #description ) a
    pivot (max([Description]) for RowN in ([Description1],[Description2])) p
+----+--------------+-------------------+
| Id | Description1 |   Description2    |
+----+--------------+-------------------+
|  1 | Fee          | Domestic Fee      |
|  2 | Fee          | International Fee |
+----+--------------+-------------------+
输出如下:

Select * from (
    Select *, RowN = Concat('Description', Row_Number() over (partition by Id order by Id)) from #description ) a
    pivot (max([Description]) for RowN in ([Description1],[Description2])) p
+----+--------------+-------------------+
| Id | Description1 |   Description2    |
+----+--------------+-------------------+
|  1 | Fee          | Domestic Fee      |
|  2 | Fee          | International Fee |
+----+--------------+-------------------+

如果有多个描述,则可以按如下方式使用Pivot:

Select * from (
    Select *, RowN = Concat('Description', Row_Number() over (partition by Id order by Id)) from #description ) a
    pivot (max([Description]) for RowN in ([Description1],[Description2])) p
+----+--------------+-------------------+
| Id | Description1 |   Description2    |
+----+--------------+-------------------+
|  1 | Fee          | Domestic Fee      |
|  2 | Fee          | International Fee |
+----+--------------+-------------------+
输出如下:

Select * from (
    Select *, RowN = Concat('Description', Row_Number() over (partition by Id order by Id)) from #description ) a
    pivot (max([Description]) for RowN in ([Description1],[Description2])) p
+----+--------------+-------------------+
| Id | Description1 |   Description2    |
+----+--------------+-------------------+
|  1 | Fee          | Domestic Fee      |
|  2 | Fee          | International Fee |
+----+--------------+-------------------+
试试这个:

select ID, DESCRIPTION1, DESCRIPTION2
from
(
    Select a.[ID]
      ,b.[Description]
      ,'DESCRIPTION' + CAST(ROW_NUMBER() OVER(PARTITION BY a.ID ORDER BY a.ID) AS VARCHAR(255))AS RN
  from table1 a 
  JOIN  table2 b ON  a.[ID] = b.[ID] 
) d
pivot
(
  max([Description])
  for RN in (DESCRIPTION1,DESCRIPTION2)
) piv;
试试这个:

select ID, DESCRIPTION1, DESCRIPTION2
from
(
    Select a.[ID]
      ,b.[Description]
      ,'DESCRIPTION' + CAST(ROW_NUMBER() OVER(PARTITION BY a.ID ORDER BY a.ID) AS VARCHAR(255))AS RN
  from table1 a 
  JOIN  table2 b ON  a.[ID] = b.[ID] 
) d
pivot
(
  max([Description])
  for RN in (DESCRIPTION1,DESCRIPTION2)
) piv;

这是我与交叉应用操作员的建议

数据准备:

create table tst_1 (id int, dsc nvarchar(30))

insert into tst_1 (id, dsc)
values (1,'Fee'),(1,'Domestic Fee'),(2,'Fee'),(2,'International Fee')
下一步简单选择jon show您要查找的数据:

select t1.id, t1.dsc, x.dsc
from tst_1 t1
cross apply ( select row_number() over (order by id) as lp
                                        ,id
                                        ,dsc
                            from tst_1 )x
where x.id = t1.id and x.dsc <> t1.dsc
and lp%2 = 0

tst_1可以是基于选择的视图。。。从您的问题。

这是我与交叉应用操作员的建议

数据准备:

create table tst_1 (id int, dsc nvarchar(30))

insert into tst_1 (id, dsc)
values (1,'Fee'),(1,'Domestic Fee'),(2,'Fee'),(2,'International Fee')
下一步简单选择jon show您要查找的数据:

select t1.id, t1.dsc, x.dsc
from tst_1 t1
cross apply ( select row_number() over (order by id) as lp
                                        ,id
                                        ,dsc
                            from tst_1 )x
where x.id = t1.id and x.dsc <> t1.dsc
and lp%2 = 0

tst_1可以是基于选择的视图。。。从您的问题中。

搜索PIVOT。如果有两个以上的描述怎么办25年前,在ANSI-92 SQL标准中,旧样式的逗号分隔表列表样式被正确的ANSI连接语法所取代,它的使用不鼓励使用PIVOT,但它需要一些聚合函数,我不需要这些。对于Table1搜索PIVOT,每行将有固定数量的描述。如果有两个以上的描述怎么办25年前,在ANSI-92 SQL标准中,旧样式的逗号分隔表列表样式被正确的ANSI连接语法所取代,它的使用不鼓励使用PIVOT,但它需要一些聚合函数,我不需要这些。对于表1,每行将有固定数量的描述为什么是左外部连接而不是内部连接?表1中是否有[description]字段?我认为你在做假设。这只是一个假设。为什么是左外连接而不是内连接?表1中有[description]字段吗?我认为你在做假设,这只是一个假设。你必须做动态sql。如果有第三个描述,会发生什么?它没有在'for'子句中指定。您必须创建动态sql。如果有第三种描述,会发生什么?在“for”子句中没有指定。