Access SQL中的IIF函数

时间:2018-07-24 14:17:20

标签: sql ms-access iif

我正在尝试编写IIF语句以返回查询中的某些职位。

但是,当我运行我的代码时,此列甚至没有在我创建的表中返回。我在网上看了看,看来这是正确的语法。我是否缺少某种导致其不返回此列的东西?

IIF ([w.Job_Title] = "Sales Pro" OR [w.Job_Title] = "Services Pro" OR [w.Job_Title] = "Universal Pro" OR [w.Job_Title] = "Fulfillment Pro", "JobTitle", NULL),

我本来在查询末尾有一个WHERE语句,但这似乎导致我的其他数据不准确,所以我认为这种IIF方法会更好。

下面是完整查询:

SELECT cal.FiscalYear,
       cal.FiscalQuarter,
       cal.FiscalMonth,
       cal.FiscalWeekNumber,
       Format(w.Day, "Dddd") AS DayOfWeek,
       cal.Day AS CalendarDay,
       w.Day,
       c.SegStart,
       c.SPLITNAME AS SkillName,
       w.Site,
       w.Campaign,
       w.Manager,
       w.Supervisor,
       IIF ([w.Job_Title] = "Sales Pro"
            OR [w.Job_Title] = "Services Pro"
            OR [w.Job_Title] = "Universal Pro"
            OR [w.Job_Title] = "Fulfillment Pro", "JobTitle", NULL),
       w.EmployeeNumber AS SalesID,
       w.ACDID,
       c.CallID,
       w.Employee,
       c.Caller_Name AS cms_EmployeeName,
       c.Answerer_Name AS cms_ProEmployeeName,
       c.Answered,
       c.AcwTime,
       c.Agt_Released AS AgentReleased,
       c.AnsHoldTime,
       c.AnsLogin,
       c.Calling_Pty AS CallingParty,
       c.Conference,
       c.ConsultTime,
       c.Dialed_Num AS DialedNumber,
       c.DispIVector,
       c.DispSplit,
       c.DispVDN,
       c.Duration,
       c.Disposition,
       c.Held,
       c.HoldABN AS AbanOnHold,
       c.OrigLogin,
       c.QueueTime,
       c.RingTime,
       c.TalkTime,
       c.Transferred,
       c.VDN2,
       c.VDN3,
       c.VDN4,
       c.VDN5
FROM (t_wfm AS w
      INNER JOIN t_cms AS c ON (c.CALLING_PTY = w.ACDID)
        AND (Format(c.SEGSTART, "Short date") = Format(w.[Day], "Short date")))
INNER JOIN tbl_Calendar AS cal ON cal.Day = w.Day;

1 个答案:

答案 0 :(得分:0)

您尚未提供列别名。

该列最有可能在其中,但标题类似Expr1之类。

我认为您要检索的是实际标题,而不是常量"JobTitle"

   IIF ([w.Job_Title] = "Sales Pro"
        OR [w.Job_Title] = "Services Pro"
        OR [w.Job_Title] = "Universal Pro"
        OR [w.Job_Title] = "Fulfillment Pro", [w.Job_Title], NULL) AS JobTitle,

修改

如果您只想返回包含这些标题的,则检查进入WHERE子句。则不需要IIf()

WHERE ([w.Job_Title] = "Sales Pro"
        OR [w.Job_Title] = "Services Pro"
        OR [w.Job_Title] = "Universal Pro"
        OR [w.Job_Title] = "Fulfillment Pro")