1

我需要得到 2 个汇总数字,但是我遇到了问题,因为一个是针对总订单的,一个是针对不完整的订单的。这些使用相同的初始查询,但是未完成的订单有一个额外的 where 子句。可以将这些放入查询中,以便我只获得 2 列。我以前做过内部查询,但我从来没有用 2 个不同的 where 子句做过一个?!任何想法都非常受欢迎

用于总订单的查询即时消息是:

SELECT  Count(TBL_PROPERTY.PROPREF) AS TotalOrders

FROM    TBL_PROPERTY INNER JOIN 
        TBL_REPAIR_ORDER ON TBL_PROPERTY.PROPREF = TBL_REPAIR_ORDER.PROPREF INNER JOIN 
        TBL_REPAIR_VISIT ON TBL_REPAIR_ORDER.ORDERID = TBL_REPAIR_VISIT.ORDERID INNER JOIN
        tbl_contract ON tbl_repair_order.CONTRACT = tbl_contract.CONTRACT

WHERE   (TBL_CONTRACT.CONTRACT IN ('STE')) AND
        (TBL_REPAIR_ORDER.RAISEDDATE  BETWEEN '01/12/2008' AND DATEADD(hh,23,'01/01/2009'))

使用订单不完整查询即时消息:

SELECT  Count(TBL_PROPERTY.PROPREF) AS TotalOrders

FROM    TBL_PROPERTY INNER JOIN 
        TBL_REPAIR_ORDER ON TBL_PROPERTY.PROPREF = TBL_REPAIR_ORDER.PROPREF INNER JOIN 
        TBL_REPAIR_VISIT ON TBL_REPAIR_ORDER.ORDERID = TBL_REPAIR_VISIT.ORDERID INNER JOIN
        tbl_contract ON tbl_repair_order.CONTRACT = tbl_contract.CONTRACT

WHERE   (TBL_CONTRACT.CONTRACT IN ('STE')) AND
        (TBL_REPAIR_ORDER.RAISEDDATE  BETWEEN '01/12/2008' AND DATEADD(hh,23,'01/01/2009')) AND
 TBL_REPAIR_ORDER.STATUS <> 'Completed')
4

2 回答 2

1
SELECT  Count(TBL_PROPERTY.PROPREF) AS TotalOrders
      , SUM( CASE WHEN TBL_REPAIR_ORDER.STATUS <> 'Completed' THEN 1 ELSE 0 END ) AS TotalNotCompleted

也从 WHERE 子句中删除 TBL_REPAIR_ORDER.STATUS <> 'Completed'。

于 2009-01-22T16:06:29.730 回答
-2

将您拥有的两个查询用作主查询中的表/列。

于 2009-01-22T16:07:20.290 回答