1

このサンプル データセットの使用:

CREATE TABLE test. test2 (id VARCHAR(7), AA INT, BBB INT, CCC VARCHAR (12));
INSERT INTO test.test2 (id, AA, BBB,CCC) VALUES ( 'A123', 45, 123, '2011-03' );
INSERT INTO test.test2 (id, AA, BBB,CCC) VALUES ( 'A120', 52, 120, '2011-03' );
INSERT INTO test.test2 (id, AA, BBB,CCC) VALUES ( 'A133', 63, 133, '2011-03' );
INSERT INTO test.test2 (id, AA, BBB,CCC) VALUES ( 'D123', 34, 123, '2011-04' );
INSERT INTO test.test2 (id, AA, BBB,CCC) VALUES ( 'D120' ,32, 120, '2011-04' );
INSERT INTO test.test2 (id, AA, BBB,CCC) VALUES ( 'D140', 12, 140, '2011-04' ); 

3列のテーブルを探しています。

元の列からの Desc としての「Id」の順序。

CCC が同じである列 A の前または次の行 ID としての列 B "Id2"。

Id,  Id2  CCC
A120 A123 '2011-03'
A123 A133 '2011-03'
A133      '2011-03'
D120 D123 '2011-04'
D123 D140 '2011-04'
D140      '2011-04'

また

  Id,  Id2  CCC
A120      '2011-03'
A123 A120 '2011-03'
A133 A123 '2011-03'
D120      '2011-04'
D123 D120 '2011-04'
D140 D123 '2011-04'

また

   Id,  Id2  CCC
   A123 A120 '2011-03'
   A133 A123 '2011-03'
   D123 D120 '2011-04'
   D140 D123 '2011-04'

結合されたテーブルにオートクリメント列を追加してから、1、2、3 行を上下に使用できますか? id2 は、この autocrement 行に基づいていますか?

4

3 に答える 3

3
SELECT test.id, child.id, test.CCC
FROM test
LEFT JOIN test AS child ON ((test.CCC = child.CCC) and (test.id < child.id))

gets close:

+------+------+---------+
| id   | id   | CCC     |
+------+------+---------+
| A120 | A123 | 2011-03 |
| A120 | A133 | 2011-03 |
| A123 | A133 | 2011-03 |
| A133 | NULL | 2011-03 |
| D120 | D123 | 2011-04 |
| D120 | D140 | 2011-04 |
| D123 | D140 | 2011-04 |
| D140 | NULL | 2011-04 |
+------+------+---------+
于 2011-04-21T22:25:44.187 に答える
1

マークのベースで動作します:

SELECT test.id, child.id, test.CCC
FROM test
  LEFT JOIN test AS child
    ON (test.CCC = child.CCC)
      AND (test.id < child.id)
WHERE NOT EXISTS
  ( SELECT 1
    FROM test AS middle
    WHERE (test.CCC = middle.CCC)
      AND (test.id < middle.id)
      AND (middle.id < child.id)
  )
  OR child.id IS NULL
ORDER BY test.id

これは、より複雑なクエリで役立つ場合があります。

CREATE VIEW testWithRowId AS
  ( SELECT test.id
         , COUNT(test.id) AS rownum
         , test.CCC
    FROM test
      JOIN test AS child
        ON (test.CCC = child.CCC)
          AND (test.id >= child.id)
    GROUP BY test.CCC
           , test.id 
  )

そしてそれを使用します:

SELECT t1.id
     , t2.id AS idShifted
     , t1.CCC
FROM testWithRowId t1
  LEFT JOIN testWithRowId t2
    ON (t2.CCC = t1.CCC) 
      AND (t2.rownum = t1.rownum + 1)   ---- replace this 1 with 2 or 3, etc
ORDER BY t1.CCC                         ---- for a shift 2 or shift 3, etc
       , t1.rownum
于 2011-04-21T22:39:46.237 に答える
1
Select T.Id, NextTest.NextId As Id2, T.CCC
From Test2 As T
    Left Join   (
                Select T1.Id, Min( T2.Id ) As NextId
                From Test2 As T1
                    Left Join Test2 As T2
                        On T2.CCC = T1.CCC
                            And T2.Id > T1.Id
                Group By T1.Id
                ) As NextTest
            On NextTest.Id = T.Id
Order By T.Id       

これにより、目的の出力の最初のセットにあるものが正確に返されます。

于 2011-04-21T22:30:53.190 に答える