1

在这里,我有 2 个查询:

SELECT bk, search, ckey, dn
FROM 
   (SELECT t1_bk_no AS 'bk', 
         t1_full_key AS 'ckey', 
         CAST(t1_info1 AS VARCHAR(100)) AS [1],
         CAST(t1_info2 AS VARCHAR(100)) AS [2],
         CAST(t1_info3 AS VARCHAR(100)) AS [3],
         CAST(t1_info4 AS VARCHAR(100)) AS [4],
         CAST(t1_info5 AS VARCHAR(100)) AS [5]
   FROM dbo.FirstTable) PNT
   UNPIVOT( searcg FOR dn IN ( [1],[2],[3],[4],[5] ) ) AS UPV
WHERE search NOT IN ( '0', '999999999', '') AND search IS NOT NULL

SELECT bk, search, ckey, dn
FROM 
   (SELECT t2_bk_no AS 'bk', 
         t2_full_key AS 'ckey', 
         CAST(t2_info1 AS VARCHAR(100)) AS [6],
         CAST(t2_info2 AS VARCHAR(100)) AS [7],
         CAST(t2_info3 AS VARCHAR(100)) AS [8]
   FROM dbo.SecondTable) PNT
   UNPIVOT( search FOR dnIN ( [1],[5],[7] ) ) AS UPV
WHERE search NOT IN ( '0', '999999999', '') AND search IS NOT NULL

基本上我想加入这两张桌子。努力寻找放置连接语句的位置。

任何帮助表示赞赏,谢谢。

4

1 回答 1

0

在没有看到所需结果的情况下,您似乎可以使用 UNION ALL 查询,因为列看起来是相同的:

SELECT bk, search, ckey, dn
FROM 
(
  SELECT t1_bk_no AS 'bk', 
         t1_full_key AS 'ckey', 
         CAST(t1_info1 AS VARCHAR(100)) AS [1],
         CAST(t1_info2 AS VARCHAR(100)) AS [2],
         CAST(t1_info3 AS VARCHAR(100)) AS [3],
         CAST(t1_info4 AS VARCHAR(100)) AS [4],
         CAST(t1_info5 AS VARCHAR(100)) AS [5]
   FROM dbo.FirstTable
) PNT
UNPIVOT( search FOR dn IN ( [1],[2],[3],[4],[5] ) ) AS UPV
WHERE search NOT IN ( '0', '999999999', '') AND search IS NOT NULL
UNION ALL
SELECT bk, search, ckey, dn
FROM 
(
   SELECT t2_bk_no AS 'bk', 
         t2_full_key AS 'ckey', 
         CAST(t2_info1 AS VARCHAR(100)) AS [6],
         CAST(t2_info2 AS VARCHAR(100)) AS [7],
         CAST(t2_info3 AS VARCHAR(100)) AS [8]
   FROM dbo.SecondTable
) PNT
UNPIVOT( search FOR dnIN ( [1],[5],[7] ) ) AS UPV
WHERE search NOT IN ( '0', '999999999', '') AND search IS NOT NULL;
于 2013-06-04T10:24:20.277 回答