Redian新闻
>
请教关于下面这个sql code地解释
avatar
请教关于下面这个sql code地解释# Database - 数据库
i*g
1
从网上看到这个关于select top N rows from each group的例子:
Let’s say I want to select the two cheapest fruits from each type. Here’s
a first try:
+--------+----------+-------+
| type | variety | price |
+--------+----------+-------+
| apple | gala | 2.79 |
| apple | fuji | 0.24 |
| orange | valencia | 3.59 |
| orange | navel | 9.36 |
| pear | bradford | 6.05 |
| pear | bartlett | 2.14 |
| cherry | bing | 2.55 |
| cherry | chelan | 6.33 |
+--------+----------+-------+
给出的sol
avatar
B*g
2
???
f 是水果表

s

【在 i********g 的大作中提到】
: 从网上看到这个关于select top N rows from each group的例子:
: Let’s say I want to select the two cheapest fruits from each type. Here’s
: a first try:
: +--------+----------+-------+
: | type | variety | price |
: +--------+----------+-------+
: | apple | gala | 2.79 |
: | apple | fuji | 0.24 |
: | orange | valencia | 3.59 |
: | orange | navel | 9.36 |

avatar
i*g
3
我刚改了原文,hehe,不过还是看不明白那个程序

【在 B*****g 的大作中提到】
: ???
: f 是水果表
:
: s

avatar
B*g
4
google “Subqueries”

【在 i********g 的大作中提到】
: 我刚改了原文,hehe,不过还是看不明白那个程序
avatar
C*r
5
这种回复跟放屁一样

【在 B*****g 的大作中提到】
: google “Subqueries”
avatar
B*g
6
不一样,俺这个还有点用

【在 C**********r 的大作中提到】
: 这种回复跟放屁一样
avatar
k*s
7
where f.type = fruits.type and f.price < fruits.price
fruits.type 和 fruits.price 指向的外层的fruit表,内层的fruit表已经被alias了
。所以单独run里面的sql会报错。
avatar
x*e
8
This is a very difficult subquery.
It is hard to understand it even after you have been dealing with SQL for
years.
You can start with join/outerjoin and union first.
And try to rewrite join queries to subqueires and vice versa.
After a while, this one won't be that difficult to understand.

s

【在 i********g 的大作中提到】
: 从网上看到这个关于select top N rows from each group的例子:
: Let’s say I want to select the two cheapest fruits from each type. Here’s
: a first try:
: +--------+----------+-------+
: | type | variety | price |
: +--------+----------+-------+
: | apple | gala | 2.79 |
: | apple | fuji | 0.24 |
: | orange | valencia | 3.59 |
: | orange | navel | 9.36 |

avatar
b*e
9
You will get three cheapest fruits if you use<=2.
You can understand it in this way. "Select" read database row by row. For
every row of data, it will get the logic true/false result in the "where"
clause. If true then output.
Hope this is helpful.

s

【在 i********g 的大作中提到】
: 从网上看到这个关于select top N rows from each group的例子:
: Let’s say I want to select the two cheapest fruits from each type. Here’s
: a first try:
: +--------+----------+-------+
: | type | variety | price |
: +--------+----------+-------+
: | apple | gala | 2.79 |
: | apple | fuji | 0.24 |
: | orange | valencia | 3.59 |
: | orange | navel | 9.36 |

avatar
b*e
10
Here is another example of such kind of subquary.
if object_id('##Table1')<>0 drop table ##Table1
Create table ##Table1 (
[Record Value] varchar(20), [Record Date] smalldatetime
)
go
insert into ##Table1 values ('Record 1','07/21/2003')
insert into ##Table1 values ('Record 2','07/22/2003')
insert into ##Table1 values ('Record 3','07/23/2003')
insert into ##Table1 values ('Record 4','07/24/2003')
insert into ##Table1 values ('Record 5','07/25/2003')
insert into ##Table1 values ('Record 6',
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。