我在SQL数据库中有一个表,数据是每天导入的,
现在计划统计一下每天前10个记录(按数量排序)的合计.
如果采用:SELECT SUM(QTY) FROM (SELECT TOP 10 QTY FORM 表 WHERE DATE='2007-8-13' ODER BY QTY DESC )则只能得到这一天的合计结果.
如果采用:SELECT SUM((QTY) FROM (SELECT TOP 10 QTY FORM 表 ORDER BY QTY DESC)则只能得到表中所有天数的前10行合计结果.
请高手指点采用什么语句可以解决问题,谢谢先!

解决方案 »

  1.   

    --方法一:
    Select [DATE], SUM(QTY) As SUMQTY From 表 A
    Where (Select Count([DATE]) From 表 Where [DATE] = A.[DATE] And QTY > A.QTY) < 10
    Group By [DATE]--方法二:
    Select [DATE], SUM(QTY) As SUMQTY From 表 A
    Where Exists (Select Count([DATE]) From 表 Where [DATE] = A.[DATE] And QTY > A.QTY Having Count([DATE]) < 10)
    Group By [DATE]--方法三:
    Select [DATE], SUM(QTY) As SUMQTY From 表 A
    Where QTY In (Select TOP 10 QTY From 表 Where [DATE] = A.[DATE] Order By QTY Desc)
    Group By [DATE]
      

  2.   

    按PAOLOU的指示,我想方法一和方法二的结果不对.
    按方法三编写了语句:
    Select Code,ExcghDate,sum(qty) as SumQty from PQexchgbyDate A
    Where id=3 and QTY In (Select TOP 10 QTY From PQexchgbydate Where ExcghDate=A.ExcghDate Order By QTY Desc)
    Group by ExcghDAte,code
    执行了好长好长时间,没有反应,只好中取消.请各位前辈继续指导!!
      

  3.   

    --试试这个
    declare @table table(date datetime,qty numeric(13,3))
    insert into @table 
    select '2007-07-01',123.12
    union all select '2007-07-01',12.245
    union all select '2007-07-01',15.245
    union all select '2007-07-01',17.245
    union all select '2007-07-01',22.245
    union all select '2007-07-01',32.245
    union all select '2007-07-01',52.245
    union all select '2007-07-01',37.245
    union all select '2007-07-01',19.245
    union all select '2007-07-01',33.245
    union all select '2007-07-01',42.245
    union all select '2007-07-01',4563.245
    union all select '2007-07-02',543.245
    union all select '2007-07-02',14532.245
    union all select '2007-07-02',536245
    union all select '2007-07-02',2131.245
    union all select '2007-07-02',7856.245
    union all select '2007-07-02',123.245
    union all select '2007-07-02',786.245
    union all select '2007-07-02',789.245
    union all select '2007-07-02',465.245
    union all select '2007-07-02',78.245
    union all select '2007-07-02',456.245
    union all select '2007-07-02',645.245
    union all select '2007-07-03',12.245
    union all select '2007-07-03',15.245
    union all select '2007-07-03',17.245
    union all select '2007-07-03',22.245
    union all select '2007-07-03',32.245
    union all select '2007-07-03',52.245
    union all select '2007-07-03',37.245
    union all select '2007-07-03',19.245
    union all select '2007-07-03',33.245
    union all select '2007-07-03',42.245
    union all select '2007-07-03',4563.245
    select distinct(a.date),(select sum(b.qty)from (select top 10 date,qty from @table where date=a.date order by qty desc) b )qty from @table  a group by a.date,qty(35 row(s) affected)
    date                    qty
    ----------------------- ---------------------------------------
    2007-07-01 00:00:00.000 4942.325
    2007-07-02 00:00:00.000 564450.205
    2007-07-03 00:00:00.000 4834.450(3 row(s) affected)
      

  4.   

    patrickwu() ( ) 信誉:100  2007-8-13 20:38:43  得分: 0  
     
     
       
    按PAOLOU的指示,我想方法一和方法二的结果不对.------------------
    是你想的不對,還是測試出來的不對?三個語句的結果應該是一樣的。你先測試看看