Oracle 11.2将记录拆分为多个记录

时间:2018-07-11 10:10:35

标签: database oracle

原始表:

N tag1 tag2 tag3 tag4 tag5 tag6 countBlankRU countBlankEN countLetterRU countLetterEN countOrderRU countOrderEn
1  y    y    y    n    n    n    50             100            10            null           null        null
2  n    y    n    y    n    n    null            50           null            50            null        null

我想分割为:

记录1

N countBlankRU
1  50

记录2

N countBlankEN
1  100

记录3

N countLetterRU
1  10

记录4

N countBlankEN
2  50

记录5

N countLetterEN
2  50

2 个答案:

答案 0 :(得分:5)

您要寻找的基本结构是unpivot

<html>
<head>
    <title>Pagination</title>
</head>
<body>
<?php 

    $conn = mysqli_connect('localhost','root','','northwind');
    $data_per_page = 10;
    $select = "SELECT * FROM `customers`";  
    $select_run = mysqli_query($conn, $select);
    $records = mysqli_num_rows($select_run);
    // while ($result = mysqli_fetch_array($select_run)) {      
    //     echo $result['CompanyName'] . '<br>';        
    // }
    // $records;
    echo "<br>";
    $no_of_page = ceil($records / $data_per_page);
    if(!isset($_GET['page'])){
        $page = 1;
    }else{
        $page = $_GET['page'];
    }
    $page_limit_data = ($page - 1) * 10;
    $select = "SELECT * FROM customers LIMIT " . $page_limit_data . ',' . $data_per_page ;
    $select_run = mysqli_query($conn, $select);
    while ($row_select = mysqli_fetch_array($select_run)){
        echo $row_select['CompanyName'] . '<br>' ;
    }
    for($page=1; $page<= $no_of_page; $page++){ 
        echo "<a href='pagination.php?page=$page'> $page" . ', ';   
    }
?>
<br>
<h1> Testing Limit Functions Here  </h1>
<?php 
$limit = "SELECT CompanyName From customers LIMIT 10 OFFSET 5";
$limit_run = mysqli_query($conn , $limit);
while($limit_result = mysqli_fetch_array($limit_run)){
    echo $limit_result['CompanyName'] . '<br>';
}
?>
</body>
</html>

您可以使用任何您喜欢的文本代替上面示例中的标签号,并且您似乎想要多列,您也可以这样做:

select n, tag
from your_table
unpivot(flag for tag in (tag1 as 1, tag2 as 2, tag3 as 3, tag4 as 4, tag5 as 5, tag6 as 6))
where flag = 'y';

         N        TAG
---------- ----------
         1          1
         1          2
         1          3
         2          2
         2          4

或者如果文本来自源行的另一列,并且您想为每个未透视的行都包含相同的文本,则类似:

select n, fieldiftag, fieldiftag2
from your_table
unpivot(flag for (fieldiftag, fieldiftag2) in (
  tag1 as ('tag 1 text 1', 'tag 1 text 2'),
  tag2 as ('tag 2 text 1', 'tag 3 text 2'),
  tag3 as ('tag 3 text 1', 'tag 4 text 2'),
  tag4 as ('tag 4 text 1', 'tag 5 text 2'),
  tag5 as ('tag 5 text 1', 'tag 6 text 2'),
  tag6 as ('tag 6 text 1', 'tag 7 text 2')
))
where flag = 'y';

         N FIELDIFTAG   FIELDIFTAG2 
---------- ------------ ------------
         1 tag 1 text 1 tag 1 text 2
         1 tag 2 text 1 tag 3 text 2
         1 tag 3 text 1 tag 4 text 2
         2 tag 2 text 1 tag 3 text 2
         2 tag 4 text 1 tag 5 text 2

或者为每个标记匹配从源表的不同列中选取文本值:

with your_table (N, tag1, tag2, tag3, tag4, tag5, tag6, text) as (
            select 1, 'y', 'y', 'y', 'n', 'n', 'n', 'text from row 1' from dual
  union all select 2, 'n', 'y', 'n', 'y', 'n', 'n', 'text from row 2' from dual
)
select n, text
from your_table
unpivot(flag for tag in (tag1 as 1, tag2 as 2, tag3 as 3, tag4 as 4, tag5 as 5, tag6 as 6))
where flag = 'y';

         N TEXT           
---------- ---------------
         1 text from row 1
         1 text from row 1
         1 text from row 1
         2 text from row 2
         2 text from row 2

您可以将每个标签匹配项扩展为多个文本列:

with your_table (N, tag1, tag2, tag3, tag4, tag5, tag6,
  fieldiftag1_1, fieldiftag2_1, fieldiftag3_1, fieldiftag4_1, fieldiftag5_1, fieldiftag6_1)
