2015-01-01 69 views
1

嗨,我有這個database我試圖選擇員工可以採用的所有程序(課程),以及那些類型和子類型,它們是基於程序規範(Employee Job and Position)的程序規範DEPARTMENT和單位以及部分 所以我想以選擇具有程序說明該員工的所有程序號7 但是當我試圖做的是這樣的:從多重關係中選擇

SELECT * FROM [dbo].[ProgramSpecification] 
       INNER JOIN [dbo].[ProgramsSpecificationDetails] ON [dbo].[ProgramSpecification].PROGRAM_SPECIFCATION_ID = [dbo].[ProgramsSpecificationDetails].PROGRAM_SPECIFCATION_ID 
       INNER JOIN [dbo].[Programs] ON [dbo].[Programs].ProgramID=[dbo].[ProgramsSpecificationDetails].ProgramID 
       INNER JOIN [dbo].[ProgramSubTypesAndProgram] ON [dbo].[Programs].ProgramID = [dbo].[ProgramSubTypesAndProgram].ProgramID 
       INNER JOIN [dbo].[ProgramSubTypes] ON [dbo].[ProgramSubTypes].ProgramSubTypeID = [dbo].[ProgramSubTypesAndProgram].ProgramSubTypeID 
       INNER JOIN [dbo].[ProgramTypeAndSubType] ON [dbo].[ProgramSubTypes].ProgramSubTypeID = [dbo].[ProgramTypeAndSubType].ProgramSubTypeID 
       INNER JOIN [dbo].[ProgramsTypes] ON [dbo].[ProgramsTypes].ProgeamTypeID = [dbo].[ProgramTypeAndSubType].ProgeamTypeID 

該預期的結果:

PROGRAM_SPECIFCATION_ID POSITION_ID POSITION JOB_ID JOB DEPARTMENT_ID DEPARTMENT_NAME UNIT_ID UNIT_NAME SECTION_ID SECTION_NAME ProgramID PROGRAM_SPECIFCATION_ID ProgramID ProgramName ProgramSubTypeID ProgramID ProgramSubTypeID ProgramSubTypeName ProgeamTypeID ProgramSubTypeID ProgeamTypeID ProgramTypeName NominationTypeID 
7 12424 Account Payable Accountant 48 Accountant 587 Finance 650 Accounting 582 Account Payables 1 7 1 Corporate Culture 1 1 1 General 1 1 1 Business-Awareness 1 
7 12424 Account Payable Accountant 48 Accountant 587 Finance 650 Accounting 582 Account Payables 2 7 2 Business Etiquette 1 2 1 General 1 1 1 Business-Awareness 1 
7 12424 Account Payable Accountant 48 Accountant 587 Finance 650 Accounting 582 Account Payables 3 7 3 Technical Awareness 2 3 2 TC 1 2 1 Business-Awareness 1 
7 12424 Account Payable Accountant 48 Accountant 587 Finance 650 Accounting 582 Account Payables 4 7 4 Information Security For non Technical People 3 4 3 IS 1 3 1 Business-Awareness 1 
7 12424 Account Payable Accountant 48 Accountant 587 Finance 650 Accounting 582 Account Payables 18 7 18 Supply Chain Management 11 18 11 Procurement 2 11 2 Business-Specialized 1 
7 12424 Account Payable Accountant 48 Accountant 587 Finance 650 Accounting 582 Account Payables 33 7 33 Hyperion Financial Management 13 33 13 Finance 2 13 2 Business-Specialized 1 
7 12424 Account Payable Accountant 48 Accountant 587 Finance 650 Accounting 582 Account Payables 35 7 35 Oracle Financial Applications Overview 13 35 13 Finance 2 13 2 Business-Specialized 1 
7 12424 Account Payable Accountant 48 Accountant 587 Finance 650 Accounting 582 Account Payables 36 7 36 IFRS 13 36 13 Finance 2 13 2 Business-Specialized 1 
7 12424 Account Payable Accountant 48 Accountant 587 Finance 650 Accounting 582 Account Payables 116 7 116 The 7 Habits 41 116 41 Achievement Orientation 4 41 4 CompetencyBased 1 

而是它返回這樣

