简体   繁体   English

如何在SQL Server 2014中创建视图以查看缺少某些数据的交叉引用表中的数据?

[英]How do I create a view in SQL Server 2014 to see data from cross referenced tables where some data is missing?

I am trying to generate a report from a normalized database with tables a, a_xref_b, and b. 我正在尝试从具有表a,a_xref_b和b的规范化数据库生成报告。

CREATE TABLE a (a_rid INT primary key, a1 varchar(20), );

CREATE TABLE b (b_rid INT primary key, b1 varchar(20), b2 varchar(20));

CREATE TABLE a_xref_b (a_rid INT, b_rid INT,xref_type int
CONSTRAINT fk_a FOREIGN KEY (a_rid) REFERENCES a (a_rid),
CONSTRAINT fk_b FOREIGN KEY (b_rid) REFERENCES b (b_rid));

INSERT INTO a VALUES (1,'John'), (2,'Sue')
INSERT INTO b VALUES (1,'Atlanta','GA'), (2,'Macon','GA'), (3,'Opp','AL')
INSERT INTO a_xref_b VALUES (1,1,1), (1,2,2), (2,3,1)

Table A and B are cross-referenced and have the cross reference has a type, which defines what the data in B represents. 表A和B被交叉引用,并且交叉引用具有类型,该类型定义B中的数据表示什么。

Scenario - John is provided a main city and an alternate. 场景-向约翰提供了主要城市和替代城市。 Sue only provided a main city. 苏只提供了一个主要城市。

I am trying to write a view to load page that has the name, main city, and alternate. 我正在尝试编写一个视图以加载具有名称,主要城市和替代名称的页面。

Name   b_rid_t1  City_t1   St_1  b_rid_2   City_t2  St_t2
John   1         Atlanta   GA    2         Macon    GA
Sue    3         Opp       AL    NULL      NULL     NULL

I tried this 我试过了

select
   a.a1,
   b_1.b_rid,
   b_1.b1,
   b_1.b2,
   b_2.b_rid,
   b_2.b1,
   b_2.b2      
from
   a     
left join
   a_xref_b as xf1 
      on a.a_rid = xf1.a_rid      
inner join
   b as b_1 
      on xf1.b_rid = b_1.b_rid 
      and xf1.xref_type = 1     
left join
   a_xref_b xf2 
      on a.a_rid = xf2.a_rid      
inner join
   b as b_2 
      on xf2.b_rid = b_2.b_rid 
      and xf2.xref_type = 2

However it drops Sue's record. 但是,它降低了Sue的记录。 If I change it to left joins from the xref to b, then I get duplicate records. 如果我将其从外部参照更改为左联接,则将获得重复的记录。

The real world scenario is a contact table cross referenced with an address table and the xref table has the type field. 实际情况是联系表与地址表交叉引用,并且外部参照表具有类型字段。 We're trying to build a view to load the page. 我们正在尝试构建一个视图来加载页面。

Try this, you should add xref_type condition with a_xref_b table join. 尝试此操作,您应该使用a_xref_b表联接添加xref_type条件。

select
   a.a1,
   b_1.b_rid,
   b_1.b1,
   b_1.b2,
   b_2.b_rid,
   b_2.b1,
   b_2.b2      
from
   a     
left join a_xref_b as xf1 on a.a_rid = xf1.a_rid      
   and xf1.xref_type = 1     
left join b as b_1 on xf1.b_rid = b_1.b_rid       
left join a_xref_b xf2 on a.a_rid = xf2.a_rid      
   and xf2.xref_type = 2
left join b as b_2 on xf2.b_rid = b_2.b_rid 

You need to use a Pivot table or use a SQL statement that give the same kind of result. 您需要使用数据透视表或使用提供相同结果的SQL语句。 For example: 例如:

;WITH    Main
          AS ( SELECT   a1 AS Name
                       ,b1 AS City
                       ,b2 AS Code
                       ,xref_type
               FROM     a_xref_b
                        LEFT JOIN a ON a_xref_b.a_rid = a.a_rid
                        LEFT JOIN b ON a_xref_b.b_rid = b.b_rid
             )
    SELECT  Name
           ,MAX(CASE WHEN xref_type = 1 THEN City
                END) FirstCity
           ,MAX(CASE WHEN xref_type = 1 THEN Code
                END) FirstCode
           ,MAX(CASE WHEN xref_type = 2 THEN City
                END) AlternateCity
           ,MAX(CASE WHEN xref_type = 2 THEN Code
                END) AlternateCode
    FROM    Main
    GROUP BY Name

Another approach could be using Union: 另一种方法可能是使用Union:

select max(name) name, max(id1) id1, max(city1) city1, max(state1) state1, max(id2) id2, max(city2) city2, max(state2) state2
from(
    select a.a1 as name, xf1.b_rid id1, xf1.b1 city1, xf1.b2 state1, null id2, null city2, null state2
    from a left join 
    (select a_rid, b1, b2, b.b_rid from a_xref_b inner join b on a_xref_b.b_rid = b.b_rid 
    where xref_type = 1) xf1 on a.a_rid = xf1.a_rid 
    union
    select a.a1 as name, null id1, null city1, null state1,xf1.b_rid id2, xf1.b1 city2, xf1.b2 state2
    from a left join 
    (select a_rid, b1, b2, b.b_rid from a_xref_b inner join b on a_xref_b.b_rid = b.b_rid 
    where xref_type = 2) xf1 on a.a_rid = xf1.a_rid 
)t group by name

暂无
暂无

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

相关问题 如何从 SQL Server 2014 数据库中的表中删除所有数据,但保留所有表? - How to delete all data from tables in a SQL Server 2014 database, but keep all the tables? 创建合并两个表的视图MS SQL Server 2014 - Create a view combining two tables ms sql server 2014 我在SqlDeveloper中创建了一些表,但是看不到表中存储的数据。 为什么我看不到表格中的数据? - I create some tables in SqlDeveloper but I can not see the data stored in the tables. Why I can't see the data in my tables? 如何使用SQL在两个表中选择数据,其中每个表中的字段匹配? - How do i select data across two tables using SQL where a field from each table matches? SQL 2014 如何从一天发生的事件中提取数据? - SQL 2014 How do I pull data from events that happen on a single day? 如何在SQL Server中列出丢失的数据? - How do I list missing data in SQL Server? 三个表之间的数据差异/比较-SQL Server 2014 - Data discrepancies/comparison between three tables - SQL Server 2014 SQL - JOIN 3 个表,但显示中间表中缺少数据的位置 - SQL - JOIN 3 tables but show where data is missing from the middle table 如何从未输入某些日期的数据库表中创建30天的数据数组? - How do I create an array of data for 30 days from database table where some dates are not entered? 如何从2个表中进行区分联合并将数据插入第3个表-SQL Server 2017 - How do I do distinct union from 2 tables and insert data to a 3rd table - SQL Server 2017
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM