特定场景的SQL内部/子组查询

特定场景的SQL内部/子组查询,sql,group-by,subquery,Sql,Group By,Subquery,我有一张如下的桌子 模式:ID |类别|关键字|出价 编写一个sql,根据每个类别的出价提取前5个关键字 详情: oracle 10.x上考虑的表定义: create table test ( ID number, Category varchar (20), Keyword varchar (20), BidPrice number ); 数据: 预期产出: 注意:答案必须仅在SQL中,不使用任何Oracle或数据库特定功能。使用 试试这个: select K

我有一张如下的桌子

模式:ID |类别|关键字|出价

编写一个sql,根据每个类别的出价提取前5个关键字

详情:

oracle 10.x上考虑的表定义:

create table test (
    ID number,
    Category varchar (20),
    Keyword varchar (20),
    BidPrice number
);
数据:

预期产出:

注意:答案必须仅在SQL中,不使用任何Oracle或数据库特定功能。

使用

试试这个:

select KEYWORD, CATEGORY ,BIDPRICE 
from 
(
  select KEYWORD, CATEGORY ,BIDPRICE , 
         ROW_NUMBER() over (partition by Category order by BidPrice desc)  as rn
  from test
) a
where a.rn <= 5;

我提出了这个问题,但如果它是最大的,它将离开第六排。有人能帮忙吗

select a.keyword, a.category, a.bidprice
from
  test a,
  (
    select b.id, b.category
    from test b
    where (select distinct(c.category) from test c where b.category=c.category) = b.category
  ) xx
where a.id in (select ss.id from test ss where a.category=ss.category and rownum<=5)
group by a.category, a.bidprice, a.keyword
order by a.category, a.bidprice desc;
伙计们

我解决了这个问题,请考虑一下:

select a.keyword, a.category, a.bidprice
from
  test a,
  (
    select b.id, b.category
    from test b
    where (select distinct(c.category) from test c where b.category=c.category) = b.category
    order by b.bidprice desc
  ) xx
where a.id in (
  select id
  from (select dd.id, dd.category from test dd order by dd.bidprice desc) ss
  where a.category=ss.category
    and rownum<=5
)
group by a.category, a.bidprice, a.keyword
order by a.category, a.bidprice desc;

是不是6,“A类”,“关键字A6”,133?i、 e.第6行中包含的实际上是该类别中最高的价格不应该包含在您的输出中A类的前5名中吗?您是对的,133应该包含在其中。@JohnWoo:*子查询中似乎没有。。我已将*替换为列名。。现在它开始工作了……对不起,乔,我问的只是SQL,没有使用任何Oracle函数。@shaILU:这个答案中没有Oracle的具体内容。如果您担心行号,它是标准的一部分,并且受到许多SQL产品的支持。对不起,Joe,我只问SQL,没有使用任何Oracle函数。@shaILU SQL表示结构化查询语言。您正在使用什么RDBMS?关系数据库管理系统?例如SQL Server、Oracle、MySQL、PostgreSQL或什么我认识约翰。但实际上项目需求说,我们不想使用RDBMS特定的功能。只需要SQL。。我们仍然使用Oracle 10g John。请注意,rownum是Oracle特有的功能。因此,您的解决方案不是纯SQL。
select KEYWORD, CATEGORY ,BIDPRICE 
from 
(
  select KEYWORD, CATEGORY ,BIDPRICE , 
         ROW_NUMBER() over (partition by Category order by BidPrice desc)  as rn
  from test
) a
where a.rn <= 5;
select a.keyword, a.category, a.bidprice
from
  test a,
  (
    select b.id, b.category
    from test b
    where (select distinct(c.category) from test c where b.category=c.category) = b.category
  ) xx
where a.id in (select ss.id from test ss where a.category=ss.category and rownum<=5)
group by a.category, a.bidprice, a.keyword
order by a.category, a.bidprice desc;
select a.keyword, a.category, a.bidprice
from
  test a,
  (
    select b.id, b.category
    from test b
    where (select distinct(c.category) from test c where b.category=c.category) = b.category
    order by b.bidprice desc
  ) xx
where a.id in (
  select id
  from (select dd.id, dd.category from test dd order by dd.bidprice desc) ss
  where a.category=ss.category
    and rownum<=5
)
group by a.category, a.bidprice, a.keyword
order by a.category, a.bidprice desc;