目前,我将数据分解为月份间隔。这就是我希望它显示的方式,但我只想显示ordered_by
对于整个表LoadCount
总共300或更多的那些。所以基本上,我想抛弃任何至少没有300 {/ 1>的ordered_by
SELECT YEAR(stop.actual_arrival) AS Year, MONTH(stop.actual_arrival) AS Month, COUNT(stop.id) AS DeliveryCount, orders.ordered_by, COUNT(DISTINCT orders.id)
AS LoadCount
FROM stop INNER JOIN
(SELECT company_id, order_id, tractor_id
FROM billing_history
GROUP BY order_id, tractor_id, company_id) AS derivedtbl_1 ON stop.company_id = derivedtbl_1.company_id AND stop.order_id = derivedtbl_1.order_id INNER JOIN
tractor ON derivedtbl_1.company_id = tractor.company_id AND derivedtbl_1.tractor_id = tractor.id INNER JOIN
orders ON derivedtbl_1.company_id = orders.company_id AND derivedtbl_1.order_id = orders.id
WHERE (orders.order_type_id IN ('12', '13')) AND (stop.stop_type = 'SO') AND (stop.actual_arrival >= DATEADD(month, - 18, GETDATE())) AND (orders.customer_id = 945000) AND
(orders.ordered_by IS NOT NULL)
GROUP BY YEAR(stop.actual_arrival), MONTH(stop.actual_arrival), orders.ordered_by
ORDER BY Year, Month, orders.ordered_by
我一直在来回询问我是否需要派生表或什么......任何帮助都会非常感激。谢谢你们。
我试图抛弃任何总共不超过300的ordered_by
。如果它们在整个电路板上加起来达到300,我希望它们能够显示出来,即使它们在那条线上是5。
答案 0 :(得分:0)
如果我正确理解了您的问题,您可以使用select子句中的CASE语句来显示NULL或orders.ordered_by,具体取决于LoadCount的值。这是一个包含一些示例的页面:http://msdn.microsoft.com/en-us/library/ms181765(v=sql.110).aspx
答案 1 :(得分:0)
如果你要做的就是抛弃任何ordered_by
小于300的结果,你可以在HAVING
GROUP BY
子句
<snip...>
GROUP BY YEAR(stop.actual_arrival), MONTH(stop.actual_arrival), orders.ordered_by
HAVING orders.ordered_by >= 300
ORDER BY Year, Month, orders.ordered_by