alter proc [dbo].[Pd_TB_Profit_DP]
(@FDateB datetime,@FDateE datetime,@ssql varchar(8000))
as
set nocount on   
select convert(varchar(10),[FDate],120) as  FDate ,
sum(case when t2.Ftype=2 then t1.FQty end) FTQty,
sum(case when t2.FType=2 then (t1.FDAmount+t2.FDDHLFee-t1.Fcost-t2.FDHLFee)end) FTprofit,
sum(case when t2.Ftype=1 then t1.FQty end) FSQty,
sum(case when t2.FType=1 then (t1.FDAmount+t2.FDDHLFee-t1.Fcost-t2.FDHLFee) end) FSprofit,
sum(t1.FQty) FZQty,sum(t1.FDAmount+t2.FDDHLFee-t1.Fcost-t2.FDHLFee) FZprofit
from SU_Sale_TBStockBillEntry t1 inner join SU_Sale_TBStockBill t2 on t1.fid=t2.fid  where FDate>=@FDateE and FDate<=@FDateB
GROUP BY convert(varchar(10),[FDate],120)
union all
select '合计',sum(case when t2.Ftype=2 then t1.FQty end) FTQty,
sum(case when t2.FType=2 then (t1.FDAmount+t2.FDDHLFee-t1.Fcost-t2.FDHLFee)end) FTprofit,
sum(case when t2.Ftype=1 then t1.FQty end) FSQty,
sum(case when t2.FType=1 then (t1.FDAmount+t2.FDDHLFee-t1.Fcost-t2.FDHLFee) end) FSprofit,
sum(t1.FQty) FZQty,sum(t1.FDAmount+t2.FDDHLFee-t1.Fcost-t2.FDHLFee) FZprofit
from SU_Sale_TBStockBillEntry t1 inner join SU_Sale_TBStockBill t2 on t1.fid=t2.fid  where FDate>=@FDateE and FDate<=@FDateB
这是我写的一个存储过程
两个select 用union连接起来的 如果 没有 后面的where 语句 查询出来的数据是这样的
FDate  FTQty  FTprofit  FSQty  FSprofit  FZQty FZprofit
xxx     xxx    xxx       xxx     xxx      xxx   xxx    
xxx     xxx    xxx       xxx     xxx      xxx   xxx    
xxx     xxx    xxx       xxx     xxx      xxx   xxx    
合计    xxx    xxx       xxx     xxx      xxx   xxx    
但是我加上where语句之后就这能查询出来
FDate  FTQty  FTprofit  FSQty  FSprofit  FZQty FZprofit   
合计    xxx    xxx       xxx     xxx      xxx   xxx   
就是第一个select语句 没有查到数据
这是怎么回事  然后 case when 是根据 FTYpe来分类的 一个总数据 FQTY  Fprofit  Ftyp=2的 为 Ftqty  Ftprofit 这个没写错吧 
如果还需要什么数据 我后面在补

