转载

ChatGPT插件例子 SQL翻译

将自然语言翻译成 SQL 查询。

提问

### Postgres SQL tables, with their properties: 

# # Employee(id, name, department_id) 

# Department(id, name, address) 

# Salary_Payments(id, employee_id, amount, date) 

### A query to list the names of the departments which employed more than 10 employees in the last 3 months SELECT


回答

SELECT d.name

 FROM Department d

 INNER JOIN Employee e ON d.id = e.department_id

 INNER JOIN Salary_Payments sp ON e.id = sp.employee_id

 WHERE sp.date > NOW() - INTERVAL '3 months'

 GROUP BY d.name

 HAVING COUNT(*) > 10

API调用

import os
import openai

openai.api_key = os.getenv("OPENAI_API_KEY")

response = openai.Completion.create(
  model="text-davinci-003",
  prompt="### Postgres SQL tables, with their properties:\n#\n# Employee(id, name, department_id)\n# Department(id, name, address)\n# Salary_Payments(id, employee_id, amount, date)\n#\n### A query to list the names of the departments which employed more than 10 employees in the last 3 months\nSELECT",
  temperature=0,
  max_tokens=150,
  top_p=1.0,
  frequency_penalty=0.0,
  presence_penalty=0.0,
  stop=["#", ";"]
)