试图从两个不同的数据库中填充事实表

时间:2017-03-05 07:19:56

标签: sql etl

一直试图填写FactVisits,任何帮助都将非常感激。我还必须从我能看到的内容中选择一个度量,但使用两个数据库是棘手的部分。

二:数据库的PDF链接: DWCorporateReports Database Patients Database

[code]
INSERT INTO FactVisits
(    --VisitKey int PRIMARY KEY Not Null
 DateKey 
,ClinicKey 
,PatientKey 
,DoctorKey 
,ProcedureKey 
,Charge 
)
SELECT fv.[DateKey] = dd.DateKey
  ,ClinicKey = [DWCorporateReports].[dbo].[DimClinics].[ClinicKey]
  ,PatientKey = [DWCorporateReports].[dbo].[DimPatients].[PatientKey]
  ,DoctorKey = [DWCorporateReports].[dbo].[DimDoctors].[DoctorKey]
  ,ProcedureKey = [DWCorporateReports].[dbo].[DimProcedure].[ProcedureKey]
  ,CAST(pv.Charge AS decimal(18,4)) = dip.Charge

--DimDates to     FactVisits.
FROM [DWCorporateReports].[dbo].[DimDates] AS dd
INNER JOIN [DWCorporateReports].[dbo].[FactVisits] AS fv 

 ON fv.DateKey = dd.DateKey

 --DimClinics to FactVisits
 INNER JOIN [DWCorporateReports].[dbo].[DimClinics] AS dc 
 ON fv.ClinicKey = dc.ClinicKey

 --DimPatients to FactVisits.
 INNER JOIN [DWCorporateReports].[dbo].[DimPatients] AS dp 
 ON fv.PatientKey = dp.PatientKey

 --DimDoctors to FactVisits.
 INNER JOIN [DWCorporateReports].[dbo].[DimDoctors] AS did 
 ON fv.DoctorKey = did.DoctorKey

 --DimProcedures to FactVisits.
 INNER JOIN [DWCorporateReports].[dbo].[DimProcedures] AS dip 
 ON fv.ProcedureKey = dip.ProcedureKey

 --(OLTP) Patient database to (OLAP) DWCorporateReports Database.
 INNER JOIN [Patients].[dbo].[Visits] AS pv 
 ON pv.ID =  dip.ProcedureKey

--Foreign Keys to FactVisits Table.
ALTER TABLE FactVisits 
ADD CONSTRAINT fk_DimDates_DateKey 
FOREIGN KEY (DateKey) 
REFERENCES DimDates (DateKey) --Constraint in the FactVisits Table.

ALTER TABLE FactVisits 
ADD CONSTRAINT fk_DimClinics_ClinicKey_ClinicKey 
FOREIGN KEY (ClinicKey) 
REFERENCES DimClinics (ClinicKey) --Constraint in the FactVisits Table.

ALTER TABLE FactVisits 
ADD CONSTRAINT fk_DimPatients_PatientKey 
FOREIGN KEY (PatientKey) 
REFERENCES DimPatients (PatientKey) --Constraint in the FactVisits Table.

ALTER TABLE FactVisits 
ADD CONSTRAINT fk_DimDoctors_DoctorKey_DoctorKey 
FOREIGN KEY (DoctorKey) 
REFERENCES DimDoctors (DoctorKey) --Constraint in the FactVisits Table.

ALTER TABLE FactVisits 
ADD CONSTRAINT fk_DimProcedures_ProcedureKey 
FOREIGN KEY (ProcedureKey) 
REFERENCES DimProcedures (ProcedureKey) --Constraint in the FactVisits Table.
 [/code]

如果有人知道如何做这项工作并给我举例, 我将非常感激。

1 个答案:

答案 0 :(得分:0)

[CODE]
INSERT INTO FactVisits
(    --VisitKey int PRIMARY KEY Not Null

     DateKey 
    ,ClinicKey 
    ,PatientKey 
    ,DoctorKey 
    ,ProcedureKey 
    ,Charge)

SELECT dd.DateKey 
      ,dc.ClinicKey --[DWCorporateReports].[dbo].[DimClinics].[ClinicKey]
      ,dp.PatientKey  --[DWCorporateReports].[dbo].[DimPatients].[PatientKey]
      ,doc.DoctorKey--[DWCorporateReports].[dbo].[DimDoctors].[DoctorKey]
      ,dpro.ProcedureKey --[DWCorporateReports].[dbo].[DimProcedure].[ProcedureKey]
      ,v.Charge   --[Patients].[dbo].[Visits]
FROM [Patients].[dbo].[Visits]  AS v
        INNER JOIN [DWCorporateReports].[dbo].[DimDates] AS dd  
            ON CAST(v.[Date] AS date) = CAST(dd.FullDate AS date)

        INNER JOIN [DWCorporateReports].[dbo].[DimClinics] AS dc  
            ON SUBSTRING(CAST(v.Clinic AS VARCHAR(3)), 1, 1)= dc.ClinicID

        INNER JOIN [DWCorporateReports].[dbo].[DimPatients] AS dp  
            ON v.Patient = dp.PatientID

        INNER JOIN [DWCorporateReports].[dbo].[DimDoctors] AS doc  
            ON ISNULL(v.Doctor,-1) = doc.DoctorID

        INNER JOIN [DWCorporateReports].[dbo].[DimProcedures] AS dPro  
            ON v.[Procedure] = dpro.ProcedureID
WHERE Doctor IS NOT NULL

UNION ALL

SELECT dd.DateKey 
      ,dc.ClinicKey      --[DWCorporateReports].[dbo].[DimClinics].[ClinicKey]
      ,dp.PatientKey     --[DWCorporateReports].[dbo].[DimPatients].[PatientKey]
      ,-1 AS DoctorKey   --[DWCorporateReports].[dbo].[DimDoctors].[DoctorKey]
      ,dpro.ProcedureKey --[DWCorporateReports].[dbo].[DimProcedure].[ProcedureKey]
      ,v.Charge   --[Patients].[dbo].[Visits]
FROM [Patients].[dbo].[Visits]  AS v
        INNER JOIN [DWCorporateReports].[dbo].[DimDates] AS dd  
            ON CAST(v.[Date] AS date) = CAST(dd.FullDate AS date)

        INNER JOIN [DWCorporateReports].[dbo].[DimClinics] AS dc  
            ON SUBSTRING(CAST(v.Clinic AS VARCHAR(3)), 1, 1)= dc.ClinicID

        INNER JOIN [DWCorporateReports].[dbo].[DimPatients] AS dp  
            ON v.Patient = dp.PatientID     

        INNER JOIN [DWCorporateReports].[dbo].[DimProcedures] AS dPro  
            ON v.[Procedure] = dpro.ProcedureID
WHERE v.Doctor IS NULL

[/CODE]