このスキーマを持つテーブルが3つあります。
表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)
説明:
各プロファイルに対して複数の場所が存在する可能性があり、各場所に対して複数の評価が存在する可能性があります。
すなわち
- tmpGroundPreferenceProfileとtmpGroundPreferenceLocationsの間には1対多の関係があります
- tmpGroundPreferenceLocationsと#tmpGroundPreferenceRatingsの間には1対多の関係があります
質問:
1週間に対して複数の評価が存在する可能性があります。次の順番で評価を取得したいのですが、
最初に地区、牧場、およびフィールドを確認します-この組み合わせに対して評価が存在する場合はそれを取得し、評価が存在しない場合はそれを取得し、次に地区と牧場の組み合わせを確認します。この組み合わせに対して評価が存在する場合はそれを取得し、評価が存在しない場合はそれを取得します存在する場合は、地区の場所を確認して評価を取得します。
注:
第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)
皆さんからの助けは大歓迎です。