将5个查询合并为1

时间:2017-07-27 05:58:27

标签: sql sql-server-2014

我运行了以下代码(参见下图中的输出)。而我想要做的是将所有5个mysql_cteX表合并到一起,将X = 1,2,3,4,5合并为一个完整的查询表。

--------------------------------------
-- MERGING ALL QUERIES INTO 1 TABLE --
--------------------------------------
--Creating temp tables for common maturity TickerID data
declare @product nvarchar(max) = 'CCoal'
declare @product2 nvarchar(max)

set @mysql1 = '
if object_id(''tempdb..#mysql_cte1'') is not null drop table #mysql_cte1 
create table #mysql_cte1(DateTime datetime,'+quotename(Concat(@product, ' ', 'TickerID'))+' int,'+quotename(Concat(@product2, ' ', 'TickerID'))+' int,'+quotename(Concat(@product, ' ', 'Bid'))+' float,'+quotename(Concat(@product, ' ', 'Ask'))+' float,'+quotename(Concat(@product2, ' ', 'Bid'))+' float,'+quotename(Concat(@product2, ' ', 'Ask'))+' float, '+quotename(Concat(@product, ' ', 'Bid - ', @product2, ' ','Ask'))+' float,'+quotename(Concat(@product, ' ', 'Ask - ', @product2, ' ','Bid'))+' float)
select a.mtime as DateTime,
    a.tickerid as '+quotename(Concat(@product, ' ', 'TickerID'))+',
    b.tickerid as '+quotename(Concat(@product2, ' ', 'TickerID'))+',
    a.bid as '+quotename(Concat(@product, ' ', 'Bid'))+',
    a.ask as '+quotename(Concat(@product, ' ', 'Ask'))+',
    b.bid as '+quotename(Concat(@product2, ' ', 'Bid'))+',
    b.ask as '+quotename(Concat(@product2, ' ', 'Ask'))+',
    ISNULL(a.bid, 0) - ISNULL(b.ask, 0) as '+quotename(Concat(@product, ' ', 'Bid - ', @product2, ' ','Ask'))+',
    ISNULL(a.ask, 0) - ISNULL(b.bid, 0) as '+quotename(Concat(@product, ' ', 'Ask - ', @product2, ' ','Bid'))+'
from #Product_cte1 a left outer join #Product2_cte1 b 
on a.mtime = b.mtime
Order by DateTime DESC

'
set @mysql2 = '
if object_id(''tempdb..#mysql_cte2'') is not null drop table #mysql_cte2 
create table #mysql_cte2(DateTime datetime,'+quotename(Concat(@product, ' ', 'TickerID'))+' int,'+quotename(Concat(@product2, ' ', 'TickerID'))+' int,'+quotename(Concat(@product, ' ', 'Bid'))+' float,'+quotename(Concat(@product, ' ', 'Ask'))+' float,'+quotename(Concat(@product2, ' ', 'Bid'))+' float,'+quotename(Concat(@product2, ' ', 'Ask'))+' float, '+quotename(Concat(@product, ' ', 'Bid - ', @product2, ' ','Ask'))+' float,'+quotename(Concat(@product, ' ', 'Ask - ', @product2, ' ','Bid'))+' float)
select a.mtime as DateTime,
    a.tickerid as '+quotename(Concat(@product, ' ', 'TickerID'))+',
    b.tickerid as '+quotename(Concat(@product2, ' ', 'TickerID'))+',
    a.bid as '+quotename(Concat(@product, ' ', 'Bid'))+',
    a.ask as '+quotename(Concat(@product, ' ', 'Ask'))+',
    b.bid as '+quotename(Concat(@product2, ' ', 'Bid'))+',
    b.ask as '+quotename(Concat(@product2, ' ', 'Ask'))+',
    ISNULL(a.bid, 0) - ISNULL(b.ask, 0) as '+quotename(Concat(@product, ' ', 'Bid - ', @product2, ' ','Ask'))+',
    ISNULL(a.ask, 0) - ISNULL(b.bid, 0) as '+quotename(Concat(@product, ' ', 'Ask - ', @product2, ' ','Bid'))+'
from #Product_cte2 a left outer join #Product2_cte2 b 
on a.mtime = b.mtime
Order by DateTime DESC

'
set @mysql3 = '
if object_id(''tempdb..#mysql_cte3'') is not null drop table #mysql_cte3 
create table #mysql_cte3(DateTime datetime,'+quotename(Concat(@product, ' ', 'TickerID'))+' int,'+quotename(Concat(@product2, ' ', 'TickerID'))+' int,'+quotename(Concat(@product, ' ', 'Bid'))+' float,'+quotename(Concat(@product, ' ', 'Ask'))+' float,'+quotename(Concat(@product2, ' ', 'Bid'))+' float,'+quotename(Concat(@product2, ' ', 'Ask'))+' float, '+quotename(Concat(@product, ' ', 'Bid - ', @product2, ' ','Ask'))+' float,'+quotename(Concat(@product, ' ', 'Ask - ', @product2, ' ','Bid'))+' float)
select a.mtime as DateTime,
    a.tickerid as '+quotename(Concat(@product, ' ', 'TickerID'))+',
    b.tickerid as '+quotename(Concat(@product2, ' ', 'TickerID'))+',
    a.bid as '+quotename(Concat(@product, ' ', 'Bid'))+',
    a.ask as '+quotename(Concat(@product, ' ', 'Ask'))+',
    b.bid as '+quotename(Concat(@product2, ' ', 'Bid'))+',
    b.ask as '+quotename(Concat(@product2, ' ', 'Ask'))+',
    ISNULL(a.bid, 0) - ISNULL(b.ask, 0) as '+quotename(Concat(@product, ' ', 'Bid - ', @product2, ' ','Ask'))+',
    ISNULL(a.ask, 0) - ISNULL(b.bid, 0) as '+quotename(Concat(@product, ' ', 'Ask - ', @product2, ' ','Bid'))+'
from #Product_cte3 a left outer join #Product2_cte3 b 
on a.mtime = b.mtime
Order by DateTime DESC

'
set @mysql4 = '
if object_id(''tempdb..#mysql_cte4'') is not null drop table #mysql_cte4
create table #mysql_cte4(DateTime datetime,'+quotename(Concat(@product, ' ', 'TickerID'))+' int,'+quotename(Concat(@product2, ' ', 'TickerID'))+' int,'+quotename(Concat(@product, ' ', 'Bid'))+' float,'+quotename(Concat(@product, ' ', 'Ask'))+' float,'+quotename(Concat(@product2, ' ', 'Bid'))+' float,'+quotename(Concat(@product2, ' ', 'Ask'))+' float, '+quotename(Concat(@product, ' ', 'Bid - ', @product2, ' ','Ask'))+' float,'+quotename(Concat(@product, ' ', 'Ask - ', @product2, ' ','Bid'))+' float)
select a.mtime as DateTime,
    a.tickerid as '+quotename(Concat(@product, ' ', 'TickerID'))+',
    b.tickerid as '+quotename(Concat(@product2, ' ', 'TickerID'))+',
    a.bid as '+quotename(Concat(@product, ' ', 'Bid'))+',
    a.ask as '+quotename(Concat(@product, ' ', 'Ask'))+',
    b.bid as '+quotename(Concat(@product2, ' ', 'Bid'))+',
    b.ask as '+quotename(Concat(@product2, ' ', 'Ask'))+',
    ISNULL(a.bid, 0) - ISNULL(b.ask, 0) as '+quotename(Concat(@product, ' ', 'Bid - ', @product2, ' ','Ask'))+',
    ISNULL(a.ask, 0) - ISNULL(b.bid, 0) as '+quotename(Concat(@product, ' ', 'Ask - ', @product2, ' ','Bid'))+'
from #Product_cte4 a left outer join #Product2_cte4 b 
on a.mtime = b.mtime
Order by DateTime DESC

'
set @mysql5 = '
if object_id(''tempdb..#mysql_cte5'') is not null drop table #mysql_cte5 
create table #mysql_cte5(DateTime datetime,'+quotename(Concat(@product, ' ', 'TickerID'))+' int,'+quotename(Concat(@product2, ' ', 'TickerID'))+' int,'+quotename(Concat(@product, ' ', 'Bid'))+' float,'+quotename(Concat(@product, ' ', 'Ask'))+' float,'+quotename(Concat(@product2, ' ', 'Bid'))+' float,'+quotename(Concat(@product2, ' ', 'Ask'))+' float, '+quotename(Concat(@product, ' ', 'Bid - ', @product2, ' ','Ask'))+' float,'+quotename(Concat(@product, ' ', 'Ask - ', @product2, ' ','Bid'))+' float)
select a.mtime as DateTime,
    a.tickerid as '+quotename(Concat(@product, ' ', 'TickerID'))+',
    b.tickerid as '+quotename(Concat(@product2, ' ', 'TickerID'))+',
    a.bid as '+quotename(Concat(@product, ' ', 'Bid'))+',
    a.ask as '+quotename(Concat(@product, ' ', 'Ask'))+',
    b.bid as '+quotename(Concat(@product2, ' ', 'Bid'))+',
    b.ask as '+quotename(Concat(@product2, ' ', 'Ask'))+',
    ISNULL(a.bid, 0) - ISNULL(b.ask, 0) as '+quotename(Concat(@product, ' ', 'Bid - ', @product2, ' ','Ask'))+',
    ISNULL(a.ask, 0) - ISNULL(b.bid, 0) as '+quotename(Concat(@product, ' ', 'Ask - ', @product2, ' ','Bid'))+'
from #Product_cte5 a left outer join #Product2_cte5 b 
on a.mtime = b.mtime
Order by DateTime DESC

'

exec(@mysql1)
exec(@mysql2)
exec(@mysql3)
exec(@mysql4)
exec(@mysql5)
;

--Join the 5 merged queries together in parallel for comparison
set @Mysql = '
Select DateTime,'+quotename(Concat(@product, ' ', 'TickerID'))+','+quotename(Concat(@product2, ' ', 'TickerID'))+','+quotename(Concat(@product, ' ', 'Bid'))+','+quotename(Concat(@product, ' ', 'Ask'))+','+quotename(Concat(@product2, ' ', 'Bid'))+','+quotename(Concat(@product2, ' ', 'Ask'))+','+quotename(Concat(@product, ' ', 'Bid - ', @product2, ' ','Ask'))+','+quotename(Concat(@product, ' ', 'Ask - ', @product2, ' ','Bid'))+' from #mysql_cte1
UNION ALL
Select DateTime,'+quotename(Concat(@product, ' ', 'TickerID'))+','+quotename(Concat(@product2, ' ', 'TickerID'))+','+quotename(Concat(@product, ' ', 'Bid'))+','+quotename(Concat(@product, ' ', 'Ask'))+','+quotename(Concat(@product2, ' ', 'Bid'))+','+quotename(Concat(@product2, ' ', 'Ask'))+','+quotename(Concat(@product, ' ', 'Bid - ', @product2, ' ','Ask'))+','+quotename(Concat(@product, ' ', 'Ask - ', @product2, ' ','Bid'))+' from #mysql_cte2
UNION ALL
Select DateTime,'+quotename(Concat(@product, ' ', 'TickerID'))+','+quotename(Concat(@product2, ' ', 'TickerID'))+','+quotename(Concat(@product, ' ', 'Bid'))+','+quotename(Concat(@product, ' ', 'Ask'))+','+quotename(Concat(@product2, ' ', 'Bid'))+','+quotename(Concat(@product2, ' ', 'Ask'))+','+quotename(Concat(@product, ' ', 'Bid - ', @product2, ' ','Ask'))+','+quotename(Concat(@product, ' ', 'Ask - ', @product2, ' ','Bid'))+' from #mysql_cte3
UNION ALL
Select DateTime,'+quotename(Concat(@product, ' ', 'TickerID'))+','+quotename(Concat(@product2, ' ', 'TickerID'))+','+quotename(Concat(@product, ' ', 'Bid'))+','+quotename(Concat(@product, ' ', 'Ask'))+','+quotename(Concat(@product2, ' ', 'Bid'))+','+quotename(Concat(@product2, ' ', 'Ask'))+','+quotename(Concat(@product, ' ', 'Bid - ', @product2, ' ','Ask'))+','+quotename(Concat(@product, ' ', 'Ask - ', @product2, ' ','Bid'))+' from #mysql_cte4
UNION ALL
Select DateTime,'+quotename(Concat(@product, ' ', 'TickerID'))+','+quotename(Concat(@product2, ' ', 'TickerID'))+','+quotename(Concat(@product, ' ', 'Bid'))+','+quotename(Concat(@product, ' ', 'Ask'))+','+quotename(Concat(@product2, ' ', 'Bid'))+','+quotename(Concat(@product2, ' ', 'Ask'))+','+quotename(Concat(@product, ' ', 'Bid - ', @product2, ' ','Ask'))+','+quotename(Concat(@product, ' ', 'Ask - ', @product2, ' ','Bid'))+' from #mysql_cte5
Order by DateTime DESC
'

exec (@Mysql)

但是,我得到的输出是这些,而不是1个完整的合并表: 5 mysql_cteX tables

请协助,我不太确定我做错了什么。我得到的错误信息是: 消息208,级别16,状态0,行25 无效的对象名称' #mysql_cte1'。

1 个答案:

答案 0 :(得分:1)

如果所有表具有相同数量的列和对应数据类型

你可以使用union来“合并”表格(例如前3个表格),UNION ALL合并所有行.. UNION合并不同的行

   select a.mtime as DateTime,
    a.tickerid as '+quotename(Concat(@product, ' ', 'TickerID'))+',
    b.tickerid as '+quotename(Concat(@product2, ' ', 'TickerID'))+',
    a.bid as '+quotename(Concat(@product, ' ', 'Bid'))+',
    a.ask as '+quotename(Concat(@product, ' ', 'Ask'))+',
    b.bid as '+quotename(Concat(@product2, ' ', 'Bid'))+',
    b.ask as '+quotename(Concat(@product2, ' ', 'Ask'))+',
    ISNULL(a.bid, 0) - ISNULL(b.ask, 0) as '+quotename(Concat(@product, ' ', 'Bid - ', @product2, ' ','Ask'))+',
    ISNULL(a.ask, 0) - ISNULL(b.bid, 0) as '+quotename(Concat(@product, ' ', 'Ask - ', @product2, ' ','Bid'))+'
from #Product_cte1 a left outer join #Product2_cte1 b 
on a.mtime = b.mtime


UNION ALL 

select a.mtime as DateTime,
    a.tickerid as '+quotename(Concat(@product, ' ', 'TickerID'))+',
    b.tickerid as '+quotename(Concat(@product2, ' ', 'TickerID'))+',
    a.bid as '+quotename(Concat(@product, ' ', 'Bid'))+',
    a.ask as '+quotename(Concat(@product, ' ', 'Ask'))+',
    b.bid as '+quotename(Concat(@product2, ' ', 'Bid'))+',
    b.ask as '+quotename(Concat(@product2, ' ', 'Ask'))+',
    ISNULL(a.bid, 0) - ISNULL(b.ask, 0) as '+quotename(Concat(@product, ' ', 'Bid - ', @product2, ' ','Ask'))+',
    ISNULL(a.ask, 0) - ISNULL(b.bid, 0) as '+quotename(Concat(@product, ' ', 'Ask - ', @product2, ' ','Bid'))+'
from #Product_cte2 a left outer join #Product2_cte2 b 
on a.mtime = b.mtime


UNION ALL 

select a.mtime as DateTime,
    a.tickerid as '+quotename(Concat(@product, ' ', 'TickerID'))+',
    b.tickerid as '+quotename(Concat(@product2, ' ', 'TickerID'))+',
    a.bid as '+quotename(Concat(@product, ' ', 'Bid'))+',
    a.ask as '+quotename(Concat(@product, ' ', 'Ask'))+',
    b.bid as '+quotename(Concat(@product2, ' ', 'Bid'))+',
    b.ask as '+quotename(Concat(@product2, ' ', 'Ask'))+',
    ISNULL(a.bid, 0) - ISNULL(b.ask, 0) as '+quotename(Concat(@product, ' ', 'Bid - ', @product2, ' ','Ask'))+',
    ISNULL(a.ask, 0) - ISNULL(b.bid, 0) as '+quotename(Concat(@product, ' ', 'Ask - ', @product2, ' ','Bid'))+'
from #Product_cte3 a left outer join #Product2_cte3 b 
on a.mtime = b.mtime
Order by DateTime DESC