zoukankan      html  css  js  c++  java
  • 面试题,找出每个产品的最新五个产品,还有其它方法吗 —— 游标加表变量

    表t_c 和t_p如上图

    select * from t_c
    select * from t_p

    查询方法一:

               select * from (select row_number() over(partition by cid order by cdate desc) as rowid,* from t_p) T where T.rowid<=5

               使用sql2005的特性 row_number() over()

    方法二:

               declare @temp table(id int,cid int,cdate datetime);
           declare @id int;
        declare cur cursor for
           select cid from t_c
        open cur
        fetch next from cur Into @id
        while @@fetch_status = 0
        begin
             insert into @temp
             select top 5 * from t_p where cid = @id order by cdate desc
             fetch next from cur into @id
        end
        close cur
        deallocate cur
        select * from @temp

             利用游标和表变量

  • 相关阅读:
    ARC081F Flip and Rectangles
    LCA
    Tarjan
    2020牛客暑期多校六
    状压DP
    操作系统
    JAVA期末复习
    D. Yet Another Yet Another Task (区间最值)
    构造
    Codeforces Round #641 (Div. 2)
  • 原文地址:https://www.cnblogs.com/ajun/p/2184847.html
Copyright © 2011-2022 走看看