You are a data science expert.Below, you are presented with a database schema and a…
- User
You are a data science expert.Below, you are presented with a database schema and a question. Your task is to read the schema, understand the question, and generate a valid SQLite query to answer the question. /* Database schema: / Create Table
customers(CustomerID, integer, Comment: "identification of the customer", -- PRIMARY KEYSegment, text, Comment: "client segment", Value examples: ['SME', 'LAM', 'KAM'], ) Create Tableyearmonth(CustomerID, integer, Comment: "customer id", -- PRIMARY KEYDate, text, Comment: "date", Value examples: ['201301', '201207', '201304'], -- PRIMARY KEYConsumption, real, Comment: "consumption", ) This schema offers an in-depth description of the database’s architecture, detailing tables, columns, primary keys, foreign keys, and any pertinent information regarding relationships or constraints. Special attention should be given to the examples listed beside each column, as they directly hint at which columns are relevant to our query. / Question: / What was the average monthly consumption of customers in SME for the year 2013? / External Knowledge: */ Average Monthly consumption = AVG(Consumption) / 12; Year 2013 can be presented as Between 201301 And 201312; The first 4 strings of the Date values in the yearmonth table can represent year. */ Please pay attention to the following points during the SQL generation process: 1. Priority should be given to columns that have been explicitly matched with examples relevant to the question’s context and do not output columns not relevant to the question's context. 2. If the question does not explicitly mention including the results obtained from the function in the SELECT fields of the generated SQL, it will be assumed that they should not be included by default. 3. When performing time comparison queries involving date/datetime fields or text fields that store time, use time functions like SUBSTR or STRFTIME or LIKE for conversion before comparison. 4. When using the window function RANK() to sort TOPN samples, the WHERE condition should be set to RANK() calculation result < TOPN instead of <= TOPN. 5. Return the SQL by format:sql xxx. Take a deep breath and think step by step to find the correct SQLite SQL query: - Assistantsql
SELECT AVG(MonthlyAvgConsumption) AS AvgMonthlyConsumption FROM ( SELECT CustomerID, SUM(Consumption) / 12 AS MonthlyAvgConsumption FROM customers JOIN yearmonth ON customers.CustomerID = yearmonth.CustomerID WHERE Segment = 'SME' AND Date BETWEEN '201301' AND '201312' GROUP BY CustomerID ) sub;