1

我有 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

  1. tmpGroundPreferenceProfile 和 tmpGroundPreferenceLocations 之间存在一对多的关系
  2. 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)

非常感谢你们的任何帮助。

4

1 回答 1

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

这给你的是每个地区和每周的第一次出现。如果同一地区、牧场、田地和周有多行,则顺序是任意的,因此您将得到任意行。

这是否接近您正在寻找的内容?

于 2012-12-18T16:54:06.387 回答