将行转换为列时摆脱 NULL 行
getting rid of NULL rows when transforming a row into a column
我有以下 SQL 代码:
SELECT TOP (1000)
a.[JourneyNumber]
,a.[JourneyDate]
,a.[tReg_ID]
,a.[Reg]
,a.[ID]
,b.tLocationPosition_ID
,c.LifeCode
,c.LifeTotal
,CASE WHEN c.LifeCode LIKE 'LCF1' AND b.tLocationPosition_ID = 1 THEN c.LifeTotal END as ALCF1
,CASE WHEN c.LifeCode LIKE 'LCF1' AND b.tLocationPosition_ID = 2 THEN c.LifeTotal END as BLCF1
FROM [RALNHVTST].[dbo].[tRegJourney] as a
LEFT JOIN [RALNHVTST].[dbo].[tRegJourneyLogBook] as b
ON a.ID = b.tRegJourney_ID
LEFT JOIN [RALNHVTST].[dbo].[tRegJourneyLogBookLifeCodeEvents] AS c
ON b.ID = c.tRegJourneyLogBook_ID
WHERE b.tLocation_ID = 720
AND a.tReg_ID = 73 OR a.tReg_ID = 38
AND (b.tLocationPosition_ID = 1 OR b.tLocationPosition_ID = 2)
AND (c.LifeCode LIKE 'LCF1' )
ORDER BY JourneyDate
在 SELECt 语句中,我试图将主要包含相同信息的多行转换为列。
这样:
JourneyNo JourneyDate Reg ID tLocationPos_ID LifeCodeLifeTotal
4A 2015-08-31 00:00:00.000 OO-NSN 45023 1 LCF1 68.0000
4A 2015-08-31 00:00:00.000 OO-NSN 45023 2 LCF1 67.0000
变成这样:
JourneyNumber JourneyDate Reg ID LifeCode ALCF1 BLCF1
4A 2015-08-31 00:00:00.000 OO-NSN 45023 LCF1 68.0000 67.000
但我得到的是:
JourneyNumber JourneyDate Reg ID LifeCode ALCF1 BLCF1
4A 2015-08-31 00:00:00.000 OO-NSN 45023 LCF1 68.0000 NULL
4A 2015-08-31 00:00:00.000 OO-NSN 45023 LCF1 NULL 67.0000
谁能帮我解决这个问题?
谢谢!
使用聚合
SELECT TOP (1000)
a.[JourneyNumber]
,a.[JourneyDate]
,a.[tReg_ID]
,a.[Reg]
,a.[ID]
,b.tLocationPosition_ID
,c.LifeCode
,c.LifeTotal
,max(CASE WHEN c.LifeCode LIKE 'LCF1' AND b.tLocationPosition_ID = 1 THEN c.LifeTotal END) as ALCF1
,max(CASE WHEN c.LifeCode LIKE 'LCF1' AND b.tLocationPosition_ID = 2 THEN c.LifeTotal END) as BLCF1
FROM [RALNHVTST].[dbo].[tRegJourney] as a
LEFT JOIN [RALNHVTST].[dbo].[tRegJourneyLogBook] as b
ON a.ID = b.tRegJourney_ID
LEFT JOIN [RALNHVTST].[dbo].[tRegJourneyLogBookLifeCodeEvents] AS c
ON b.ID = c.tRegJourneyLogBook_ID
WHERE b.tLocation_ID = 720
AND a.tReg_ID = 73 OR a.tReg_ID = 38
AND (b.tLocationPosition_ID = 1 OR b.tLocationPosition_ID = 2)
AND (c.LifeCode LIKE 'LCF1' )
group by a.[JourneyNumber]
,a.[JourneyDate]
,a.[tReg_ID]
,a.[Reg]
,a.[ID]
,b.tLocationPosition_ID
,c.LifeCode
,c.LifeTotal
ORDER BY JourneyDate
修正您的查询,使 table 别名有意义!
我也怀疑你的 WHERE
子句是否真的如你所愿。在下面的查询中,我稍微更改了条件,因此第二个 table 的条件适用于结果中的所有行。
为了性能,我推荐CROSS APPLY
:
SELECT TOP (1000) rj.[JourneyNumber], rj.[JourneyDate]
rj.[tReg_ID], rj.[Reg], rj.[ID]
rjlb.tLocationPosition_ID,
.LifeCode,
ce.ALCF1, ce.BLCF1
FROM [RALNHVTST].[dbo].[tRegJourney] rj
CROSS APPLY
(SELECT SUM(CASE WHEN rjlb.tLocationPosition_ID = 1 THEN ce.LifeTotal END) as ALCF1,
SUM(CASE WHEN rjlb.tLocationPosition_ID = 2 THEN ce.LifeTotal END) as BLCF1
FROM [RALNHVTST].[dbo].[tRegJourneyLogBook] rjlb
[RALNHVTST].[dbo]. tRegJourneyLogBookLifeCodeEvents ce
ON rjlb.ID = ce.tRegJourneyLogBook_ID AND
WHERE rj.ID = rjlb.tRegJourney_ID AND
rjlb.tLocationPosition_ID IN (1, 2) AND
rjlb.tLocation_ID = 720 AND
ce.LifeCode LIKE 'LCF1'
) ce
WHERE rj.tReg_ID IN (73, 38
ORDER BY JourneyDate ;
我有以下 SQL 代码:
SELECT TOP (1000)
a.[JourneyNumber]
,a.[JourneyDate]
,a.[tReg_ID]
,a.[Reg]
,a.[ID]
,b.tLocationPosition_ID
,c.LifeCode
,c.LifeTotal
,CASE WHEN c.LifeCode LIKE 'LCF1' AND b.tLocationPosition_ID = 1 THEN c.LifeTotal END as ALCF1
,CASE WHEN c.LifeCode LIKE 'LCF1' AND b.tLocationPosition_ID = 2 THEN c.LifeTotal END as BLCF1
FROM [RALNHVTST].[dbo].[tRegJourney] as a
LEFT JOIN [RALNHVTST].[dbo].[tRegJourneyLogBook] as b
ON a.ID = b.tRegJourney_ID
LEFT JOIN [RALNHVTST].[dbo].[tRegJourneyLogBookLifeCodeEvents] AS c
ON b.ID = c.tRegJourneyLogBook_ID
WHERE b.tLocation_ID = 720
AND a.tReg_ID = 73 OR a.tReg_ID = 38
AND (b.tLocationPosition_ID = 1 OR b.tLocationPosition_ID = 2)
AND (c.LifeCode LIKE 'LCF1' )
ORDER BY JourneyDate
在 SELECt 语句中,我试图将主要包含相同信息的多行转换为列。
这样:
JourneyNo JourneyDate Reg ID tLocationPos_ID LifeCodeLifeTotal
4A 2015-08-31 00:00:00.000 OO-NSN 45023 1 LCF1 68.0000
4A 2015-08-31 00:00:00.000 OO-NSN 45023 2 LCF1 67.0000
变成这样:
JourneyNumber JourneyDate Reg ID LifeCode ALCF1 BLCF1
4A 2015-08-31 00:00:00.000 OO-NSN 45023 LCF1 68.0000 67.000
但我得到的是:
JourneyNumber JourneyDate Reg ID LifeCode ALCF1 BLCF1
4A 2015-08-31 00:00:00.000 OO-NSN 45023 LCF1 68.0000 NULL
4A 2015-08-31 00:00:00.000 OO-NSN 45023 LCF1 NULL 67.0000
谁能帮我解决这个问题?
谢谢!
使用聚合
SELECT TOP (1000)
a.[JourneyNumber]
,a.[JourneyDate]
,a.[tReg_ID]
,a.[Reg]
,a.[ID]
,b.tLocationPosition_ID
,c.LifeCode
,c.LifeTotal
,max(CASE WHEN c.LifeCode LIKE 'LCF1' AND b.tLocationPosition_ID = 1 THEN c.LifeTotal END) as ALCF1
,max(CASE WHEN c.LifeCode LIKE 'LCF1' AND b.tLocationPosition_ID = 2 THEN c.LifeTotal END) as BLCF1
FROM [RALNHVTST].[dbo].[tRegJourney] as a
LEFT JOIN [RALNHVTST].[dbo].[tRegJourneyLogBook] as b
ON a.ID = b.tRegJourney_ID
LEFT JOIN [RALNHVTST].[dbo].[tRegJourneyLogBookLifeCodeEvents] AS c
ON b.ID = c.tRegJourneyLogBook_ID
WHERE b.tLocation_ID = 720
AND a.tReg_ID = 73 OR a.tReg_ID = 38
AND (b.tLocationPosition_ID = 1 OR b.tLocationPosition_ID = 2)
AND (c.LifeCode LIKE 'LCF1' )
group by a.[JourneyNumber]
,a.[JourneyDate]
,a.[tReg_ID]
,a.[Reg]
,a.[ID]
,b.tLocationPosition_ID
,c.LifeCode
,c.LifeTotal
ORDER BY JourneyDate
修正您的查询,使 table 别名有意义!
我也怀疑你的 WHERE
子句是否真的如你所愿。在下面的查询中,我稍微更改了条件,因此第二个 table 的条件适用于结果中的所有行。
为了性能,我推荐CROSS APPLY
:
SELECT TOP (1000) rj.[JourneyNumber], rj.[JourneyDate]
rj.[tReg_ID], rj.[Reg], rj.[ID]
rjlb.tLocationPosition_ID,
.LifeCode,
ce.ALCF1, ce.BLCF1
FROM [RALNHVTST].[dbo].[tRegJourney] rj
CROSS APPLY
(SELECT SUM(CASE WHEN rjlb.tLocationPosition_ID = 1 THEN ce.LifeTotal END) as ALCF1,
SUM(CASE WHEN rjlb.tLocationPosition_ID = 2 THEN ce.LifeTotal END) as BLCF1
FROM [RALNHVTST].[dbo].[tRegJourneyLogBook] rjlb
[RALNHVTST].[dbo]. tRegJourneyLogBookLifeCodeEvents ce
ON rjlb.ID = ce.tRegJourneyLogBook_ID AND
WHERE rj.ID = rjlb.tRegJourney_ID AND
rjlb.tLocationPosition_ID IN (1, 2) AND
rjlb.tLocation_ID = 720 AND
ce.LifeCode LIKE 'LCF1'
) ce
WHERE rj.tReg_ID IN (73, 38
ORDER BY JourneyDate ;