一个表A 2个字段 saletime 例子:2009-3-16 2:15:10 salevalue(金额) :35.5
要根据 日期 对金额 进行加法 看每天的营业额 但是一天有好多条记录 不同的时分秒 我该怎么按天 统计呢?请大虾指点!

解决方案 »

  1.   


    select sub(salevalue) from TableA where to_date(saletime,'yyyy-MM-dd') = to_date('传入的日期','yyyy-MM-dd')
      

  2.   

    select convert(varchar,saletime,111),sum(salevalue) from tb
    group by convert(varchar,saletime,111)
      

  3.   

    SELECT CONVERT(NVARCHAR(8),saleTime,112),SUM(金额) FROM A
      

  4.   

    SELECT CONVERT(NVARCHAR(8) as 日期,saleTime,112),SUM(金额) as 金额 FROM A GROUP BY CONVERT(NVARCHAR(8),
      

  5.   


     --年
    select client_id , 
      sum(case when datepart(month,order_time) = 1 then pro_amount*pro_price else 0 end) '1月',
      sum(case when datepart(month,order_time) = 2 then pro_amount*pro_price else 0 end) '2月',
      sum(case when datepart(month,order_time) = 3 then pro_amount*pro_price else 0 end) '3月',
      sum(case when datepart(month,order_time) = 4 then pro_amount*pro_price else 0 end) '4月',
      sum(case when datepart(month,order_time) = 5 then pro_amount*pro_price else 0 end) '5月',
      sum(case when datepart(month,order_time) = 6 then pro_amount*pro_price else 0 end) '6月',
      sum(case when datepart(month,order_time) = 7 then pro_amount*pro_price else 0 end) '7月',
      sum(case when datepart(month,order_time) = 8 then pro_amount*pro_price else 0 end) '8月',
      sum(case when datepart(month,order_time) = 9 then pro_amount*pro_price else 0 end) '9月',
      sum(case when datepart(month,order_time) = 10 then pro_amount*pro_price else 0 end) '10月',
      sum(case when datepart(month,order_time) = 11 then pro_amount*pro_price else 0 end) '11月',
      sum(case when datepart(month,order_time) = 12 then pro_amount*pro_price else 0 end) '12月'
    from order,item where order.order_id = item.order_id
    group by client_id
    -----------------------------------------
    --季度
    select client_id , 
      sum(case when datepart(quarter,order_time) = 1 then pro_amount*pro_price else 0 end) '第一季度',
      sum(case when datepart(quarter,order_time) = 2 then pro_amount*pro_price else 0 end) '第二季度',
      sum(case when datepart(quarter,order_time) = 3 then pro_amount*pro_price else 0 end) '第三季度',
      sum(case when datepart(quarter,order_time) = 4 then pro_amount*pro_price else 0 end) '第四季度'
    from order,item where order.order_id = item.order_id
    group by client_id
    -------------------------------------------------
    create table [order]
    (
    order_id int,
    client_id int,
    order_time datetime
    )
    create table item
    (
    item_idint,
    order_id int,
    pro_id int,
    pro_amount int,
    pro_price int
    )
    insert into [order] 
    select 1,1,'2007-1-5'
    union all
    select 2,1,'2007-1-7'
    union all
    select 3,1,'2007-6-5'
    union all
    select 4,3,'2007-2-5'
    union all
    select 5,3,'2007-2-18'
    insert into item
    select 1,1,1,10,10
    union all
    select 2,1,3,5,15
    union all
    select 3,2,1,5,12
    union all
    select 4,3,2,10,8
    union all
    select 5,4,3,2,15
    union all
    select 6,5,2,6,10select client_id , 
      sum(case when month(order_time) = 1 then pro_amount*pro_price else 0 end) '1月',
      sum(case when month(order_time) = 2 then pro_amount*pro_price else 0 end) '2月',
      sum(case when month(order_time) = 3 then pro_amount*pro_price else 0 end) '3月',
      sum(case when month(order_time) = 4 then pro_amount*pro_price else 0 end) '4月',
      sum(case when month(order_time) = 5 then pro_amount*pro_price else 0 end) '5月',
      sum(case when month(order_time) = 6 then pro_amount*pro_price else 0 end) '6月',
      sum(case when month(order_time) = 7 then pro_amount*pro_price else 0 end) '7月',
      sum(case when month(order_time) = 8 then pro_amount*pro_price else 0 end) '8月',
      sum(case when month(order_time) = 9 then pro_amount*pro_price else 0 end) '9月',
      sum(case when month(order_time) = 10 then pro_amount*pro_price else 0 end) '10月',
      sum(case when month(order_time) = 11 then pro_amount*pro_price else 0 end) '11月',
      sum(case when month(order_time) = 12 then pro_amount*pro_price else 0 end) '12月'
    from [order],item where [order].order_id = item.order_id
    group by client_id
    ---------------------------------------------------
    --周
    select client_id , 
      sum(case when datepart(week,order_time) = 1 then pro_amount*pro_price else 0 end) '第一周',
      sum(case when datepart(week,order_time) = 2 then pro_amount*pro_price else 0 end) '第二周',
      sum(case when datepart(week,order_time) = 3 then pro_amount*pro_price else 0 end) '第三周',
      sum(case when datepart(week,order_time) = 4 then pro_amount*pro_price else 0 end) '第四周',
      ......................
    from order,item where order.order_id = item.order_id
    group by client_id
    -----------------------------------------------------------------------
    --日
    select client_id , convert(varchar(7),order_time,120) 月份,
      sum(case when datepart(day,order_time) = 1 then pro_amount*pro_price else 0 end) '1',
      sum(case when datepart(day,order_time) = 2 then pro_amount*pro_price else 0 end) '2',
      sum(case when datepart(day,order_time) = 3 then pro_amount*pro_price else 0 end) '3',
      sum(case when datepart(day,order_time) = 4 then pro_amount*pro_price else 0 end) '4',
      ......................
      sum(case when datepart(day,order_time) = 4 then pro_amount*pro_price else 0 end) '31'
    from order,item where order.order_id = item.order_id
    group by client_id,convert(varchar(7),order_time,120)
    ----------------------------------------------------------------------
    --按周一、二计算(假设order_time为日期型数据,即不含有时,分,秒等)
    select client_id ,
      sum(case when order_time = DATEADD(wk,  DATEDIFF(wk,0,getdate()),  0) - 1 then pro_amount*pro_price else 0 end) '周日',
      sum(case when order_time = DATEADD(wk,  DATEDIFF(wk,0,getdate()),  0) then pro_amount*pro_price else 0 end) '周一',
      sum(case when order_time = DATEADD(wk,  DATEDIFF(wk,0,getdate()),  0) + 1 then pro_amount*pro_price else 0 end) '周二',
      sum(case when order_time = DATEADD(wk,  DATEDIFF(wk,0,getdate()),  0) + 2 then pro_amount*pro_price else 0 end) '周三',
      sum(case when order_time = DATEADD(wk,  DATEDIFF(wk,0,getdate()),  0) + 3 then pro_amount*pro_price else 0 end) '周四',
      sum(case when order_time = DATEADD(wk,  DATEDIFF(wk,0,getdate()),  0) + 4 then pro_amount*pro_price else 0 end) '周五',
      sum(case when order_time = DATEADD(wk,  DATEDIFF(wk,0,getdate()),  0) + 5 then pro_amount*pro_price else 0 end) '周六'
    from order,item where order.order_id = item.order_id
    group by client_id
      

  6.   

    select   datename(mm,date),sum(money)   from   table   group   by   datename(mm,date)
      

  7.   

    select   datename(mm,date),sum(money)   from   table   group   by   datename(d,date)
      

  8.   

    SELECT convert(varchar(10),saletime,120) as saletime,sum(salevalue) as Total  FROM T GROUP BY  convert(varchar(10),saletime,120)
      

  9.   

        where  datapart(day,'数据库时间')=datapart(day,'传入时间') 
      再取和就可以了sum