按方法分组错误

时间:2017-10-03 07:51:06

标签: sql-server

我写了一个查询来组合多个表中的记录。由采购订单,采购订单项

命名的表
   [ Note: The column names are not original names, it just for a model data]

采购订单表格中有订单明细,

id    date     vendorid   totalitems  totalqty   grossamnt   netamnt  taxamt
----------------------------------------------------------------------------
1   03/10/17     00001      2           6           12000     13000    1000

采购订单项表包含此类订单明细

poid  id  productcode  qty   rate  tax(%) taxamnt  total
--------------------------------------------------------
1      1     12001      3    6000   2.5    500     6500
2      1     12000      3    6000   2.5    500     6500

我的查询是,

 select po.POID,po.SupplierId,po.TotalItems from 
  PurchaseOrder po, PurchaseOrderItem poi where po.POID=poi.POID group by 
  po.POID, po.SupplierId,po.TotalItems 

查询返回,

 id  vendorid  totalitems
--------------------------
 1     00001      2
 1     00001      2

预期输出是,

id vendorid totalitems
------------------------
1    00001    2

1 个答案:

答案 0 :(得分:0)

您使用的是过时的连接方法,请在此处阅读:

ANSI vs. non-ANSI SQL JOIN syntax

您也加入了另一张桌子,但从未使用过它:

select po.POID,po.SupplierId,po.TotalItems 
from PurchaseOrder po, PurchaseOrderItem poi 
where po.POID=poi.POID 
group by po.POID, po.SupplierId,po.TotalItems

可以是:

select po.POID,po.SupplierId,po.TotalItems 
from PurchaseOrder po
group by po.POID, po.SupplierId,po.TotalItem

OR

select DISTINCT 
       po.POID,
       po.SupplierId,
       po.TotalItems 
from   PurchaseOrder po