以下语句将返回指定时间段内所有店面商品销售排行(按利润多少),由于各个分店所销售商品都是重复,该语句不管是否同名商品都分列一行单独核算,现在要求将各商品销售情况汇总,不考虑是哪个分店售出,不知道从何下手,在此先谢过各位大虾,在线等
Declare @p1BegDate char(10)
Declare @p2EndDate char(10)
Declare @P3OrgCode char(6)select @p1BegDate=:p1BegDate
select @p2EndDate=:p2EndDate
select @P3OrgCode=:p3OrgCode
Declare @i int
Declare @sDate char(10)
Declare @sSQL varchar(8000)If Exists(Select name from tempdb..sysobjects where name='##TmpSale' and XType='U')
  Drop table ##TmpSale
Create Table ##TmpSale
  (OrgCode char(10),PluCode char(30),DepCode char(10),VendorCode char(30) NULL,Counts money,Cost money,
   NetCost money,Total money,Amount money,NetAmount money,NormalDsc money,VipDsc money,PriceDsc money,
   GrossProfit money,NetProfit money,AdjustProfit money,NetAdjustprofit money,AccDate char(10)
   )Set @sDate=@p1BegDate
While Left(@sDate,7)<=Left(@p2EndDate,7)
begin
  If Exists(Select name from sysobjects where name='item'+Left(@sDate,4)+SubString(@sDate,6,2)
            and XType='U')
  begin
    set @sSQL = '
        insert into ##TmpSale(OrgCode,PluCode,DepCode,VendorCode,Counts,Cost,NetCost,Total,Amount,
                              NetAmount,NormalDsc,VipDsc,PriceDsc,GrossProfit,NetProfit,AdjustProfit,
                              NetAdjustprofit,AccDate )
        select OrgCode,PluCode,DepCode,VendorCode,Counts,Cost,NetCost,Total,Amount,
               NetAmount,NormalDsc,VipDsc,PriceDsc,GrossProfit,NetProfit,AdjustProfit,
               NetAdjustprofit,AccDate from goodssale'+Left(@sDate,4)+SubString(@sDate,6,2)+'
        where accdate >='''+@p1BegDate+''' and accdate <= '''+@p2enddate+'''
      '
    if @P3OrgCode <> '<全部>'
    begin
      set @sSQL = @sSQL + ' and OrgCode='''+@P3OrgCode+''' '
    end
    Exec(@sSQl)
  end
  Set @sDate=Convert(Char(10),DateAdd(month,1,@sDate),120)
endset @sSQL = '
    select OrgCode as 组织编码,Dept.DepCode as 部门编码,DepName as 部门名称,
           sGoods.PluCode as 商品编码,PluName as 商品名称,
           sum(Counts) AS 销售数量,sum(Amount) AS 含税销售收入,
           sum(NetAmount) AS 无税销售收入,
           sum(GrossProfit-AdjustProfit) AS 毛利, 
           sum(NetProfit-NetAdjustprofit) AS 净毛利,
           case Sum(Amount) when 0 then 100*(case when sum(GrossProfit-AdjustProfit)>=0 then 1 else -1 end)  
             else sum(GrossProfit-AdjustProfit)/Sum(Amount)*100 end as 毛利率,   
           sum(sGoods.Cost+AdjustProfit) AS 含税销售成本, 
           sum(sGoods.NetCost+NetAdjustProfit) AS 无税销售成本, 
           sum(NormalDsc+VipDsc+PriceDsc) AS 商品总优惠,
           sum(sGoods.Total) as 售价金额,
           sum(NormalDsc) as 普通顾客优惠,sum(VipDsc) as 会员优惠金额,
           sum(PriceDsc) as 调价变动金额 
   into ##TmpSaleNew 
   from ##TmpSale as sGoods,Goods,Dept,xtRightDept x
   where  Goods.pluCode=sGoods.pluCode   
          and x.staffCode='''+@sysUserCode+'''     
         and (Dept.DepCode=x.DepCode or Dept.UpperCode1=x.DepCode 
              or Dept.UpperCode2=x.DepCode or Dept.UpperCode3=x.DepCode 
              or Dept.UpperCode4=x.DepCode)
         and (Goods.DepCode=Dept.DepCode)
   group by OrgCode,Dept.DepCode,DepName,sGoods.PluCode,PluName'--and x.staffCode='''+@sysUserCode+'''
if @sysOrderSQL<>''
  set @sSQL=@sSQL+' order by '+@sysOrderSQL
else
  set @sSQL=@sSQL+' order by sum(GrossProfit-AdjustProfit) DESC'Exec(@sSQL)
Select Identity(int,1,1) as 销售排名,* into ##TmpSaleNew1 from ##TmpSaleNew 
select * from ##TmpSaleNew1
/*使用完毕以后删除创建的临时表*/
If Exists(Select name from tempdb..sysobjects where name='##TmpSale' and XType='U')
  Drop table ##TmpSale
If Exists(Select name from tempdb..sysobjects where name='##TmpSaleNew' and XType='U')
  Drop table ##TmpSaleNewIf Exists(Select name from tempdb..sysobjects where name='##TmpSaleNew1' and XType='U')
  Drop table ##TmpSaleNew1
/***********************************************************/