sql server 2000将详细总和与总金额进行比较

时间:2013-11-04 01:44:33

标签: sql sql-server sql-server-2000

我需要一些帮助。我有发票表和发票明细表。我将我关心的数据从发票明细表中提取到临时表(用于其他计算)。我需要找到详细信息行总和不等于发票表中总费用的发票。这是我的代码:

Create table #inv_det (
invco varchar(6),
invno int,
invtot money,
invpd int);

Insert into #inv_det (invco,invno,invtot,invpd)
Select invd_inv_co, invd_inv_no, invd_total_cost, invd_glpd_no 
from invdet 
inner join invoice on invd_inv_co=inv_co and invd_inv_no=inv_no 
where inv_dt>=cast('10-01-13 00:00:00' as datetime) and inv_dt<=cast('10-31-13 11:59:59' as datetime);

这是我的问题:

Select invco, invno from #inv_det 
inner join invoice on invco=inv_co and invno=inv_no 
where inv_dt>=cast('10-01-13 00:00:00' as datetime) 
  and inv_dt<=cast('10-31-13 11:59:59' as datetime) 
having sum(invtot)<>inv_total_amt
group by invco, invno;

当我运行查询时,我得到“服务器:消息156,级别15,状态1,行5” 关键字“group”附近的语法不正确。“

我希望我能很好地解释我的目标。希望有人可以指出我所缺少的东西。我尝试了很多变化。

谢谢!

2 个答案:

答案 0 :(得分:2)

GROUP BYHAVING在SQL中是错误的方法

答案 1 :(得分:2)

GROUP BY应该出现在HAVING之前:

Select invco,invno from #inv_det inner join invoice on
invco=inv_co and invno=inv_no where inv_dt>=cast('10-01-13 00:00:00' as datetime)
and inv_dt<=cast('10-31-13 11:59:59' as datetime) 
group by invco, invno
having sum(invtot)<>inv_total_amt;