[英]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.