我试图编写一个JOIN来生成一个结果集,按照下面的输出示例,但没有 WHERE子句,我只是为了显示正确的数据应该是什么样子。如果排除WHERE子句,则会给出错误的结果。
#TBlA表列出假设站点上的SQL Server实例,其中包含SQL Server版本和内部版本号,例如。 '12 .0.5579.0'
#TBlB表是所有(本示例中已简化)SQL Server版本和内部版本号的列表。
目的是获取一个结果集,列出每个SQL Server实例,SQLVersion和ProductVersion(Build)以及更高<#bbB中可用的任何ProductVersion(等同于Service Pack和累积更新) / strong>比给定SQL实例的Version和ProductVersion的当前ProductVersion。
如果查看示例结果集,它应该相对于#TblB中较高的ProductVersion行重复#TblA行。顺便提一下,这将允许Reporting Services中的Grouped报告。
SET NOCOUNT ON
GO
IF OBJECT_ID('tempdb..#tblA') IS NOT NULL
DROP TABLE #tblA
create table #tblA -- build info for specific servers
(
SQLInstance varchar(128),
SQLVersion varchar(32),
ProductVersion varchar(32)
)
GO
INSERT INTO #tblA VALUES ( 'MyServer1', 'SQL Server 2016', '13.0.4001.0' )
INSERT INTO #tblA VALUES ( 'MyServer2', 'SQL Server 2016', '13.0.4001.0' )
INSERT INTO #tblA VALUES ( 'MyServer3', 'SQL Server 2014', '12.0.2000.8' )
INSERT INTO #tblA VALUES ( 'MyServer4', 'SQL Server 2014', '12.0.5000.0' )
INSERT INTO #tblA VALUES ( 'MyServer5', 'SQL Server 2012', '11.0.6020.0' )
INSERT INTO #tblA VALUES ( 'MyServer6', 'SQL Server 2012', '11.0.5058.0' )
GO
IF OBJECT_ID('tempdb..#tblB') IS NOT NULL
DROP TABLE #tblB
create table #tblB -- ALL builds available
(
SQLVersion varchar(32),
ProductVersion varchar(32)
)
INSERT INTO #tblB VALUES ( 'SQL Server 2017', '14.0.3025.34' )
INSERT INTO #tblB VALUES ( 'SQL Server 2017', '14.0.3023.8' )
INSERT INTO #tblB VALUES ( 'SQL Server 2017', '14.0.3022.28' )
INSERT INTO #tblB VALUES ( 'SQL Server 2017', '14.0.3015.40' )
INSERT INTO #tblB VALUES ( 'SQL Server 2017', '14.0.3008.27' )
INSERT INTO #tblB VALUES ( 'SQL Server 2017', '14.0.3006.16' )
INSERT INTO #tblB VALUES ( 'SQL Server 2016', '13.0.5026.0' )
INSERT INTO #tblB VALUES ( 'SQL Server 2016', '13.0.4474.0' )
INSERT INTO #tblB VALUES ( 'SQL Server 2016', '13.0.4466.4' )
INSERT INTO #tblB VALUES ( 'SQL Server 2016', '13.0.4457.0' )
INSERT INTO #tblB VALUES ( 'SQL Server 2016', '13.0.4451.0' )
INSERT INTO #tblB VALUES ( 'SQL Server 2016', '13.0.4446.0' )
INSERT INTO #tblB VALUES ( 'SQL Server 2016', '13.0.4435.0' )
INSERT INTO #tblB VALUES ( 'SQL Server 2016', '13.0.4422.0' )
INSERT INTO #tblB VALUES ( 'SQL Server 2016', '13.0.4411.0' )
INSERT INTO #tblB VALUES ( 'SQL Server 2016', '13.0.4001.0' )
INSERT INTO #tblB VALUES ( 'SQL Server 2014', '12.0.5579.0' )
INSERT INTO #tblB VALUES ( 'SQL Server 2014', '12.0.5571.0' )
INSERT INTO #tblB VALUES ( 'SQL Server 2014', '12.0.5557.0' )
INSERT INTO #tblB VALUES ( 'SQL Server 2014', '12.0.5556.0' )
INSERT INTO #tblB VALUES ( 'SQL Server 2014', '12.0.5553.0' )
INSERT INTO #tblB VALUES ( 'SQL Server 2014', '12.0.5546.0' )
INSERT INTO #tblB VALUES ( 'SQL Server 2014', '12.0.5540.0' )
INSERT INTO #tblB VALUES ( 'SQL Server 2014', '12.0.5538.0' )
INSERT INTO #tblB VALUES ( 'SQL Server 2014', '12.0.5522.0' )
INSERT INTO #tblB VALUES ( 'SQL Server 2014', '12.0.5511.0' )
INSERT INTO #tblB VALUES ( 'SQL Server 2014', '12.0.5000.0' )
INSERT INTO #tblB VALUES ( 'SQL Server 2014', '12.0.4522.0' )
INSERT INTO #tblB VALUES ( 'SQL Server 2014', '12.0.4511.0' )
INSERT INTO #tblB VALUES ( 'SQL Server 2014', '12.0.2000.8' )
INSERT INTO #tblB VALUES ( 'SQL Server 2012', '11.0.7001.0' )
INSERT INTO #tblB VALUES ( 'SQL Server 2012', '11.0.6607.3' )
INSERT INTO #tblB VALUES ( 'SQL Server 2012', '11.0.6598.0' )
INSERT INTO #tblB VALUES ( 'SQL Server 2012', '11.0.6594.0' )
INSERT INTO #tblB VALUES ( 'SQL Server 2012', '11.0.6579.0' )
INSERT INTO #tblB VALUES ( 'SQL Server 2012', '11.0.6567.0' )
INSERT INTO #tblB VALUES ( 'SQL Server 2012', '11.0.6544.0' )
INSERT INTO #tblB VALUES ( 'SQL Server 2012', '11.0.6540.0' )
INSERT INTO #tblB VALUES ( 'SQL Server 2012', '11.0.6537.0' )
INSERT INTO #tblB VALUES ( 'SQL Server 2012', '11.0.6523.0' )
INSERT INTO #tblB VALUES ( 'SQL Server 2012', '11.0.6518.0' )
INSERT INTO #tblB VALUES ( 'SQL Server 2012', '11.0.6020.0' )
INSERT INTO #tblB VALUES ( 'SQL Server 2012', '11.0.5678.0' )
查询是:
-- The following gives a correct dataset in the format required for grouped reporting but ONLY because of the WHERE clause
-- Want a neat query that will produce the same format for all versions without the WHERE clause
SELECT
a.*,
b.*
FROM
#tblB b
left join #tblA a
ON b.ProductVersion > a.ProductVersion
AND a.SQLVersion = b.SQLVersion
WHERE
a.SQLVersion = 'SQL Server 2014'
输出: -
**SQLInstance SQLVersion ProductVersion SQLVersion ProductVersion**
MyServer3 SQL Server 2014 12.0.2000.8 SQL Server 2014 12.0.5579.0
MyServer3 SQL Server 2014 12.0.2000.8 SQL Server 2014 12.0.5571.0
MyServer3 SQL Server 2014 12.0.2000.8 SQL Server 2014 12.0.5557.0
MyServer3 SQL Server 2014 12.0.2000.8 SQL Server 2014 12.0.5556.0
MyServer3 SQL Server 2014 12.0.2000.8 SQL Server 2014 12.0.5553.0
MyServer3 SQL Server 2014 12.0.2000.8 SQL Server 2014 12.0.5546.0
MyServer3 SQL Server 2014 12.0.2000.8 SQL Server 2014 12.0.5540.0
MyServer3 SQL Server 2014 12.0.2000.8 SQL Server 2014 12.0.5538.0
MyServer3 SQL Server 2014 12.0.2000.8 SQL Server 2014 12.0.5522.0
MyServer3 SQL Server 2014 12.0.2000.8 SQL Server 2014 12.0.5511.0
MyServer3 SQL Server 2014 12.0.2000.8 SQL Server 2014 12.0.5000.0
MyServer3 SQL Server 2014 12.0.2000.8 SQL Server 2014 12.0.4522.0
MyServer3 SQL Server 2014 12.0.2000.8 SQL Server 2014 12.0.4511.0
MyServer4 SQL Server 2014 12.0.5000.0 SQL Server 2014 12.0.5579.0
MyServer4 SQL Server 2014 12.0.5000.0 SQL Server 2014 12.0.5571.0
MyServer4 SQL Server 2014 12.0.5000.0 SQL Server 2014 12.0.5557.0
MyServer4 SQL Server 2014 12.0.5000.0 SQL Server 2014 12.0.5556.0
MyServer4 SQL Server 2014 12.0.5000.0 SQL Server 2014 12.0.5553.0
MyServer4 SQL Server 2014 12.0.5000.0 SQL Server 2014 12.0.5546.0
MyServer4 SQL Server 2014 12.0.5000.0 SQL Server 2014 12.0.5540.0
MyServer4 SQL Server 2014 12.0.5000.0 SQL Server 2014 12.0.5538.0
MyServer4 SQL Server 2014 12.0.5000.0 SQL Server 2014 12.0.5522.0
MyServer4 SQL Server 2014 12.0.5000.0 SQL Server 2014 12.0.5511.0
答案 0 :(得分:1)
这是你想要的吗?
SELECT a.*, b.*
FROM #tblB b JOIN
#tblA a
ON b.ProductVersion > a.ProductVersion AND
a.SQLVersion = b.SQLVersion
ORDER BY a.SQLVersion;
我无法弄清楚为什么在最终查询中需要LEFT JOIN
。由于INNER JOIN
子句,您的版本实际上是WHERE
。