我有 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)
说明:
针对每个配置文件可以有多个位置,并且针对每个位置可以有多个评级。
IE
- tmpGroundPreferenceProfile 和 tmpGroundPreferenceLocations 之间存在一对多的关系
- tmpGroundPreferenceLocations 和 #tmpGroundPreferenceRatings 之间存在一对多的关系
问题:
一周内可以有多个评级。我想按以下顺序获得评分,
首先检查 District、Ranch 和 Field - 如果针对此组合的评级存在,则获取它,如果评级不存在则获取它,然后检查 District 和 Ranch 组合,如果针对此组合存在评级,则获取它,如果评级不存在存在然后检查地区位置以获得评级。
注意:
第 1 周可以存在多个评级,即可以存在于 District 和 Ranch,也可以存在于 District、Ranch 和 Field 等等......
我需要每周明智地获得记录,
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)
非常感谢你们的任何帮助。