SQL LEFT JOIN not pulling intended data - sqlite

I have the below tables:
CATEGORIES:
id | name | category_group | cate_type_id
1 | Entertainment | Entertainment | 1
2 | Electricity | Utilities | 8
3 | Water | Utilities | 8
4 | Rent | Living Exp | 6
5 | credit card | Finance | 5
BUDGET-ITEMS:
id | budget_id | cat_id | category_group| budget_yr | budget_01 | budget_02 | ... | budget_12
1 | 1 | 1 | Entertainment | 2022 | 500 | |
2 | 1 | 2 | Utilities | 2022 | 1500 | |
3 | 1 | 3 | Utilities | 2022 | | 250 |
I want to pull all items from Category table with mapping budget columns. Below is my JOIN.
SELECT c.id as base_id,c.name,c.category_type_id, c.category_group as base_group, b.*
FROM category c
LEFT JOIN budget_items b ON c.id = b.category_id
WHERE c.category_type_id NOT IN (5)
ORDER BY c.category_type_id, c.category_group ASC
I expect the below output:
id | budget_id | cat_id | catgroup | budget_yr | budget_01 | budget_02 | ... | budget_12
1 | 1 | 1 | Entertainment | 2022 | 500 | |
2 | 1 | 2 | Utilities | 2022 | 1500 | |
3 | 1 | 3 | Utilities | 2022 | | 250 |
4 | 1 | 4 | Living Exp | 2022 | | |
However, I get like below (truncated base* columns here for space):
id | budget_id | cat_id | catgroup | budget_yr | budget_01 | budget_02 | ... | budget_12
1 | 1 | 1 | Entertainment | 2022 | 500 | |
2 | 1 | 2 | Utilities | 2022 | | 1500 |
3 | 1 | 3 | Utilities | 2022 | | |
4 | 1 | 4 | Living Exp | 2022 | | |
My query looks OK, not sure where it is going wrong. Does anyone see the issue?
Thanks in advance for your kind help.
Edit:
I have truncated some columns for space here. the problem is the values are aligned to different budget columns. I get the columns correctly from the left table.
Edit:
Thanks to everyone who pitched in to help, I finally figured the issue was with my data. The query was actually working fine. This community is amazing.

Not sure what you're doing, but it seems to work as expected for me:
Schema (SQLite v3.30)
CREATE TABLE items (
`id` INTEGER,
`budget_id` INTEGER,
`cat_id` INTEGER,
`catgroup` VARCHAR(13),
`budget_yr` INTEGER,
`budget_01` INTEGER,
`budget_02` INTEGER
);
INSERT INTO items
(`id`, `budget_id`, `cat_id`, `catgroup`, `budget_yr`, `budget_01`, `budget_02`)
VALUES
('1', '1', '1', 'Entertainment', '2022', '500', null),
('2', '1', '2', 'Utilities', '2022', '1500', null),
('3', '1', '3', 'Utilities', '2022', null, '250');
CREATE TABLE cats (
`id` INTEGER,
`name` VARCHAR(13),
`category_group` VARCHAR(13),
`cate_type_id` INTEGER
);
INSERT INTO cats
(`id`, `name`, `category_group`, `cate_type_id`)
VALUES
('1', 'Entertainment', 'Entertainment', '1'),
('2', 'Electricity', 'Utilities', '8'),
('3', 'Water', 'Utilities', '8'),
('4', 'Rent', 'Living Exp', '6'),
('5', 'credit card', 'Finance', '5');
Query
SELECT c.id
, budget_id
, cat_id
, catgroup
, budget_yr
, budget_01
, budget_02
FROM cats c
LEFT JOIN items i ON c.id = i.cat_id
WHERE c.cate_type_id <> 5;
id
budget_id
cat_id
catgroup
budget_yr
budget_01
budget_02
1
1
1
Entertainment
2022
500
2
1
2
Utilities
2022
1500
3
1
3
Utilities
2022
250
4
View on DB Fiddle

Related

Sqlite count occurence per year

