原脚本为SELECT P.*, P2.PublishMoney2 as PublishMoney2 FROM ( SELECT LEFT(p1.MonthPeriod,4) AS YearPeriod ,p1.MonthPeriod ,(SUM(p1.PublishMoney)/1000000)AS PublishMoney ,p1.CSH_QuDaoName ,p1.ProductName FROM dbo.HS_Team26_NetPublishMoney_Month p1 WITH(NOLOCK) WHERE p1.MonthPeriod >= 202201 GROUP BY LEFT(p1.MonthPeriod,4) ,p1.MonthPeriod ,p1.CSH_QuDaoName ,p1.ProductName ) p LEFT JOIN ( SELECT p1.MonthPeriod ,(SUM(p1.PublishMoney)/1000000)AS PublishMoney2 ,p1.CSH_QuDaoName ,p1.ProductName FROM dbo.HS_Team26_PublishMoney_Month p1 WITH(NOLOCK) WHERE p1.MonthPeriod >= 202201 GROUP BY p1.MonthPeriod ,p1.CSH_QuDaoName ,p1.ProductName ) p2 ON (p.MonthPeriod-100)=p2.MonthPeriod AND p. CSH_QuDaoName =p2. CSH_QuDaoName AND p. ProductName =p2. ProductName,取出来的p2.PublishMoney2有空值,需要将空值替换为0
时间: 2024-02-20 22:00:45 浏览: 115
好的,您的要求是将原脚本中的p2.PublishMoney2中的空值替换为0,我可以为您提供以下修改后的脚本:
```
SELECT P.*, ISNULL(P2.PublishMoney2, 0) AS PublishMoney2
FROM
(
SELECT
LEFT(p1.MonthPeriod, 4) AS YearPeriod,
p1.MonthPeriod,
(SUM(p1.PublishMoney) / 1000000) AS PublishMoney,
p1.CSH_QuDaoName,
p1.ProductName
FROM dbo.HS_Team26_NetPublishMoney_Month p1 WITH (NOLOCK)
WHERE p1.MonthPeriod >= 202201
GROUP BY
LEFT(p1.MonthPeriod, 4),
p1.MonthPeriod,
p1.CSH_QuDaoName,
p1.ProductName
) P
LEFT JOIN
(
SELECT
p1.MonthPeriod,
(SUM(p1.PublishMoney) / 1000000) AS PublishMoney2,
p1.CSH_QuDaoName,
p1.ProductName
FROM dbo.HS_Team26_PublishMoney_Month p1 WITH (NOLOCK)
WHERE p1.MonthPeriod >= 202201
GROUP BY
p1.MonthPeriod,
p1.CSH_QuDaoName,
p1.ProductName
) P2
ON (P.MonthPeriod - 100) = P2.MonthPeriod
AND P.CSH_QuDaoName = P2.CSH_QuDaoName
AND P.ProductName = P2.ProductName;
```
修改后的脚本中,我们使用了ISNULL函数,将p2.PublishMoney2中的空值替换为0。
阅读全文