Sql按日周月数据查询

查询本日的记录
select count(*) from tableName where (DATEDIFF(dd, theDate, GETDATE()) = 0)
查询本周的记录
select count(*) from tableName where (DATEDIFF(wk, theDate, GETDATE()) = 0)
查询本月的记录
select count(*) from tableName where (DATEDIFF(mm, theDate, GETDATE()) = 0)
查询本季的记录
select count(*) from tableName where (DATEDIFF(qq, theDate, GETDATE()) = 0)
查询本年的记录
select count(*) from tableName where (DATEDIFF(yy, theDate, GETDATE()) = 0)

///2个参数:

intTop 取多少条

type 是取日,周或月

 

create PROCEDURE [dbo].[Product_Ranking]
 @intTop int,
    @type int
AS
    declare @sql varchar(500),
            @where varchar(70)
    if @type=1
      set  @where=' WHERE (DATEDIFF(dd, ClickDate, GETDATE()) = 0) '
    else
     begin
       if @type=2
        set   @where=' WHERE (DATEDIFF(wk, ClickDate, GETDATE()) = 0)'
       else
          begin
           if @type=3
            set  @where=' WHERE (DATEDIFF(mm, ClickDate, GETDATE()) = 0) '
           else
            set @where=' where 1=2 '
          end
      end
    set @sql='SELECT ProductID,ProductName,
(case StandPrice when 0 then
  (case AuctionStartPrice when 0 then
      (case CurrentPrice when 0 then 0 else CurrentPrice end)
      else AuctionStartPrice end)
 else StandPrice end) as Prices
 FROM  HLShop_Product WHERE
((StandPrice<>0 or AuctionStartPrice<>0 or CurrentPrice<>0) and ProductID IN('
   set @sql=@sql+'SELECT TOP '+cast(@intTop   as   varchar)+' ProductID FROM HLShop_Product_Click'
   set @sql=@sql+@where+' GROUP BY ProductID ORDER BY COUNT(ClickID) DESC))'
  exec(@sql)
这是一个动态sql的存储过程,功能是对商品进行按日,周,月来进行排行

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值