SQL Server自引用查询

时间:2014-05-19 13:10:18

标签: sql sql-server tsql sql-server-2012 data-warehouse

请考虑以下事项:

CREATE DATABASE TEST
USE TEST

CREATE TABLE Student
    (
      StudentID INT IDENTITY
                    PRIMARY KEY ,
      FirstName NVARCHAR(50) ,
      LastName NVARCHAR(50)
    )

CREATE TABLE StudentComponent
    (
      StudentComponentID INT IDENTITY
                             PRIMARY KEY ,
      StudentID INT FOREIGN KEY REFERENCES dbo.Student(StudentID) ,
      ComponentName NVARCHAR(50) ,
      ComponentRef NVARCHAR(50) ,
      ComponentType NCHAR(2)
    )

CREATE TABLE Component
    (
      ComponentID INT IDENTITY
                      PRIMARY KEY ,
      StudentComponentID INT FOREIGN KEY REFERENCES dbo.StudentComponent(StudentComponentID) ,
      ComponentName NVARCHAR(50) ,
      ComponentRef NVARCHAR(50) ,
      ComponentType NCHAR(2)
    )

我有3张桌子

  1. 学生
  2. StudentComponent
  3. 组件
  4. 拆分为维度

    CREATE TABLE DimClass
        (ClassDwKey INT,
        ClassName NVARCHAR(50),
        ClassRef NVARCHAR(50))
    
        CREATE TABLE DimCollege
        (CollegeDwKey INT,
        CollegeName NVARCHAR(50),
        CollegeRef NVARCHAR(50))
    
        CREATE TABLE DimSubject
        (SubjectDwKey INT,
        SubjectName NVARCHAR(50),
        SubjectRef NVARCHAR(50))
    
        CREATE TABLE DimStudent
        (StudentDwKey INT,
        StudentName NVARCHAR(50))
    
        INSERT INTO 
    
        CREATE TABLE FactAcademicEvent
        (StudentDwKey int, ClassDwKey int, CollegeDwKey int, SubjectDwKey INT)
    

    它们之间的关系顺序相同(学生 - > StudentComponent - >组件)。我已将组件拆分为各自的尺寸,DimClass,DimCollege,DimSubject(按组件类型字段拆分,=' CL'用于课程,' SU'用于主题,' CO& #39;对于大学等。我正在尝试加载一个事实表,从我的维度获取我的代理键并生成一行以下

    StudentDwKey(来自dimstudent),ClassDwKey,CollegeDwKey,SubjectDwKey。维度和组件表ComponentKey之间有一个链接,它通过studentcomponent表链接到student。

    StudentComponent和Component基本相同,除了StudentComponent引用学生,因此有更多行。

    有什么想法吗?

2 个答案:

答案 0 :(得分:1)

编辑:

根据OP的编辑和评论完成更改。

SELECT
  s.StudentID,
  MAX(dim_s.StudentDwKey )   AS StudentDwKey,
  MAX(dim_cl.ClassDwKey  )   AS ClassDwKey,
  MAX(dim_su.SubjectDwKey)   AS SubjectDwKey,
  MAX(dim_co.CollegeDwKey)   AS CollegeDwKey
FROM
  Student            AS s
LEFT JOIN
  StudentComponent   AS sc
    ON  sc.StudentID    = s.StudentID
LEFT JOIN
  dimStudent         AS dim_s
    ON  dim_s.StudentName = s.StudentName  -- or whatever is a reliable join
LEFT JOIN
  dimClass           AS dim_cl
    ON  dim_cl.ClassRef   = sc.ComponentRef
    AND sc.ComponentType  = 'CL'
LEFT JOIN
  dimSubject         AS dim_su
    ON  dim_su.SubjectRef = sc.ComponentRef
    AND su.ComponentType  = 'SU'
LEFT JOIN
  dimCollege         AS dim_co
    ON  dim_co.CollegeRef = sc.ComponentRef
    AND sc.ComponentType  = 'CO'
GROUP BY
  s.StudentID

答案 1 :(得分:1)

将学生详细信息保存在Student表中(您已完成)和组件表中的组件详细信息(您尚未完成),并使用StudentComponent表将它们链接在一起。像这样:

CREATE DATABASE TEST
USE TEST

CREATE TABLE Student
    (
      StudentID INT IDENTITY
                    PRIMARY KEY ,
      FirstName NVARCHAR(50) ,
      LastName NVARCHAR(50)
    )

CREATE TABLE StudentComponent
    (
      StudentComponentID INT IDENTITY
                             PRIMARY KEY ,
      StudentID INT FOREIGN KEY REFERENCES dbo.Student(StudentID) ,
      ComponentID INT FOREIGN KEY REFERENCES dbo.Component(ComponentID),
    )

CREATE TABLE Component
    (
      ComponentID INT IDENTITY
                      PRIMARY KEY ,
      ComponentName NVARCHAR(50) ,
      ComponentRef NVARCHAR(50) ,
      ComponentType NCHAR(2)
    )