简体   繁体   中英

T-SQL Sum IIf query

    SELECT t1.detail_Date AS clmDate
    ,t1.detail_agentID AS clmPID
    ,t3.HODCode AS clmHOD
    ,t1.detail_CallsHandled AS clmIBConnects
    ,t1.detail_TalkTime AS clmIBTalk
    ,t1.detail_WrapTime AS clmIBWrap
    ,t1.detail_HoldTime AS clmIBHold
    ,t1.detail_HandleTime AS clmIBTotal
    ,Sum(IIf([fldCallRoute.Value] = [Inbound],0)) AS clmIBConnects
--  ,Sum(IIf([fldCallRoute] = "Inbound", [clmTalk], 0)) AS IBTalk
--  ,Sum(IIf([fldCallRoute] = "Inbound", [clmWrap], 0)) AS IBWrap
--  ,Sum(IIf([fldCallRoute] = "Inbound", [clmTalk] + [clmWrap] + [clmHold], 0)) AS IBTotal
    ,Sum(0) AS clmOBSignedOn
    ,Sum(0) AS Preview
    ,Sum(0) AS OBTalk
    ,Sum(0) AS OBUpdate
    ,Sum(0) AS OBWork
    ,Sum(0) AS OBConnects
--  ,Sum(IIf([fldCallRoute] = "Inbound", [clmHold], 0)) AS IBHold
--  ,Sum(IIf([fldCallRoute] = "Outbound", [clmHold], 0)) AS OBHold}

I am not getting the sum(iif.. part of the queries more specifically: ,Sum(IIf([fldCallRoute] = "Inbound", [clmIBConnects], 0)) AS clmIBConnects

it shows invalid column name 'Inbound' invalid column name 'clmIBConnects'?

fldCallRoute column is in table t2 which has the entries Inbound so I'm not sure why its saying a column name inbound?

You need single quotes for string constants:

Sum(IIf([fldCallRoute] = 'Inbound', [clmTalk], 0)) AS IBTalk,

I have a preference for case over iif() . case is ANSI standard and available in almost all databases:

SUM(CASE WHEN fldCallRoute = 'Inbound' THEN clmTalk ELSE 0 END) as IBTalk,

You need to use single quote ' for a string . Double quotes " indicate a column , usually used for reserved words :

,Sum(IIf([fldCallRoute] = 'Inbound', [clmIBConnects], 0)) AS clmIBConnects

You also have 6 columns being evaluated as Sum(0) , which is always 0 .. Typo?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM