在编写具有多个一对多关系的SQL Query时需要帮助

时间:2012-12-18 14:44:43

标签: sql-server

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

描述

对于每个配置文件,可以有多个位置,每个位置可以有多个评级。

即。

  1. tmpGroundPreferenceProfile与tmpGroundPreferenceLocations之间存在一对多关系
  2. tmpGroundPreferenceLocations与#tmpGroundPreferenceRatings之间存在一对多关系
  3. 问题

    一周内可能有多个评级。我想按以下顺序获得评级,

    首先检查区域,牧场和区域 - 如果对该组合存在评级,那么如果评级不存在则得到它,然后检查区域和牧场组合,如果对该组合存在评级,那么在评级时得到它不存在然后检查区位置以获得评级。

    注意

    对于第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 个答案:

答案 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

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

这接近你想要的吗?