So let's say I have a table in my Sqlite database with some information about some files, with the following structure:
| id | file format | creation date |
----------------------------------------------------------
| 1 | Word | 2010:02:12 13:31:33+01:00 |
| 2 | PSD | 2021:02:23 15:44:51+01:00 |
| 3 | Word | 2019:02:13 14:18:11+01:00 |
| 4 | Word | 2010:02:12 13:31:20+01:00 |
| 5 | Word | 2003:05:25 18:55:10+02:00 |
| 6 | PSD | 2014:07:20 20:55:58+02:00 |
| 7 | Word | 2014:07:20 21:09:24+02:00 |
| 8 | TIFF | 2011:03:30 11:56:56+02:00 |
| 9 | PSD | 2015:07:15 14:34:36+02:00 |
| 10 | PSD | 2009:08:29 11:25:57+02:00 |
| 11 | Word | 2003:05:25 20:06:18+02:00 |
I would like results that show me a chronology of how many of each file format were created in a given year – something along the lines of this:
|Format| 2003 | 2009 | 2010 | 2011 | 2014 | 2015 | 2019 | 2021 |
----------------------------------------------------------------
| Word | 2 | 0 | 0 | 2 | 0 | 0 | 2 | 0 |
| PSD | 0 | 1 | 0 | 0 | 1 | 1 | 0 | 1 |
| TIFF | 0 | 0 | 0 | 1 | 0 | 0 | 1 | 0 |
I've gotten kinda close (I think) with this, but am stuck:
SELECT
file_format,
COUNT(CASE file_format WHEN creation_date LIKE '%2010%' THEN 1 ELSE 0 END),
COUNT(CASE file_format WHEN creation_date LIKE '%2011%' THEN 1 ELSE 0 END),
COUNT(CASE file_format WHEN creation_date LIKE '%2012%' THEN 1 ELSE 0 END)
FROM
fileinfo
GROUP BY
file_format;
When I do this I am getting unique amounts for each file format, but the same count for every year…
|Format| 2010 | 2011 | 2012 |
-----------------------------
| Word | 4 | 4 | 4 |
| PSD | 1 | 1 | 1 |
| TIFF | 6 | 6 | 6 |
Why am I getting that incorrect tally, and moreover, is there a smarter way of querying that doesn't rely on the year being statically searched for as a string for every single year? If it helps, the column headers and row headers could be switched – doesn't matter to me. Please help a n00b :(
Use SUM() aggregate function for conditional aggregation:
SELECT file_format,
SUM(creation_date LIKE '2010%') AS `2010`,
SUM(creation_date LIKE '2011%') AS `2011`,
..........................................
FROM fileinfo
GROUP BY file_format;
See the demo.

Computing Balance Sheet

With the following query:
select b1.Name DrBook, c1.Name DrControl, b2.Name CrBook, c2.Name CrControl, tn.Amount
from Transactions tn
left join Books b1 on b1.Id = tn.DrBook
left join Books b2 on b2.Id = tn.CrBook
left join ControlLedgers c1 on c1.Id = tn.DrControl
left join ControlLedgers c2 on c2.Id = tn.CrControl
I get this result set for Balance Sheet:
+---------------------+-----------+---------------------+-----------+--------+
| DrBook | DrControl | CrBook | CrControl | Amount |
+---------------------+-----------+---------------------+-----------+--------+
| Current Assets | Cash | Fund | Initial | 100000 |
| Current Assets | Cash | Fund | Initial | 100000 |
| Current Assets | Cash | Fund | Initial | 100000 |
| Current Assets | Cash | Fund | Initial | 100000 |
| Current Assets | Cash | Fund | Initial | 100000 |
| Expenses | Foods | Current Liabilities | Payables | 10000 |
| Current Liabilities | Payables | Current Assets | Cash | 5000 |
+---------------------+-----------+---------------------+-----------+--------+
To present the Balance Sheet in my Application What I'm doing right now is issuing two queries and get two result sets like these:
query1:
select b1.Name DrBook, c1.Name DrControl, SUM(tn.Amount) Amount
from Transactions tn
left join Books b1 on b1.Id = tn.DrBook
left join ControlLedgers c1 on c1.Id = tn.DrControl
group by DrBook, DrControl
result set 1:
+---------------------+-----------+--------+
| DrBook | DrControl | Amount |
+---------------------+-----------+--------+
| Current Assets | Cash | 500000 |
| Expenses | Foods | 10000 |
| Current Liabilities | Payables | 5000 |
+---------------------+-----------+--------+
query 2:
select b1.Name CrBook, c1.Name CrControl, SUM(tn.Amount) Amount
from Transactions tn
left join Books b1 on b1.Id = tn.CrBook
left join ControlLedgers c1 on c1.Id = tn.CrControl
group by CrBook, CrControl
result set 2:
+---------------------+-----------+--------+
| CrBook | CrControl | Amount |
+---------------------+-----------+--------+
| Current Assets | Cash | 5000 |
| Current Liabilities | Payables | 10000 |
| Fund | Initial | 500000 |
+---------------------+-----------+--------+
and subtract result set 2 from result set 1 if it is Assets or Expenses (in this case Current Assets and Expenses) and result set 1 from result set 2 if it is Liabilities, Incomes or Fund (in this case Current Liabilities and Fund) to get final result set like this:
+---------------------+---------------+---------+
| Book | ControlLedger | Balance |
+---------------------+---------------+---------+
| Current Assets | Cash | 495000 |
| Expenses | Food | 10000 |
| Current Liabilities | Payables | 5000 |
| Fund | Initial | 500000 |
+---------------------+---------------+---------+
I've tried some case statement to get the final result set through sql query instead of computing manually in application code BUT those didn't work!
EDIT
Here's the definition for the Table:
CREATE TABLE "Transactions"(
"Id" INTEGER NOT NULL,
"Date" TEXT NOT NULL,
"DrBook" INTEGER NOT NULL,
"CrBook" INTEGER NOT NULL,
"DrControl" INTEGER NOT NULL,
"CrControl" INTEGER NOT NULL,
"DrLedger" INTEGER,
"CrLedger" INTEGER,
"DrSubLedger" INTEGER,
"CrSubLedger" INTEGER,
"DrPartyGroup" INTEGER,
"CrPartyGroup" INTEGER,
"DrParty" INTEGER,
"CrParty" INTEGER,
"DrMember" INTEGER,
"CrMember" INTEGER,
"Amount" INTEGER NOT NULL,
"Narration" TEXT,
FOREIGN KEY("DrBook") REFERENCES "Books"("Id"),
FOREIGN KEY("CrBook") REFERENCES "Books"("Id"),
FOREIGN KEY("DrControl") REFERENCES "ControlLedgers"("Id"),
FOREIGN KEY("CrControl") REFERENCES "ControlLedgers"("Id"),
FOREIGN KEY("DrLedger") REFERENCES "Ledgers"("Id"),
FOREIGN KEY("CrLedger") REFERENCES "Ledgers"("Id"),
FOREIGN KEY("DrSubLedger") REFERENCES "SubLedgers"("Id"),
FOREIGN KEY("CrSubLedger") REFERENCES "SubLedgers"("Id"),
FOREIGN KEY("DrPartyGroup") REFERENCES PartyGroups("Id"),
FOREIGN KEY("CrPartyGroup") REFERENCES PartyGroups("Id"),
FOREIGN KEY("DrParty") REFERENCES "Parties"("Id"),
FOREIGN KEY("CrParty") REFERENCES "Parties"("Id"),
FOREIGN KEY("DrMember") REFERENCES "Members"("Id"),
FOREIGN KEY("CrMember") REFERENCES "Members"("Id")
);
for each Journal I insert one row and it contains both Debit, Credit and Amount Information. I don't have Dr/CrProduct or Dr/CrServices since this has been designed for Individual's and Families' Bookkeeping and Accounting.
For a purchase of food from Mr. A, for example, I pass (1):
Expenses -> Food -> Rice -> Fine Rice A/c Dr. 10000
Current Liabilities -> Payables A/C Cr. 10000
if it's on credit and when the amount of purchase is paid in cash, I pass (2):
Current Liabilities -> Payables A/C Dr. 10000
Current Assets -> Cash -> In Hand -> Emon A/c Cr. 10000
In the table it becomes:
+----+------------+--------+--------+-----------+-----------+----------+----------+-------------+-------------+--------------+--------------+---------+---------+----------+----------+--------+----------------+
| Id | Date | DrBook | CrBook | DrControl | CrControl | DrLedger | CrLedger | DrSubLedger | CrSubLedger | DrPartyGroup | CrPartyGroup | DrParty | CrParty | DrMember | CrMember | Amount | Narration |
+----+------------+--------+--------+-----------+-----------+----------+----------+-------------+-------------+--------------+--------------+---------+---------+----------+----------+--------+----------------+
| 3 | 2020-06-15 | 3 | 5 | 9 | 18 | 2 | | 2 | | | 4 | | 1 | | | 10000 | Some Narration |
| 3 | 2020-06-15 | 5 | 2 | 18 | 7 | | 1 | | 1 | 4 | | 1 | | | | 10000 | |
+----+------------+--------+--------+-----------+-----------+----------+----------+-------------+-------------+--------------+--------------+---------+---------+----------+----------+--------+----------------+
and here's a quick dissection of the first row:
+--------------+----------------+---------------------+
| Columns | Values | Mappings |
+--------------+----------------+---------------------+
| DrBook | 3 | Expenses |
| CrBook | 5 | Current Liabilities |
| DrControl | 9 | Food |
| CrControl | 18 | Payables |
| DrLedger | 2 | Rice |
| DrSubLedger | 2 | Fine Rice |
| CrPartyGroup | 4 | Groceries |
| CrParty | 1 | Mr. A |
| Amount | 10000 | |
| Narration | Some Narration | |
+--------------+----------------+---------------------+
Not sure whether the following is the only way:
with t1(Id, Book, Control, Amount) as (
select tn.DrBook, b1.Name, c1.Name, sum(tn.Amount)
from Transactions tn
left join Books b1 on b1.Id = tn.DrBook
left join ControlLedgers c1 on c1.Id = tn.DrControl
group by DrBook, DrControl
),
t2 as (
select tn.CrBook, b1.Name, c1.Name, -1*sum(tn.Amount)
from Transactions tn
left join Books b1 on b1.Id = tn.CrBook
left join ControlLedgers c1 on c1.Id = tn.CrControl
group by CrBook, CrControl
),
t3 as (
select * from t1 union all select * from t2
)
select Book, Control,
case when Id <=3 then sum(Amount)
else -1*sum(Amount) end Balance
from t3 group by Book, Control order by Id
and it produces exactly what I expected:
+---------------------+----------+---------+
| Book | Control | Balance |
+---------------------+----------+---------+
| Current Assets | Cash | 495000 |
| Current Liabilities | Payables | 5000 |
| Expenses | Foods | 10000 |
| Fund | Initial | 500000 |
+---------------------+----------+---------+

How to select sqlite columns based on a row lookup

I'm trying to compose an SQLite query and I've found a problem that's beyond my skillset. I'm trying to output columns that are based on the rows of another referenced table.
Food_List:
| food_id | name |
|---------|-----------|
| 1 | Apple |
| 2 | Orange |
| 3 | Pear |
Nutrient_Definition:
| nutrient_id | name |
|-------------|-----------|
| 21 | Carbs |
| 22 | Protein |
| 23 | Fat |
Nutrient_Data:
| food_id | nutrient_id | value |
|---------|-------------|-------|
| 1 | 21 | 50 |
| 1 | 22 | 24 |
| 1 | 23 | 63 |
| 2 | 22 | 12 |
| 2 | 23 | 95 |
| 3 | 21 | 66 |
| 3 | 22 | 87 |
| 3 | 23 | 38 |
Output:
| food_id | name | Carbs | Protein | Fat |
|---------|-----------|-------|---------|-----|
| 1 | Apple | 50 | 24 | 63 |
| 2 | Orange | | 12 | 95 |
| 3 | Pear | 66 | 87 | 38 |
(Note that Orange does not have a "Carbs" entry in the Nutrient_Data table)
I believe the following will do what you want :-
DROP TABLE IF EXISTS food_list;
CREATE TABLE IF NOT EXISTS food_list(food_id INTEGER PRIMARY KEY, name TEXT);
DROP TABLE IF EXISTS nutrient_definition;
CREATE TABLE IF NOT EXISTS nutrient_definition(nutrient_id INTEGER PRIMARY KEY, name TEXT);
DROP TABLE IF EXISTS nutrient_data;
CREATE TABLE IF NOT EXISTS nutrient_data(food_id INTEGER, nutrient_id INTEGER, value INTEGER);
INSERT INTO food_list (name) VALUES
('apple'),('orange'),('pear')
;
INSERT INTO nutrient_definition (name) VALUES
('carbs'),('protien'),('fat')
;
INSERT INTO nutrient_data VALUES
(1,1,50),(1,2,24),(1,3,63),
(2,2,12),(2,3,95),
(3,1,66),(3,2,87),(3,3,38)
;
SELECT food_list.food_id,food_list.name,
(
SELECT value
FROM nutrient_data
WHERE nutrient_data.food_id = food_list.food_id AND
nutrient_data.nutrient_id = (SELECT nutrient_definition.nutrient_id FROM nutrient_definition WHERE nutrient_definition.name = 'carbs')
),
(
SELECT value
FROM nutrient_data
WHERE nutrient_data.food_id = food_list.food_id AND
nutrient_data.nutrient_id = (SELECT nutrient_definition.nutrient_id FROM nutrient_definition WHERE nutrient_definition.name = 'protien')
),
(
SELECT value
FROM nutrient_data
WHERE nutrient_data.food_id = food_list.food_id AND
nutrient_data.nutrient_id = (SELECT nutrient_definition.nutrient_id FROM nutrient_definition WHERE nutrient_definition.name = 'fat')
)
FROM food_list
;
Results in :-

Master-Detail show data SQL

I'm working with SQL Server and I have this 3 tables
STUDENTS
| id | student |
-------------
| 1 | Ronald |
| 2 | Jenny |
SCORES
| id | score | period | student |
| 1 | 8 | 1 | 1 |
| 2 | 9 | 2 | 1 |
PERIODS
| id | period |
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
| 4 | 4 |
And I want a query that returns this result:
| student | score1 | score2 | score3 | score4 |
| Ronald | 8 | 9 | null | null |
| Jenny | null | null | null | null |
As you can see, the number of scores depends of the periods because sometimes it can be 4 o 3 periods.
I don't know if I have the wrong idea or should I make this in the application, but I want some help.
You need to PIVOT your data e.g.
select Y.Student, [1], [2], [3], [4]
from (
select T.Student, P.[Period], S.Score
from Students T
cross join [Periods] P
left join Scores S on S.[Period] = P.id and S.Student = T.id
) X
pivot
(
sum(Score)
for [Period] in ([1],[2],[3],[4])
) Y
Reference: https://learn.microsoft.com/en-us/sql/t-sql/queries/from-using-pivot-and-unpivot?view=sql-server-20

distinct sum does not distinct values

I have 2 tables, reservations and articles:
Reservations
------------------------------
Id | Name | City |
------------------------------
1 | Mike | Stockholm
2 | Daniel | Gothenburg
2 | Daniel | Gothenburg
3 | Andre | Gothenburg (Majorna)
Articles
-------------------------------------------------------------
ArticleId | Name | Amount | ReservationId |
-------------------------------------------------------------
10 | Coconuts | 1 | 1
10 | Coconuts | 4 | 2
11 | Apples | 2 | 2
12 | Oranges | 2 | 3
I want to select Articles Name and the sum of Articles.Amount per Articles.ArticleId and Reservations.City.
My code:
SELECT distinct r.ID,a.Name as ArticleName,
sum(a.Amount) as ArticlesAmount,
substr(r.City,1,3) as ToCityName
FROM Reservations r
INNER JOIN Articles a
on r.Id = a.ReservationId
WHERE a.Name <> ''
GROUP BY ToCityName,a.ArticleId,a.Name
ORDER BY ToCityName ASC
This gives me following result:
Id | ArticleName | ArticlesAmount | ToCityName
2 | Coconuts | 8 | Got
2 | Apples | 4 | Got
3 | Oranges | 2 | Got
1 | Coconuts | 1 | Sto
But i want:
Id | ArticleName | ArticlesAmount | ToCityName
2 | Coconuts | 4 | Got
2 | Apples | 2 | Got
3 | Oranges | 2 | Got
1 | Coconuts | 1 | Sto
Help would be appreciated, and an explanation please :)
Fiddle
Have a look at SQLFiddle
Code:
SELECT distinct r.ID,a.Name as ArticleName,
sum(distinct a.Amount) as ArticlesAmount,
substr(r.City,1,3) as ToCityName
FROM Reservations r
INNER JOIN Articles a
on r.Id = a.ReservationId
WHERE a.Name <> ''
GROUP BY ToCityName,a.ArticleId,a.Name
ORDER BY ToCityName ASC
You want to ensure you sum the amount by the distinct number of times it appears per group.
I had added Articles again to select requested rows again... here is query
SELECT DISTINCT
r.ID,
a.`Name` AS ArticleName,
Articles.Amount,
substr(r.City, 1, 3) AS ToCityName
FROM
Reservations r
INNER JOIN Articles a ON r.Id = a.ReservationId
INNER JOIN Articles ON a.ReservationId = Articles.ReservationId
AND a.ArticleId = Articles.ArticleId
WHERE
a. NAME <> ''
GROUP BY
ToCityName,
a.ArticleId,
a. NAME
ORDER BY
ToCityName ASC

Resources