【问题标题】:How to query database with python如何使用python查询数据库
【发布时间】:2021-10-15 06:26:56
【问题描述】:

我有以下 PostgreSQL 表:

     cust     prod  day  month  year state  quant
0    Bloom    Pepsi    2     12  2011    NY   4232
1    Bloom    Bread   23      5  2015    PA   4167
2    Bloom    Pepsi   22      1  2016    CT   4404
3    Bloom   Fruits   11      1  2010    NJ   4369
4    Bloom     Milk    7     11  2016    CT    210

我必须找到并显示每个州的 Bloom 平均销售额,并显示如下:

CUST   AVG_NY  AVG_CT AVG_NJ
Bloom  28923   3241   1873

我将数据转换为以下形式:

[('Bloom', 'Pepsi', 2, 12, 2011, 'NY', 4232), ('Bloom', 'Eggs', 30, 11, 2010, 'NJ', 559), ('Bloom', 'Yogurt', 25, 7, 2014, 'PA', 17), ('Bloom', 'Yogurt', 3, 4, 2011, 'NJ', 1203), ('Bloom', 'Coke', 7, 2, 2010, 'NY', 1229), ('Bloom', 'Coke', 6, 10, 2018, 'PA', 2867), ('Bloom', 'Soap', 6, 1, 2015, 'CT', 4623), ('Bloom', 'Milk', 8, 9, 2010, 'NJ', 1106), ('Bloom', 'Milk', 19, 4, 2013, 'NY', 3516), ('Bloom', 'Soap', 7, 6, 
2015, 'PA', 3404)]

下面是我的代码,可能是最糟糕的方法:

connection = psycopg2.connect(user="postgres",
                                  password="ss",
                                  host="127.0.0.1",
                                  port="8800",
                                  database="postgres")
cursor = connection.cursor()
postgreSQL_select_Query = "select * from sales"
cursor.execute(postgreSQL_select_Query)

mobile_records = cursor.fetchall()
def takeSecond(elem):
    return elem[0][0]


mobile_records.sort(key=takeSecond)
Bloom1 = []
for i in mobile_records:
    if i[5] == 'NY' and i[0] == 'Bloom':
        Bloom1.append(i)
s1 = 0
for j in Bloom1:
    s1 += j[6]
avg1 = s1/len(Bloom1)


Bloom2 = []
for i in mobile_records:
    if i[5] == 'CT' and i[0] == 'Bloom':
        Bloom2.append(i)
s2 = 0
for j in Bloom2:
    s2 += j[6]
avg2 = s2/len(Bloom2)


Bloom3 = []
for i in mobile_records:
    if i[5] == 'NJ' and i[0] == 'Bloom':
        Bloom3.append(i)
s3 = 0
for j in Bloom3:
    s3 += j[6]
avg3 = s3/len(Bloom3) 

我什至如何开始实现这一目标?

【问题讨论】:

  • 看看Pandas
  • 您需要每个州的quant 的平均值吗?
  • 为什么不用group by语句查询数据库?
  • @MΛIK 是的,每个州的 quant
  • 我会说检查@MΛIK的答案。我会假设你的意思是前提条件。因此,它只是一个与 GROUP BY 一起使用的简单 WHERE 子句。

标签: python database postgresql list


【解决方案1】:

您应该更深入地了解 SQL。完全没有必要那样做。只需使用 group by 语句即可。

statement = "SELECT state, AVG(quant) FROM sales WHERE cust = Bloom GROUP BY state"

执行后,您可以简单地遍历返回的列表并检查每个状态。

data = cursor.fetchall()
for dataset in data:
    if dataset[0] == 'NJ':
        # do something with dataset[1]

注意:dataset[0] 存储状态,dataset[1] 存储平均值。

【讨论】:

  • 因此,我只能使用 Python 获取这些数据,并且 select * from sales 是我可以在作业中使用的唯一查询。这就是为什么我不能使用 group by 或任何其他 SQL 语句
  • @swombhai 好吧,现在我明白你的问题了!你以前应该这么说的!是否允许选择特定列或只选择所有内容?
  • 一切,我认为我应该为此使用 Pandas。
  • @swombhai Pandas 会让它变得更容易,但使用普通 python 也可以实现。
猜你喜欢
  • 2023-03-23
  • 1970-01-01
  • 2019-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-17
  • 2021-09-17
相关资源
最近更新 更多