SQL Python用户指定查询

2024-05-18 23:41:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用下面的SQL语句来选择存储在数据库中的每个产品的月销售额。根据用户输入选择产品。此查询适用于此表。你知道吗

+------------+----------+-------+-------+-----------+
| orderDate  | Espresso | Latte | Mocha | Cappucino |
+------------+----------+-------+-------+-----------+
| 2019-01-01 |       18 |    20 |    10 |        12 |
| 2019-01-02 |       13 |    11 |    20 |        10 |
| 2019-01-03 |       12 |    14 |    13 |        14 |
| 2019-01-04 |       20 |    13 |    15 |        14 |
| 2019-01-05 |       17 |    18 |    11 |        16 |
+------------+----------+-------+-------+-----------+


import sqlite3
title = input("Enter column to search")

sql = """SELECT SUM(sub.sales) as total_sales,
                strftime("%m-%Y", sub.[orderDate]) as 'month-year'
         FROM
              (SELECT [orderDate], Espresso AS sales, 'Espresso' as item
               FROM groupedSales
               UNION ALL
               SELECT [orderDate], Latte AS sales, 'Latte' as item
               FROM groupedSales
               UNION ALL
               SELECT [orderDate], Mocha AS sales, 'Mocha' as item
               FROM groupedSales
               UNION ALL
               SELECT [orderDate], Cappucino AS sales, 'Cappucino' as item
               FROM groupedSales
               UNION ALL
               SELECT [orderDate], Americano AS sales, 'Americano' as item
               FROM groupedSales
               UNION ALL 
              ) AS sub
         WHERE sub.[item] = ? AND milkOptions = 'Soya'
         GROUP BY strftime("%m-%Y", sub.[orderDate])
     """

conn=sqlite3.connect("system.db")
cur=conn.cursor()
aggregateIndividuals = cur.execute(sql, (title, milkOptions,)).fetchall()

valueArray = []
valueArray2 = []
for values in aggregateIndividuals:
    print(values)

下面是一个典型的输出:

输入列以搜索Espresso

(494,‘2019年1月’)

(440,‘2019年2月’)

(447,‘2019年3月’)

(447,‘2019年4月’)

(452,‘2019年5月’)

(439,‘2019年6月’)

(433,‘2019年7月’)

(482,‘2019年8月’)

(443,‘2019年9月’)

(440,‘2019年10月’)

(441,'11-2019')

(458,‘2019年12月’)

但是,上面的表是测试数据,并不代表客户订单中的实际表的格式。如果product列下没有产品标题,而是产品名称的单元格,那么如何调整查询以从新表中选择每个产品的月销售额。 下面是我要使用此查询的实际表:

+---------+-----------+--------+-------------+------------+----------+-------+------------+
| orderid |  product  |  size  | milkOptions | orderDate  | quantity | price | customerid |
+---------+-----------+--------+-------------+------------+----------+-------+------------+
|       1 | Espresso  | Small  | Soya        | 2019-10-29 |        1 | 1.0   |          1 |
|       2 | Cappucino | Small  | SemiSkimmed | 2019-10-29 |        1 | 1.0   |          1 |
|       3 | Cappucino | Small  | SemiSkimmed | 2019-10-29 |        1 | 1.0   |          1 |
|       4 | Cappucino | Medium | SemiSkimmed | 2019-10-29 |        1 | 1.0   |          1 |
+---------+-----------+--------+-------------+------------+----------+-------+------------+

我正在使用SQlite3的DB Browser。注意:我已经减少了两个数据库上的记录,以尽量少地再现这一点,但是在我实际浏览器的orderDate列中有一个一年中的每一天的记录,并且在我的第二个表中有更多订购不同产品的示例,但是我已经减少了这个示例。你知道吗


Tags: from产品asallitemselectespressounion
1条回答
网友
1楼 · 发布于 2024-05-18 23:41:00

您只是在寻找聚合查询吗?你知道吗

select orderdate, product, sum(quantity * price) as total_sales
from t
group by orderdate
order by orderdate;

相关问题 更多 >

    热门问题