大家。 我有一张这样的桌子:
cal:Message.Attach="[Event MouseDoubleClick] = [Action Launch]"
(我使用'限制'以及' WHERE'子句以避免打印出太多记录,表格很长。)
现在我希望在指定的时间窗口之间获得平均温度等级。
我可以通过以下方式生成临时表来获得结果:
1,创建一个临时表:
appcfg.py download_app -A <your_app_id> -V <your_app_version> <output_dir>
2, 从临时表s61获得排名:
sqlite> select * from dayobs where year>=2015 and month=4 and days>=29 and station< 53333 limit 100;
station year month days rain ave_temp min_temp max_temp sunshine date_ymd
---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- -----------------
53111 2015 4 29 0.0 212.0 96.0 315.0 121 04/29/15 00:00:00
53111 2015 4 30 4.0 181.0 124.0 264.0 48 04/30/15 00:00:00
53111 2016 4 29 0.0 201.0 94.0 302.0 120 04/29/16 00:00:00
53111 2016 4 30 0.0 187.0 107.0 263.0 41 04/30/16 00:00:00
53222 2015 4 29 0.0 198.0 118.0 277.0 122 04/29/15 00:00:00
53222 2015 4 30 17.0 141.0 86.0 225.0 2 04/30/15 00:00:00
53222 2016 4 29 0.0 193.0 110.0 274.0 114 04/29/16 00:00:00
53222 2016 4 30 0.0 190.0 122.0 256.0 78 04/30/16 00:00:00
这正是我想要的。
但我的问题是,如何在没有此临时表的情况下获得此功能?由于此版本的sqlite(版本3.8.2 2013-12-06)不支持&#39; #39;条款。并使用复合选择将导致错误:
sqlite> create table s61 as
...> select station,year,avg(ave_temp)/10 as temp from dayobs where month=4 and days>29 and year>2013 and station<53333 group by station, year;
sqlite> select * from s61;
station year temp
---------- ---------- ----------
53111 2014 17.7
53111 2015 18.1
53111 2016 18.7
53222 2014 16.1
53222 2015 14.1
53222 2016 19.0
我不想创建临时表并且每次都丢弃它。 我也试图使用&#39; HAVING&#39;,但这不是我所期望的:
sqlite> select station,year,
...> temp,
...> (select count()+1 from s61 as r where r.temp<s61.temp and s61.station==r.station) as rank_t
...> from s61
...> where year==2015 group by station order by station;
station year temp rank_t
---------- ---------- ---------- ----------
53111 2015 18.1 2
53222 2015 14.1 1
似乎sqlite不能很好地使用SELECT结果作为数据源。任何类似这样的尝试都会导致滥用聚合&#39;错误:
选择.... count()... from(select ....)as a_table;
任何人都可以帮助我?
答案 0 :(得分:0)
方法1:
将此版本的sqlite3更新为最新版本,即新版本 有 WITH 子句。但在我尝试更新sqlite3之前,我想我可以给方法2一个tyr:
方法2:
也许它有点棘手但有效:
- ex5.sql:
select c.station,c.year,c.temp,count(s.station) as rank_t
from
(select station,year,avg(ave_temp)/10 as temp from dayobs where station<53333 and month=5 and year=2016 and days>=1 and days<=15 group by station,year) as c
join
(select station,year,avg(ave_temp)/10 as temp from dayobs where station<53333 and month=5 and year>=2010 and days>=1 and days<=15 group by station,year) as s
on s.station==c.station
where s.temp>=c.temp group by s.station;
select station,year,avg(ave_temp)/10 as temp from dayobs where station<53333 and month=5 and year>=2010 and days>=1 and days<=15 group by station,year;
你可以看到结果与我的期望完全一致:
sqlite> .read ex5.sql
station year temp rank_t
---------- ---------- ---------- ----------
53111 2016 15.74 5
53222 2016 13.3133333 6
station year temp
---------- ---------- ----------
53111 2010 17.28
53111 2011 15.6933333
53111 2012 18.5733333
53111 2013 18.2333333
53111 2014 14.3733333
53111 2015 16.4933333
53111 2016 15.74
53222 2010 15.1
53222 2011 13.6133333
53222 2012 15.3133333
53222 2013 15.8333333
53222 2014 12.4666666
53222 2015 14.72
53222 2016 13.3133333
此方法避免在REFERENCE表的选择结果上使用COUNT()。