having子句中如何有'distinct'
How to have 'distinct' in having clause
编辑:这是一个示例关系!我需要它处理更大的关系,所以没有解决方法!
所以我得到了一个简单的任务,起初我没有看到可能有什么问题,现在我只是不明白为什么它不起作用。
假设我有 table 个人和他们的朋友,我想 select 有 2 个或更多朋友的人。
人
------------------------------
|person | friend | relation |
|-----------------------------
|ana | jon | friend |
|ana | jon | lover |
|ana | phillip| friend |
|ana | kiki | friend |
|mary | jannet | friend |
|mary | jannet | lover |
|peter | july | friend |
我想做一个
SELECT person FROM people GROUP BY person HAVING count(distinct friend) > 1;
并得到
-------
| ana |
-------
但是在 HAVING 子句中使用“distinct
”时出现语法错误。
我知道“distinct
”是投影子句的一部分,但是
如何让“count
”只计算不同的条目而不需要额外的子查询或其他东西?
编辑:我能想到的最好的是:
SELECT tmp.person FROM (SELECT person, count(distinct friend)
AS numfriends FROM people GROUP BY person) AS tmp
WHERE tmp.numfriends > 1;
你需要这样写
SELECT person
FROM people
WHERE relation = 'friend'
GROUP BY person
HAVING count(*) > 1;
只需检查一下即可。
declare @t table(person varchar(50), Friend VARCHAR(50), relation VARCHAR(50))
INSERT INTO @T VALUES('ana', 'jon','friend')
,('ana', 'jon','lover')
,('ana', 'phillip','friend')
,('ana', 'kiki','friend')
,('mary', 'jannat','friend')
,('mary', 'jannat','lover')
SELECT DISTINCT PERSON FROM
(
SELECT person FROM @t GROUP BY person HAVING count(friend) > 1
) a
来自文档
http://www-01.ibm.com/support/knowledgecenter/SSGU8G_12.1.0/com.ibm.sqls.doc/ids_sqs_0162.htm
The condition in the HAVING clause cannot include a DISTINCT or UNIQUE
aggregate expression.
一种变通方法是让 select
中的计数不同
SELECT
person,
count(distinct friend) as f_count
FROM people
GROUP BY person
HAVING f_count > 1;
更新:
查了资料,发现了事实
The HAVING clause is evaluated before the SELECT - so the server
doesn't yet know about that alias.
所以要实现目标,可以这样做
select
person,
f_count
from(
SELECT
person,
count(distinct friend) as f_count
FROM people
GROUP BY person
)x
where f_count > 1
只是通过使用更多分组依据的替代方法 -
select cust_xref_id
from(
select cust_xref_id,cm11
from temp_nippon_cust
group by cust_xref_id,cm11
) temp
group by cust_xref_id
having count(cm11) > 1
编辑:这是一个示例关系!我需要它处理更大的关系,所以没有解决方法!
所以我得到了一个简单的任务,起初我没有看到可能有什么问题,现在我只是不明白为什么它不起作用。
假设我有 table 个人和他们的朋友,我想 select 有 2 个或更多朋友的人。
人
------------------------------
|person | friend | relation |
|-----------------------------
|ana | jon | friend |
|ana | jon | lover |
|ana | phillip| friend |
|ana | kiki | friend |
|mary | jannet | friend |
|mary | jannet | lover |
|peter | july | friend |
我想做一个
SELECT person FROM people GROUP BY person HAVING count(distinct friend) > 1;
并得到
-------
| ana |
-------
但是在 HAVING 子句中使用“distinct
”时出现语法错误。
我知道“distinct
”是投影子句的一部分,但是
如何让“count
”只计算不同的条目而不需要额外的子查询或其他东西?
编辑:我能想到的最好的是:
SELECT tmp.person FROM (SELECT person, count(distinct friend)
AS numfriends FROM people GROUP BY person) AS tmp
WHERE tmp.numfriends > 1;
你需要这样写
SELECT person
FROM people
WHERE relation = 'friend'
GROUP BY person
HAVING count(*) > 1;
只需检查一下即可。
declare @t table(person varchar(50), Friend VARCHAR(50), relation VARCHAR(50))
INSERT INTO @T VALUES('ana', 'jon','friend')
,('ana', 'jon','lover')
,('ana', 'phillip','friend')
,('ana', 'kiki','friend')
,('mary', 'jannat','friend')
,('mary', 'jannat','lover')
SELECT DISTINCT PERSON FROM
(
SELECT person FROM @t GROUP BY person HAVING count(friend) > 1
) a
来自文档
http://www-01.ibm.com/support/knowledgecenter/SSGU8G_12.1.0/com.ibm.sqls.doc/ids_sqs_0162.htm
The condition in the HAVING clause cannot include a DISTINCT or UNIQUE aggregate expression.
一种变通方法是让 select
中的计数不同SELECT
person,
count(distinct friend) as f_count
FROM people
GROUP BY person
HAVING f_count > 1;
更新:
查了资料,发现了事实
The HAVING clause is evaluated before the SELECT - so the server doesn't yet know about that alias.
所以要实现目标,可以这样做
select
person,
f_count
from(
SELECT
person,
count(distinct friend) as f_count
FROM people
GROUP BY person
)x
where f_count > 1
只是通过使用更多分组依据的替代方法 -
select cust_xref_id
from(
select cust_xref_id,cm11
from temp_nippon_cust
group by cust_xref_id,cm11
) temp
group by cust_xref_id
having count(cm11) > 1