假设有表A,记录如下:
bjbh    zrs   jgrs
001     50     32
002     35     28
003     30     16
bjbh为班级编号,zrs为总人数,jgrs为及格人数。班级编号是主键不会重复。
想通过sql查询出这样的记录bjbh   lsh  sfjg
001     1     1
001     2     1
001     3     1
...
001     32    1
001     33    0
001     34    0
001     35    0
...
001     50    0
002     1     1
002     2     1
002     3     1
...
002     28    1
002     29    0
002     30    0
002     31    0
...
002     35    0

其中bjbh为班级编号,lsh为流水号,sfjg为是否及格(0/1)
跪求sql,在线等 

解决方案 »

  1.   

    select bjbh,lsh=number+1,jgrs=case when number+1<=[jgrs] then 1 else 0 end 
    from [TB],spt_values where type='p' and number<[zrs] and zrs=[zrs]
      

  2.   

    exec cs'
    $TB
    bjbh    zrs  jgrs
    001    50    32
    002    35    28
    003    30    16 
    '--> 测试数据:[TB]
    if object_id('[TB]') is not null drop table [TB]
    create table [TB]([bjbh] varchar(3),[zrs] int,[jgrs] int)
    insert [TB]
    select '001',50,32 union all
    select '002',35,28 union all
    select '003',30,16select bjbh,lsh=number+1,jgrs=case when number+1<=[jgrs] then 1 else 0 end 
    from [TB],spt_values where type='p' and number<[zrs]/*
    bjbh lsh         jgrs
    ---- ----------- -----------
    001  1           1
    001  2           1
    001  3           1
    001  4           1
    001  5           1
    001  6           1
    001  7           1
    001  8           1
    001  9           1
    001  10          1
    001  11          1
    001  12          1
    001  13          1
    001  14          1
    001  15          1
    001  16          1
    001  17          1
    001  18          1
    001  19          1
    001  20          1
    001  21          1
    001  22          1
    001  23          1
    001  24          1
    001  25          1
    001  26          1
    001  27          1
    001  28          1
    001  29          1
    001  30          1
    001  31          1
    001  32          1
    001  33          0
    001  34          0
    001  35          0
    001  36          0
    001  37          0
    001  38          0
    001  39          0
    001  40          0
    001  41          0
    001  42          0
    001  43          0
    001  44          0
    001  45          0
    001  46          0
    001  47          0
    001  48          0
    001  49          0
    001  50          0
    002  1           1
    002  2           1
    002  3           1
    002  4           1
    002  5           1
    002  6           1
    002  7           1
    002  8           1
    002  9           1
    002  10          1
    002  11          1
    002  12          1
    002  13          1
    002  14          1
    002  15          1
    002  16          1
    002  17          1
    002  18          1
    002  19          1
    002  20          1
    002  21          1
    002  22          1
    002  23          1
    002  24          1
    002  25          1
    002  26          1
    002  27          1
    002  28          1
    002  29          0
    002  30          0
    002  31          0
    002  32          0
    002  33          0
    002  34          0
    002  35          0
    003  1           1
    003  2           1
    003  3           1
    003  4           1
    003  5           1
    003  6           1
    003  7           1
    003  8           1
    003  9           1
    003  10          1
    003  11          1
    003  12          1
    003  13          1
    003  14          1
    003  15          1
    003  16          1
    003  17          0
    003  18          0
    003  19          0
    003  20          0
    003  21          0
    003  22          0
    003  23          0
    003  24          0
    003  25          0
    003  26          0
    003  27          0
    003  28          0
    003  29          0
    003  30          0(115 行受影响)*/
    drop table TB
      

  3.   


    create table A(bjbh varchar(03),zrs int,jgrs int)
    insert into A select '001',50,32
    insert into A select '002',35,28
    insert into A select '003',30,16Goselect distinct A.bjbh,B.number as lsh,
    case when A.jgrs>=B.number then 1 else 0 end as sfjg
    from A
    left join master..spt_values B
    on A.zrs>=B.number  and B.type='P' and B.number>0/*
    001 1 1
    001 2 1
    001 3 1
    001 4 1
    001 5 1
    001 6 1
    001 7 1
    001 8 1
    001 9 1
    001 10 1
    001 11 1
    001 12 1
    001 13 1
    001 14 1
    001 15 1
    001 16 1
    001 17 1
    001 18 1
    001 19 1
    001 20 1
    001 21 1
    001 22 1
    001 23 1
    001 24 1
    001 25 1
    001 26 1
    001 27 1
    001 28 1
    001 29 1
    001 30 1
    001 31 1
    001 32 1
    001 33 0
    001 34 0
    001 35 0
    001 36 0
    001 37 0
    001 38 0
    001 39 0
    001 40 0
    001 41 0
    001 42 0
    001 43 0
    001 44 0
    001 45 0
    001 46 0
    001 47 0
    001 48 0
    001 49 0
    001 50 0
    002 1 1
    002 2 1
    002 3 1
    002 4 1
    002 5 1
    002 6 1
    002 7 1
    002 8 1
    002 9 1
    002 10 1
    002 11 1
    002 12 1
    002 13 1
    002 14 1
    002 15 1
    002 16 1
    002 17 1
    002 18 1
    002 19 1
    002 20 1
    002 21 1
    002 22 1
    002 23 1
    002 24 1
    002 25 1
    002 26 1
    002 27 1
    002 28 1
    002 29 0
    002 30 0
    002 31 0
    002 32 0
    002 33 0
    002 34 0
    002 35 0
    003 1 1
    003 2 1
    003 3 1
    003 4 1
    003 5 1
    003 6 1
    003 7 1
    003 8 1
    003 9 1
    003 10 1
    003 11 1
    003 12 1
    003 13 1
    003 14 1
    003 15 1
    003 16 1
    003 17 0
    003 18 0
    003 19 0
    003 20 0
    003 21 0
    003 22 0
    003 23 0
    003 24 0
    003 25 0
    003 26 0
    003 27 0
    003 28 0
    003 29 0
    003 30 0
    */
    GO
    drop table A
      

  4.   

    if object_id('[tb]') is not null drop table [tb]
    go
    create table [tb] (bjbh nvarchar(6),zrs int,jgrs int)
    insert into [tb]
    select '001',50,32 union all
    select '002',35,28 union all
    select '003',30,16
    select bjbh,
           number+1 zrs,
           case when number+1<=[jgrs] then 1 else 0 end as sfjg
    from tb a,master..spt_values b
    where b.number<a.zrs and b.type='P'
      

  5.   

    create table tb(bjbh varchar(10),   zrs int, jgrs int)
    insert into tb values('001' ,   50  ,  32 )
    insert into tb values('002' ,   35  ,  28 )
    insert into tb values('003' ,   30  ,  16 )
    go--利用一个临时表
    SELECT TOP 8000 id = IDENTITY(int, 1, 1) INTO # FROM syscolumns a, syscolumns b select m.bjbh , n.id , case when n.id <= m.jgrs then '1' else 0 end sfjg from tb m , # n 
    where n.id <= m.zrsdrop table tb , #/*
    bjbh       id          sfjg        
    ---------- ----------- ----------- 
    001        1           1
    001        2           1
    001        3           1
    001        4           1
    001        5           1
    001        6           1
    001        7           1
    001        8           1
    001        9           1
    001        10          1
    001        11          1
    001        12          1
    001        13          1
    001        14          1
    001        15          1
    001        16          1
    001        17          1
    001        18          1
    001        19          1
    001        20          1
    001        21          1
    001        22          1
    001        23          1
    001        24          1
    001        25          1
    001        26          1
    001        27          1
    001        28          1
    001        29          1
    001        30          1
    001        31          1
    001        32          1
    001        33          0
    001        34          0
    001        35          0
    001        36          0
    001        37          0
    001        38          0
    001        39          0
    001        40          0
    001        41          0
    001        42          0
    001        43          0
    001        44          0
    001        45          0
    001        46          0
    001        47          0
    001        48          0
    001        49          0
    001        50          0
    002        1           1
    002        2           1
    002        3           1
    002        4           1
    002        5           1
    002        6           1
    002        7           1
    002        8           1
    002        9           1
    002        10          1
    002        11          1
    002        12          1
    002        13          1
    002        14          1
    002        15          1
    002        16          1
    002        17          1
    002        18          1
    002        19          1
    002        20          1
    002        21          1
    002        22          1
    002        23          1
    002        24          1
    002        25          1
    002        26          1
    002        27          1
    002        28          1
    002        29          0
    002        30          0
    002        31          0
    002        32          0
    002        33          0
    002        34          0
    002        35          0
    003        1           1
    003        2           1
    003        3           1
    003        4           1
    003        5           1
    003        6           1
    003        7           1
    003        8           1
    003        9           1
    003        10          1
    003        11          1
    003        12          1
    003        13          1
    003        14          1
    003        15          1
    003        16          1
    003        17          0
    003        18          0
    003        19          0
    003        20          0
    003        21          0
    003        22          0
    003        23          0
    003        24          0
    003        25          0
    003        26          0
    003        27          0
    003        28          0
    003        29          0
    003        30          0(所影响的行数为 115 行)*/
      

  6.   

    use tempdb
    if object_id('tb') is not null drop table tb
    go
    create table tb([bjbh] varchar(50),[lsh] INT,[sfjg] INT)
    insert into tb
    select '001',1,1 union all
    select '001',2,1 union all
    select '001',3,1 union all
    select '001',4,1 union all
    select '001',5,0 union all
    select '001',6,0 union all
    select '001',7,0 union all
    select '001',8,0 union all
    select '002',1,1 union all
    select '002',2,1 union all
    select '002',3,1 union all
    select '002',4,1 union all
    select '002',5,1 union all
    select '002',6,0 union all
    select '002',7,0 union all
    select '002',8,0 
    go
    --select * from tb
    select bjbh,count(lsh) as zrs,sum(sfjg) as jgrs
    from tb 
    group by bjbh
    /*
    bjbh           zrs         jgrs
    -------------- ----------- -----------
    001            8           4
    002            8           5
    */