如何从同一张表查询不同的行

时间:2019-03-28 17:10:06

标签: sql oracle oracle11g

RowNum  ,Source_element,    source_value

1   ,    header1      ,      mail
1   ,    header2     ,       location
1   ,    header3     ,       address

2   ,    header1       ,     mail
2   ,    header2    ,        location
2   ,    header3    ,        US

3   ,    header1       ,     Email
3   ,    header2       ,     location
3   ,    header3      ,      Canada

上面是我的桌子。 目前,例如,我为每个Rownum使用3条记录,但是它可以是基于我们获取的源文件的任何记录

我需要在其中写一个查询

  

如果对于任何给定的相同rownum,header1 = mail和header3 = US,则提取该rownum 2

  

如果header1 =电子邮件,header2 =位置,header3 =加拿大,则提取第3行

任何线索都将对我有帮助。.我对如何进行工作一无所知。

3 个答案:

答案 0 :(得分:0)

我不清楚您真正想要什么,但是起点是使用条件聚合。即CASE表达式和MIN()MAX()SUM()聚合函数。

例如...

WITH
  pivoted AS
(
  SELECT
    rownum,
    MAX(CASE WHEN source_element = 'header1' THEN source_value END) AS header1,
    MAX(CASE WHEN source_element = 'header2' THEN source_value END) AS header2,
    MAX(CASE WHEN source_element = 'header2' THEN source_value END) AS header3
  FROM
    yourTable
  GROUP BY
    rownum
)
SELECT
  <something>
FROM
  pivoted
WHERE
  (header1='mail' AND header3='US')
  OR
  (header1='Email' AND header2='location' AND header3='Canada')

希望这会帮助您入门。

答案 1 :(得分:0)

不优雅,但是:

    CREATE TABLE Demo
    (
     Row_Num        VARCHAR2(0016)
    ,Source_Element VARCHAR2(0016)
    ,Source_Value   VARCHAR2(0016)
    );

INSERT INTO Demo (Row_Num,Source_Element,Source_Value)
SELECT * FROM
(
      SELECT '1' AS Row_Num, 'header1' AS Source_Element, 'mail' AS Source_Value FROM dual
UNION SELECT '1' AS Row_Num, 'header2' AS Source_Element, 'location' AS Source_Value FROM dual
UNION SELECT '1' AS Row_Num, 'header3' AS Source_Element, 'address' AS Source_Value FROM dual
--
UNION SELECT '2' AS Row_Num, 'header1' AS Source_Element, 'mail' AS Source_Value FROM dual
UNION SELECT '2' AS Row_Num, 'header2' AS Source_Element, 'location' AS Source_Value FROM dual
UNION SELECT '2' AS Row_Num, 'header3' AS Source_Element, 'US' AS Source_Value FROM dual
--
UNION SELECT '3' AS Row_Num, 'header1' AS Source_Element, 'Email' AS Source_Value FROM dual
UNION SELECT '3' AS Row_Num, 'header2' AS Source_Element, 'location' AS Source_Value FROM dual
UNION SELECT '3' AS Row_Num, 'header3' AS Source_Element, 'Canada' AS Source_Value FROM dual
);

SELECT row_num
FROM
(
SELECT
     h1.row_num
    ,h1.source_value        AS Header_1
    ,h2.source_value        AS Header_2
    ,h3.source_value        AS Header_3
FROM demo h1
    --
    JOIN demo h2
        ON h2.row_num = h1.row_num
    --
    JOIN demo h3
        ON h3.row_num = h2.row_num
WHERE (1 = 1)
  AND h1.source_element = 'header1'
  AND h2.source_element = 'header2'
  AND h3.source_element = 'header3'
)
WHERE (1 = 1)
  AND header_1 = 'mail'
  AND header_3 = 'US'
;

答案 2 :(得分:0)

这听起来像是您想使用PIVOT的情况。类似于以下内容:

SELECT "RowNum" FROM
(
  SELECT "RowNum", source_element, source_value
  FROM testtable
)
PIVOT
(
  max(source_value)
  FOR source_element IN ('header1','header2','header3')
)
WHERE
 ("'header1'"='mail' and "'header3'"='US')
 OR
 ("'header1'"='Email' and "'header2'"='location' and "'header3'"='Canada')
;

以下是it working with your sample data.

的示例

基本思想是PIVOT将您的header1 / header2 / header3行转换为列,然后您可以将条件应用于单个行。