2012-12-18 40 views
1

我有3代表與此架構編寫SQL查詢:需要幫助與多個一對多關係

表1

CREATE TABLE #tmpGroundPreferenceProfile 
(
    [Id] [int] NOT NULL, 
    [Name] [varchar](50) NULL 
) 
INSERT #tmpGroundPreferenceProfile ([Id], [Name]) VALUES (1, N'Profile1') 
INSERT #tmpGroundPreferenceProfile ([Id], [Name]) VALUES (2, N'Profile2') 
INSERT #tmpGroundPreferenceProfile ([Id], [Name]) VALUES (3, N'Profile3') 

表2

CREATE TABLE #tmpGroundPreferenceLocations(
    [Id] [int] NOT NULL, 
    [GroundPreferenceProfileId] [int] NULL, 
    [DistrictId] [int] NULL, 
    [RanchId] [int] NULL, 
    [FieldId] [int] NULL 
) 

INSERT #tmpGroundPreferenceLocations ([Id], [GroundPreferenceProfileId], [DistrictId], [RanchId], [FieldId]) VALUES (1, 1, 1, NULL, NULL) 
INSERT #tmpGroundPreferenceLocations ([Id], [GroundPreferenceProfileId], [DistrictId], [RanchId], [FieldId]) VALUES (2, 1, 1, 1, NULL) 
INSERT #tmpGroundPreferenceLocations ([Id], [GroundPreferenceProfileId], [DistrictId], [RanchId], [FieldId]) VALUES (3, 1, 1, 1, 1) 

表3

CREATE TABLE #tmpGroundPreferenceRatings(
    [Id] [int] NOT NULL, 
    [GroundPreferenceLocationId] [int] NULL, 
    [Rating] [int] NULL, 
    [Week] [int] NULL 
) 
INSERT #tmpGroundPreferenceRatings ([Id], [GroundPreferenceLocationId], [Rating], [Week]) VALUES (2, 1, 11, 1) 
INSERT #tmpGroundPreferenceRatings ([Id], [GroundPreferenceLocationId], [Rating], [Week]) VALUES (3, 1, 12, 2) 
INSERT #tmpGroundPreferenceRatings ([Id], [GroundPreferenceLocationId], [Rating], [Week]) VALUES (4, 1, 13, 3) 
INSERT #tmpGroundPreferenceRatings ([Id], [GroundPreferenceLocationId], [Rating], [Week]) VALUES (5, 2, 21, 1) 
INSERT #tmpGroundPreferenceRatings ([Id], [GroundPreferenceLocationId], [Rating], [Week]) VALUES (6, 2, 22, 2) 
INSERT #tmpGroundPreferenceRatings ([Id], [GroundPreferenceLocationId], [Rating], [Week]) VALUES (7, 2, 23, 3) 
INSERT #tmpGroundPreferenceRatings ([Id], [GroundPreferenceLocationId], [Rating], [Week]) VALUES (8, 3, 31, 1) 
INSERT #tmpGroundPreferenceRatings ([Id], [GroundPreferenceLocationId], [Rating], [Week]) VALUES (9, 3, 32, 2) 
INSERT #tmpGroundPreferenceRatings ([Id], [GroundPreferenceLocationId], [Rating], [Week]) VALUES (10, 3, 33, 3) 
INSERT #tmpGroundPreferenceRatings ([Id], [GroundPreferenceLocationId], [Rating], [Week]) VALUES (11, 1, 14, 4) 
INSERT #tmpGroundPreferenceRatings ([Id], [GroundPreferenceLocationId], [Rating], [Week]) VALUES (12, 1, 15, 5) 
INSERT #tmpGroundPreferenceRatings ([Id], [GroundPreferenceLocationId], [Rating], [Week]) VALUES (13, 2, 24, 6) 
INSERT #tmpGroundPreferenceRatings ([Id], [GroundPreferenceLocationId], [Rating], [Week]) VALUES (14, 2, 25, 7) 

說明

針對每個配置文件可以有多個位置,並針對每個位置可以有多個等級。

  1. 有tmpGroundPreferenceProfile和tmpGroundPreferenceLocations之間的一個一對多的關係艦
  2. 有tmpGroundPreferenceLocations和#tmpGroundPreferenceRatings

問題之間的一個一對多的關係船:

可以有多個評分存在一週。我想按以下順序獲得評級:

首先檢查區域,牧場和田地 - 如果對這個組合的評級存在,那麼得到它,否則如果評級不存在,則檢查區域和牧場組合,如果對這個組合的評級存在,那麼得到它如果評級不存在,然後檢查地區的位置獲得評級。

注意

可以有多個等級存在對第1周,即可以存在地區和牧場,可以存在區,牧場和場等等......

我需要獲得記錄明智的,

SELECT * 
FROM #tmpGroundPreferenceProfile GPP 
INNER JOIN #tmpGroundPreferenceLocations GPL  
ON GPL.GroundPreferenceProfileId = GPP.Id 
INNER JOIN #tmpGroundPreferenceRatings GPR 
ON GPR.GroundPreferenceLocationId = GPL.Id 
WHERE GPP.Id = 1  
AND GPR.[Week] IN (1,3,4) 

任何幫助你們是高度讚賞。

+1

你可以發佈查詢所需的結果嗎? – Taryn

+1

這裏有一個小提琴,如果有人需要:http://www.sqlfiddle.com/#!6/4808f/2/0 –

+0

不清楚這裏。你想每週記錄一次?每個配置文件每週一個記錄? –

回答

0

正如其他人所指出的,您的問題並不清楚。無論如何,我拍了一張照片,有時候可以幫助澄清事情。

SELECT * 
FROM (

    SELECT ROW_NUMBER() OVER (PARTITION BY GPR.Week 
         ORDER BY GPL.DistrictId, 
          GPL.RanchID DESC, 
          GPL.FieldID DESC 
         ) AS RN, 
     GPP.ID, 
     GPL.DistrictId, 
     GPL.RanchID, 
     GPL.FieldID, 
     GPR.Rating, 
     GPR.Week 
    FROM tmpGroundPreferenceProfile GPP 
    JOIN tmpGroundPreferenceLocations GPL 
    ON GPL.GroundPreferenceProfileId = GPP.Id 
    JOIN tmpGroundPreferenceRatings GPR 
    ON GPR.GroundPreferenceLocationId = GPL.Id 
    WHERE GPP.Id = 1 
) X 
WHERE X.RN = 1 
ORDER BY X.Week, X.districtid 

這給了你什麼是每個地區和周的第一次出現。如果同一個分區,牧場,場和周有多行,則順序是任意的,所以你會得到一個任意的行。

這是接近你在找什麼?

+0

感謝您對此的快速回答。 –