【问题标题】:Create View based on join of column name and table value of two tables根据两个表的列名和表值的连接创建视图
【发布时间】:2019-10-27 21:57:53
【问题描述】:

我有两张桌子。

Table Ah:

Country HDI Luck SomeOtherColumn SoOn

1       1   2    x               y
2       1   2    b               c
3       2   3    v               g
4       3   4    e               y
5       2   2    b               g
6       4   1    n               k

还有第二个

Table Bu

Attribute Value Meaning
Country   1     RichAndLuckyCountry
Country   2     AnotherRichAndLuckyCountry
Country   3     AlsoQuiteRichButStillLuckyCountry
Country   4     NotSoRichAndSadAboutItCountry
Country   5     DoingWellCountry
Country   6     DontWorryBeHappyCountry
HDI       1     Very high
HDI       2     High
HDI       3     Medium
HDI       4     Low
Luck      1     Very high
Luck      2     High
Luck      3     Medium
Luck      4     Low

我需要的结果视图如下所示:

Table Result

Country Country_Dissolved                  HDI HDI_Dissolved Luck Luck_Dissolved SomeOtherColumn SoOn
1       RichAndLuckyCountry                1   Very high     2    High           x               y
2       AnotherRichAndLuckyCountry         1   Very high     2    High           b               c
3       AlsoQuiteRichButStillLuckyCountry 2   High          3    Medium         v               g
4       NotSoRichAndSadAboutItCountry      3   Medium        4    Low            e               y
5       DoingWellCountry                   2   High          2    High           b               g
6       DontWorryBeHappyCountry            4   Low           1    Very High      n               k   

我只用一列和 where 子句来完成它:

CREATE OR REPLACE VIEW Result AS
SELECT Ah.Country. Bu.Meaning as County_Dissolved
FROM Ah
INNER JOIN Bu
ON Ah.Country = Bu.Value
WHERE Bu.Attribute = 'Country'

我可能需要一些命令来遍历列名并将列名与属性列中的值连接起来,因为真实的表有更多可能的组合,所以只为每种情况制作多个 SQL 语句是没有解决方案的。

如何创建类似于上述Result 表的视图? 用表Bu中的键分解表Ah的值。

SQL Fiddle.

任何帮助将不胜感激。 提前致谢!

【问题讨论】:

  • 现在你知道为什么实体-属性-值模型如此广泛地鄙视我的数据库从业者了。不为模式建模所节省的时间被浪费在编写极其复​​杂且性能不佳的 SQL 上。
  • 您的问题是什么? PS 请在代码问题中提供minimal reproducible example--剪切&粘贴&可运行代码;具有期望和实际输出(包括逐字错误消息)的示例输入(作为初始化代码);标签和版本;明确的规范和解释。这包括您可以提供的最少代码,即您显示的代码可以通过您显示的代码扩展为不正常。 (调试基础。)对于包含 DBMS/产品和 DDL 的 SQL,其中包括约束和索引以及表格格式的初始化。

标签: sql oracle join view


【解决方案1】:

你需要AhBu中的3个left joins:

SELECT Ah.Country,
  MAX(CASE WHEN b1.Attribute = 'Country' THEN b1.Meaning END) Country_Dissolved,
  Ah.HDI,
  MAX(CASE WHEN b2.Attribute = 'HDI' THEN b2.Meaning END) HDI_Dissolved,
  Ah.Luck,
  MAX(CASE WHEN b3.Attribute = 'Luck' THEN b3.Meaning END) Luck_Dissolved,
  Ah.SomeOtherColumn, 
  Ah.SoOn
FROM Ah
LEFT JOIN Bu b1 ON b1.Value = Ah.Country AND b1.Attribute = 'Country'
LEFT JOIN Bu b2 ON b2.Value = Ah.HDI AND b2.Attribute = 'HDI'
LEFT JOIN Bu b3 ON b3.Value = Ah.Luck AND b3.Attribute = 'Luck'
GROUP BY Ah.Country, Ah.HDI, Ah.Luck, Ah.SomeOtherColumn, Ah.SoOn
ORDER BY Ah.Country

请参阅demo

【讨论】:

    【解决方案2】:

    您的查询似乎是正确的。

    在这种情况下我的建议是尝试

    CREATE OR REPLACE VIEW Result AS
    SELECT Ah.Country, Bu.Meaning as County_Dissolved
    FROM Ah
    INNER JOIN Bu
    ON Ah.Country = Bu.Value
    WHERE Bu.Attribute in ( 'Country','HDI','Luck')
    

    做一个select distinct attribute from Bu,然后像上面的查询一样复制和添加。

    【讨论】:

      猜你喜欢
      • 2021-01-14
      • 2012-09-03
      • 2018-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-20
      • 2011-12-31
      • 2019-03-12
      相关资源
      最近更新 更多