表结构如下id    title    sendTime
1    wwwww     2010-08-06
2    sfddd     2010-08-07
3    ffff      2010-09-01
4    rrrr      2010-09-02
5    ffff      2010-09-02
6    kkkk      2010-09-03
7    jjjj      2010-10-01想实现先按月分组然后再按日分组,最终表现形式如下:                8月新闻
6日新闻
1    wwwww     2010-08-06
7日新闻
2    sfddd     2010-08-07
               9月新闻  
1日新闻
3    ffff      2010-09-01
2日新闻
4    rrrr      2010-09-02
5    ffff      2010-09-02
……请问该怎么实现呢? 

解决方案 »

  1.   

    格式好像在程序上处理就可以,在sql要这样的格式?
      

  2.   

    select id ,title ,sendTime from
    (
    select null id , null title , convert(varchar(7),sendTime,120) + '月新闻' sendTime , convert(varchar(4),sendtime,120) px from tb
    union all
    select null id , null title , datename(day,sendTime) + '日新闻' sendTime,convert(varchar(7),sendtime,120) px from tb
    union all
    select id , title , convert(varchar(10),sendtime,120) sendtime , convert(varchar(4),sendtime,120) px from tb
    ) t
    order by px
      

  3.   

    select cast(datepart(mm,sendtime) as varchar(2))+'月新闻',
    cast(datepart(dd,sendtime) as varchar(2))+'日新闻', * from tb order by sendtime desc
      

  4.   

    真要顶在记录的上面就用union吧,如#3
      

  5.   

    create table tb(id int,title varchar(20),sendTime datetime)
    insert into tb values(1 ,'wwwww' ,'2010-08-06')
    insert into tb values(2 ,'sfddd' ,'2010-08-07')
    insert into tb values(3 ,'ffff'  ,'2010-09-01')
    insert into tb values(4 ,'rrrr'  ,'2010-09-02')
    insert into tb values(5 ,'ffff'  ,'2010-09-02')
    insert into tb values(6 ,'kkkk'  ,'2010-09-03')
    insert into tb values(7 ,'jjjj'  ,'2010-10-01')
    goselect id ,title ,sendTime from
    (
    select null id , null title , convert(varchar(7),sendTime,120) + '月新闻' sendTime , convert(varchar(10),sendtime,120) px1,px2=1 from tb
    union all
    select null id , null title , datename(day,sendTime) + '日新闻' sendTime,convert(varchar(10),sendtime,120) px1,px2=2 from tb
    union all
    select id , title , convert(varchar(10),sendtime,120) sendtime , convert(varchar(10),sendtime,120) px1,px2 = 3 from tb
    ) t
    order by px1,px2drop table tb/*
    id          title                sendTime                             
    ----------- -------------------- ------------------------------------ 
    NULL        NULL                 2010-08月新闻
    NULL        NULL                 6日新闻
    1           wwwww                2010-08-06
    NULL        NULL                 2010-08月新闻
    NULL        NULL                 7日新闻
    2           sfddd                2010-08-07
    NULL        NULL                 2010-09月新闻
    NULL        NULL                 1日新闻
    3           ffff                 2010-09-01
    NULL        NULL                 2010-09月新闻
    NULL        NULL                 2010-09月新闻
    NULL        NULL                 2日新闻
    NULL        NULL                 2日新闻
    4           rrrr                 2010-09-02
    5           ffff                 2010-09-02
    NULL        NULL                 2010-09月新闻
    NULL        NULL                 3日新闻
    6           kkkk                 2010-09-03
    NULL        NULL                 2010-10月新闻
    NULL        NULL                 1日新闻
    7           jjjj                 2010-10-01(所影响的行数为 21 行)*/
      

  6.   

    create table tb(id int,title varchar(20),sendTime datetime)
    insert into tb values(1 ,'wwwww' ,'2010-08-06')
    insert into tb values(2 ,'sfddd' ,'2010-08-07')
    insert into tb values(3 ,'ffff'  ,'2010-09-01')
    insert into tb values(4 ,'rrrr'  ,'2010-09-02')
    insert into tb values(5 ,'ffff'  ,'2010-09-02')
    insert into tb values(6 ,'kkkk'  ,'2010-09-03')
    insert into tb values(7 ,'jjjj'  ,'2010-10-01')
    goselect id ,title ,sendTime from
    (
    select '' id , '' title , convert(varchar(7),sendTime,120) + '月新闻' sendTime , convert(varchar(10),sendtime,120) px1,px2=1 from tb
    union all
    select '' id , '' title , datename(day,sendTime) + '日新闻' sendTime,convert(varchar(10),sendtime,120) px1,px2=2 from tb
    union all
    select ltrim(id) , title , convert(varchar(10),sendtime,120) sendtime , convert(varchar(10),sendtime,120) px1,px2 = 3 from tb
    ) t
    order by px1,px2drop table tb/*
    id           title                sendTime                             
    ------------ -------------------- ------------------------------------ 
                                      2010-08月新闻
                                      6日新闻
    1            wwwww                2010-08-06
                                      2010-08月新闻
                                      7日新闻
    2            sfddd                2010-08-07
                                      2010-09月新闻
                                      1日新闻
    3            ffff                 2010-09-01
                                      2010-09月新闻
                                      2010-09月新闻
                                      2日新闻
                                      2日新闻
    4            rrrr                 2010-09-02
    5            ffff                 2010-09-02
                                      2010-09月新闻
                                      3日新闻
    6            kkkk                 2010-09-03
                                      2010-10月新闻
                                      1日新闻
    7            jjjj                 2010-10-01(所影响的行数为 21 行)
    */
      

  7.   

    多谢大家,多谢(爱新觉罗.毓华) 我根据你的那个语句这样查询的select id ,cmsTitle ,sendTime from
    (
    select null id , null cmsTitle , convert(varchar(7),sendTime,120) + '月新闻' sendTime , convert(varchar(4),sendtime,120) px from [iyu_Notice]where IsHls = 1 and year(sendTime) = '2010'
    union all
    select null id , null cmsTitle , datename(day,sendTime) + '日新闻' sendTime,convert(varchar(7),sendtime,120) px from [iyu_Notice]where IsHls = 1 and year(sendTime) = '2010'
    union all
    select id , cmsTitle , convert(varchar(10),sendtime,120) sendtime , convert(varchar(4),sendtime,120) px from [iyu_Notice]where IsHls = 1 and year(sendTime) = '2010'
    ) t
    order by px结果显示如下:NULL NULL 2010-07月新闻
    NULL NULL 2010-07月新闻
    NULL NULL 2010-07月新闻
    NULL NULL 2010-07月新闻
    NULL NULL 2010-07月新闻
    NULL NULL 2010-07月新闻
    NULL NULL 2010-01月新闻
    NULL NULL 2010-01月新闻
    NULL NULL 2010-01月新闻
    NULL NULL 2010-01月新闻
    NULL NULL 2010-01月新闻
    NULL NULL 2010-01月新闻
    NULL NULL 2010-07月新闻
    NULL NULL 2010-07月新闻
    4 永定河绿色生态发展带建设仪式启动 2010-07-28
    7 test 2010-07-27
    17 123 2010-07-30
    19 test 2010-07-28
    20 4534525 2010-07-28
    21 342422 2010-07-28
    23 市人大石景山区代表团举行会前活动 2010-01-01
    24 区长周茂非专题听取2008年财政收入 2010-01-01
    25 区领导节前查安全 2010-01-01
    26 多部门联合检查加气站 2010-01-01
    27 2009年第一天石景山节日气氛浓 2010-01-01
    28 元旦佳节石景山游乐园洋味儿活动造气氛 2010-01-01
    1456 测试绿色通道 2010-07-27
    1460 test 2010-07-28
    NULL NULL 1日新闻
    NULL NULL 1日新闻
    NULL NULL 1日新闻
    NULL NULL 1日新闻
    NULL NULL 1日新闻
    NULL NULL 1日新闻
    NULL NULL 27日新闻
    NULL NULL 28日新闻
    NULL NULL 28日新闻
    NULL NULL 27日新闻
    NULL NULL 30日新闻
    NULL NULL 28日新闻
    NULL NULL 28日新闻
    NULL NULL 28日新闻
    我怎么把结果整理成目标格式呢?
      

  8.   

    再来,上面考虑不全.
    create table tb(id int,title varchar(20),sendTime datetime)
    insert into tb values(1 ,'wwwww' ,'2010-08-06')
    insert into tb values(2 ,'sfddd' ,'2010-08-07')
    insert into tb values(3 ,'ffff'  ,'2010-09-01')
    insert into tb values(4 ,'rrrr'  ,'2010-09-02')
    insert into tb values(5 ,'ffff'  ,'2010-09-02')
    insert into tb values(6 ,'kkkk'  ,'2010-09-03')
    insert into tb values(7 ,'jjjj'  ,'2010-10-01')
    goselect id ,title ,sendTime from
    (
    select distinct '' id , '' title , convert(varchar(7),sendTime,120) + '月新闻' sendTime , convert(varchar(7),sendtime,120) + '-01' px1,px2=1 from tb
    union all
    select distinct '' id , '' title , datename(day,sendTime) + '日新闻' sendTime,convert(varchar(10),sendtime,120) px1,px2=2 from tb
    union all
    select ltrim(id) , title , convert(varchar(10),sendtime,120) sendtime , convert(varchar(10),sendtime,120) px1,px2 = 3 from tb
    ) t
    order by px1,px2drop table tb/*
    id           title                sendTime                             
    ------------ -------------------- ------------------------------------ 
                                      2010-08月新闻
                                      6日新闻
    1            wwwww                2010-08-06
                                      7日新闻
    2            sfddd                2010-08-07
                                      2010-09月新闻
                                      1日新闻
    3            ffff                 2010-09-01
                                      2日新闻
    4            rrrr                 2010-09-02
    5            ffff                 2010-09-02
                                      3日新闻
    6            kkkk                 2010-09-03
                                      2010-10月新闻
                                      1日新闻
    7            jjjj                 2010-10-01(所影响的行数为 16 行)
    */
      

  9.   

    在sql里要这样的格式有意义吗,难道你的程序上直接就能把这个格式显示出来?
      

  10.   

    2010-01月新闻
    1日新闻
    23 市人大石景山区代表团举行会前活动 2010-01-01
    24 区长周茂非专题听取2008年财政收入 2010-01-01
    25 区领导节前查安全 2010-01-01
    26 多部门联合检查加气站 2010-01-01
    27 2009年第一天石景山节日气氛浓 2010-01-01
    28 元旦佳节石景山游乐园洋味儿活动造气氛 2010-01-01
    2010-07月新闻
    27日新闻
    7 test 2010-07-27
    1456 测试绿色通道 2010-07-27
    28日新闻
    1460 test 2010-07-28
    19 test 2010-07-28
    20 4534525 2010-07-28
    21 342422 2010-07-28
    4 永定河绿色生态发展带建设仪式启动 2010-07-28
    30日新闻
    17 123 2010-07-30