• <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>
  • sqlServer中 分類報表sql技巧 ,很實用

    發表于:2007-05-25來源:作者:點擊數: 標簽:sql報表SQLServer分類實用
    有表 tableA 如下: ----------------------------------------------------- id, type , countNumber , countDate 1A102005-02-01 21:23:34 2B82005-02-01 21:23:34 3C52005-02-01 21:23:34 4B42005-02-01 11:23:34 5C52005-02-01 22:23:34 6A112005-02-02

    有表 tableA 如下:
    -----------------------------------------------------
    id,    type ,   countNumber ,   countDate
    1 A 10  2005-02-01 21:23:34
    2 B 8  2005-02-01 21:23:34
    3 C 5  2005-02-01 21:23:34
    4 B 4  2005-02-01 11:23:34
    5 C 5  2005-02-01 22:23:34
    6 A 11  2005-02-02 12:23:34
    7 B 9  2005-02-02 14:23:34
    8 C 8  2005-02-02 17:23:34
    9 A 15  2005-02-02 19:23:34
    10 C 6  2005-02-02 04:23:34
    11 A 7  2005-02-03 1:23:34
    12 B 11  2005-02-03 2:23:34
    13 C 12  2005-02-03 5:23:34
    .......................

    想要用一條語句生成如下的報表

    日期   類型A 類型B 類型C 
    2005-02-01   10 12 10 
    2005-02-02   26 9 14 
    2005-02-03   7 11 12 


    我用的是如下的sql語句:

    SELECT
     a.DATEFMT
     ,(select sum(*) from tableA where type='A' and convert(char(10),countDate,20)= a.DATEFMT) typeA
     ,(select sum(*) from tableA where type='B' and convert(char(10),countDate,20)= a.DATEFMT) typeB
     ,(select sum(*) from tableA where type='C' and convert(char(10),countDate,20)= a.DATEFMT) typeC

    FROM
    (
     SELECT convert(char(10),countDate,20) DATEFMT
     FROM tableA
     GROUP BY convert(char(10),countDate,20)
    ) a


    但是這條語句在sqlserver 上執行起來特別的慢,要很久(20多秒),tabelA 中有9萬條數據. 而我在mySql(表中有2萬條數據)上執行就快很多!

    后來在論壇上看到高手這樣寫

    select convert(char(10),countDate,20),
    typeA=sum(case type when A then CountNumber else 0 end),
    typeB=sum(case type when B then CountNumber else 0 end),
    typeC=sum(case type when C then CountNumber else 0 end)
    From tableA
    group by convert(char(10),countDate,20)

    只用 1秒 不到!  唉! 厲害厲害!


    原文轉自:http://www.kjueaiud.com

    老湿亚洲永久精品ww47香蕉图片_日韩欧美中文字幕北美法律_国产AV永久无码天堂影院_久久婷婷综合色丁香五月

  • <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>