解决方案 »

  1.   

    select
      *
    from
      (
    select convert(varchar(10),[FDate],120) as  FDate ,
    sum(case when t2.Ftype=2 then t1.FQty end) FTQty,
    sum(case when t2.FType=2 then (t1.FDAmount+t2.FDDHLFee-t1.Fcost-t2.FDHLFee)end) FTprofit,
    sum(case when t2.Ftype=1 then t1.FQty end) FSQty,
    sum(case when t2.FType=1 then (t1.FDAmount+t2.FDDHLFee-t1.Fcost-t2.FDHLFee) end) FSprofit,
    sum(t1.FQty) FZQty,sum(t1.FDAmount+t2.FDDHLFee-t1.Fcost-t2.FDHLFee) FZprofit
    from SU_Sale_TBStockBillEntry t1 inner join SU_Sale_TBStockBill t2 on t1.fid=t2.fid  where FDate>=@FDateE and FDate<=@FDateB
    GROUP BY convert(varchar(10),[FDate],120)
    union all
    select '合计',sum(case when t2.Ftype=2 then t1.FQty end) FTQty,
    sum(case when t2.FType=2 then (t1.FDAmount+t2.FDDHLFee-t1.Fcost-t2.FDHLFee)end) FTprofit,
    sum(case when t2.Ftype=1 then t1.FQty end) FSQty,
    sum(case when t2.FType=1 then (t1.FDAmount+t2.FDDHLFee-t1.Fcost-t2.FDHLFee) end) FSprofit,
    sum(t1.FQty) FZQty,sum(t1.FDAmount+t2.FDDHLFee-t1.Fcost-t2.FDHLFee) FZprofit
    from SU_Sale_TBStockBillEntry t1 inner join SU_Sale_TBStockBill t2 on t1.fid=t2.fid )t where FDate>=@FDateE and FDate<=@FDateB
      

  2.   

    select convert(varchar(10),[FDate],120) as  FDate ,
    sum(case when t2.Ftype=2 then t1.FQty end) FTQty,
    sum(case when t2.FType=2 then (t1.FDAmount+t2.FDDHLFee-t1.Fcost-t2.FDHLFee)end) FTprofit,
    sum(case when t2.Ftype=1 then t1.FQty end) FSQty,
    sum(case when t2.FType=1 then (t1.FDAmount+t2.FDDHLFee-t1.Fcost-t2.FDHLFee) end) FSprofit,
    sum(t1.FQty) FZQty,sum(t1.FDAmount+t2.FDDHLFee-t1.Fcost-t2.FDHLFee) FZprofit
    from SU_Sale_TBStockBillEntry t1 inner join SU_Sale_TBStockBill t2 on t1.fid=t2.fid  where FDate>=@FDateE and FDate<=@FDateB
    GROUP BY convert(varchar(10),[FDate],120)
    单独执行该段语句能否查到值?
    或者把存储过程中的where条件放到having字句中试试。
      

  3.   

    但是我加上where语句之后就这能查询出来?--加的條件是?
      

  4.   

    第一段单独运行,有结果吗?
    select convert(varchar(10),[FDate],120) as  FDate ,
    sum(case when t2.Ftype=2 then t1.FQty end) FTQty,
    sum(case when t2.FType=2 then (t1.FDAmount+t2.FDDHLFee-t1.Fcost-t2.FDHLFee)end) FTprofit,
    sum(case when t2.Ftype=1 then t1.FQty end) FSQty,
    sum(case when t2.FType=1 then (t1.FDAmount+t2.FDDHLFee-t1.Fcost-t2.FDHLFee) end) FSprofit,
    sum(t1.FQty) FZQty,sum(t1.FDAmount+t2.FDDHLFee-t1.Fcost-t2.FDHLFee) FZprofit
    from SU_Sale_TBStockBillEntry t1 inner join SU_Sale_TBStockBill t2 on t1.fid=t2.fid  where FDate>=@FDateE and FDate<=@FDateB
    GROUP BY convert(varchar(10),[FDate],120)
      

  5.   

    消息 241,级别 16,状态 1,过程 Pd_TB_Profit_DP,第 5 行
    从字符串向 datetime 转换时失败。 能否说下思路 错误
      

  6.   

    太多了 不好一一回复了,我在说下吧,如果我把FDateE 跟FDateB  放入实际时间 比如 '2011-01-01 11:11:11.000' '2011-11-11 11:11:11.000' 这两个语句能够查询出我要的正确数据 
    但是我用 [Pd_TB_Profit_DP] '2011-01-01 11:11:11.000','2011-11-11 11:11:11.000','' 这样执行存储过程的时候  就只有 一个 合计  然后就是null  null  出现了
      

  7.   

    你的@ssql没有用到.你那个语句是要放到@ssql中,动态执行吗?
      

  8.   

    如果是这样,需要拼接.特别是你的日期的地方,需要转换为字符串.
    参考如下:
    --动态sql语句基本语法
     
    1 :普通SQL语句可以用Exec执行 eg: Select * from tableName 
    Exec('select * from tableName') 
    Exec sp_executesql N'select * from tableName' -- 请注意字符串前一定要加N 2:字段名,表名,数据库名之类作为变量时,必须用动态SQL eg: 
    declare @fname varchar(20) 
    set @fname = 'FiledName' 
    Select @fname from tableName -- 错误,不会提示错误,但结果为固定值FiledName,并非所要。 
    Exec('select ' + @fname + ' from tableName') -- 请注意 加号前后的 单引号的边上加空格 当然将字符串改成变量的形式也可 
    declare @fname varchar(20) 
    set @fname = 'FiledName' --设置字段名 declare @s varchar(1000) 
    set @s = 'select ' + @fname + ' from tableName' 
    Exec(@s) -- 成功 
    exec sp_executesql @s -- 此句会报错 declare @s Nvarchar(1000) -- 注意此处改为nvarchar(1000) 
    set @s = 'select ' + @fname + ' from tableName' 
    Exec(@s) -- 成功 
    exec sp_executesql @s -- 此句正确 3. 输出参数 
    declare @num int, 
    @sqls nvarchar(4000) 
    set @sqls='select count(*) from tableName' 
    exec(@sqls) 
    --如何将exec执行结果放入变量中? declare @num int, 
    @sqls nvarchar(4000) 
    set @sqls='select @a=count(*) from tableName ' 
    exec sp_executesql @sqls,N'@a int output',@num output 
    select @num