as (
            select 1, 'y', 'y', 'y', 'n', 'n', 'n',
              'text1_1', 'text2_1', 'text3_1', null, null, null
            from dual
  union all select 2, 'n', 'y', 'n', 'y', 'n', 'n',
              null, 'text2_1', null, 'text4_1', null, null
            from dual
)
select n, text
from your_table
unpivot((flag, text) for tag in (
  (tag1, fieldiftag1_1),
  (tag2, fieldiftag2_1),
  (tag3, fieldiftag3_1),
  (tag4, fieldiftag4_1),
  (tag5, fieldiftag5_1),
  (tag5, fieldiftag6_1)))
where flag = 'y';

         N TEXT   
---------- -------
         1 text1_1
         1 text2_1
         1 text3_1
         2 text2_1
         2 text4_1

答案 1 :(得分:3)

您可以使用UNPIVOT并在值为y时进行过滤以获取记录。除此之外,还不清楚您的text值来自何处或额外的fieldIFtagN_M列的用途是什么。

SQL Fiddle

Oracle 11g R2架构设置

CREATE TABLE table_name ( N, tag1, tag2, tag3, tag4, tag5, tag6 ) AS
SELECT 1, 'y', 'y', 'y', 'n', 'n', 'n' FROM DUAL UNION ALL
SELECT 2, 'n', 'y', 'n', 'y', 'n', 'n' FROM DUAL;

查询1

SELECT N, tag, 'text'
FROM   table_name
UNPIVOT ( value FOR tag IN ( tag1, tag2, tag3, tag4, tag5, tag6 ) )
WHERE  value = 'y'

Results

| N |  TAG | 'TEXT' |
|---|------|--------|
| 1 | TAG1 |   text |
| 1 | TAG2 |   text |
| 1 | TAG3 |   text |
| 2 | TAG2 |   text |
| 2 | TAG4 |   text |

更新,包括一些其他字段:

SQL Fiddle

Oracle 11g R2架构设置

CREATE TABLE table_name (
  N, tag1, tag2, tag3, tag4, tag5, tag6,
  fieldIF1, fieldIF2, fieldIF3, fieldIF4, fieldIF5, fieldIF6
) AS
SELECT 1, 'y', 'y', 'y', 'n', 'n', 'n', '1,1', '1,2', '1,3', '1,4', '1,5', '1,6' FROM DUAL UNION ALL
SELECT 2, 'n', 'y', 'n', 'y', 'n', 'n', '2,1', '2,2', '2,3', '2,4', '2,5', '2,6' FROM DUAL;

查询1

SELECT N,
       tag,
       CASE tag
       WHEN 'TAG1' THEN fieldIF1
       WHEN 'TAG2' THEN fieldIF2
       WHEN 'TAG3' THEN fieldIF3
       WHEN 'TAG4' THEN fieldIF4
       WHEN 'TAG5' THEN fieldIF5
       WHEN 'TAG6' THEN fieldIF6
       END AS fieldIF
FROM   table_name
UNPIVOT (
  value FOR tag IN ( tag1, tag2, tag3, tag4, tag5, tag6 )
)
WHERE  value = 'y'

Results

| N |  TAG | FIELDIF |
|---|------|---------|
| 1 | TAG1 |     1,1 |
| 1 | TAG2 |     1,2 |
| 1 | TAG3 |     1,3 |
| 2 | TAG2 |     2,2 |
| 2 | TAG4 |     2,4 |

更新2 -使用更新后的示例数据:

SQL Fiddle

Oracle 11g R2架构设置

CREATE TABLE table_name (
  N,
  tag1,          tag2,
  tag3,          tag4,
  tag5,          tag6,
  countBlankRU,  countBlankEN,
  countLetterRU, countLetterEN,
  countOrderRU,  countOrderEn
) AS
SELECT 1, 'y', 'y', 'y', 'n', 'n', 'n',   50,  100,   10, null, CAST(null AS NUMBER(3,0)), CAST(null AS NUMBER(3,0)) FROM DUAL UNION ALL
SELECT 2, 'n', 'y', 'n', 'y', 'n', 'n', null,   50, null,   50, null, null FROM DUAL;

查询1

SELECT N,
       tag,
       fieldIF
FROM   table_name
UNPIVOT (
  (value,fieldIf) FOR tag IN (
    (tag1, countBlankRU)  AS 'CountBlankRU',
    (tag2, countBlankEN)  AS 'CountBlankEN',
    (tag3, countLetterRU) AS 'CountLetterRU',
    (tag4, countLetterEN) AS 'CountLetterEN',
    (tag5, countOrderRU)  AS 'CountOrderRU',
    (tag6, countOrderEN)  AS 'CountOrderEN'
  )
)
WHERE  value = 'y'

Results

| N |           TAG | FIELDIF |
|---|---------------|---------|
| 1 |  CountBlankRU |      50 |
| 1 |  CountBlankEN |     100 |
| 1 | CountLetterRU |      10 |
| 2 |  CountBlankEN |      50 |
| 2 | CountLetterEN |      50 |
相关问题