1

我是 sql server 2012 的新手,想了解如何使用 t-sql 创建以下输出。有很多俱乐部号码,所以必须有一个循环或光标。请帮忙!!

club_number  name                                             number
---------- -------------------------------------------------- -----------
355292     NULL                                               NULL
NULL       Giviton Mbunge                                     355308
NULL       Etero Aaron                                        355317
NULL       Evason Banda                                       355326
NULL       Kachibobo Batoni                                   355335
NULL       Kashamba Nkhani                                    355344
355353     NULL                                               NULL
NULL       Daniel Banda                                       355362
NULL       James Aaron                                        355371
NULL       Amson Kamanga                                      355380
NULL       Gostino George                                     355399
355405     NULL                                               NULL
NULL       Yohane Zimba                                       355414
NULL       Haward M.Chilembwe                                 355423
NULL       Zikiele Blangete                                   355432
355441     NULL                                               NULL

结果:我想看到上面的表如下,哪个查询可以做到?请帮忙

club_number  name                                             number
---------- -------------------------------------------------- -----------
355292       NULL                                               NULL
355292       Giviton Mbunge                                     355308
355292       Etero Aaron                                        355317
355292       Evason Banda                                       355326
355292       Kachibobo Batoni                                   355335
355292       Kashamba Nkhani                                    355344
355353       NULL                                               NULL
355353       Daniel Banda                                       355362
355353       James Aaron                                        355371
355353       Amson Kamanga                                      355380
355353       Gostino George                                     355399
355405       NULL                                               NULL
355405       Yohane Zimba                                       355414
355405       Haward M.Chilembwe                                 355423
355405       Zikiele Blangete                                   355432
355441       NULL                                               NULL
4

1 回答 1

5
SELECT club_number = MAX(club_number) OVER 
    (
      ORDER BY COALESCE(club_number, number)
      ROWS UNBOUNDED PRECEDING
    ), 
  name, number
FROM dbo.your_table
ORDER BY club_number;
于 2013-03-08T15:37:49.973 回答