t表有3个字段,a,b,c表中的数据是
2,1,2
3,1,2
3,2,3
4,2,1想得到的是
2,1,1
2,1,2
3,1,1
3,1,2
3,2,3
3,2,4
3,2,5
4,2,1说明一下,
从原始数据
2,1,2
希望得出
2,1,1
2,1,2这样2条从原始数据
3,1,2
3,2,3
希望得出
3,1,1
3,1,2
3,2,3
3,2,4
3,2,5
这样5条从原始数据
4,2,1
希望得出
4,2,2(这个2,是原始数据2×1的结果)
这样一条有难度啊,,,,,,无弄不明白啊···求助!

解决方案 »

  1.   


    create table t(a int,b int,c int)
    insert into t
    select 2,1,2 union all
    select 3,1,2 union all
    select 3,2,3 union all
    select 4,2,1
    goselect a.a,a.b,b.number
    from t a,master..spt_values b
    where b.[type] = 'p' and b.number between 1 and a.cdrop table t/************a           b           number
    ----------- ----------- -----------
    2           1           1
    2           1           2
    3           1           1
    3           1           2
    3           2           1
    3           2           2
    3           2           3
    4           2           1(8 行受影响)
      

  2.   

    --CTE实现DECLARE @T TABLE (
    a INT
    ,b INT
    ,c INT
    )
    INSERT @T SELECT
    2,1,2
    UNION ALL SELECT
    3,1,2
    UNION ALL SELECT
    3,2,3
    UNION ALL SELECT
    4,2,1;WITH CTE AS (
    SELECT * FROM @T
    UNION ALL
    SELECT A,B,C=C-1 FROM CTE
    WHERE C>1

    SELECT * FROM CTE
    ORDER BY A,B,C--结果
    a b c
    2 1 1
    2 1 2
    3 1 1
    3 1 2
    3 2 1
    3 2 2
    3 2 3
    4 2 1
      

  3.   


    create table t(a int,b int,c int)
    insert into t
    select 2,1,2 union all
    select 3,1,2 union all
    select 3,2,3 union all
    select 4,2,1
    go;with AcHerat as
    (
    select a.a,a.b,1 as c,row_number() over (order by getdate()) rid
    from t a,master..spt_values b
    where b.[type] = 'p' and b.number between 1 and a.c
    )select a,b,(select sum(c) from AcHerat where rid <= t.rid and a = t.a) c
    from AcHerat tdrop table t/***************a           b           c
    ----------- ----------- -----------
    2           1           1
    2           1           2
    3           1           1
    3           1           2
    3           2           3
    3           2           4
    3           2           5
    4           2           1(8 行受影响)
      

  4.   

    --CTE实现DECLARE @T TABLE (
    a INT
    ,b INT
    ,c INT
    )
    INSERT @T SELECT
    2,1,2
    UNION ALL SELECT
    3,1,2
    UNION ALL SELECT
    3,2,3
    UNION ALL SELECT
    4,2,1;WITH CTE1 AS (
    SELECT A,B,C= (SELECT SUM(C) FROM @T WHERE A=A.A AND B<=A.B) FROM @T A
    ),
    CTE AS (
    SELECT * FROM CTE1
    UNION ALL
    SELECT A,B,C=A.C-1 FROM CTE A
    WHERE NOT EXISTS (
      SELECT 1 FROM CTE1 WHERE A= A.A AND C=A.C -1
      )
      AND A.C >1

    SELECT * FROM CTE
    ORDER BY A,B,C--结果
    A B C
    2 1 1
    2 1 2
    3 1 1
    3 1 2
    3 2 3
    3 2 4
    3 2 5
    4 2 1
      

  5.   

    忘了说了,是SQL 2000不支持with关键字,难度又加大了···
      

  6.   


    create table t(a int,b int,c int)
    insert into t
    select 2,1,2 union all
    select 3,1,2 union all
    select 3,2,3 union all
    select 4,2,1
    goselect a.a,a.b,c,1 as d,identity(int,1,1) rid,
    (select count(*) from t where a = a.a) num
    into #tb
    from t a,master..spt_values b
    where b.[type] = 'p' and b.number between 1 and a.cselect a,b,
    (case when c > 1 then (select sum(d) from #tb where rid <= t.rid and a = t.a)
    else c*b end)c
    from #tb tdrop table t,#tb/**************a           b           c
    ----------- ----------- -----------
    2           1           1
    2           1           2
    3           1           1
    3           1           2
    3           2           3
    3           2           4
    3           2           5
    4           2           2(8 行受影响)
      

  7.   

    忘了说了,是SQL 2000不支持with关键字,难度又加大了···最后一条,就是2×1的那条,不实现就不实现吧,,,,这个最后拿代码里单独处理好了,现在的问题是数据库是2000,不支持with