遍历游标 想取 2行或3行 数据

快乐大法师 2014-07-07 10:22:43
一个游标如下
ID productID type
001 1 A
002 2 A
003 2 B
004 3 B
005 4 A
006 5 A
007 5 B

现在想遍历游标 然后判断 同一个 productID

IF type A
。。。。

IF type B
。。。。

IF type A and B
。。。


想了半天 想不明白怎么实现 请大家帮忙 遍历只是一行一行的 但是 如果 A B 都有的情况 就是两行
...全文
204 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
现在 2014-07-08
  • 打赏
  • 举报
回复

。。。。。
SELECT PRODUCTID, SUM(DECODE(TYPE, 'A', 1, 2)) "A=1 B=2 AB=3" FROM T GROUP BY PRODUCTID;

kingkingzhu 2014-07-08
  • 打赏
  • 举报
回复
with t as ( select '001' as id,1 as productid, 'A' as type from dual union select '002' as id,2 as productid, 'A' as type from dual union select '003' as id,2 as productid, 'B' as type from dual union select '004' as id,3 as productid, 'B' as type from dual union select '005' as id,4 as productid, 'A' as type from dual union select '006' as id,5 as productid, 'A' as type from dual union select '007' as id,5 as productid, 'B' as type from dual) select t.id,t.productid,t.type, sum(case when type='A' then 1 else 0 end) over(partition by productid,type) as aorb,--1代表A,0代表B sum(case when type='A' then 1 else 0 end) over(partition by productid) as acount, sum(case when type='B' then 1 else 0 end) over(partition by productid) as bcount from t 依据几个标示 应该可以判定了吧
CT_LXL 2014-07-08
  • 打赏
  • 举报
回复
引用 楼主 kkxxllasasas 的回复:
一个游标如下 ID productID type 001 1 A 002 2 A 003 2 B 004 3 B 005 4 A 006 5 A 007 5 B 现在想遍历游标 然后判断 同一个 productID IF type A 。。。。 IF type B 。。。。 IF type A and B 。。。 想了半天 想不明白怎么实现 请大家帮忙 遍历只是一行一行的 但是 如果 A B 都有的情况 就是两行
把你游标更改一下,让取出来的数据如下: productID a_count b_count 1 1 0 2 1 1 3 0 1 4 1 0 5 1 1
guostong 2014-07-07
  • 打赏
  • 举报
回复
with t as ( select productid, sum( case when type = 'A' then 1 else 0 end ) A, sum( case when type = 'B' the 1 else 0 end ) B from tablename group by productid ) select productid, case when A > 0 and B > 0 then 'A and B' when A > 0 and B = 0 then 'A only' when A = 0 and B > 0 then 'B only' end from t
打开下面链接,直接免费下载资源: https://renmaiwang.cn/s/b3iyn 在Redis中,获取带有特定前缀的key是一项常见的需求,特别是在进数据统计、清理或监控时。传统的`keys`命令虽然能快速找到所有匹配前缀的key,但在大型数据库中,由于其全量扫描的特性,可能导致服务器阻塞。因此,Redis提供了更为安全和高效的`SCAN`命令来解决这个问题。`SCAN`命令是一个增量的遍历工具,它不是一次性返回所有匹配的key,而是每次返回一部分,通过游标(cursor)机制进迭代。例如,`SCAN 0`开始遍历,返回新的游标ID和当前批次的key列表。这种方式避免了阻塞问题,适用于大规模数据集。`SCAN`命令有以下优点:1. 提供键空间的遍历,复杂度为O(1),整体遍历只需要O(N)时间。2. 支持GLOB风格的模式匹配,可以指定匹配规则,如查找所有以特定前缀开头的key。3. 可以设置`COUNT`参数,作为返回数据条数的建议值,但实际返回数量不固定。4. 游标机制使得客户端只需维护一个状态,实现简单。然而,`SCAN`命令也有其不足之处:1. 无法提供完整的快照遍历,如果在遍历过程中数据发生变化,可能会错过某些key。2. 每次返回的数据条数不确定,取决于内部实现。3. 返回的数据可能存在重复,需要在应用层处理重入逻辑。在Python中,我们可以使用`redis`库来方便地操作`SCAN`命令。`scan_iter`函数是一个迭代器,可以用来遍历匹配特定模式的所有key。以下是一个简单的Python脚本实例,用于统计并输出指定前缀的key数量:```python#!/usr/bin/env python# -*- coding: UTF-8 -*-__author__ = "lcl"import sysimport redisimport ospo

17,137

社区成员

发帖
与我相关
我的任务
社区描述
Oracle开发相关技术讨论
社区管理员
  • 开发
  • Lucifer三思而后行
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