Predict Store Sales Data With MindsDB using Machine Learning and SQL
Tags: sql, tutorial, mindsdb, hacktoberfest2022
Published: 2022-10-13 16:07:27


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3kcxb3s7walryp46zmtc.png)## Introduction
MindsDB is the next step we beginners can take towards learning data science and machine learning. MindsDB has embedded their software with AI and Machine Learning in order to assist us with recognizing the patterns of data, predict trends and train machine learning model in minutes.

In this tutorial, We will explore how we can use MindsDB Cloud to train a Time Series Model and predict the Store Sales Data. You can download the dataset which we will use in this tutorial [here](https://www.kaggle.com/datasets/laurinbrechter/store-sales-data?select=Store+632.csv) (The file name is Store 632.csv).

## Before We Start!!
If you don't have a MindsDB Cloud yet, you can create a free trial account [here](https://cloud.mindsdb.com/register).

Once you're logged in, you will be directed to MindsDB Cloud Editor.

## Adding Data to the Cloud
**Step 1:** On the top left corner of your screen, you will find 2 buttons 'Query' and 'Add Data'. Click the `Add Data` in order to import data into your **MindsDB Cloud Editor**

**Step 2:** In the **Datasource** page select `Files` and choose `Import File`.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nz35ivdyzyxojv2vv26n.png)


**Step 3:** Select the file you want to import and give a `Table name`. Once done, click `Save and Continue`.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zr6bi51lahh7l018n7qg.png)


**Step 4:** Once the file is uploaded, you will see 2 queries in the MindsDB Cloud Editor and execute them to see if the is created with proper data.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8vm6p8vdg9wyudysmt0c.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a8rnuyrxqpxyv4b4zd2m.png)

## Training a Predictor Model

Training a Predictor Model has never been easier, with MindsDB you just need basic SQL knowledge to get the job done.

**Step 1:** Use `CREATE PREDICTOR` syntax, and specify what query to train `FROM` and what to `PREDICT`. Below is a template.

```
CREATE PREDICTOR mindsdb.[predictor_name]
FROM [integration_name]
    (SELECT [sequential_column], [partition_column], [other_column], [target_column] FROM [table_name])
PREDICT [target_column]

ORDER BY [sequential_column]
GROUP BY [partition_column]

WINDOW [int]
HORIZON [int]

```
Now let plugin the parameters into the query and execute it. After execution, we will get a message whether it was successful or not.

```
CREATE PREDICTOR mindsdb.nest
FROM files.stores_sales
    (SELECT * FROM files.store_sales)
PREDICT `Dollar Sales`
ORDER BY Date
GROUP BY SKU
WINDOW 20
HORIZON 7;
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xol6t6cqtdjsnm3vtfyc.png)


**Step 2:** If your dataset is large, the Predictor may take a while to train. While it is training, you can check it's status by running this query.

```
SELECT * 
FROM mindsdb.predictors
WHERE name='[predictor_name]'
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9x4v2g09xe348thntwvm.png)


## Making Predictions
We can make predictions by simply treating it as a query.

```
SELECT `Dollar Sales`,
        `Dollar Sales_explain`
From mindsdb.nexte
WHERE `Unit Sales` = 2;
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i511ohxlh8o8a5od5ww0.png)


You can also make a bulk prediction by join the table with your model.

```
SELECT t.`Dollar Sales` as real_p,
        m.`Dollar Sales` as predicted_p,
        t.`Unit Sales`, t.Date, t.SKU
FROM files.store_sales as t
JOIN mindsdb.example_model as m limit 100;
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bbyqxdi00zzp9rrrut2t.png)

## Conclusion
Congratulation, you have just trained your own `Time Series Predictive Model`. To recap our tutorial, we setup our own MindsDB Cloud account, uploaded our datasets, created and trained our predictive model and make predictions with our uploaded data.

Now, I would recommend you to test out other datasets with MindsDB and improve your machine learning and data science skill.''

Remember, Always to have fun coding!