1、按id分别计算总费用select id,sum(price*num) 总费用 where date between '2003-2-20' and '2003-3-20' group by id
2、一条记录的费用:当前记录和后一条记录的时间差×price×num,如:datediff(day,2003-2-20,2003-3-1)×0.2×2select id,isnull(datediff(day,(select max(date) from 表 aa where aa.时间<表.时间),时间),1)*price*num 总费用 from 表 where date between '2003-2-20' and '2003-3-20'

解决方案 »

  1.   

    select A.*, 
     datediff(day,A.date,(select min(date) from yourtable where date > A.date and id = A.id))×A.price*A.num as 总费用 
     from yourtable as A
      

  2.   

    1,select id,sum(price*num) as TotalAmount from table where [date]》='2003-2-20' and [date]<'2003-3-21'
      

  3.   

    2.
    select A.*, 
     datediff(day,A.date,
       isnull((select min(date) from yourtable where date > A.date and id = A.id),
         cast('2003-3-20' as datetime)) *A.price*A.num as 总费用 
     from yourtable as A
    where date betwennt '2003-2-20' and '2003-3-20'
    1.
    select id, sum(总费用) from 
    (select A.*, 
     datediff(day,A.date,
       isnull((select min(date) from yourtable where date > A.date and id = A.id),
         cast('2003-3-20' as datetime)) *A.price*A.num as 总费用 
     from yourtable as A
    where date betwennt '2003-2-20' and '2003-3-20') as B
    group by B.id
      

  4.   

    2,select identity(int,1,1)as iid,* into #temp from tableselect a.id,cast(cast(b.[date] as datetime)-cast(a.[date] as datetime)as int)*0.2*2 as TotalAmount
    from #temp a left join #temp b on a.id=b.id and a.iid=b.iid+1
      

  5.   

    2,
    try:select identity(int,1,1)as iid,* into #temp from table
    select id,sum(totalamount) as totalamount from (
    select a.id,cast(cast(isnull(b.[date],a.[date]) as datetime)-cast(a.[date] as datetime)as int)*0.2*2 as TotalAmount
    from #temp a left join #temp b on a.id=b.id and a.iid=b.iid+1)aaa
    group by id
      

  6.   

    1:
    select id,sum(price*num) 总费用 where date between '2003-2-20' and '2003-3-20' group by id
    2:
    select A.*, 
     datediff(day,A.date,
       isnull((select min(date) from yourtable where date > A.date and id = A.id),
         cast('2003-3-20' as datetime)) *A.price*A.num as 总费用 
     from yourtable as A
    where date betwennt '2003-2-20' and '2003-3-20'
      

  7.   

    tj_dns(愉快的登山者)的已经很接近
    我自己看了
    谢谢