基于sqlcoder_7b生成sql语句
2023-11-24 本文已影响0人
梅西爱骑车
prompt英文版本如下:
### Task
Generate a SQL query to answer the following question:
`What is our total revenue by product in the last week?`
### Database Schema
This query will run on a database whose schema is represented in this string:
CREATE TABLE products (
product_id INTEGER PRIMARY KEY, -- Unique ID for each product
name VARCHAR(50), -- Name of the product
price DECIMAL(10,2), -- Price of each unit of the product
quantity INTEGER -- Current quantity in stock
);
CREATE TABLE customers (
customer_id INTEGER PRIMARY KEY, -- Unique ID for each customer
name VARCHAR(50), -- Name of the customer
address VARCHAR(100) -- Mailing address of the customer
);
CREATE TABLE salespeople (
salesperson_id INTEGER PRIMARY KEY, -- Unique ID for each salesperson
name VARCHAR(50), -- Name of the salesperson
region VARCHAR(50) -- Geographic sales region
);
CREATE TABLE sales (
sale_id INTEGER PRIMARY KEY, -- Unique ID for each sale
product_id INTEGER, -- ID of product sold
customer_id INTEGER, -- ID of customer who made purchase
salesperson_id INTEGER, -- ID of salesperson who made the sale
sale_date DATE, -- Date the sale occurred
quantity INTEGER -- Quantity of product sold
);
CREATE TABLE product_suppliers (
supplier_id INTEGER PRIMARY KEY, -- Unique ID for each supplier
product_id INTEGER, -- Product ID supplied
supply_price DECIMAL(10,2) -- Unit price charged by supplier
);
-- sales.product_id can be joined with products.product_id
-- sales.customer_id can be joined with customers.customer_id
-- sales.salesperson_id can be joined with salespeople.salesperson_id
-- product_suppliers.product_id can be joined with products.product_id
### SQL
Given the database schema, here is the SQL query that answers `What is our total revenue by product in the last week?`:
```sql
prompt中文版本如下:
### Task
生成一个SQL来回答如下问题:
`上一周按照产品分组查询各个产品的总计收入是多少?`
### Database Schema
查询语句基于如下使用字符串描述的数据库:
CREATE TABLE products (
product_id INTEGER PRIMARY KEY, -- 每个产品的唯一ID
name VARCHAR(50), -- 产品名称
price DECIMAL(10,2), -- 产品价格
quantity INTEGER -- 当前库存量
);
CREATE TABLE customers (
customer_id INTEGER PRIMARY KEY, -- 每个客户端唯一ID
name VARCHAR(50), -- 客户姓名
address VARCHAR(100) -- 客户端email地址
);
CREATE TABLE salespeople (
salesperson_id INTEGER PRIMARY KEY, -- 每个销售人员ID
name VARCHAR(50), -- 销售人员姓名
region VARCHAR(50) -- 销售人员的地理销售区域,也就是销售员的负责区域
);
CREATE TABLE sales (
sale_id INTEGER PRIMARY KEY, -- 每笔销售订单的ID
product_id INTEGER, -- 售出的商品ID
customer_id INTEGER, -- 购买的客户ID
salesperson_id INTEGER, -- 销售人员ID
sale_date DATE, -- 销售日期
quantity INTEGER -- 产品的售出数量
);
CREATE TABLE product_suppliers (
supplier_id INTEGER PRIMARY KEY, -- 供应商ID
product_id INTEGER, -- 供应商的产品ID
supply_price DECIMAL(10,2) -- 供应商的单价
);
-- sales.product_id 可以和 products.product_id进行连接
-- sales.customer_id 可以和customers.customer_id进行连接
-- sales.salesperson_id 可以和 salespeople.salesperson_id进行连接
-- product_suppliers.product_id 可以和 products.product_id进行连接
### SQL
通过以上给定的数据库描述,我再重复一遍编写SQL查询语句来回答 `上一周按照产品分组查询各个产品的总计收入是多少?`:
```sql
默认为语法是 PostgreSQL ,输出如下SQL查询语句:
SELECT p.name, SUM(s.quantity * ps.supply_price) AS total_revenue FROM products p JOIN sales s ON p.product_id = s.product_id JOIN product_suppliers ps ON p.product_id = ps.product_id WHERE s.sale_date >= (CURRENT_DATE - interval '1 week') GROUP BY p.name;
我自己格式化后的效果:
SELECT
p.name,
SUM(s.quantity * ps.supply_price) AS total_revenue
FROM
products p
JOIN
sales s
ON
p.product_id = s.product_id
JOIN
product_suppliers ps
ON
p.product_id = ps.product_id
WHERE
s.sale_date >= (CURRENT_DATE - INTERVAL '1 week')
GROUP BY
p.name;
我让它生成MySQL语法的他不会,还是输出PGsql的语句,可以自己找sql翻译工具进行转换,相信通过不断增加MySQL的训练数据进行训练,它支持MySQL语法指日可待。