select sum(b.销售额) as total from 
(
select  a.货号,sum(a.数量) as 销售数量,sum(a.售价 * a.数量) as 销售额  from 
 (
 select 货号,售价, sum(数量) as 数量 from sale_detail where --货号 in (select  product.货号 from product inner join provider on product.供应商编号=provider.供应商编号 where product.货号 like '%') and 
 时间 between cast('2003-10-7 00:00:00' as smalldatetime) 
 and  cast('2003-10-7 23:00:00' as smalldatetime) group by 货号,售价
 union all 
 select 货号,售价, sum(数量) as 数量 from old_sale_detail --where 货号 in (select  product.货号 from product inner join provider on product.供应商编号=provider.供应商编号 where product.货号 like '%') and
时间 between cast('2003-10-7 00:00:00' as smalldatetime) 
 and  cast('2003-10-7 23:00:00' as smalldatetime)  group by 货号,售价
 ) a  group by a.货号 
) b这个呢?和哪个一样?

解决方案 »

  1.   

    是:select sum(b.销售额) as total from 
    (
    select  a.货号,sum(a.数量) as 销售数量,sum(a.售价 * a.数量) as 销售额  from 
     (
     select 货号,售价, sum(数量) as 数量 from sale_detail where --货号 in (select  product.货号 from product inner join provider on product.供应商编号=provider.供应商编号 where product.货号 like '%') and 
     时间 between cast('2003-10-7 00:00:00' as smalldatetime) 
     and  cast('2003-10-7 23:00:00' as smalldatetime) group by 货号,售价
     union all 
     select 货号,售价, sum(数量) as 数量 from old_sale_detail where  --货号 in (select  product.货号 from product inner join provider on product.供应商编号=provider.供应商编号 where product.货号 like '%') and
    时间 between cast('2003-10-7 00:00:00' as smalldatetime) 
     and  cast('2003-10-7 23:00:00' as smalldatetime)  group by 货号,售价
     ) a  group by a.货号 
    ) b
      

  2.   

    to  pengdali(大力 V3.0) 看起来一样,但执行的结果是不一样的,你要不要用实际数据试试看
      

  3.   

    还是你的货号 in (select  product.货号 from product inner join provider on product.供应商编号=provider.供应商编号 where product.货号 like '%') and 这句有问题!
      

  4.   

    select  product.货号 from product left join provider on product.供应商编号=provider.供应商编号 where product.货号 like '%'或:select  product.货号 from product full join provider on product.供应商编号=provider.供应商编号 where product.货号 like '%'试试
      

  5.   

    谢谢你了,pengdali(大力 V3.0) 我们找到了原因,有些货号被从product中删除了,而我用的是等值连接,所以有些记录就没有被连接上,故两个查询的结果不对