SQL Server从多个表中选择列名
我在SQL Server中有三个表,其结构如下: col1 col2 a1 a2。。。一 col1 col2 b1 b2。。。bn, col1 col2 c1 c2。。。cn 前两个记录是相同的,col1和col2,但是表的长度不同 我需要选择表的列名,我试图实现的结果如下: col1,col2,a1,b1,c1,a2,b2,c2SQL Server从多个表中选择列名,sql,sql-server,Sql,Sql Server,我在SQL Server中有三个表,其结构如下: col1 col2 a1 a2。。。一 col1 col2 b1 b2。。。bn, col1 col2 c1 c2。。。cn 前两个记录是相同的,col1和col2,但是表的长度不同 我需要选择表的列名,我试图实现的结果如下: col1,col2,a1,b1,c1,a2,b2,c2 有办法吗?这是可能的,但结果被合并到一列三个表中 比如说 SELECT A.col1 +'/' +B.col1 +'/' + C.col1 As Col1 , A.
有办法吗?这是可能的,但结果被合并到一列三个表中 比如说
SELECT A.col1 +'/' +B.col1 +'/' + C.col1 As Col1 ,
A.col2 +'/' +B.col2 +'/' + C.col2 As col2 ,a1, b1, c1, a2, b2, c2 ,
* FROM A
INNER JOIN B
ON A.ID =B.ID
INNER JOIN C
ON C.ID = B.ID
SQL Server不是创建通用结果集的正确工具。引擎需要提前知道会发生什么。嗯,您可以尝试使用动态SQL找到解决方案 我想提出两种不同的方法 这两种方法都可以处理任意数量的表,只要它们都具有具有适当类型的col1和col2列 让我们先创建一个简单的mokcup场景: 非常务实的方法: 尝试以下简单的完全外部联接: 结果
+------+------+-----------+-----------+------+------+------------+-------------------------+
| col1 | col2 | SomeMore1 | SomeMore2 | col1 | col2 | OtherType1 | OtherType2 |
+------+------+-----------+-----------+------+------+------------+-------------------------+
| 1 | 1 | blah 1.1 | blub 1.1 | 1 | 1 | 101 | 2019-03-08 10:53:20.257 |
+------+------+-----------+-----------+------+------+------------+-------------------------+
| 1 | 2 | blah 1.2 | blub 1.2 | 1 | 2 | 102 | 2019-03-09 10:53:20.257 |
+------+------+-----------+-----------+------+------+------------+-------------------------+
| 1 | 100 | not in t2 | not in t2 | NULL | NULL | NULL | NULL |
+------+------+-----------+-----------+------+------+------------+-------------------------+
| NULL | NULL | NULL | NULL | 1 | 200 | 200 | 2019-09-24 10:53:20.257 |
+------+------+-----------+-----------+------+------+------------+-------------------------+
+------+------+-----------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------+
| col1 | col2 | Content1 | Content2 |
+------+------+-----------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------+
| 1 | 1 | <row><col1>1</col1><col2>1</col2><SomeMore1>blah 1.1</SomeMore1><SomeMore2>blub 1.1</SomeMore2></row> | <row><col1>1</col1><col2>1</col2><OtherType1>101</OtherType1><OtherType2>2019-03-08T11:03:49.877</OtherType2></row> |
+------+------+-----------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------+
| 1 | 2 | <row><col1>1</col1><col2>2</col2><SomeMore1>blah 1.2</SomeMore1><SomeMore2>blub 1.2</SomeMore2></row> | <row><col1>1</col1><col2>2</col2><OtherType1>102</OtherType1><OtherType2>2019-03-09T11:03:49.877</OtherType2></row> |
+------+------+-----------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------+
| 1 | 100 | <row><col1>1</col1><col2>100</col2><SomeMore1>not in t2</SomeMore1><SomeMore2>not in t2</SomeMore2></row> | NULL |
+------+------+-----------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------+
| 1 | 200 | NULL | <row><col1>1</col1><col2>200</col2><OtherType1>200</OtherType1><OtherType2>2019-09-24T11:03:49.877</OtherType2></row> |
+------+------+-----------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------+
但您必须处理非唯一的列名。。。此时,动态创建的语句可以提供帮助
使用容器类型XML的通用方法
当您事先不知道结果时,可以将结果打包到容器中。这允许RDBMS有一个清晰的结构,并将如何处理此集合的问题转移给消费者
cte将读取所有现有的col1和col2对
这对值的每个表行都作为XML插入
任何表中不存在的对显示为NULL
试试这个
WITH AllDistinctCol1Col2Values AS
(
SELECT col1,col2 FROM @mockup1
UNION ALL
SELECT col1,col2 FROM @mockup2
--add all your tables here
)
SELECT col1,col2
,(SELECT * FROM @mockup1 x WHERE c1c2.col1=x.col1 AND c1c2.col2=x.col2 FOR XML PATH('row'),TYPE) AS Content1
,(SELECT * FROM @mockup2 x WHERE c1c2.col1=x.col1 AND c1c2.col2=x.col2 FOR XML PATH('row'),TYPE) AS Content2
FROM AllDistinctCol1Col2Values c1c2
GROUP BY col1,col2;
结果
+------+------+-----------+-----------+------+------+------------+-------------------------+
| col1 | col2 | SomeMore1 | SomeMore2 | col1 | col2 | OtherType1 | OtherType2 |
+------+------+-----------+-----------+------+------+------------+-------------------------+
| 1 | 1 | blah 1.1 | blub 1.1 | 1 | 1 | 101 | 2019-03-08 10:53:20.257 |
+------+------+-----------+-----------+------+------+------------+-------------------------+
| 1 | 2 | blah 1.2 | blub 1.2 | 1 | 2 | 102 | 2019-03-09 10:53:20.257 |
+------+------+-----------+-----------+------+------+------------+-------------------------+
| 1 | 100 | not in t2 | not in t2 | NULL | NULL | NULL | NULL |
+------+------+-----------+-----------+------+------+------------+-------------------------+
| NULL | NULL | NULL | NULL | 1 | 200 | 200 | 2019-09-24 10:53:20.257 |
+------+------+-----------+-----------+------+------+------------+-------------------------+
+------+------+-----------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------+
| col1 | col2 | Content1 | Content2 |
+------+------+-----------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------+
| 1 | 1 | <row><col1>1</col1><col2>1</col2><SomeMore1>blah 1.1</SomeMore1><SomeMore2>blub 1.1</SomeMore2></row> | <row><col1>1</col1><col2>1</col2><OtherType1>101</OtherType1><OtherType2>2019-03-08T11:03:49.877</OtherType2></row> |
+------+------+-----------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------+
| 1 | 2 | <row><col1>1</col1><col2>2</col2><SomeMore1>blah 1.2</SomeMore1><SomeMore2>blub 1.2</SomeMore2></row> | <row><col1>1</col1><col2>2</col2><OtherType1>102</OtherType1><OtherType2>2019-03-09T11:03:49.877</OtherType2></row> |
+------+------+-----------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------+
| 1 | 100 | <row><col1>1</col1><col2>100</col2><SomeMore1>not in t2</SomeMore1><SomeMore2>not in t2</SomeMore2></row> | NULL |
+------+------+-----------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------+
| 1 | 200 | NULL | <row><col1>1</col1><col2>200</col2><OtherType1>200</OtherType1><OtherType2>2019-09-24T11:03:49.877</OtherType2></row> |
+------+------+-----------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------+
这是不可能的,事实上,这是一个毫无意义的问题,除非你也告诉我们这三个表之间的关系。你说一个表有不同的长度是什么意思?实际样本数据和预期结果可能有助于我们了解您在这里追求的目标,但是,目前还不清楚。三个表中的col1和col2是相同的。表1:col1,col2,a1,a2,a3。。。an,表2:col1,col2,b1,b2,b3。。。bn,表3:col1,col2,c1,c2,c3。。。cn我需要选择列标题并实现如下结果:col1、col2、a1、b1、c1、a2、b2、c2、a3、b3、c3等等。很抱歉,我没有SQL Server方面的经验。您有3个表,但不告诉我们如何加入这些表。理想情况下,您可以提供用于创建这些表的实际SQL代码,并指定这些表如何连接以获得所需的输出。