nested select in mysqlite
I am learning sqlite using the example from sqldf, I have come across example 3 about nested select and I don't quite understand the code:
sqldf("select iris.Species '[Species]',
avg(Sepal_Length) '[Avg of SLs > avg SL]'
from iris,
(select Species, avg(Sepal_Length) SLavg
from iris group by Species) SLavg
where iris.Species = SLavg.Species
and Sepal_Length > SLavg
group by iris.Species")
I think the code above is trying to:
- create a table with
[Species]and[Avg of SLs > avg SL]using data 开发者_运维问答fromiris(from line 1-3) - create a table named
SLavgwith Species and average Sepal_Length for each Species fromiris(from line 4-6)
I wonder why SLavg is present in line 4-5 for 2 times, I tried removing SLavg and sqldf gives me statement: no such column: SLavg and statement: no such column: SLavg.Species.
The code queries two columns from cross-join of two tables:
select c1,c2 from iris,T2 where .... group by ....
Where T2 is a subquery which is given shorthand 'SLavg':
select d1, d2 from iris group by ...... // subquery
SLavg is used for both subquery name and column name, and that's confusing. Let's give them different names:
select
iris.Species '[Species]', avg(Sepal_Length) '[Avg of SLs > avg SL]'
from
iris,
(select Species, avg(Sepal_Length) AVGcol from iris group by Species) AVGtbl
where
iris.Species = AVGtbl.Species and Sepal_Length > AVGcol
group by
iris.Species
Now it should be more understandable:
- the subquery
AVGtblproduces two columns (Species,AVGcol) - the cross-join of
irisandAVGtblproduces four columns (iris.Species,iris.Sepal_Length,AVGtbl.Species,AVGtbl.AVGcol).
The where clause of top-level select operates on these four.
加载中,请稍侯......
精彩评论