我的代码:
IF OBJECT_ID('tempdb..#Temp15') IS NOT NULL BEGIN
drop table #Temp15
end
SELECT * into #Temp15
from (
select
cast(ROW_NUMBER() OVER(PARTITION BY CM3RM1.NUMBER ORDER BY INCIDENTSM1.OPEN_TIME)as int) as tempid
,INCIDENTSM1.INCIDENT_ID AS SD_NR
,INCIDENTSM1.OPEN_TIME AS SD_OP
,INCIDENTSM1.CLOSE_TIME AS SD_CL
FROM INCIDENTSM1
LEFT OUTER JOIN SCRELATIONM1 ON INCIDENTSM1.INCIDENT_ID = SCRELATIONM1.SOURCE COLLATE SQL_Latin1_General_CP1_CI_AS
LEFT OUTER JOIN CM3RM1 ON CM3RM1.NUMBER = SCRELATIONM1.DEPEND
WHERE CM3RM1.NUMBER IN ('C45168')
)
as databases (tempid, SD_NR, SD_OP, SD_CL)
IF OBJECT_ID('tempdb..#Temp1') IS NOT NULL BEGIN
drop table #Temp1
end
SELECT * into #Temp1
from (
select
cast(ROW_NUMBER() OVER(PARTITION BY parent_change ORDER BY number)as int) as tempid
,number as tasknr
,category as cat
,orig_date_entered as taskop
,parent_change as chgnr1
,case when (CM3TM1.status = 'closed') then CM3TM1.date_entered else NULL end as taskcl
FROM CM3TM1
WHERE parent_change IN ('C45168')
and category = 'NEU Development'
)
as databases (tempid, tasknr, devcat, taskop, chgnr1, taskcl)
IF OBJECT_ID('tempdb..#Temp2') IS NOT NULL BEGIN
drop table #Temp2
end
SELECT * into #Temp2
from (
select
cast(ROW_NUMBER() OVER(PARTITION BY parent_change ORDER BY number)as int) as tempid
,number as tasknr
,category as cat
,orig_date_entered as taskop
,case when (CM3TM1.status = 'closed') then CM3TM1.date_entered else NULL end as taskcl
FROM CM3TM1
WHERE parent_change IN ('C45168')
and category = 'NEU Impl. to Production'
)
as databases (tempid, tasknr, prodcat, taskop, taskcl)
IF OBJECT_ID('tempdb..#Temp4') IS NOT NULL BEGIN
drop table #Temp4
end
SELECT * into #Temp4
from (
select distinct
cast(ROW_NUMBER() OVER(PARTITION BY CM3RM1.NUMBER ORDER BY ACTIVITYCM3M1.datestamp)as int) as tempid
,ACTIVITYCM3M1.datestamp as CHR_T
from CM3RM1
INNER JOIN CM3RM2
ON CM3RM1.NUMBER = CM3RM2.NUMBER
LEFT OUTER JOIN ACTIVITYCM3M1
ON CM3RM1.NUMBER = ACTIVITYCM3M1.NUMBER
where ACTIVITYCM3M1.description like '%to "Change Review"%'
and CM3RM1.NUMBER IN ('C45168')
)
as databases (tempid, CHR_T)
IF OBJECT_ID('tempdb..#Temp5') IS NOT NULL BEGIN
drop table #Temp5
end
SELECT * into #Temp5
from (
select
cast(ROW_NUMBER() OVER(PARTITION BY CM3RM1.NUMBER ORDER BY APPROVALLOGM1.DATE)as int) as tempid
,APPROVALLOGM1.DATE as APT
from CM3RM1
INNER JOIN CM3RM2
ON CM3RM1.NUMBER = CM3RM2.NUMBER
LEFT OUTER JOIN APPROVALLOGM1
ON CM3RM1.NUMBER = APPROVALLOGM1.UNIQUE_KEY
where APPROVALLOGM1.current_phase = 'Change Review'
and APPROVALLOGM1.[group] like 'ABB Ventyx CLS SD'
and CM3RM1.NUMBER IN ('C45168')
)
as databases (tempid, APT)
IF OBJECT_ID('tempdb..#Temp6') IS NOT NULL BEGIN
drop table #Temp6
end
SELECT * into #Temp6
from (
select
cast(ROW_NUMBER() OVER(PARTITION BY CM3RM2.NUMBER ORDER BY APPROVALLOGM1.DATE)as int) as tempid
,APPROVALLOGM1.DATE as APT2
from CM3RM1
INNER JOIN CM3RM2
ON CM3RM1.NUMBER = CM3RM2.NUMBER
LEFT OUTER JOIN APPROVALLOGM1
ON CM3RM1.NUMBER = APPROVALLOGM1.UNIQUE_KEY
where APPROVALLOGM1.current_phase = 'Change Review'
and APPROVALLOGM1.[group] not like 'ABB Ventyx CLS SD'
and CM3RM1.NUMBER IN ('C45168')
)
as databases (tempid, APT2)
IF OBJECT_ID('tempdb..#Temp7') IS NOT NULL BEGIN
drop table #Temp7
end
SELECT * into #Temp7
from (
select distinct
cast(ROW_NUMBER() OVER(PARTITION BY CM3RM1.NUMBER ORDER BY ACTIVITYCM3M1.datestamp)as int) as tempid
,ACTIVITYCM3M1.datestamp as CHA_T
from CM3RM1
INNER JOIN CM3RM2
ON CM3RM1.NUMBER = CM3RM2.NUMBER
LEFT OUTER JOIN ACTIVITYCM3M1
ON CM3RM1.NUMBER = ACTIVITYCM3M1.NUMBER
where ACTIVITYCM3M1.description like '%Review" to "Change Assessment%'
and CM3RM1.NUMBER IN ('C45168')
)
as databases (tempid, CHA_T)
IF OBJECT_ID('tempdb..#Temp3') IS NOT NULL BEGIN
drop table #Temp3
end
SELECT * into #Temp3
from (
select
cast(ROW_NUMBER() OVER(PARTITION BY CM3RM1.NUMBER ORDER BY CM3RM1.orig_date_entered)as int) as tempid
,CM3RM1.NUMBER as chgnr
,CM3RM1.orig_date_entered AS CH_OP
,CM3RM2.AFFECTED_ITEM AS Service
FROM CM3RM1
INNER JOIN CM3RM2 ON CM3RM1.NUMBER = CM3RM2.NUMBER
LEFT OUTER JOIN SCRELATIONM1 ON CM3RM1.NUMBER = SCRELATIONM1.DEPEND COLLATE SQL_Latin1_General_CP1_CI_AS
WHERE CM3RM1.NUMBER IN ('C45168')
)
as databases (tempid, chgnr, CH_OP, Service)
select
isnull(#Temp15.SD_NR, '') as [Interaction ID]
,isnull(LEFT(CONVERT(VARCHAR, #Temp15.SD_OP, 120), 16), '') as [Interaction Opened]
,isnull(LEFT(CONVERT(VARCHAR, #Temp3.CH_OP, 120), 16), '') as [Escalated Ticket ]
,isnull(#Temp3.chgnr, '') as [Change ID]
,isnull(LEFT(CONVERT(VARCHAR, #Temp4.CHR_T, 120), 16), '') as [Change Review Phase Start]
,isnull(LEFT(CONVERT(VARCHAR, #Temp5.APT, 120), 16), '') as [Approver 1]
,isnull(LEFT(CONVERT(VARCHAR, #Temp6.APT2, 120), 16), '') as [Approver 2]
,isnull(LEFT(CONVERT(VARCHAR, #Temp7.CHA_T, 120), 16), '') as [Change Assessment & Planning Phase Start]
from #Temp3 full join #Temp1 on #Temp3.tempid = #Temp1.tempid
full join #Temp2 on #Temp3.tempid = #Temp2.tempid
full join #Temp4 on #Temp3.tempid = #Temp4.tempid
full join #Temp5 on #Temp3.tempid = #Temp5.tempid
full join #Temp6 on #Temp3.tempid = #Temp6.tempid
full join #Temp7 on #Temp3.tempid = #Temp7.tempid
full join #Temp15 on #Temp3.tempid = #Temp15.tempid
我希望以这样的方式连接几个临时表,根据哪个表具有最大记录,它总是产品最大行数。使用上面的代码,它会产生意外的数据移位。 请看屏幕截图的样子:
无法使用左连接,因为没有规则哪个列的行数最多。 某个特定列(该示例中为[更改ID])永远不为空,并且始终具有一行。 其他人有时可能是空的。
我尝试用不同的查询重现问题,因为它被建议。当我使用如下代码分别运行两个临时表时:
IF (SELECT object_id('TempDB..#t3')) IS NOT NULL
BEGIN
DROP TABLE #t3
END
create table #t3 (id int, tasknr varchar(50), cat varchar(50), taskop datetime, taskcl datetime)
insert into #t3 (id, tasknr, cat, taskop, taskcl)
select
cast(ROW_NUMBER() OVER(PARTITION BY parent_change ORDER BY number)as int) as id
,number as tasknr
,category as cat
,orig_date_entered as taskop
,case when (CM3TM1.status = 'closed') then CM3TM1.date_entered else NULL end as taskcl
FROM CM3TM1
WHERE parent_change IN ('C45168')
and category = 'NEU Development'
select * from #t3
IF (SELECT object_id('TempDB..#t4')) IS NOT NULL
BEGIN
DROP TABLE #t4
END
create table #t4 (id int, tasknr varchar(50), cat varchar(50), taskop datetime, taskcl datetime)
insert into #t4 (id, tasknr, cat, taskop, taskcl)
select
cast(ROW_NUMBER() OVER(PARTITION BY parent_change ORDER BY number)as int) as id
,number as tasknr
,category as cat
,orig_date_entered as taskop
,case when (CM3TM1.status = 'closed') then CM3TM1.date_entered else NULL end as taskcl
FROM CM3TM1
WHERE parent_change IN ('C45168')
and category = 'NEU Impl. to Production'
select * from #t4
它看起来是正确的但当我在查询中运行它时如下:
IF (SELECT object_id('TempDB..#t1')) IS NOT NULL
BEGIN
DROP TABLE #t1
END
create table #t1 (id int, chgnr varchar(50))
insert into #t1 (id, chgnr)
select
cast(ROW_NUMBER() OVER(PARTITION BY number ORDER BY number)as int) as id
,number as chgnr
FROM CM3RM1
WHERE number IN ('C45168')
IF (SELECT object_id('TempDB..#t2')) IS NOT NULL
BEGIN
DROP TABLE #t2
END
create table #t2 (id int, SD_NR varchar(50), SD_OP datetime, SD_CL datetime)
insert into #t2 (id, SD_NR, SD_OP, SD_CL)
select
cast(ROW_NUMBER() OVER(PARTITION BY CM3RM1.NUMBER ORDER BY INCIDENTSM1.OPEN_TIME)as int) as id
,INCIDENTSM1.INCIDENT_ID AS SD_NR
,INCIDENTSM1.OPEN_TIME AS SD_OP
,INCIDENTSM1.CLOSE_TIME AS SD_CL
FROM INCIDENTSM1
LEFT OUTER JOIN SCRELATIONM1 ON INCIDENTSM1.INCIDENT_ID = SCRELATIONM1.SOURCE COLLATE SQL_Latin1_General_CP1_CI_AS
LEFT OUTER JOIN CM3RM1 ON CM3RM1.NUMBER = SCRELATIONM1.DEPEND
WHERE CM3RM1.NUMBER IN ('C45168')
IF (SELECT object_id('TempDB..#t3')) IS NOT NULL
BEGIN
DROP TABLE #t3
END
create table #t3 (id int, tasknr varchar(50), cat varchar(50), taskop datetime, taskcl datetime)
insert into #t3 (id, tasknr, cat, taskop, taskcl)
select
cast(ROW_NUMBER() OVER(PARTITION BY parent_change ORDER BY number)as int) as id
,number as tasknr
,category as cat
,orig_date_entered as taskop
,case when (CM3TM1.status = 'closed') then CM3TM1.date_entered else NULL end as taskcl
FROM CM3TM1
WHERE parent_change IN ('C45168')
and category = 'NEU Development'
IF (SELECT object_id('TempDB..#t4')) IS NOT NULL
BEGIN
DROP TABLE #t4
END
create table #t4 (id int, tasknr varchar(50), cat varchar(50), taskop datetime, taskcl datetime)
insert into #t4 (id, tasknr, cat, taskop, taskcl)
select
cast(ROW_NUMBER() OVER(PARTITION BY parent_change ORDER BY number)as int) as id
,number as tasknr
,category as cat
,orig_date_entered as taskop
,case when (CM3TM1.status = 'closed') then CM3TM1.date_entered else NULL end as taskcl
FROM CM3TM1
WHERE parent_change IN ('C45168')
and category = 'NEU Impl. to Production'
select #t2.SD_NR, #t2.SD_OP, #t1.chgnr, #t3.tasknr devtasknr, #t3.taskop devtaskop, #t4.tasknr prodtasknr, #t4.taskop prodtaskop
from #t1
full join #t2 on #t2.id = #t1.id
full join #t3 on #t3.id = #t1.id
full join #t4 on #t4.id = #t1.id
看起来不正确,请在此处查看比较:http://snag.gy/N1HeI.jpg
为什么T85341和T85179在id = 2时都不在同一行?
有谁能告诉我如何解决这个问题?