PROGRAM_SPECIFCATION_ID POSITION_ID POSITION JOB_ID JOB DEPARTMENT_ID DEPARTMENT_NAME UNIT_ID UNIT_NAME SECTION_ID SECTION_NAME ProgramID PROGRAM_SPECIFCATION_ID ProgramID ProgramName ProgramSubTypeID ProgramID ProgramSubTypeID ProgramSubTypeName ProgeamTypeID ProgramSubTypeID ProgeamTypeID ProgramTypeName NominationTypeID 
7 12424 Account Payable Accountant 48 Accountant 587 Finance 650 Accounting 582 Account Payables 1 7 1 Corporate Culture 1 1 1 General 1 1 1 Business-Awareness 1 
7 12424 Account Payable Accountant 48 Accountant 587 Finance 650 Accounting 582 Account Payables 1 7 1 Corporate Culture 1 1 1 General 3 1 3 Business-Technical 1 
7 12424 Account Payable Accountant 48 Accountant 587 Finance 650 Accounting 582 Account Payables 1 7 1 Corporate Culture 1 1 1 General 5 1 5 Leadership 2 
7 12424 Account Payable Accountant 48 Accountant 587 Finance 650 Accounting 582 Account Payables 2 7 2 Business Etiquette 1 2 1 General 1 1 1 Business-Awareness 1 
7 12424 Account Payable Accountant 48 Accountant 587 Finance 650 Accounting 582 Account Payables 2 7 2 Business Etiquette 1 2 1 General 3 1 3 Business-Technical 1 
7 12424 Account Payable Accountant 48 Accountant 587 Finance 650 Accounting 582 Account Payables 2 7 2 Business Etiquette 1 2 1 General 5 1 5 Leadership 2 
7 12424 Account Payable Accountant 48 Accountant 587 Finance 650 Accounting 582 Account Payables 3 7 3 Technical Awareness 2 3 2 TC 1 2 1 Business-Awareness 1 
7 12424 Account Payable Accountant 48 Accountant 587 Finance 650 Accounting 582 Account Payables 4 7 4 Information Security For non Technical People 3 4 3 IS 1 3 1 Business-Awareness 1 
7 12424 Account Payable Accountant 48 Accountant 587 Finance 650 Accounting 582 Account Payables 18 7 18 Supply Chain Management 11 18 11 Procurement 2 11 2 Business-Specialized 1 
7 12424 Account Payable Accountant 48 Accountant 587 Finance 650 Accounting 582 Account Payables 33 7 33 Hyperion Financial Management 13 33 13 Finance 2 13 2 Business-Specialized 1 
7 12424 Account Payable Accountant 48 Accountant 587 Finance 650 Accounting 582 Account Payables 35 7 35 Oracle Financial Applications Overview 13 35 13 Finance 2 13 2 Business-Specialized 1 
7 12424 Account Payable Accountant 48 Accountant 587 Finance 650 Accounting 582 Account Payables 36 7 36 IFRS 13 36 13 Finance 2 13 2 Business-Specialized 1 
7 12424 Account Payable Accountant 48 Accountant 587 Finance 650 Accounting 582 Account Payables 116 7 116 The 7 Habits 41 116 41 Achievement Orientation 4 41 4 CompetencyBased 1 
7 12424 Account Payable Accountant 48 Accountant 587 Finance 650 Accounting 582 Account Payables 116 7 116 The 7 Habits 42 116 42 Adaptability 4 42 4 CompetencyBased 1 
在線路2 3 5 6

結果它返回不同的節目類型相同的程序明知通緝的結果僅僅是一個與ProgramTypeName商業意識和ProgramSubTypeName一般但 因爲ProgramTypeProgramSubType有多對多的關係還給所有具有ProgramSubType一般知道程序說明7號它需要與鏈接ProgramType(選擇)枝條的ProgramTypeName商業意識和ProgramSubTypeName一般

,並在上線的14,但代替ProgramType這裏的拖表是同樣的事情ProgramsProgramSubType有多對多的關係,所以它的結果返回所有具有相同ProgramID不同ProgramSubTypeID知道ProgramName中程序規格編號7通緝的結果只是程序ProgramID 116和所以請任何幫助,我該怎麼做,很多在此先感謝。

+0

在這樣之情況,你總是使用排名功能與分區,最有可能ROW_NUMBER() – KumarHarsh

回答

0

我改變了我的數據庫架構是這樣的db schema然後用這個拖查詢的到 觀看所有方案,有細節和觀看所有方案可供位置

觀看所有方案,有細節:

SELECT * 
FROM [dbo].[Programs] 
INNER JOIN [dbo].[ProgramDetails] ON [dbo].[Programs].ProgramID = [dbo].[ProgramDetails].ProgramID 
INNER JOIN [dbo].[ProgramSubTypes] ON [dbo].[ProgramSubTypes].ProgramSubTypeID = [dbo].[ProgramDetails].ProgramSubTypeID 
INNER JOIN [dbo].[ProgramsTypes] ON [dbo].[ProgramsTypes].ProgramTypeID = [dbo].[ProgramDetails].ProgramTypeID 

觀看所有方案可供位置:

SELECT * 
FROM [dbo].[Positions] 
INNER JOIN [dbo].[ProgramsSpecificationDetails] ON [dbo].[Positions].PositionID = [dbo].[ProgramsSpecificationDetails].PositionID 
INNER JOIN [dbo].[ProgramDetails] ON [dbo].[ProgramDetails].ProgramDetailsID = [dbo].[ProgramsSpecificationDetails].ProgramDetailsID 
INNER JOIN [dbo].[Programs] ON [dbo].[Programs].ProgramID = [dbo].[ProgramDetails].ProgramID 
INNER JOIN [dbo].[ProgramSubTypes] ON [dbo].[ProgramSubTypes].ProgramSubTypeID = [dbo].[ProgramDetails].ProgramSubTypeID 
INNER JOIN [dbo].[ProgramsTypes] ON [dbo].[ProgramsTypes].ProgramTypeID = [dbo].[ProgramDetails].ProgramTypeID 

,並給它牛逼他想要的結果

0

因爲你有兩個多到許多關係 - 表ProgramSubTypesAndProgram和ProgramTypeAndSubType你必須改變你的數據庫架構得到單一的結果,例如。添加確切的類型到程序關係表ProgramSubTypesAndProgram或使Type-SubType關係只有一對多。