Может ли кто-нибудь помочь мне с правильным синтаксисом для приведенного ниже оператора CASE? Это часть представления, которое я создаю, когда цена возврата составляет половину стоимости расчетной стоимости в таблице рабочих мест, когда описание заряда имеет «Return» в нем. его высказывание неправильного синтаксиса ключевое слово ELSE. Заранее спасибо.Оператор CASE с SUM SQL
`
/****** Object: View [dbo].[MMXREPORT] Script Date: 11/08/2016 14:06:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
Alter view [dbo].[MMXREPORT] AS
DECLARE @output int
SELECT isnull(Cus.CUSTOMERNUMBER,'') AS "ACCOUNT",
isnull(dbo.Jobs.DELDATEANDTIME,'') as "DELIVERY DATE" ,
isnull(dbo.Jobs.JOBREFERENCE,'') AS "MMX REFERENCE",
isnull(dbo.Jobs.JOBNUMBER,'') AS "INDIGO REF",
isnull(dbo.Jobs.COLPOSTCODE,'') AS "COLLECTION POSTCODE",
isnull(dbo.Jobs.DELTOWN,'') AS "DELIVERY TOWN",
isnull(dbo.Jobs.DELLPOSTCODE,'') AS "DELIVERY POST CODE",
CASE when (select count (*) from jobcharge jobc inner join charge
on charge.chargeid = jobc.chargeid
where jobc.jobid = jobs.jobid and charge.description like '%return%') > 0 THEN 'YES'
ELSE 'NO' END as "RETURN AUTHORISED",
isnull(dbo.GetClarionTime (DELREADYAT),'') AS "PLANNED DELIVERY TIME",
'' AS "DRIVER ARRIVAL TIME",
'' AS "ENGINEER ARRIVAL TIME",
isnull(convert(char(5), dbo.Jobs.PODDATEANDTIME, 108),'') AS"POD TIME",
isnull(dbo.Jobs.PODNAME,'') AS"POD NAME",
isnull(dbo.Vehicle.VEHICLE,'') AS "VEHICLE SIZE",
isnull(dbo.Jobs.ACTUALMILEAGE,'0') AS "MILEAGE",
isnull(dbo.Jobs.CALCULATEDCOST,'') AS "MILEAGE PRICE",
SELECT @output = count (*) from jobcharge jobc inner join charge
ON charge.chargeid = jobc.chargeid
WHERE jobc.jobid = jobs.jobid AND charge.description LIKE '%return%'
CASE WHEN @output > 0 THEN SUM(JOBS.CALCULATEDCOST/2)
ELSE '0.00' END
AS "RETURN PRICE",
isnull((select sum (dbo.JobCharge.FLATRATE) from jobcharge inner join charge on charge.chargeid = jobcharge.chargeid
WHERE dbo.Charge.DESCRIPTION like '%congestion%' and jobcharge.jobid = jobs.jobid),0) AS "CONGESTION CHARGE",
'' AS "CONGESTION CHARGE COMMENTS",
isnull((select sum (dbo.JobCharge.FLATRATE) from jobcharge inner join charge on charge.chargeid = jobcharge.chargeid
WHERE dbo.Charge.DESCRIPTION like '%waiting time%' and jobcharge.jobid = jobs.jobid),0) AS "WAITING TIME CHARGE",
'' AS "WAITING TIME COMMENTS",
isnull((select sum (dbo.JobCharge.FLATRATE) from jobcharge inner join charge on charge.chargeid = jobcharge.chargeid
WHERE dbo.Charge.DESCRIPTION like '%hrs%' and jobcharge.jobid = jobs.jobid),0) AS "ANY OTHER CHARGES",
'' AS "ANY OTHER COMMENTS",
'' AS "TOTAL COST",JOBS.CUSTOMERID
FROM dbo.Jobs left outer JOIN
dbo.Vehicle ON dbo.Jobs.VEHICLEID = dbo.Vehicle.VEHICLEID left outer JOIN
-- dbo.JobCharge ON dbo.Jobs.JobID = dbo.JobCharge.JOBID left outer JOIN
dbo.Customer CUS ON dbo.Jobs.CUSTOMERID = CUS.CUSTOMERID
` выше полный запрос.
There'n нет 'от' в части «THEN SELECT». Но трудно сказать, как это исправить, потому что я не знаю, чего вы на самом деле хотите. – dnoeth
Показать весь запрос – Mike
Это полный запрос ..... –