按组并根据条件进行筛选

[复制链接]
查看11 | 回复9 | 2015-7-28 15:35:34 | 显示全部楼层 |阅读模式
表如下:
idid_number name type
1 123 testyes
2 123 testyes
3777
aano
4777
aano
5888
aayes
6888
aano
7888
aano
按id_number进行分组,即id_number值一样的为一组数据,分以下三种情况
归类:yes
1 123 testyes
2 123 testyes

归类:no
3777
aano
4777
aano

归类:all
5888
aayes
6888
aano
7888
aano

以上这三种情况 sql 要怎么写啊

回复

使用道具 举报

千问 | 2015-7-28 15:35:34 | 显示全部楼层
---没有明白意思,分组统计么?
我认为是这样
SELECT id_number,max(name),max(type)
FROM表
group by id_number
回复

使用道具 举报

千问 | 2015-7-28 15:35:34 | 显示全部楼层
xingwu12531 发表于 2016-5-10 20:42
---没有明白意思,分组统计么?
我认为是这样
SELECT id_number,max(name),max(type)

说白了,我要得到以下3种数据
若id_number重复,且其对应的type全部为yes,则检索出来
1 123 testyes
2 123 testyes
若id_number重复,且其对应的type全部为no,则检索出来
3777
aano
4777
aano
若id_number重复,且其对应的type即有yes又有no的,则检索出来
5888
aayes
6888
aano
7888
aano
回复

使用道具 举报

千问 | 2015-7-28 15:35:34 | 显示全部楼层
xingwu12531 发表于 2016-5-10 20:42
---没有明白意思,分组统计么?
我认为是这样
SELECT id_number,max(name),max(type)

这个sql 查出来就是全部数据了
idid_number name type
1 123 testyes
2 123 testyes
3777
aano
4777
aano
5888
aayes
6888
aano
7888
aano
我想要3种情况的数据,不懂你明白了没
回复

使用道具 举报

千问 | 2015-7-28 15:35:34 | 显示全部楼层
lian08 发表于 2016-5-10 20:52
这个sql 查出来就是全部数据了
idid_number name type
1 123 testyes

select * from 表 t
WHERE EXISTS (SELECTID_NUMBER

FROM表GG

WHERE T.ID_NUMBER=GG.ID_NUMBER

GROUP BY ID_NUMBER

HAVING COUNT(ID_NUMBER)>=2)
回复

使用道具 举报

千问 | 2015-7-28 15:35:34 | 显示全部楼层
--1
select *
from (select max(decode(type, 'yes', 1, 2)) over(partition by id_number) mm,

t.*
from t)
where mm = 1;
--2
select *
from (select max(decode(type, 'no', 1, 2)) over(partition by id_number) mm,

t.*
from t)
where mm = 1;
--3
select *
from (select count(distinct type) over(partition by id_number) cnt, t.*
from t)
where cnt = 2;

回复

使用道具 举报

千问 | 2015-7-28 15:35:34 | 显示全部楼层
lian08 发表于 2016-5-10 20:50
说白了,我要得到以下3种数据
若id_number重复,且其对应的type全部为yes,则检索出来

全为yes max()=yes and min()=yes
剩下你类推
回复

使用道具 举报

千问 | 2015-7-28 15:35:34 | 显示全部楼层
liuzhe_521 发表于 2016-5-10 21:05
--1
select *
from (select max(decode(type, 'yes', 1, 2)) over(partition by id_number) mm,

哈哈 非常感激啊
回复

使用道具 举报

千问 | 2015-7-28 15:35:34 | 显示全部楼层
xingwu12531 发表于 2016-5-10 21:04
select * from 表 t
WHERE EXISTS (SELECTID_NUMBER

FROM表GG

可能你还是没理解我意思但还是很感谢你哈
回复

使用道具 举报

千问 | 2015-7-28 15:35:34 | 显示全部楼层
lian08 发表于 2016-5-10 21:21
可能你还是没理解我意思但还是很感谢你哈

你的1楼就是需求不明确,3楼才说清楚
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行