SQL Server 용으로 작성했지만 실제 쿼리는 다른 데이터베이스에서 작동해야합니다.
설정 데이터 :
declare @Fruit table (FruitID int not null,Fruit varchar(10) not null,UnitPrice int not null)
insert into @Fruit(FruitID,Fruit,UnitPrice) values
(1,'Orange',3),
(2,'Apple',2),
(3,'Grape',4)
declare @FruitDetails table (FruitID int not null,Picture varchar(20) not null,Color varchar(10) not null)
insert into @FruitDetails (FruitID,Picture,Color) values
(1,'Orange_o.jpg','Orange'),
(3,'Grape_p.jpg','Purple'),
(3,'Grape_g.jpg','Green'),
(2,'Apple_g.jpg','Green'),
(2,'Apple_r.jpg','Red')
검색어 :
select
f.Fruit,
f.UnitPrice,
fd.Picture,
fd.Color
from
@Fruit f
inner join
@FruitDetails fd
on
f.FruitID = fd.FruitID
left join
@FruitDetails fd_anti
on
f.FruitID = fd_anti.FruitID and
fd_anti.Picture < fd.Picture --This is the condition for picking a better row
where
fd_anti.FruitID is null --This eliminates rows where a better row was picked
결과 : 당신의 예상 결과와 일치하지 않는,하지만 당신은 우리에게 좋은를 부여하지 않은
Fruit UnitPrice Picture Color
---------- ----------- -------------------- ----------
Orange 3 Orange_o.jpg Orange
Grape 4 Grape_g.jpg Green
Apple 2 Apple_g.jpg Green
FruitDetail
에서 "최상의"행을 선택하는 조건에 대한 정의.
무슨에 합류? 당신은 지금 어떤 질문을하나요? –
'FruitDetails'에서 올바른 행을 어떻게 선택합니까? 포도가 자주색이고 사과가 녹색이라는 것을 어떻게 알 수 있습니까? –
은 로직을 wats합니까? – manurajhada