!求一条SQL,立马结帖!
1. budID为大厦ID,该表中会出现重复
2. imgID为大厦对应的图片ID,该表中也会重复(2个大厦同用一个图片的时候).
3.imgTYpeID 大厦图片类型ID(重复)
4.default_YN 大厦图片的是否为默认(即.同一个大厦的同种类型的图片有多个时,优先取默认为Y的,如果都是N,则取第一条).
budID imgID imgTypeID default_YN
1 2 1 N
1 3 1 Y
1 4 2 N
2 5 2 N
2 6 1 Y
期望的结果是:
budID imgID imgTypeID default_YN
1 3 1 Y
1 4 2 N
2 5 2 N
2 6 1 Y
即:每个大厦的同一种图片类型只有一个图片.
------解决方案--------------------create table Test
(
budID int,
imgID int,
imgTypeId int,
default_YN varchar(2)
)
insert Test select 1,2,1, 'N '
insert Test select 1,3,1, 'Y '
insert Test select 1,4,2, 'N '
insert Test select 2,5,2, 'N '
insert Test select 2,6,1, 'Y '
select T.*
from Test T
where cast(T.budID as varchar) + cast(T.imgID as varchar) + cast(T.imgTypeId as varchar) in
(select top 1 cast(budID as varchar) + cast(imgID as varchar) + cast(imgTypeId as varchar) from Test where T.budID=budID and T.imgTypeId=imgTypeId order by default_YN DESC )
------解决方案--------------------select budID,distinct imgID,distinct imgtypeID,defaul