2010-03-25 8 views
1

tblUserProfile에서, 하나 개의 테이블에서 다른 임의의 행을 행을 얻기 위해 - 거기에 바로 profileID가를합니다 (이 또 다른 테이블 - 나는 모든 프로필 정보 (너무 많은 필드)를 보유하고 테이블쿼리는 다른

tblMonthlyProfiles이 아이디어는이 테이블이 때로는 월간 프로파일 (선택시)이되는 2 개의 프로파일을 보유한다는 것입니다.

이제 월별 프로파일을 표시해야 할 때이 tblMonthlyProfiles에서 선택하고 tblUserProfile과 결합하여 모든 유효한 정보를 얻습니다.

tblMonthlyProfile에 행이 없으면 월간 프로필 섹션이 표시되지 않습니다.

이제는 월간 프로필을 항상 표시해야합니다. monthlyProfiles에 행이 없으면 tblUserProfile에서 2 개의 무작위 프로파일을 선택해야합니다. monthlyProfiles에 행이 하나만 있으면 tblUserProfile에서 임의의 행 하나만 선택해야합니다.

하나의 쿼리로 모든 것을 수행하는 가장 좋은 방법은 무엇입니까?

나는이

같은 것이 tblUserProfile P NEWID()

에 의해 M.profileid = P.profileid 주문 tblMonthlyProfiles M 가입 LEFT OUTER에서 상위 2 *를 선택 생각하지만 이것은 항상 나에게 준다 tblProfile에서 2 개의 임의의 행. 이 문제를 어떻게 해결할 수 있습니까? 이 같은

답변

1

시도 뭔가 :

SELECT TOP 2 Field1, Field2, Field3, FinalOrder FROM 
(
select top 2 Field1, Field2, Field3, FinalOrder, '1' As FinalOrder from tblUserProfile P JOIN tblMonthlyProfiles M on M.profileid = P.profileid 
UNION 
select top 2 Field1, Field2, Field3, FinalOrder, '2' AS FinalOrder from tblUserProfile P LEFT OUTER JOIN tblMonthlyProfiles M on M.profileid = P.profileid ORDER BY NEWID() 
) 
ORDER BY FinalOrder 

(올바르게처럼) 후 2 개 무작위 프로파일 (많은이 존재하는 경우)이 개 매월 프로파일을 선택하고있는 생각하고 UNION 그들. 그 시점에서 2 ~ 4 개의 레코드가 있습니다. 톱 2를 잡아라. FinalOrder 열은 매월 처음 시도하고 얻는 것을 확인하는 쉬운 방법입니다.

테이블 구조를 제어하는 ​​경우 부울 필드 IsMonthlyProfileUserProfile 테이블에 추가하기 만하면 쉽게 문제를 해결할 수 있습니다. 그 다음은 같은 당신이 뭔가를 할 수있는 SQL 2000 + 준수 구문에서는 단일 테이블 쿼리, order by IsBoolean, NewID()

+0

더 자세히 살펴보면 두 번째 SELECT가 월간 테이블에 다시 가입해야한다고 생각하지 않습니다. 하지만 어쨌든 당신이 그 아이디어를 얻길 바랍니다. – LesterDove

+0

하지만이 쿼리에서 임의의 것은 어디로 이동합니까? 프로파일 테이블에는 수백 개의 레코드가 있으며 임의의 2 개의 임의 행이 필요합니다. –

+0

두 번째 쿼리에는 NEWID() 랜덤 화기가 있어야합니다. 편집했습니다. – LesterDove

0

의 : SQL을 사용하여

Select ... 
From (
     Select TOP 2 ... 
     From tblUserProfile As UP 
     Where Not Exists(Select 1 From tblMonthlyProfile As MP1) 
     Order By NewId() 
     ) As RandomProfile 
Union All 
Select MP.... 
From tblUserProfile As UP 
    Join tblMonthlyProfile As MP 
     On MP.ProfileId = UP.ProfileId 
Where (Select Count(*) From tblMonthlyProfile As MP1 ) >= 1 
Union All 
Select ... 
From (
     Select TOP 1 ... 
     From tblUserProfile As UP 
     Where (Select Count(*) From tblMonthlyProfile As MP1 ) = 1 
     Order By NewId() 
     ) As RandomProfile 

는 2005 + CTE 당신은 할 수 있습니다 :

With 
    TwoRandomProfiles As 
    (
    Select TOP 2 ..., ROW_NUMBER() OVER (ORDER BY UP.ProfileID) As Num 
    From tblUserProfile As UP 
    Order By NewId() 
    ) 
Select MP.Col1, ... 
From tblUserProfile As UP 
    Join tblMonthlyProfile As MP 
     On MP.ProfileId = UP.ProfileId 
Where (Select Count(*) From tblMonthlyProfile As MP1 ) >= 1 
Union All 
Select ... 
From TwoRandomProfiles   
Where Not Exists(Select 1 From tblMonthlyProfile As MP1) 
Union All 
Select ... 
From TwoRandomProfiles 
Where (Select Count(*) From tblMonthlyProfile As MP1 ) = 1 
    And Num = 1 

열팽창 계수가있다 무작위 프로파일을 한번만 쿼리하고 ROW_NUMBER() 컬럼을 사용하는 이점.

분명히 모든 UNION 문에서 열의 수와 유형이 일치해야합니다.