sqlite & postgreSQL

Posted sshcmd学python

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sqlite & postgreSQL相关的知识,希望对你有一定的参考价值。


下载 postgreSQL



cmd  已经安装anaconda
pip3 install psycopg2
以便 jupyter 或者 python3 中 import psycopg2



先复习这点

GROUP BY   &  DISTINCT

The GROUP BY SQL statement allows us to compute summary statistics by “group,” or unique value. When we use this statement, SQL creates a group for each unique value in a column or set of columns (the same values we get when we use the DISTINCT statement), and then does the calculations for them.

When we want to filter on a column generated by a GROUP BY query, we can use the HAVING statement.

SELECT Major_category,AVG(ShareWomen) FROM recent_grads GROUP BY Major_category



SELECT Major_category, AVG(Employed) / AVG(Total) AS share_employed FROM recent_grads GROUP BY Major_category;


HAVING



SELECT Major_category ,AVG(Low_wage_jobs)/AVG(Total) share_low_wage FROM recent_gradsGROUP BY Major_category HAVING share_low_wage >.1


ROUND

SELECT ROUND(ShareWomen, 4),Major_category FROM recent_gradsLIMIT 10



SELECT Major_category ,ROUND((AVG(College_jobs)/AVG(Total)),3)  share_degree_jobs FROM recent_grads GROUP BY Major_category HAVING share_degree_jobs<.3



CAST

SELECT CAST(Women as Float) / CAST(Total as Float) FROM recent_grads limit 5



Subqueries   run first

SELECT Major,Unemployment_rate FROM recent_grads WHERE Unemployment_rate< (SELECT AVG(Unemployment_rate) FROM recent_grads)ORDER BY Unemployment_rate


Subquery in SELECT


SELECT COUNT(*), (SELECT COUNT(*) FROM recent_grads) FROM recent_gradsWHERE ShareWomen > (SELECT AVG(ShareWomen) FROM recent_grads)






SELECT  (CAST(COUNT(*) as float)  /  CAST((SELECT COUNT(*) FROM recent_grads)as float))AS proportion_abv_avgFROM recent_gradsWHERE ShareWomen > (SELECT AVG(ShareWomen) FROM recent_grads);




IN

SELECT Major, Major_category FROM recent_gradsWHERE Major_category IN ('Business', 'Engineering')LIMIT 7;





SELECT Major, Major_category FROM recent_gradsWHERE Major_category IN (select Major_category from recent_gradsgroup by Major_categoryorder by SUM(Total) DESClimit 5);




Integrating a subquery with the outer query

select AVG(           cast(Sample_size as float)/cast(Total as float) ) avg_ratio from recent_grads;
select Major, Major_category, cast(Sample_size as float)/cast(Total as float) ratio from recent_gradswhere ratio > (select AVG(cast(Sample_size as float)/cast(Total as float)) avg_ratiofrom recent_grads);










以上是关于sqlite & postgreSQL的主要内容,如果未能解决你的问题,请参考以下文章

Django:将数据从 SQLite 移动到 PostgreSQL

在新的 Rails 项目中从 SQLite 更改为 PostgreSQL

NodeJS,将来自 MySQL 和 PostgreSQL 的数据存储在 SQLite 中

PostgreSQL (简称gp)小集

Django:将项目从 sqlite 迁移到 PostgreSQL 的最佳实践是啥

如何从 SQLite 迁移到 PostgreSQL (Rails)