-1

I've queried the results I'm looking for, now I simply want to compare "OnTime" Y/N for a percentage "OnTime", but I'm having trouble comparing the alias and/or using the column name in the PRINT function. Any suggestions?

SELECT
shmast.fcnumber AS Customer,
shmast.fcbcompany AS Company,
somast.fsono AS SalesOrder,
somast.fduedate AS DueDate, 
shmast.fshipno AS Shipper, 
shmast.fshipdate AS ShipDate,
--DATEDIFF(DAY,somast.fduedate,shmast.fshipdate) AS 'Days',
CASE
    WHEN shmast.fshipdate <= somast.fduedate THEN 'Y'
    WHEN shmast.fshipdate > somast.fduedate THEN 'N'
END AS OnTime
FROM somast
JOIN shmast ON somast.fsono = shmast.fcsono
WHERE shmast.fcnumber = '000111'
PRINT (OnTime = 'Y') / COUNT (*)
/*GROUP BY
shmast.fcnumber,
shmast.fcbcompany,
somast.fsono,
somast.fduedate, 
shmast.fshipno, 
shmast.fshipdate*/
4

1 回答 1

1

在你的描述之后再试一次

SELECT
sum(CASE WHEN shmast.fshipdate<=somast.fduedate THEN 100.0 END)/count(*) AS OnTime
FROM somast
JOIN shmast ON somast.fsono = shmast.fcsono
WHERE shmast.fcnumber = '000111'
于 2013-07-21T00:34:56.383 回答