2012-12-18 2 views
1

많은 관계로 여러 하나와 SQL 쿼리를 작성에 도움이 필요하십니까내가이 스키마로 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 주일 동안 존재합니다.

첫 번째 구역, 목장 및 필드를 확인하십시오 -이 조합에 대해 평가가있는 경우 평가가 존재하지 않으면 다른 정보를 얻은 다음 구역 및 목장 조합을 확인하십시오. 이 조합에 대해 평점이 존재한다면 평점이 존재하지 않으면 다른 점수를 얻은 다음 지구 위치를 확인하여 평점을 얻으십시오.

:

여러 평가가있을 수는 지구와 목장이 존재 할 수있는 주 (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

여기 명확하지 않습니다. 일주일에 한 레코드가 필요하십니까?프로필 당 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 

각 지역 및 주간에 처음으로 나타나는 것은 무엇입니까? 같은 지구, 목장, 필드 및 주에 대해 여러 행이있는 경우 순서는 임의적이므로 임의의 행을 가져옵니다.

찾고 계신가요?

+0

빠른 답변 주셔서 감사합니다. –