在SQL Server 2008中遇到子句问题

[英]HAVING Clause Issues in SQL Server 2008


I am experiencing issues with my HAVING clause.
Explanation: Each orderno has at least one rxnum tied to it. But each rxnum can have multiple scripts (scriptitemcnt)'s. My problem is that I am trying to use a HAVING clause to ONLY pull orderno's that have the SUM of items less than or equal to 8. The query will execute, but it is still pulling orders that have item sum greater than 8. Here is my code:

我遇到了HAVING子句的问题。说明:每个orderno至少有一个rxnum绑定它。但是每个rxnum都可以有多个脚本(scriptitemcnt)。我的问题是我正在尝试使用HAVING子句来仅拉动具有小于或等于8的项的SUM的orderno。查询将执行,但它仍然会拉动项目总和大于8的订单。这是我的代码:

SELECT
oh.orderno,
od.rxnum,
SUM(od.scriptitemcnt) as scriptitemcnt,
od.ndctopick,
od.drugdesc,
od.unitno,
od.status,
od.datetimefilled,
od.packingunit,
od.datetimepacked,
oh.totesideinorder

FROM
mck_hvs.oldorderdetails od with( nolock ),
mck_hvs.oldorderheader oh with( nolock )

WHERE
oh.orderno = od.orderno
and od.status != 5
and ( @dateFrom is NULL or od.datetimepacked >= cast( @dateFrom as datetime ) )
and ( @dateTo   is NULL or od.datetimepacked  <  cast( @dateTo as datetime ) + 1 )
and oh.totesideinorder = 'N'
and od.packingunit NOT IN (695, 696, 697, 698)

GROUP BY
oh.orderno,
od.rxnum,
od.scriptitemcnt,
od.ndctopick,
od.drugdesc,
od.rxnum,
od.unitno,
od.status,
od.datetimefilled,
od.packingunit,
od.datetimepacked,
oh.totesideinorder

HAVING 
SUM(od.scriptitemcnt) <= '8'

ORDER BY
oh.orderno asc,
od.rxnum asc

1 个解决方案

#1


0  

I see two possible problems. First, you're comparing the count against the string '8' instead of the number 8. Depending on how SQL server interprets things, that could result in numbers like 10 being treated as less than 8.

我看到两个可能的问题。首先,您将计数与字符串'8'而不是数字8进行比较。根据SQL Server解释事物的方式,可能会导致像10这样的数字被视为小于8。

Now that's the simple issue. From your description it sounds like you want orders that have a total count not greater than 8. If that's right, then the bigger issue follows...

现在这是一个简单的问题。根据您的描述,您似乎想要总计数不超过8的订单。如果这是正确的,那么更大的问题是......

When you use GROUP BY, aggregates (like sum()) count within a group. Because you're showing (i.e. SELECTing) - and therefore grouping by - the RXNUM, the sum is per RXNUM, not per order. If you want to pull a record for each RXNUM but filter based on an aggregate for the entire ORDERNO it's a little trickier than what you're doing here.

使用GROUP BY时,聚合(如sum())计入组内。因为您正在显示(即选择) - 因此按RXNUM分组,所以总和是每个RXNUM,而不是每个订单。如果你想为每个RXNUM拉一条记录,但是根据整个ORDERNO的聚合过滤它,这比你在这里做的要复杂一点。

You can use SUM() OVER() instead of SUM() as a starting point. But IIRC this can't be used directly for filtering in sql-server, so you end up needing a subquery. Something like

您可以使用SUM()OVER()而不是SUM()作为起点。但是IIRC不能直接用于sql-server中的过滤,所以你最终需要一个子查询。就像是

select *
  from (select oh.orderno, od.rxnum
             , sum(od.scriptitemcnt) over(partition by orderno) as order_scriptitemcnt
               -- other data
          from -- ...
       )
 where order_scriptitemcnt <= 8

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2017/05/04/5e194c2919b1f29296853f7acea50f61.html



 
  © 2014-2022 ITdaan.com 联系我们: