p****e 发帖数: 165 | 1 我想要的结果很简单,就是对联系人表按Region分组,然后列出整张表的所有列(
ContactName, Address, City, Region),给的query sample是用self-join,见下:
SELECT DISTINCT c1.ContactName, c1.Address, c1.City, c1.Region
FROM Customers AS c1, Customers AS c2
WHERE c1.Region = c2.Region
AND c1.ContactName <> c2.ContactName
ORDER BY c1.Region, c1.ContactName
但我直觉就不会想到用self-join, 一看分组就想用group by。是不是分组如果不涉及
aggregate,还是用self-join比较好?
想要的结果见附件,就是对region分组,然后显示其他各项。 | g*********r 发帖数: 2847 | 2 not 100% sure what you want, but it seems 'select distinct * FROM Customers
order by region,ContactName ' works | a*****3 发帖数: 601 | 3 难道这位失眠mm 就是为了这个整夜失眠 ?
我老觉得group by确实好用 很直接,但有个重要限制: 在select里面其他字段就不能
直接放进去了. 比如下句在oracle里面就会报错.
select region, name
from view1
group by region
但在sas里面没问题. 其他数据库没试过, teradata也不行.
这个时候就得用subquery绕出来.
嗨 其实大家都懂的.
【在 p****e 的大作中提到】 : 我想要的结果很简单,就是对联系人表按Region分组,然后列出整张表的所有列( : ContactName, Address, City, Region),给的query sample是用self-join,见下: : SELECT DISTINCT c1.ContactName, c1.Address, c1.City, c1.Region : FROM Customers AS c1, Customers AS c2 : WHERE c1.Region = c2.Region : AND c1.ContactName <> c2.ContactName : ORDER BY c1.Region, c1.ContactName : 但我直觉就不会想到用self-join, 一看分组就想用group by。是不是分组如果不涉及 : aggregate,还是用self-join比较好? : 想要的结果见附件,就是对region分组,然后显示其他各项。
| p****e 发帖数: 165 | 4 哇,原来这么简单,我是在网上看一个SQL tutorial,其中self-join是如我贴中的那个
query那样用的,按region分组,居然得self-join,所以我就搬上来问一问。
Customers
【在 g*********r 的大作中提到】 : not 100% sure what you want, but it seems 'select distinct * FROM Customers : order by region,ContactName ' works
| l******n 发帖数: 9344 | 5 把name也放到group by里边,就完了
【在 a*****3 的大作中提到】 : 难道这位失眠mm 就是为了这个整夜失眠 ? : 我老觉得group by确实好用 很直接,但有个重要限制: 在select里面其他字段就不能 : 直接放进去了. 比如下句在oracle里面就会报错. : select region, name : from view1 : group by region : 但在sas里面没问题. 其他数据库没试过, teradata也不行. : 这个时候就得用subquery绕出来. : 嗨 其实大家都懂的.
| p****e 发帖数: 165 | 6 但我如果要select全部(全部列包括region, name, city, address),然后只group by
region and name就不行
难道要select * from customers
group by region, name, city, address?
【在 l******n 的大作中提到】 : 把name也放到group by里边,就完了
| g*********r 发帖数: 2847 | 7
by
right. and it is the same as select distinct *
【在 p****e 的大作中提到】 : 但我如果要select全部(全部列包括region, name, city, address),然后只group by : region and name就不行 : 难道要select * from customers : group by region, name, city, address?
| p********r 发帖数: 1465 | 8 你用了group by了就是在region level上显示表了,那些人名、地址之类的怎么可能并
到一起呢,自然会报错了
可以 select count(name) as name,
或者用min()、max()这样的限制 | g****8 发帖数: 2828 | 9 I think LZ's problem is a simple order problem .
No need for group by.
The purpose of using group by is not she thought of . | g****8 发帖数: 2828 | 10 agree, I think when you need any calculation in sql by group, then group by
will be used.
【在 p********r 的大作中提到】 : 你用了group by了就是在region level上显示表了,那些人名、地址之类的怎么可能并 : 到一起呢,自然会报错了 : 可以 select count(name) as name, : 或者用min()、max()这样的限制
| p****e 发帖数: 165 | 11 很对,我这个问题确实只是一个order by的问题
【在 g****8 的大作中提到】 : I think LZ's problem is a simple order problem . : No need for group by. : The purpose of using group by is not she thought of .
| p********r 发帖数: 1465 | 12 哦,你一说我再一看主贴。LZ自己表述不清楚
by
【在 g****8 的大作中提到】 : agree, I think when you need any calculation in sql by group, then group by : will be used.
| s*****n 发帖数: 2174 | 13 你这不叫按Region分组。
GROUP BY region的话, 结果里面每个不同的region只会有一行。
你这个同一个region在结果里面还有不同的行, 根本不是
GROUP BY要做的事情。
【在 p****e 的大作中提到】 : 我想要的结果很简单,就是对联系人表按Region分组,然后列出整张表的所有列( : ContactName, Address, City, Region),给的query sample是用self-join,见下: : SELECT DISTINCT c1.ContactName, c1.Address, c1.City, c1.Region : FROM Customers AS c1, Customers AS c2 : WHERE c1.Region = c2.Region : AND c1.ContactName <> c2.ContactName : ORDER BY c1.Region, c1.ContactName : 但我直觉就不会想到用self-join, 一看分组就想用group by。是不是分组如果不涉及 : aggregate,还是用self-join比较好? : 想要的结果见附件,就是对region分组,然后显示其他各项。
|
|