日期:2014-05-17  浏览次数:20434 次

新手写的语句有问题,请高手帮查下问题
select * from (
select 
i1.FDate as 销售单据日期,
i1.FBillNo as 销售出库编号,
o.FName as 客户名称,
t1.FNumber as 物料代码,
t1.FName as 物料名称,
t1.FModel as 规格型号,
i2.FQty as 出库数量,
i2.FPrice as 单位成本,
i2.FAmount as 成本,
v1.FNumber as 凭证字号
 from ICStockBill i1 
 join ICStockBillEntry i2 on i1.FInterID=i2.FInterID 
 join ICHookRelations h1 on i1.FInterID=h1.FIBInterID and h1.FEntryID=i2.FEntryID
 join t_ICItem t1 on t1.FItemID=i2.FItemID 
 join t_Organization o on i1.FSupplyID=o.FItemID
 join t_Voucher v1 on v1.FVoucherID=i1.FVchInterID
where 
i1.FDate<'********'
and h1.FYear='*FYear*'
and h1.FPeriod='*Month*'
and i1.FTranType='21'
and h1.FIBTag='1'
union 
select 
null as 销售单据日期,
'合计' as 销售出库编号,
null as 客户名称,
null  as 物料代码,
null  as 物料名称,
null  as 规格型号,
sum(i2.FQty) as 出库数量,
null as 单位成本,
sum(i2.FAmount) as 成本,
null as 凭证字号
 from ICStockBill i1 
 join ICStockBillEntry i2 on i1.FInterID=i2.FInterID 
 join ICHookRelations h1 on i1.FInterID=h1.FIBInterID and h1.FEntryID=i2.FEntryID
 join t_ICItem t1 on t1.FItemID=i2.FItemID 
 join t_Organization o on i1.FSupplyID=o.FItemID
 join t_Voucher v1 on v1.FVoucherID=i1.FVchInterID
where 
i1.FDate<'********'
and h1.FYear='*FYear*'
and h1.FPeriod='*Month*'
and i1.FTranType='21'
and h1.FIBTag='1'
) vvv
order by 销售出库编号
/*这个语句执行后的,明细数量与合计数量不一样,不知道问题出在哪里,请高手帮看看,非常感激*/

------解决方案--------------------
你将union换成union all试试
------解决方案--------------------
单独执行这个看合计对不对?

      SELECT
        NULL AS 销售单据日期 ,
        '合计' AS 销售出库编号 ,
        NULL AS 客户名称 ,
        NULL AS 物料代码 ,
        NULL AS 物料名称 ,
        NULL AS 规格型号 ,
        SUM(i2.FQty) AS 出库数量 ,
        NULL AS 单位成本 ,
        SUM(i2.FAmount) AS 成本 ,
        NULL AS 凭证字号
      FROM
        ICStockBill i1
      JOIN