많은 관계로 여러 하나와 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)
설명 :
각 프로파일에 대하여 여러 위치가있을 수 있으며, 각 위치에 대해 여러 평가가있을 수 있습니다.
즉
- tmpGroundPreferenceProfile와 tmpGroundPreferenceLocations 사이에 일대 다 관계 선박이
- 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)
여러분의 도움을 매우 높이 평가한다.
원하는 쿼리 결과를 게시 할 수 있습니까? – Taryn
누군가가 필요하면 다음과 같은 바이올린이 있습니다. http://www.sqlfiddle.com/#!6/4808f/2/0 –
여기 명확하지 않습니다. 일주일에 한 레코드가 필요하십니까?프로필 당 1 주일에 하나의 기록이 있습니까? –