日期:2014-05-16  浏览次数:20784 次

Oracle合并语句
select count(user_id) from (select distinct user_id from 表1)//这句查的是从表1中查出不同的user_id
select count(*) from (select * from 表2 where product_name is not null order by product_bussiness_count desc) where user_id={0} and sort_id=11 and (rownum<2)这句是根据条件查询
我现在想做的是:加了一个AspNetPage控件,之前没加分页时,实现的是先查出不同的user_id,然后再根据user_id和sort_id查出产品(每个用户只提取1个产品),但现在加上分页,就不知道怎么做了,能不能把上面的两句话合成一句?
------解决方案--------------------

--你的第一句 select count(user_id) from (select distinct user_id from 表1)
--你的第二句 select count(*) from (select * from 表2 where product_name is not null order by product_bussiness_count desc) where user_id={0} and sort_id=11 and (rownum<2)
--你说一个用户提取一个产品 那可不可以理解为 表2中的sort_id和表1中的user_id一一对应?
--你是不是就是要统计 不同的user_id对应的你第二句查询结果的总数量?
--那么可以这么做
select count(*) 
from (select * 
      from 表2 
      where product_name is not null 
      order by product_bussiness_count desc) 
where user_id in (select count(user_id) from (select distinct user_id from 表1)) 
and sort_id=11 
and rownum<2
--中间标出了一段就是合并修改的

------解决方案--------------------
select rownum,c.*
from 
(
select a.user_id
       ,b.*
       ,row_number() over(partition by a.user_id order by b.product_bussiness_count desc) rn
from 表1 a,表2 b 
where a.user_id = b.user_id  and b.product_name is not null and b.ort_id=11
) c
where rn = 1