原始的数据
id name secondname type
1 商品一 1
1 商品一 1
2 商品二 2
3 商品三 套餐一 3
3 商品三 套餐一 3
3 商品四 套餐二 3
3 商品四 套餐二 3 现在想要的数据
id name secondname type
1 商品一 1
2 商品二 2
3 商品三 套餐一 3
3 商品四 套餐二 3 我是想,只要类别为3的,都要取他相同secondname的一条记录。别的类别是不管重复不重复都要全部取完。
有没有Sql语句能一下子就取完的,谢谢大家。
id name secondname type
1 商品一 1
1 商品一 1
2 商品二 2
3 商品三 套餐一 3
3 商品三 套餐一 3
3 商品四 套餐二 3
3 商品四 套餐二 3 现在想要的数据
id name secondname type
1 商品一 1
2 商品二 2
3 商品三 套餐一 3
3 商品四 套餐二 3 我是想,只要类别为3的,都要取他相同secondname的一条记录。别的类别是不管重复不重复都要全部取完。
有没有Sql语句能一下子就取完的,谢谢大家。
union all
select B.* from
(
select distinct name
from table_name
where type = 3
) A
cross apply
(
select top(1) * from table_name
where name = A.name
) B
from from t
where type <3
union all
select max(id),max(name),secondname,max(type)
from t
where type = 3
group by secondname
having count(secondname) = 2
select distinct * from tb where type=3
from from t
where type <3
union all
select max(id),max(name),secondname,max(type)
from t
where type = 3
group by secondname
having count(secondname) = 2