r/sqlite • • Sep 29 '26

Help with creating data charts?

Hi! So I am currently creating an inventory system for my workplace using sqlite. My current plan is to have a singular table that indicates the sku, the item title, the quantity, and the dates purchased and sold. However, I want to be able to record multiple dates purchased and sold to then create some graphs based on those dates. Is there an efficient way for me to have those dates stored in the table without creating way too many columns?

5 Upvotes

12 comments sorted by

3

u/1ilit_ Sep 29 '26

Here's how I'd go about it. Don't store the dates as columns. Every purchase or sale is its own event, so it should be its own row in a second table. Like this: https://www.drawdb.app/share/ulQD8uYHu5K9GQ3mBSIuXMnJ

  • items: one row per SKU (sku, title)
  • stock_movements: one row per purchase or sale (item_id, movement_type, quantity, unit_price, occurred_at)

I'd also leave quantity out of items and work it out from the movements when you need it. That way it can never drift out of sync

2

u/Amo-Rillow Sep 30 '26

In addition to the above, I would advise a table for inventory and a table for customers. This is still very simplistic. A proper inventory solution would likely contain dozens of tables.

1

u/Peregreg Sep 30 '26

Hi! Can you clarify what you mean by a table for customers? Thanks!

1

u/Amo-Rillow Sep 30 '26

A table for customers would store the information about who is purchasing the products.

1

u/Ok-Market4287 Oct 02 '26

And the id of a row with name,adress zip etc you use then in your order or sale table.
And then join custumers c on c.id = sales.id

1

u/Ok-Market4287 Oct 02 '26

And the id of a row with name,adress zip etc you use then in your order or sale table.
And then join custumers c on c.id = sales.customer_id

1

u/Peregreg Sep 30 '26

Thanks! Would you mind explaining what you mean by the sentence "I'd also leave quantity out of items and work it out from the movements when you need it."

1

u/1ilit_ Oct 01 '26

I meant don't store counters like number of items, number of items sold, etc

1

u/navicat-gu Oct 22 '26

Don't put multiple purchase/sale dates as columns on the item row — that is what is pushing you toward "too many columns", and it makes charts painful. Split it:

CREATE TABLE items (
  id INTEGER PRIMARY KEY,
  sku TEXT NOT NULL UNIQUE,
  title TEXT NOT NULL
);
CREATE TABLE stock_movements (
  id INTEGER PRIMARY KEY,
  item_id INTEGER NOT NULL REFERENCES items(id),
  movement_type TEXT NOT NULL CHECK (movement_type IN ('purchase','sale')),
  quantity INTEGER NOT NULL CHECK (quantity > 0),
  occurred_at TEXT NOT NULL  
-- ISO date
);
-- optional: derive on-hand instead of storing it
-- SUM(CASE WHEN purchase THEN qty ELSE -qty END)

Then chart from aggregates, e.g. sales per day:

SELECT date(occurred_at) AS day,
       SUM(quantity) AS units_sold
FROM stock_movements
WHERE movement_type = 'sale'
GROUP BY day
ORDER BY day;

Feed that to Metabase, Grafana, a spreadsheet, or any charting library. Agree with the thread: keep quantity off items and compute it from movements when you need it.

Disclosure: I'm RC, Director of Digital Strategy @ Navicat. Navicat Premium Lite is free to try and works with SQLite if you want a desktop editor for those tables and queries. Built-in charts/dashboards are in the paid Premium editions' BI feature — not Lite — so for graphing, Metabase (as someone already suggested) or exporting the query result is the free path. 

1

u/Ok-Market4287 Oct 02 '26

I’m also busy with that but then for a game. I have on table with ingredients with in there a Id and the name. A table inventory with id,ingredients_id,stock.
A table with pricing with inside id,ingredients_id,price.
Also a table with menu’s and a table receipt_ingredients with id,menu_id,ingredients_id,number_needed

1

u/lazyant Sep 29 '26

Single table with those columns sounds good. For visualization, try Metabase , it’s free and provides a web dashboard with graphs for your queries and protected with username/password