如何在SQL中合并多个视图,每个记录一个观察?

时间:2014-06-23 18:40:27

标签: sql-server sql-server-2012

我有三个观点需要合并,所以每个受益人ID只能有一个记录。请参阅以下视图: ![浏览] [1]

  1. 农民观点

    (Service_Farmer,Beneficiary_ID,Household_ID,名字,姓氏,全名,性别 ,状态)

  2. 主要农民观点

    (Service_Lead_Farmer,Household_ID,Beneficiary_ID,名字,姓氏,性别,状态)

  3. SILC会员查看

    (Service_SILC,Beneficiary_ID,Household_ID,名字,姓氏,性别)

  4. 如何编写SQL Server代码来合并这些视图,以便我可以判断某人是否是农民,农民或silc会员等?

1 个答案:

答案 0 :(得分:1)

假设你有另一张表Beneficiaries,或许如下:

select b.Beneficiary_ID, f.Service_Farmer, lf.Service_Lead_Farmer, sm.Service_SILC, other_columns_as_needed
from Beneficiaries b
left join Farmers f on b.Beneficiary_ID = f.BeneficiaryID
left join LeadFarmers lf on b.Beneficiary_ID = lf.Beneficiary_ID
left join SILCMembers sm on b.Beneficiary_ID = sm.Beneficiary_ID
where whatever_conditions_are_relevant