简体   繁体   English

从两个不同的表为视图创建一列(SQL Server)

[英]Creating a column for a View from two different tables (SQL Server)

I have three tables A, B and C in a SQL Server, B and C share the same foreign key ( AgencyID ) from A , and they also share one same column name: AgencyName 我在SQL Server中有三个表A,B和C, BC共享来自A的相同外键( AgencyID ),并且它们还共享一个相同的列名: AgencyName

Table A : AgencyID , AgencyDate , AAgencyIDAgencyDate

Table B : AgencyID , AgencyNumber , AgencyName BAgencyIDAgencyNumberAgencyName

Table C : AgencyID , AgencyInvoiceNumber , AgencyName CAgencyIDAgencyInvoiceNumberAgencyName

To help you understand the relationship, basically these 3 tables are generated with Entity Framework , where B and C each extends A . 为了帮助您理解这种关系,基本上这3个表是使用Entity Framework生成的,其中BC分别扩展了A

Now I am trying to create one View from these three tables in the following way: 现在,我尝试通过以下方式从这三个表中创建一个视图:

CREATE VIEW [rdo].[Agencies] AS
SELECT
    A_1.AgencyID, A_1.AgencyDate,
    dbo.B.AgencyNumber, dbo.B.AgencyName,
    dbo.C.AgencyInvoiceNumber, dbo.C.AgencyName
FROM dbo.A AS A_1 LEFT OUTER JOIN
        dbo.B ON A_1.AgencyID = dbo.B.AgencyID LEFT OUTER JOIN
        dbo.C ON A_1.AgencyID = dbo.C.AgencyID

Now, apparently the script errors about AgencyName that column name in a view should be unique :( How would I go about this so that both AgencyName values from B and C are merged into one view? 现在,显然有关AgencyName的脚本错误是AgencyName中的列名应该是唯一的:(我将如何处理,以便将B和C中的两个AgencyName值合并到一个视图中?

Note: I know the example isn't perfect as one could just move the column to table A, but it's just a simplified example, in reality I really cannot simply move the column :( 注意:我知道这个例子并不完美,因为可以将列移动到表A,但这只是一个简化的例子,实际上我真的不能简单地将列:(


Reason for having to have duplicated columns in B and C: 必须在B和C中复制列的原因:

Here's the relationships: 这里是关系:

            A 
           / \
          A1  A2
         /      \
        B        C

A1 and A2 extends A, B and C extends A1 and A2 respectively. A1和A2分别延伸A,B和C分别延伸A1和A2。 AgencyName is an unique attribute that only B and C has. AgencyName是只有B和C具有的唯一属性。 I know a refactoring could be done here to try to get rid of this weird hierarchy, but this legacy project has already grown too big and I cannot afford the cost atm. 我知道可以在这里进行重构以摆脱这种怪异的层次结构,但是这个旧项目已经变得太大了,我负担不起atm的费用。

Give different names to those columns in the view whose name are same in your tables like below 为视图中与表中名称相同的那些列指定不同的名称,如下所示

CREATE VIEW [rdo].[Agencies] AS
SELECT
    A_1.AgencyID, A_1.AgencyDate,
    dbo.B.AgencyNumber, dbo.B.AgencyName AS AgencyName_B,
    dbo.C.AgencyInvoiceNumber, dbo.C.AgencyName AS AgencyName_C
FROM dbo.A AS A_1 LEFT OUTER JOIN
        dbo.B ON A_1.AgencyID = dbo.B.AgencyID LEFT OUTER JOIN
        dbo.C ON A_1.AgencyID = dbo.C.AgencyID
CREATE VIEW [rdo].[Agencies] AS
SELECT
    A_1.AgencyID, A_1.AgencyDate,
    dbo.B.AgencyNumber, dbo.B.AgencyName as AgencyNameB,
    dbo.C.AgencyInvoiceNumber, dbo.C.AgencyName as AgencyNameC,
FROM dbo.A AS A_1 LEFT OUTER JOIN
        dbo.B ON A_1.AgencyID = dbo.B.AgencyID LEFT OUTER JOIN
        dbo.C ON A_1.AgencyID = dbo.C.AgencyID

give alice name to AgencyName column. alice nameAgencyName列。 In your case the table dbo.table.AgencyName will have a column called 'AgencyName' for Tables B and C . 在您的情况下,表dbo.table.AgencyName将具有表BC名为“ AgencyName”的列。 That is the problem. 那就是问题所在。 Give alicename to any one of the column or both. 将alicename赋予列中的任一者或同时赋予两者。

Try using COALESCE to pick the first non null AgencyName: 尝试使用COALESCE选择第一个非null的AgencyName:

CREATE VIEW [rdo].[Agencies] AS
SELECT
    A_1.AgencyID, A_1.AgencyDate,
    dbo.B.AgencyNumber, 
    COALESCE(dbo.B.AgencyName, dbo.C.AgencyName) As AgencyName,
    dbo.C.AgencyInvoiceNumber 
FROM dbo.A AS A_1 LEFT OUTER JOIN
        dbo.B ON A_1.AgencyID = dbo.B.AgencyID LEFT OUTER JOIN
        dbo.C ON A_1.AgencyID = dbo.C.AgencyID

You may face the issue where AgencyName is different in B & C in which case you will need a set of rules to follow. 您可能会遇到B&C中的AgencyName不同的问题,在这种情况下,您需要遵循一系列规则。 My recommendation is to only have AgencyName in one of the tables (Table A?) otherwise you face potential update discrepancies. 我的建议是仅在其中一个表(表A?)中使用AgencyName,否则您将面临潜在的更新差异。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 SQL Server 中的两个表中获取不同的行 - How to get different rows from two tables in SQL Server 如何从SQL Server中两个不同的表中获取两列的总数? - How to get the total count of two columns from two different tables in SQL Server? 如何在一个简单但完整的c#项目中从两个不同的sql server数据库连接两个表? - How to join two tables from two different sql server databases in a simple but complete c# project? 合并来自C#中不同SQL Server数据库的两个表中的数据 - Merge data from two tables from different SQL Server database in C# 从两个不同的表确定SQL Server中的top-k值 - Determine top-k values in SQL Server from two different tables 如何在两个不同的表中比较/匹配SQL Server中的值 - How to compare/match values in sql server in two different tables 从asp.net中的不同SQL表创建GridView或ListView - Creating a gridview or listview from different sql tables in asp.net MVC将来自两个不同表的数据合并到一个视图中 - MVC Combine data from two different tables into one view 来自两个不同表的相乘值之和然后显示在DataGridView列中 - Sum multiplied values from two different tables then displaying in DataGridView column 如何从两个不同的表返回列值? - How to return column values from two different tables?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM