如何将以下结果放在一个存储过程里返回个临时表里,无参数,就是一些统计函数,三个表无任何关系
也就是说不要分开查询,一个存储过程搞定--【这两条查询针对表clic】
select count(cid),sum(count),sum(hasnum) from clicselect count(cid),sum(count),sum(hasnum) from clic
where month(createtime)=month(getdate()) and year(createtime)=year(getdate())
--【这两条查询针对表brow】
select count(bid),sum(wnum),sum(hasnum) from browselect count(bid),sum(wnum),sum(hasnum) from brow
where month(createtime)=month(getdate()) and year(createtime)=year(getdate())
--【这两条查询针对表surv】
select count(sid),sum(total),sum(hasnum) from survselect count(sid),sum(total),sum(hasnum) from surv
where month(createtime)=month(getdate()) and year(createtime)=year(getdate())

解决方案 »

  1.   

    加多个字段区分,一个记录集返回,类如:--【这两条查询针对表clic】
    select type= 11,count(cid) as data1,sum(count) as data2,sum(hasnum) as data3 from clic
    union all
    select type = 12,count(cid),sum(count),sum(hasnum) from clic
    where month(createtime)=month(getdate()) and year(createtime)=year(getdate())
    --【这两条查询针对表brow】
    union all
    select type = 21,count(bid),sum(wnum),sum(hasnum) from browunion all
    select type = 22,count(bid),sum(wnum),sum(hasnum) from brow
    where month(createtime)=month(getdate()) and year(createtime)=year(getdate())
    --【这两条查询针对表surv】
    union all
    select type = 31,count(sid),sum(total),sum(hasnum) from survunion all
    select type = 32,count(sid),sum(total),sum(hasnum) from surv
    where month(createtime)=month(getdate()) and year(createtime)=year(getdate())
    合不合适自己考虑,反正活人不能被尿憋死
      

  2.   


    create proc MyProc
    as
    begin
    create table Temp ( id int, [count] int, hasnum int)
    insert into Temp select count(cid),sum(count),sum(hasnum) from clic
    insert into Temp select count(cid),sum(count),sum(hasnum) from clic
    where month(createtime)=month(getdate()) and year(createtime)=year(getdate())
    insert into Temp select count(bid),sum(wnum),sum(hasnum) from brow
    insert into Temp select count(bid),sum(wnum),sum(hasnum) from brow
    where month(createtime)=month(getdate()) and year(createtime)=year(getdate())
    insert into Temp select count(sid),sum(total),sum(hasnum) from surv
    insert into Temp select count(sid),sum(total),sum(hasnum) from surv
    where month(createtime)=month(getdate()) and year(createtime)=year(getdate())
    select * from temp
    drop table temp
    end