[英]Combining Columns from different tables
I've write a SQL
code to combine several columns
from different tables. 我编写了一个
SQL
代码来组合来自不同表的几columns
。
SELECT *
FROM
(
SELECT PD_BARCODE
FROM docsadm.PD_BARCODE
WHERE SYSTEM_ID = 11660081
) t,
(
SELECT A_JAHRE
FROM docsadm.A_PD_DATENSCHUTZ
WHERE system_ID = 2066
) t2,
(
SELECT PD_PART_NAME
FROM docsadm.PD_FILE_PART
WHERE system_id = 11660082
) t3;
code works fine but if one of my where clause is not found in a table,the result is null even the other columns have value. 代码可以正常工作,但是如果在表中未找到我的where子句之一,则即使其他列也有值,结果也为null。 How you can solve this problem?
如何解决这个问题?
It looks like you are doing a cross join between the three subquery tables. 看来您正在三个子查询表之间进行交叉联接。 This would probably only yield output which makes sense if each subquery return a single value.
如果每个子查询返回单个值,这可能只会产生有意义的输出。 I might suggest instead that you use a
UNION ALL
here: 我可能建议您在这里使用
UNION ALL
:
SELECT ISNULL(PD_BARCODE, 'NA' AS value
FROM docsadm.PD_BARCODE WHERE SYSTEM_ID = 11660081
UNION ALL
SELECT ISNULL(A_JAHRE, 'NA')
FROM docsadm.A_PD_DATENSCHUTZ WHERE system_ID = 2066
UNION ALL
SELECT ISULL(PD_PART_NAME, 'NA')
FROM docsadm.PD_FILE_PART WHERE system_id = 11660082
The above union query might require a slight modification if the three columns being select don't all have the same type (which I assume to be varchar
in my query). 如果要选择的三列的类型都不相同(我假设在查询中为
varchar
),则上述联合查询可能需要进行一些修改。
If you really need these three points of data as separate columns, then you can just include the three subqueries as items in an outer select: 如果确实需要将这三个数据点作为单独的列,则可以将这三个子查询作为项包含在外部选择中:
SELECT
(SELECT ISNULL(PD_BARCODE, 'NA')
FROM docsadm.PD_BARCODE WHERE SYSTEM_ID = 11660081) AS PD_BARCODE,
(SELECT ISNULL(A_JAHRE, 'NA')
FROM docsadm.A_PD_DATENSCHUTZ WHERE system_ID = 2066) AS A_JAHRE,
(SELECT ISNULL(PD_PART_NAME, 'NA')
FROM docsadm.PD_FILE_PART WHERE system_id = 11660082) AS PD_PART_NAME;
Note that as the above is written we simply including the subqueries as values in the select statement. 请注意,如上文所述,我们只是将子查询作为值包含在select语句中。 But as you wrote your original query, you are joining the subqueries as separate tables.
但是,当您编写原始查询时,您正在将子查询作为单独的表加入。
Here is Query
. 这是
Query
。
You can replace the word ' empty
' by your required word or value 您可以将“
empty
”一词替换为所需的词或值
SELECT isnull( ( SELECT PD_BARCODE FROM docsadm.PD_BARCODE WHERE SYSTEM_ID = 11660081 ), 'Empty') AS PD_BARCODE, isnull( ( SELECT A_JAHRE FROM docsadm.A_PD_DATENSCHUTZ WHERE system_ID = 2066 ), 'Empty') AS A_JAHRE, isnull( ( SELECT PD_PART_NAME FROM docsadm.PD_FILE_PART WHERE system_id = 11660082 ), 'Empty') AS PD_PART_NAME;
This is a special case since you would be getting only one value per query of yours which you are trying to put up as column. 这是一种特殊情况,因为您要查询的每个查询将只获得一个值作为列。 In this case, you can use SQL PIVOT clause with UNION ALL of your queries like shown below.
在这种情况下,可以将SQL PIVOT子句与UNION ALL的查询一起使用,如下所示。 MIN can be used for aggregation in this case.
在这种情况下,MIN可用于聚合。
This would mean, you can get your data row-wise as you like, even for multiple different fields and then pivot it into columns in one go. 这意味着,即使对于多个不同的字段,也可以根据需要按行获取数据,然后一次性将其旋转为列。
SELECT * FROM
(
SELECT 'PD_BARCODE' as KeyItem, PD_BARCODE Name from docsadm.PD_BARCODE
where SYSTEM_ID=11660081
UNION ALL
SELECT 'A_JAHRE', A_JAHRE from docsadm.A_PD_DATENSCHUTZ
where system_ID=2066
UNION ALL
select 'PD_PART_NAME', PD_PART_NAME from docsadm.PD_FILE_PART
where system_id=11660082
) VTABLE
PIVOT(MIN(Name)
FOR KeyItem IN ([PD_BARCODE], [A_JAHRE], [PD_PART_NAME])) as pivotData;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.