0

1,2,3A isと B isを使用して 1 つのテーブルを作成しました3,5,5,9,10,10

たとえば、このテーブルから変更したい:

NP  A  B C
-- -- -- -----------------------
 1  1  3 why not
 1  1  5 weigh
 1  1  5 hello
 1  1  9 no way
 1  1 10 now or never
 1  1 10 float the boat, captain
 1  1 12 no nelp
 2  2  4 why
 2  2  6 way too much
 2  2 11 help
 3  3  1 not now
 3  3  2 not
 3  3  7 milky way
 3  3  7 this is stupid
 3  3  8 one way

このようになるには:

 A   B  C
--  --  -----------------------
 1   3  why not
     5  weigh
     5  hello
     9  no way
    10  now or never
    10  float the boat, captain
    12  no nelp

 2   4  why
     6  way too much
    11  help

 3   1  not now
     2  not
     7  milky way
     7  this is stupid
     8  one way

そのようにする方法は?これについて知っている人はいますか?

4

1 に答える 1

2

これを試してください:http://www.sqlfiddle.com/#!2/073ec/4

select A, B, C
from
( 
  select
     if (A = @last, null, A) as A,
     B, C, (@last := A) 
  from tbl, (select @last := null as x) as vars
) as q

出力:

|      A |  B |                       C |
-----------------------------------------
|      1 |  3 |                 why not |
| (null) |  5 |                   weigh |
| (null) |  5 |                   hello |
| (null) |  9 |                  no way |
| (null) | 10 |            now or never |
| (null) | 10 | float the boat, captain |
| (null) | 12 |                 no nelp |
|      2 |  4 |                     why |
| (null) |  6 |            way too much |
| (null) | 11 |                    help |
|      3 |  1 |                 not now |
| (null) |  2 |                     not |
| (null) |  7 |               milky way |
| (null) |  7 |          this is stupid |
| (null) |  8 |                 one way |

@NaimNsco

変数を明示的に宣言する必要はありません。クエリで明示的に宣言すると、意図 (上記のコード) がより明確になることがわかります。そうは言っても、変数を宣言しないことも機能します

select A, b, c
from
( 
  select
     if (A = @last, null, A) as A,
     B, C, (@last := A) 
  from tbl
) as q

見てください、それは動作します: http://www.sqlfiddle.com/#!2/073ec/6

于 2012-06-18T03:41:08.260 に答える