可以把合条件的先查询出来
再查询时就将查询出来的这些优先排序
再查询时就将查询出来的这些优先排序
解决方案 »
- 主从级联 SUM()求和
- 我的SQL server 2000安装有问题,请大家帮一下。
- insert tb select 50万行 这个语句分多次插入效率是不是高些?
- 请教一条UPDATE语句
- 邹建救我:安装SQL-Server2000,出现错误。怎么解决,为什么出现这种情况
- 开贴放点自己的东西,大家不要进啊。
- 求一个查询语句,count,sum 我晕倒了。在线等
- 请问,能不能用sql实现下面的操作????????????????????
- 由视图生成临时表排序问题
- 求这个触发器的写法
- 怎样用 inner join连接分别在两个不同数据服务器且不同数据库的表?!
- 无法录制SQLSERVER的性能计数器
create table t(product_id int identity(1,1),[level] nvarchar(10))
insert t select N'金牌'
union all select N'银牌'
union all select N'金牌'
union all select N'金牌'
union all select N'金牌'
union all select N'银牌'
union all select N'银牌'
union all select N'普通'
union all select N'普通'
union all select N'铜牌'
union all select N'铜牌'
union all select N'银牌'select top 3 * from
(select product_id,[level],(select count(1) from t t2 where t2.product_id<=t1.product_id
and t1.level=t2.level) as count from t t1) a where level=N'金牌' and [count]<=3
union all
select top 2 * from
(select product_id,[level],(select count(1) from t t2 where t2.product_id<=t1.product_id
and t1.level=t2.level) as count from t t1) a where level=N'银牌' and [count]<=2
union all
select top 1 * from
(select product_id,[level],(select count(1) from t t2 where t2.product_id<=t1.product_id
and t1.level=t2.level) as count from t t1) a where level=N'铜牌' and [count]<=1
union all
select * from
(select product_id,[level],(select count(1) from t t2 where t2.product_id<=t1.product_id
and t1.level=t2.level) as count from t t1) a where
(level=N'金牌' and [count]>3) or
(level=N'银牌' and [count]>2) or
(level=N'铜牌' and [count]>1) drop table t
/*
product_id level count
----------- ---------- -----------
1 金牌 1
3 金牌 2
4 金牌 3
2 银牌 1
6 银牌 2
10 铜牌 1
5 金牌 4
7 银牌 3
11 铜牌 2
12 银牌 4
*/