sql查询,请高手帮忙 在线等
一个表T1
编码 产品 联系人
1000 A l1
2000 A l2
2000 B l3
2000 A l4
2000 A l5
2000 B l6
1000 A l7
1000 B l8
1000 B l9
1000 B l10
查询同一个编码,产品总数百分之10条,就是说编码1000的A产品总数100就查询出10条,1000编码B产品总数50就差5条。编码2000,,3000.。。。。。。
[解决办法]
我晕了。。你有空看看吧,这个从语句上好像没什么好优化的,主要看逻辑对不对了
;with cte as
(
select C_AreaCode,c_producename,count(1) ct
from Consumer_Airconditioning_12Y10M
where C_AreaCode like '1101__'
group by C_AreaCode,c_producename
)
select * from cte,
(
select *,row_number() over(partition by C_AreaCode,C_ProduceName order by C_ID desc)rn1
from Consumer_Airconditioning_12Y10M where c_id in
(select min(c_id) from Consumer_Airconditioning_12Y10M where C_AreaCode like '1101__' group by C_Name,C_Tel)
) t1
where t1.C_AreaCode=cte.C_AreaCode and t1.c_producename=cte.c_producename
and rn1<=ct/10
order by C_AreaCode,C_ProduceName,rn1