1

我有一个正在处理的查询,基本上我有 3 列:

Code: The users Code
Orders Taken: The orders which have been taken
Orders Taken From an External Call

http://gyazo.com/b412a5bb2980feb3220355fa4a8caf5a

基本上我只需要一种方法来只允许来自外部调用的订单有一个 where 子句,当我添加我的 WHERE 子句时,它对两个表都这样做。

到目前为止我的查询:

SELECT T_Temp_RestrictedDiaryCalls.AccreditedDomainCode, 
    Count(T_Temp_RestrictedProductSalesHistory.CustomerCode) AS [Orders Taken],
    Count(T_Temp_RestrictedProductSalesHistory.CustomerCode) AS [Orders From External Calls]

    FROM T_Temp_RestrictedDiaryCalls 
        INNER JOIN T_Temp_RestrictedProductSalesHistory 
            ON (T_Temp_RestrictedDiaryCalls.CustomerCode = T_Temp_RestrictedProductSalesHistory.CustomerCode) 
            AND (T_Temp_RestrictedDiaryCalls.CallDate = T_Temp_RestrictedProductSalesHistory.EntryDate)

    GROUP BY T_Temp_RestrictedDiaryCalls.AccreditedDomainCode;

任何帮助将不胜感激。

4

3 回答 3

0

如果我做对了,您应该在[Orders From External Calls]定义中使用 CASE 语句:

SELECT T_Temp_RestrictedDiaryCalls.AccreditedDomainCode, 
    Count(T_Temp_RestrictedProductSalesHistory.CustomerCode) AS [Orders Taken],
    sum( CASE WHEN <Your condition here>  
              THEN 1 
              ELSE 0
          END) AS [Orders From External Calls]

    FROM T_Temp_RestrictedDiaryCalls 
        INNER JOIN T_Temp_RestrictedProductSalesHistory 
            ON (T_Temp_RestrictedDiaryCalls.CustomerCode = T_Temp_RestrictedProductSalesHistory.CustomerCode) 
            AND (T_Temp_RestrictedDiaryCalls.CallDate = T_Temp_RestrictedProductSalesHistory.EntryDate)

    GROUP BY T_Temp_RestrictedDiaryCalls.AccreditedDomainCode;
于 2013-11-05T12:46:11.497 回答
0

如果我理解正确,您希望这两个Orders列都计算同一个表中的数据,但对表中哪些记录计入和不计入有不同的限制。所以做类似的事情:

SELECT T_Temp_RestrictedDiaryCalls.AccreditedDomainCode, 
    Count(SalesHistoryForAllOrders.CustomerCode) AS [Orders Taken],
    Count(SalesHistoryForExternalOrders.CustomerCode) AS [Orders From External Calls]

    FROM T_Temp_RestrictedDiaryCalls 
        INNER JOIN T_Temp_RestrictedProductSalesHistory SalesHistoryForAllOrders 
            ON (T_Temp_RestrictedDiaryCalls.CustomerCode = SalesHistoryForAllOrders.CustomerCode) 
            AND (T_Temp_RestrictedDiaryCalls.CallDate = SalesHistoryForAllOrders.EntryDate)
        INNER JOIN T_Temp_RestrictedProductSalesHistory SalesHistoryForExternalOrders 
            ON (T_Temp_RestrictedDiaryCalls.CustomerCode = SalesHistoryForExternalOrders.CustomerCode) 
            AND (T_Temp_RestrictedDiaryCalls.CallDate = SalesHistoryForExternalOrders.EntryDate)

    GROUP BY T_Temp_RestrictedDiaryCalls.AccreditedDomainCode;

解释:

通过两次 INNER JOIN 表并每次为它取别名,我们现在有两个单独的表表示可以使用(称为SalesHistoryForAllOrdersSalesHistoryForExternalOrders)。

然后,您可以自由放置分别引用其中任一列的 WHERE 子句,它只会影响该列,而不影响另一列。所以要么这样做:

WHERE SalesHistoryForExternalOrders.IsAnExternalOrder > 0

或者只是在第二个 INNER JOIN 中添加一个额外的 AND:

AND SalesHistoryForExternalOrders.IsAnExternalOrder > 0

,结果是一样的:那个子句只会应用External Calls列。

于 2013-11-05T12:48:32.817 回答
0

根据您在评论中所说的话,一列可能包含所有行的计数,而另一列仅显示特定情况下的值。

所以:

select table1.a,count(table1.b),
(select count(table2.c) from table2 where "where clause")
 from table1
于 2013-11-05T12:50:58.513 回答