create proc sp_find 
@ye varchar(10)
@mon varchar(10)
as 
sql='select '买书日期','买书数' from book where @ye=year('买书日期')and @mon=month('买书日期') '
exec(sql)

解决方案 »

  1.   

    怎么两个
    http://expert.csdn.net/Expert/topic/1124/1124029.xml?temp=.2979853
      

  2.   

    例如:
    表book
    字段buy_date(买书日期 varchar(10)),num(买书数 int)
    建一个函数
    create function mon(@year varchar(4))
    returns table
    as
     return(select sum(num) number from book where substring(buy_date,1,4)=@year group by substring(buy_date,5,6))
    go
    查询时比如查年的各个月的买书数量
    select * from dbo.mon('2002')
      

  3.   

    不好意思写了不完整应该是:
    create function mon(@year varchar(4))
    returns table
    as
     return(select substring(buy_date,5,6)month,sum(num) number from book where substring(buy_date,1,4)=@year group by substring(buy_date,5,6))
    go
      

  4.   

    select month(买书日期)  as 买书月份, count(买书数) 买书数 from yourtable where year(买书日期) = 2000 group by month(买书日期)
      

  5.   

    select month(买书日期)  as 买书月份, count(买书数) 买书数 from yourtable where year(买书日期) = 2000 group by month(买书日期)如果不条语句不用GROUP BY可以运行吗??
      

  6.   

    select 1 as 买书月份, isnull(sum(买书数),0) as 买书数 from yourtable where year(买书日期) = 2000 and month(买书日期)=1
    union
    select 2 as 买书月份, isnull(sum(买书数),0) as 买书数 from yourtable where year(买书日期) = 2000 and month(买书日期)=2
    union
    select 3 as 买书月份, isnull(sum(买书数),0) as 买书数 from yourtable where year(买书日期) = 2000 and month(买书日期)=3
    union
    select 4 as 买书月份, isnull(sum(买书数),0) as 买书数 from yourtable where year(买书日期) = 2000 and month(买书日期)=4.......
      

  7.   

    2000年的这么写:select 1 as 买书月份, isnull(sum(买书数),0) as 买书数 from yourtable where year(买书日期) = 2000 and month(买书日期)=1
    union
    select 2 as 买书月份, isnull(sum(买书数),0) as 买书数 from yourtable where year(买书日期) = 2000 and month(买书日期)=2
    union
    select 3 as 买书月份, isnull(sum(买书数),0) as 买书数 from yourtable where year(买书日期) = 2000 and month(买书日期)=3
    union
    select 4 as 买书月份, isnull(sum(买书数),0) as 买书数 from yourtable where year(买书日期) = 2000 and month(买书日期)=4
    union
    select 5 as 买书月份, isnull(sum(买书数),0) as 买书数 from yourtable where year(买书日期) = 2000 and month(买书日期)=5
    union
    select 6 as 买书月份, isnull(sum(买书数),0) as 买书数 from yourtable where year(买书日期) = 2000 and month(买书日期)=6
    union
    select 7 as 买书月份, isnull(sum(买书数),0) as 买书数 from yourtable where year(买书日期) = 2000 and month(买书日期)=7
    union
    select 8 as 买书月份, isnull(sum(买书数),0) as 买书数 from yourtable where year(买书日期) = 2000 and month(买书日期)=8
    union
    select 9 as 买书月份, isnull(sum(买书数),0) as 买书数 from yourtable where year(买书日期) = 2000 and month(买书日期)=9
    union
    select 10 as 买书月份, isnull(sum(买书数),0) as 买书数 from yourtable where year(买书日期) = 2000 and month(买书日期)=10
    union
    select 11 as 买书月份, isnull(sum(买书数),0) as 买书数 from yourtable where year(买书日期) = 2000 and month(买书日期)=11
    union
    select 12 as 买书月份, isnull(sum(买书数),0) as 买书数 from yourtable where year(买书日期) = 2000 and month(买书日期)=12