使用备用值而不是null来连接sql视图

时间:2013-05-23 23:42:03

标签: sql-server sql-view

希望这张图表有意义......

问题是我在很多表中有很多列,如何在视图中获取所有列值,而不是每行都有ISNULL? (sql server 10.5)

 ITEM
+------+
| ID   |
|------|
| 1    |
| 2    |
| 3    |
+------+

 LANGUAGE
+-------+---------+
| ID    |  Name   |
|-------+---------|
| 1     | English |
| 2     | French  |
+-------+---------+

 Item Names
+----------+---------+------------+------------+
| ItemID   | LangId  |   Name     |   Color    |
|----------+---------+------------+------------|
| 1        |   1     |  apple     |  red       |
| 1        |   2     |  pomme     |  rouge     |
| 2        |   1     |  orange    |  orange    |
| 3        |   1     |  bannana   |  yellow    |
+----------+---------+------------+------------+

理想的观点

+----------+---------+------------+------------+
| ItemID   | LangId  |   Name     |   Color    |
|----------+---------+------------+------------|
| 1        |   1     |  apple     |  red       |
| 1        |   2     |  pomme     |  rouge     |
| 2        |   1     |  orange    |  orange    |
| 2        |   2     |  orange    |  orange    | <--- added automatically
| 3        |   1     |  bannana   |  yellow    |
| 3        |   2     |  bannana   |  yellow    | <--- added automatically
+----------+---------+------------+------------+

因为我正在尝试创建视图,因此存在某些限制:

视图中要修改的列必须直接引用表列中的基础数据。无法以任何其他方式派生列,例如通过以下方式:

  • 聚合函数:AVG,COUNT,SUM,MIN,MAX,GROUPING,STDEV,STDEVP,VAR和VARP。
  • 计算。无法从使用其他列的表达式计算列。通过使用集合运算符UNION,UNION ALL,CROSSJOIN,EXCEPT和INTERSECT形成的列相当于计算并且也不可更新。
然而,我创建了多个视图,这就是我之前已经解决了一些这些限制的问题。我们可以假设我已经有了这张表

中间观点:

+----------+---------+------------+------------+
| ItemID   | LangId  |   Name     |   Color    |
|----------+---------+------------+------------|
| 1        |   1     |  apple     |  red       |
| 1        |   2     |  pomme     |  rouge     |
| 2        |   1     |  orange    |  orange    |
| 3        |   1     |  bannana   |  yellow    |
+----------+---------+------------+------------+

以及:

+----------+---------+------------+------------+
| ItemID   | LangId  |   Name     |   Color    |
|----------+---------+------------+------------|
| 1        |   1     |  apple     |  red       |
| 1        |   2     |  pomme     |  rouge     |
| 2        |   1     |  orange    |  orange    |
| 2        |   2     |  -         |  -         |
| 3        |   1     |  bannana   |  yellow    |
| 3        |   2     |  -         |  -         |
+----------+---------+------------+------------+

这些是一些观点:

view1 - 所有组合

enter image description here

view2 - 与语言的所有组合 enter image description here

相应的SQL:

SELECT     dbo.view1.ItemID, dbo.view1.LanguageID, dbo.ItemLanguages.Name, dbo.ItemLanguages.Color
FROM         dbo.ItemLanguages RIGHT OUTER JOIN
                      dbo.view1 ON dbo.ItemLanguages.LanguageID = dbo.view1.LanguageID AND dbo.ItemLanguages.ItemID = dbo.view1.ItemID

观点结果2 enter image description here

这是包含视图和表格的测试数据库:http://pastebin.com/4BpBSmHY

1 个答案:

答案 0 :(得分:0)

我能够做到的一种方法是使用ISNULL

SELECT     dbo.view1.ItemID,
    dbo.view1.LanguageID,
    ISNULL(dbo.ItemLanguages.Name,
      (SELECT     TOP (1) Name
        FROM          dbo.ItemLanguages AS x
        WHERE      (ItemID = dbo.view1.ItemID))) AS Name,
    ISNULL(dbo.ItemLanguages.Color,
      (SELECT     TOP (1) Color
        FROM          dbo.ItemLanguages AS x
        WHERE      (ItemID = dbo.view1.ItemID))) AS Color,
    CASE  
        WHEN  dbo.ItemLanguages.ItemID is NULL THEN 1
        ELSE 0
    END as valid

FROM         dbo.ItemLanguages RIGHT OUTER JOIN
                 dbo.view1 ON dbo.ItemLanguages.ItemID = dbo.view1.ItemID
                          AND dbo.ItemLanguages.LanguageID = dbo.view1.LanguageID

我不喜欢这种方法的原因是我在更多专栏中这样做。我认为做多个SELECTS会大大减慢结果。

我虽然能够检查该行是否与CASE
WHEN dbo.ItemLanguages.ItemID is NULL THEN 1 ELSE 0 END as valid
一样存在 然后调用select一次并填充所有列

..在视图中