我需要这样做:
CAST(ISNULL([CompanyE.dbo.WipMaster.QtyToMake],0) As INT) As QtyToMake
但是SSMS抱怨此消息:
Msg 207, Level 16, State 1, Line 5
Invalid column name 'CompanyE.dbo.WipMaster.QtyToMake'.
这是我的整个语句,无需使用ISNULL:
DECLARE @fromdate Date = '12-1-2018', @ToDate Date = '12-31-2019'
SELECT JobDeliveryDate As [Delivery Date], Job, QtyToMake As [PNL QTY], PSAW, PPEP, PINS, PFRP, PASM, PRES, PTMI, PCOT, PFIL
FROM
(SELECT CompanyE.dbo.WipMaster.JobDeliveryDate, CompanyE.dbo.WipMaster.Job, CAST(CompanyE.dbo.WipMaster.QtyToMake As INT) As QtyToMake, CAST(CompanyE.dbo.WipJobAllLab.QtyCompleted As INT) As QtyCompleted, CompanyE.dbo.WipJobAllLab.WorkCentre
FROM CompanyE.dbo.WipMaster INNER JOIN
CompanyE.dbo.WipJobAllLab ON CompanyE.dbo.WipMaster.Job = CompanyE.dbo.WipJobAllLab.Job
WHERE (CompanyE.dbo.WipMaster.Job LIKE '%P') AND (CompanyE.dbo.WipMaster.Complete <> 'Y') AND (CompanyE.dbo.WipMaster.JobDeliveryDate BETWEEN @FromDate AND @ToDate))
AS D
PIVOT (SUM(QtyCompleted) FOR WorkCentre IN(PSAW, PPEP, PINS, PFRP, PASM, PRES, PTMI, PCOT, PFIL)) AS P
ORDER BY Job;
@Gordon Linoff-感谢代码结构中的指针,希望现在可以解决。我尝试了两个选项,如果为null则返回零,但结果仍然为NULL。这是代码:
DECLARE @fromdate Date = '2018-12-01',
@ToDate Date = '2019-12-31';
SELECT JobDeliveryDate AS [Delivery Date], Job, QtyToMake AS [PNL QTY], PSAW,
PPEP, PINS, PFRP, PASM, PRES, PTMI, PCOT, PFIL
FROM (SELECT wm.JobDeliveryDate, wm.Job,
CAST(ISNULL(wm.QtyToMake,0) AS INT) AS QtyToMake,
ISNULL(CAST(wjal.QtyCompleted AS INT),0) AS QtyCompleted,
wjal.WorkCentre
FROM CompanyE.dbo.WipMaster AS wm INNER JOIN
CompanyE.dbo.WipJobAllLab AS wjal ON wm.Job = wjal.Job
WHERE (wm.Job LIKE '%P') AND
(wm.Complete <> 'Y') AND
(wm.JobDeliveryDate BETWEEN @FromDate AND @ToDate))
AS D
PIVOT (SUM(QtyCompleted) FOR WorkCentre IN(PSAW, PPEP, PINS, PFRP, PASM, PRES, PTMI, PCOT, PFIL)) AS P
ORDER BY Job;
还有什么可以将NULL变成零?
答案 0 :(得分:1)
问题是您写了:
[CompanyE.dbo.WipMaster.QtyToMake]
这是在查找具有全名的列-句点,疣和所有。您实际上打算:
CompanyE.dbo.WipMaster.QtyToMake
如果不需要转义符,请不要使用它们。
您如何阅读?学习使用表别名和正确的日期格式:
DECLARE @fromdate Date = '2018-12-01',
@ToDate Date = '2019-12-31';
SELECT JobDeliveryDate As [Delivery Date], Job, QtyToMake As [PNL QTY],
PSAW, PPEP, PINS, PFRP, PASM, PRES, PTMI, PCOT, PFIL
FROM (SELECT wm.JobDeliveryDate, wm.Job,
CAST(wm.QtyToMake As INT) As QtyToMake,
Cast(wjal.QtyCompleted As INT) As QtyCompleted,
wjal.WorkCentre
FROM CompanyE.dbo.WipMaster wm INNER JOIN
CompanyE.dbo.WipJobAllLab wjal
ON wm.Job = wjal.Job
WHERE wm.Job LIKE '%P' AND
wm.Complete <> 'Y' AND
wm.JobDeliveryDate BETWEEN @FromDate AND @ToDate
) D
PIVOT (SUM(QtyCompleted) FOR WorkCentre IN(PSAW,PPEP, PINS, PFRP, PASM, PRES, PTMI, PCOT,PFIL)) AS P
ORDER BY Job;