获取列中值的特定标记

时间:2014-09-09 08:59:04

标签: sql oracle

我正在尝试在oracle database上构建一个SQL查询:

我得到两个查询以下结果集。

结果集1:

标记

SELECT ProjID, Tag_Code, Value_numeric, Value_String from Tags;
+-------------------------------------------------------------------------+
| ProjID             Tag_Code             Value_numeric     Value_String  |
+-------------------------------------------------------------------------+
| 1             TOT_CUST_EXPOSURE             100              null       |
| 1             EXPOSURE_DATE                 null             1.1.2014   |
| 1             DEBT                          100              null       |
| 2             TOT_CUST_EXPOSURE             232              null       |
| 2             EXPOSURE_DATE                 null             12.05.2013 |
| 2             DEBT                          2324             null       |
| 3             TOT_CUST_EXPOSURE             3452             null       |
| 3             EXPOSURE_DATE                 null             19.03.2014 |
| 3             DEBT                          2345             null       |
| 4             TOT_CUST_EXPOSURE             3245674          null       |
| 4             EXPOSURE_DATE                 null             19.03.2014 |
| 4             DEBT                          12345            null       |
+-------------------------------------------------------------------------+

结果集2:

表:项目

SELECT ProjID, CustomerName from Project;
+--------------------------------------+
| ProjID     CustomerName              |
+--------------------------------------+
| 1             a                      |
| 2             b                      |
| 3             c                      |
| 4             d                      |
+--------------------------------------+

但是,我想得到这个结果集。

+--------------------------------------------------------------------------+
| ProjID     CustomerName  TOT_CUST_EXPOSURE  EXPOSURE_DATE   DEBT         |
+--------------------------------------------------------------------------+
| 1             a              100               1.1.2014      100         |
| 2             b              232               12.05.2013    2324        |
| 3             c              3452              19.03.2014    2345        |
| 4             d              3245674           19.03.2014    12345       |
+--------------------------------------------------------------------------+

这可能在sql中吗?我该如何实现呢?我非常感谢你的回答!

1 个答案:

答案 0 :(得分:1)

基本方式,与您当前的表格(fiddle)兼容:

select
  p.ProjID ProjID,
  p.CustomerName CustomerName,
  (select Value_Numeric from tags
     where ProjID=p.ProjID
       and Tag_Code='TOT_CUST_EXPOSURE') TOT_CUST_EXPOSURE,
  (select Value_String from tags
     where ProjID=p.ProjID
       and Tag_Code='EXPOSURE_DATE') EXPOSURE_DATE,
  (select Value_Numeric from tags
     where ProjID=p.ProjID
       and Tag_Code='DEBT') DEBT
  from Project p;

另一种方式,其中Tag_Code值存储在一个Value列中。有了这个,我们可以轻松使用pivotfiddle):

select
  p.ProjID ProjID,
  p.CustomerName CustomerName,
  TOT_CUST_EXPOSURE,
  EXPOSURE_DATE,
  DEBT from
  Project p left join
  (select * from
    (select ProjID, Tag_Code, Value from tags)
    pivot 
    (max(Value) for (Tag_Code) in (
       'TOT_CUST_EXPOSURE' as TOT_CUST_EXPOSURE,
       'EXPOSURE_DATE' as EXPOSURE_DATE,
       'DEBT' as DEBT
    ))
  ) t
  on p.ProjID=t.ProjID;

如果pivot表中的记录没有null表中的相应记录,则使用上述Project方法会将Tags返回到字段。