任何人都可以在下面帮助我获得合并的 ServiceID 以及一年内与之关联的文件大小的总和。
完整代码(供参考)
If(OBJECT_ID('tempdb..#temp1') Is Not Null)
Begin
Drop Table #Temp1
End
GO
If(OBJECT_ID('tempdb..#temp2') Is Not Null)
Begin
Drop Table #Temp2
End
GO
If(OBJECT_ID('tempdb..#temp3') Is Not Null)
Begin
Drop Table #Temp3
End
GO
If(OBJECT_ID('tempdb..#temp4') Is Not Null)
Begin
Drop Table #Temp4
End
GO
SELECT *
INTO #Temp1
FROM
(
SELECT
SUBSTRING(ServiceName,2,3) AS ServiceID
, FileTotalSize as FileSize
,year = '1980' from table1
) a
--1981
SELECT *
INTO #Temp2
FROM
(
SELECT
SUBSTRING(ServiceName,2,3) AS ServiceID
, FileTotalSize as FileSize
,year = '1981'
from table2
) b
--1982
SELECT *
INTO #Temp3
FROM
(
SELECT
SUBSTRING(ServiceName,2,3) AS ServiceID
, FileTotalSize as FileSize
,year = '1982' from table3
) c
--1983
SELECT *
INTO #Temp4
FROM
(
SELECT
SUBSTRING(ServiceName,2,3) AS ServiceID
, FileTotalSize as FileSize
,year = '1983' from table4
) d
select ROW_NUMBER() OVER(ORDER BY year ASC) AS ID,ServiceID,year,FileTotalSize AS FileSize1 from #Temp1
select ROW_NUMBER() OVER(ORDER BY year ASC) AS ID,ServiceID,year,FileTotalSize AS FileSize2 from #Temp2
select ROW_NUMBER() OVER(ORDER BY year ASC) AS ID,ServiceID,year,FileTotalSize AS FileSize3 from #Temp3
select ROW_NUMBER() OVER(ORDER BY year ASC) AS ID,ServiceID,year,FileTotalSize AS FileSize4 from #Temp4
以下是我最终使用的 #temp 表中存在的 4 个查询及其输出:
select ROW_NUMBER() OVER(ORDER BY year ASC) AS ID,ServiceID,year,FileTotalSize AS FileSize1 from #Temp1
select ROW_NUMBER() OVER(ORDER BY year ASC) AS ID,ServiceID,year,FileTotalSize AS FileSize2 from #Temp2
select ROW_NUMBER() OVER(ORDER BY year ASC) AS ID,ServiceID,year,FileTotalSize AS FileSize3 from #Temp3
select ROW_NUMBER() OVER(ORDER BY year ASC) AS ID,ServiceID,year,FileTotalSize AS FileSize4 from #Temp4
上述 4 个查询的输出:
我的问题:如何使用上述 4 个查询获得以下输出?请帮助我,我需要加入的方式。我尝试了加入他们的方式,但确定这是错误的,所以寻求帮助。
谢谢
这是一个如何做到这一点的例子。