sql多条件查询

Lamcky 2018-07-14 04:56:09
各位大神求助,如类似下面数据表x,其中,1个id可能有多行相同的att,如id = 1,att=a就有两行观测值
希望查出指定att下去重id数,如a开头的att的id去重数,b开头的att的id去重数,att为c的id去重数。

单个查询比较简单,如:
select distinct(id) from x where att like 'a%';
select distinct(id) from x where att = 'c';

但是有没有办法一次过查询得出结果?

由于没有建表全县,表x数据量跑起来比较大,因此不能用以下union的方法,会每次都查询一次x:
select distinct(id) from x where att like 'a%'
union
select distinct(id) from x where att = 'c';

如果只是查次数,可以用以下方法,但是涉及id去重就不会了:
select
count(case when x.att like 'a%' then 1 end) a
count(case when x.att = 'c' then 1 end)c
from x;

表x如下:
id att
1 a
1 b
1 c
1 d
1 a
1 c
2 bd
2 cd
2 b
2 c
2 e
2 f
2 c
3 bd
3 a
3 c
3 d
3 v
3 a
3 cf
...全文
231 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
shinger126 2018-07-16
  • 打赏
  • 举报
回复
select id, left(att,1) att,count(*) cts from x group by id,left(att,1)
Lamcky 2018-07-15
  • 打赏
  • 举报
回复
引用 7 楼 hyqsweety 的回复:
是要统计按att 首字母,id分组结果的数据嘛?

select att_pre,count(distinct id) as count_id
from (
select id,left(att,1) as att_pre
from x
) a
group by att_pre


比这个复杂,示例数据比较简单,需求有可能是a开头、ab开头、等于ab之类的~
Lamcky 2018-07-15
  • 打赏
  • 举报
回复
想到一个比较笨的方法,回头试试是否可行:

select
count(distinct b_start) as b_start_id,
count(distinct b) as b_id
from
(select x.*,
case ( when x.att like 'b%' then id end ) 'b_start',
case ( when x.att = 'bd' then id end ) 'b'
from x)
hyqsweety 2018-07-15
  • 打赏
  • 举报
回复
是要统计按att 首字母,id分组结果的数据嘛?

select att_pre,count(distinct id) as count_id
from (
select id,left(att,1) as att_pre
from x
) a
group by att_pre
吉普赛的歌 2018-07-15
  • 打赏
  • 举报
回复
其实你想表达的两方面都有:
1. id 去重你不会
2. 希望提高效率

USE tempdb
GO
IF OBJECT_ID('x') IS NOT NULL DROP TABLE x
GO
CREATE TABLE x(
id INT,
att VARCHAR(50)
)
INSERT INTO x
SELECT 1,'a'
union all SELECT 1,'b'
union all select 1,'c'
union all SELECT 1,'d'
union all SELECT 1,'a'
union all SELECT 1,'c'
union all SELECT 2,'bd'
union all SELECT 2,'cd'
union all SELECT 2,'b'
union all SELECT 2,'c'
union all select 2,'e'
union all SELECT 2,'f'
union all SELECT 2,'c'
union all SELECT 3,'bd'
union all SELECT 3,'a'
union all SELECT 3,'c'
union all SELECT 3,'d'
union all SELECT 3,'v'
union all SELECT 3,'a'
union all SELECT 3,'cf'
GO
--创建聚集索引, 提高效率
CREATE CLUSTERED INDEX ix_x_att ON x(att)

--查询
SELECT
--count(case when x.att like 'a%' then 1 end) a, --你原来的写法
--count(case when x.att = 'c' then 1 end) c, --你原来的写法
count(DISTINCT case when x.att like 'a%' then id end) AS a_id_distinct,
count(DISTINCT case when x.att = 'c' then id end) AS c_id_distinct
from x;
/*
a_id_distinct c_id_distinct
2 3
*/
Lamcky 2018-07-15
  • 打赏
  • 举报
回复
引用 5 楼 yenange 的回复:
其实你想表达的两方面都有:
1. id 去重你不会
2. 希望提高效率

USE tempdb
GO
IF OBJECT_ID('x') IS NOT NULL DROP TABLE x
GO
CREATE TABLE x(
id INT,
att VARCHAR(50)
)
INSERT INTO x
SELECT 1,'a'
union all SELECT 1,'b'
union all select 1,'c'
union all SELECT 1,'d'
union all SELECT 1,'a'
union all SELECT 1,'c'
union all SELECT 2,'bd'
union all SELECT 2,'cd'
union all SELECT 2,'b'
union all SELECT 2,'c'
union all select 2,'e'
union all SELECT 2,'f'
union all SELECT 2,'c'
union all SELECT 3,'bd'
union all SELECT 3,'a'
union all SELECT 3,'c'
union all SELECT 3,'d'
union all SELECT 3,'v'
union all SELECT 3,'a'
union all SELECT 3,'cf'
GO
--创建聚集索引, 提高效率
CREATE CLUSTERED INDEX ix_x_att ON x(att)

--查询
SELECT
--count(case when x.att like 'a%' then 1 end) a, --你原来的写法
--count(case when x.att = 'c' then 1 end) c, --你原来的写法
count(DISTINCT case when x.att like 'a%' then id end) AS a_id_distinct,
count(DISTINCT case when x.att = 'c' then id end) AS c_id_distinct
from x;
/*
a_id_distinct c_id_distinct
2 3
*/


谢谢!id去重get!
但是我用的impala跑,显示不允许参数不同
也就是说如果要同时得到 x.att like 'b%'和 x.att = 'bd' 的话,会报错,不知道有没有办法得到如下结果:
att_name count_id
b% 3
bd 2
Lamcky 2018-07-15
  • 打赏
  • 举报
回复
引用 2 楼 yenange 的回复:
楼上版主的可以。
不过, 你到底是要换写法, 还是要提高 效率 ?


换个写法来提高效率,题目没有表述清楚哈~
Lamcky 2018-07-15
  • 打赏
  • 举报
回复
引用 1 楼 wmxcn2000 的回复:
-- 是要这个结果吗 ?
select distinct(id) from x where att like 'a%' or att = 'c';


不是汇总,要分别的人数,即like 'a%'的是多少,等于‘c’的是多少,而非 or~
吉普赛的歌 2018-07-14
  • 打赏
  • 举报
回复
楼上版主的可以。
不过, 你到底是要换写法, 还是要提高 效率 ?
卖水果的net 2018-07-14
  • 打赏
  • 举报
回复
-- 是要这个结果吗 ?
select distinct(id) from x where att like 'a%' or att = 'c';

22,207

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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