使用条件查询键值对 table

querying a key - value pair table with conditionals

我被这个难住了,希望得到任何帮助。

这是table的结构:

+----+-------------+-------------------+--+
| id |    name     |       value       |  |
+----+-------------+-------------------+--+
|  3 | email       | dsflkej@gmail.com |  |
|  3 | device_type | iOS               |  |
|  3 | text        | purchase          |  |
|  4 | email       | ueif@gmail.com    |  |
|  4 | device_type | iOS               |  |
|  5 | email       | tckjef@gmail.com  |  |
|  5 | device_type | Android           |  |
|  5 | text        | where can i pur   |  |
+----+-------------+-------------------+--+

我想要这样的结果:

╔════╦══════╦═════════════════╦══╦══╗
║ id ║ name ║      value      ║  ║  ║
╠════╬══════╬═════════════════╬══╬══╣
║  3 ║ text ║ purchase        ║  ║  ║
║  4 ║ text ║ Null            ║  ║  ║
║  5 ║ text ║ where can i pur ║  ║  ║
╚════╩══════╩═════════════════╩══╩══╝

由于 k/v 对中的数据结构,我无法找到正确的逻辑来获取唯一 ID,然后是名称 = 文本的行以及文本的值。当 id 没有出现名为 text 的行时,我想仍然保留该 id 但表明它不存在。

一个选项是这样的。我从 WITH 子句中的 table 获取不同的 ID 值,然后返回 table 以获取数据。

WITH distinct_ids 
  AS( SELECT DISTINCT id
        FROM your_table )
SELECT ids.id,
       nvl(tbl.name,'text'),
       tbl.value
  FROM distinct_ids ids
       LEFT OUTER JOIN your_table tbl
         ON(     ids.id = tbl.id
             AND tbl.name = 'text' )

SQL Fiddle

select id, 'text', min(decode(name, 'text',value)) 
  from table1 
group by id
order by 1 

Results:

| ID | 'TEXT' | MIN(DECODE(NAME,'TEXT',VALUE)) |
|----|--------|--------------------------------|
|  3 |   text |                       purchase |
|  4 |   text |                         (null) |
|  5 |   text |                where can i pur |