SUM SQL

时间:2016-11-11 10:11:21

标签: sql-server case

有人可以帮我解决下面CASE声明的正确语法吗?它是我正在创建的一个视图的一部分,其中当收费描述具有"返回"在里面。它说关键字ELSE的语法不正确。提前谢谢。

`

/****** 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 

` 以上是完整的查询。

3 个答案:

答案 0 :(得分:0)

这应该有效:

DECLARE @output int
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",

答案 1 :(得分:0)

似乎不完整的查询。 所以我假设必须有一个名为“JOBS”的表。 为此,尝试以下。

SELECT 
    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
                SUM(JOBS.CALCULATEDCOST/2)
          ELSE '0.00'
    END as "RETURN PRICE"  FROM JOBS 

答案 2 :(得分:0)

我认为你不需要几行的总和,只需要一个值......

CASE WHEN EXISTS(
   select 1 from jobcharge jobc inner join charge
       ON charge.chargeid = jobc.chargeid
    WHERE jobc.jobid = jobs.jobid AND charge.description LIKE '%return%
) THEN jobs.CALCULATEDCOST/2 ELSE 0.00 END as "RETURN PRICE",

使用exists (select 1 ...)可能比select count() ...

更快