在查询中组合两个字段

时间:2016-05-14 05:17:45

标签: sql database report access

我正在建立一份关于工作报价可接受性的报告。这是我目前的查询:

SELECT 
    tblSTATUS.Status, Count(tblJOB.StatusTypeID) AS CountOfStatusTypeID
FROM 
    tblSTATUS 
INNER JOIN 
    tblJOB ON tblSTATUS.StatusTypeID = tblJOB.StatusTypeID
GROUP BY 
    tblSTATUS.Status
HAVING 
    (((tblSTATUS.Status) = "Completed" OR 
      (tblSTATUS.Status) = "Accepted" OR 
      (tblSTATUS.Status) = "Rejected"));

以下是结果:

Status          CountofStatusTypeID
Accepted                 22
Completed                 4
Rejected                  3

不幸的是,我需要在报告中合并Accepted和Completed字段,因为它们都是“已接受”的作业。我找不到将这两者结合起来的方法,但仍然在报告中显示被拒绝的值。我知道如何编写查询来获取Accept或Rejected结果,但不知道如何将它们组合成可以在Access报告中使用的内容。

2 个答案:

答案 0 :(得分:0)

这是否按您的意愿工作?

SELECT "completed and accepted" as jobstatus, Count(tblJOB.StatusTypeID) AS CountOfStatusTypeID
FROM tblSTATUS INNER JOIN tblJOB ON tblSTATUS.StatusTypeID = tblJOB.StatusTypeID
where tblSTATUS.Status="Completed" Or tblSTATUS.Status="Accepted" 
union
SELECT tblSTATUS.Status as jobstatus, Count(tblJOB.StatusTypeID) AS CountOfStatusTypeID
FROM tblSTATUS INNER JOIN tblJOB ON tblSTATUS.StatusTypeID = tblJOB.StatusTypeID
where tblSTATUS.Status="Rejected";

答案 1 :(得分:0)

SELECT 
    newtblSTATUS.newstatus, Count(tblJOB.StatusTypeID) AS CountOfStatusTypeID
FROM 
    (
SELECT 
    iif(tblSTATUS.Status='Rejected','Rejected','Accepted') as newstatus, tblSTATUS.StatusTypeID 
FROM 
    tblSTATUS 
where tblSTATUS.Status in ("Completed","Accepted","Rejected")) as newtblSTATUS 
INNER JOIN 
    tblJOB ON newtblSTATUS.StatusTypeID = tblJOB.StatusTypeID
GROUP BY 
     newtblSTATUS.newstatus