2

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

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 关键字‘组’附近的语法不正确。”

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

谢谢!

4

2 回答 2

2

GROUP BY并且HAVING在你的 SQL 中是错误的

于 2013-11-04T01:48:03.417 回答
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;
于 2013-11-04T01:49:31.330 回答