SQLite: Get Output From Two Tables Using Common Reference ID - sql

I am new in SQLite and i have been working on an issue for quite a long time.
Lets say we have 2 database table say tbl_expense and tbl_category. Please find below the following table structure.
tbl_category
CREATE TABLE IF NOT EXISTS tbl_category(
category_id INTEGER PRIMARY KEY AUTOINCREMENT,
category_name VARCHAR(20) DEFAULT NULL,
category_desc VARCHAR(500) DEFAULT NULL,
category_icon VARCHAR(100) DEFAULT NULL,
category_created timestamp default CURRENT_TIMESTAMP
)
tbl_expense
CREATE TABLE IF NOT EXISTS tbl_expense(
expense_id INTEGER PRIMARY KEY AUTOINCREMENT,
expense_name VARCHAR(20) DEFAULT NULL,
expense_desc VARCHAR(500) DEFAULT NULL,
expense_type VARCHAR(20) DEFAULT NULL,
expense_amt DECIMAL(6.3) DEFAULT NULL,
expense_date TIMESTAMP DEFAULT NULL,
expense_category INTEGER DEFAULT NULL,
expense_created_date timestamp DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (expense_category) REFERENCES tbl_category(category_id)
ON DELETE SET NULL
)
Assume we have data in the tables like this below.
Expected Output:
Assure we have category_id and expense_category as common fields. How can i create an SQL Query where i can list all categories and sum of their expense amount as follows.
Please help me on this issue.

You need an INNER join of the tables and aggregation:
SELECT c.category_name Category,
SUM(e.expense_amt) Amount
FROM tbl_category c INNER JOIN tbl_expense e
ON e.expense_category = c.category_id
GROUP BY c.category_id;
If you want all categories from the table tbl_category, even those that are not present in tbl_expense, use a LEFT join and TOTAL() aggregate function:
SELECT c.category_name Category,
TOTAL(e.expense_amt) Amount
FROM tbl_category c LEFT JOIN tbl_expense e
ON e.expense_category = c.category_id
GROUP BY c.category_id;

Related

PostgreSQL query for Library Management System

Get the member ID and name of the members to whom no more books can be issued, because they have already got as many books issued as the number for which they are entitled
Following are the schemas:
Book_Records(accession_no,isbn_no)
Book(isbn_no, author, publisher, price)
Members(member_id, member_name,max_no_books,max_no_days)
Book_Issue(member_id,accession_no,issue_date,return_date)
CREATE TABLE BOOK (ISBN_NO VARCHAR(35) PRIMARY KEY,
AUTHOR VARCHAR(35) NOT NULL,
PUBLISHER VARCHAR(35) NOT NULL,
PRICE NUMERIC(10,3));
CREATE TABLE BOOK_RECORDS(ACCESSION_NO VARCHAR(35) PRIMARY KEY,
ISBN_NO VARCHAR(35) REFERENCES BOOK(ISBN_NO));
CREATE TABLE MEMBERS(MEMBER_ID VARCHAR(35) PRIMARY KEY,
MEMBER_NAME VARCHAR(35) NOT NULL,
MAX_NO_BOOKS INT,
MAX_NO_DAYS INT);
CREATE TABLE BOOK_ISSUE(MEMBER_ID VARCHAR(35) REFERENCES MEMBERS(MEMBER_ID),
ACCESSION_NO VARCHAR(35) REFERENCES
BOOK_RECORDS(ACCESSION_NO),
ISSUE_DATE DATE NOT NULL,
RETURN_DATE DATE,
PRIMARY KEY(MEMBER_ID,ACCESSION_NO));
I tried the following query but fails.
SELECT DISTINCT member_name
FROM members AS m
JOIN (
SELECT member_id, COUNT(*) AS no_books_issued
FROM book_issue
GROUP BY member_id,accesion_no
HAVING no_books_issued >= max_no_books
) AS b ON m.member_id = b.member_id;
Presumably, a query like this gets the number of books currently issued:
SELECT member_id, COUNT(*) AS num_books
FROM book_issue
WHERE return_date IS NULL
GROUP BY member_id;
My understanding of the maximum number of books would be concurrently -- that is, only count books that have not been returned. Perhaps you have a different definition.
Then, you can use this in a JOIN, doing the comparison on the maximum outside the subquery:
SELECT member_name
FROM members m JOIN
(SELECT member_id, COUNT(*) AS num_books
FROM book_issue
WHERE return_date IS NULL
GROUP BY member_id
) b
ON b.member_id = m.member_id AND
b.num_books >= m.max_no_books;
Notes:
In a JOIN, the comparison to the outer table needs to be outside the subqueries.
No SELECT DISTINCT is needed.
The GROUP BY for counting books should be only at the member level.

Select a product that is on all interventions

Hello my question is simple for some of yours ^^
I've a table product, reference, and intervention. When there is an intervention the table reference make the link between products that we need for the interventions and the intervention.
I would like to know how to do to search products that have made part of all interventions.
This are my tables :
--TABLE products
create table products (
reference char(5) not null check ( reference like 'DT___'),
designation char(50) not null,
price numeric (9,2) not null,
primary key(reference) );
-- TABLE interventions
create table interventions (
nointerv integer not null ,
dateinterv date not null,
nameresponsable char(30) not null,
nameinterv char(30) not null,
time float not null check ( temps !=0 AND temps between 0 and 8),
nocustomers integer not null ,
nofact integer not null ,
primary key( nointerv),
foreign key( noclient) references customers,
foreign key (nofacture) references facts
);
-- TABLE replacements
create table replacements (
reference char(5) not null check ( reference like 'DT%'),
nointerv integer not null,
qtereplaced smallint,
primary key ( reference, nointerv ),
foreign key (reference) references products,
foreign key(nointerv) references interventions(nointerv)
);
--EDIT :
This is a select from my replacement table
We can see in this picture that the product DT802 is used in every interventions
Thanks ;)
This will show 1 line intervention - products. Is this you are expecting for?
select interventions.nointerv, products.reference
from interventions
inner join replacements on interventions.nointerv = replacements.nointerv
inner join products on replacements.reference = products.reference;
This one?
select products.reference, products.designation
from interventions
inner join replacements on interventions.nointerv = replacements.nointerv
inner join products on replacements.reference = products.reference
group by products.reference, products.designation
having count(*) = (select count(*) from interventions);
Your question is hard to follow. If I interpret it as all nointerv in replacements whose reference contains all products, then:
select nointerv
from replacements r
group by nointerv
having count(distinct reference) = (select count(*) from products);

SQLite3 INNER JOIN with 3 tables

I'm trying to select from 3 tables with an INNER JOIN:
The tables:
CREATE TABLE tracks (
'track_id' INTEGER PRIMARY KEY NOT NULL,
'name' TEXT NOT NULL,
'length' REAL DEFAULT '0.00',
'city' TEXT
);
CREATE TABLE heats (
'heat_id' INTEGER PRIMARY KEY NOT NULL,
'track_id' INTEGER UNSIGNED NOT NULL,
'heat_pos' INTEGER UNSIGNED NOT NULL,
'day_pos' INTEGER UNSIGNED NOT NULL,
'type' TEXT NOT NULL DEFAULT 'training',
'average' REAL,
'date' TEXT,
'comment' TEXT,
FOREIGN KEY ('track_id') REFERENCES tracks ('track_id')
);
CREATE TABLE laps (
'lap_id' INTEGER PRIMARY KEY NOT NULL,
'heat_id' INTEGER UNSIGNED NOT NULL,
'laptime' REAL UNSIGNED NOT NULL,
FOREIGN KEY ('heat_id') REFERENCES heats ('heat_id')
);
When selecting information from 2 tables (laps and heats) it works like I expected:
select
laps.lap_id,
laps.laptime,
heats.heat_pos
from laps
inner join heats on laps.heat_id = heats.heat_id;
But now I want to select the corresponding tracknames from the track table:
select
laps.lap_id,
laps.laptime,
heats.heat_pos,
tracks.name
from laps, tracks, heats
inner join heats on laps.heat_id = heats.heat_id and
inner join heats on tracks.track_id = heats.track_id;
This gives me the following error:
ambiguous column name: heats.heat_pos
I'm completely lost, but I have a feeling it's just a small mistake.
Anyone knows what I'm doing wrong?
select
laps.lap_id,
laps.laptime,
heats.heat_pos,
tracks.name
from laps
inner join heats on laps.heat_id = heats.heat_id
inner join tracks on tracks.track_id = heats.track_id;
select
laps.lap_id,
laps.laptime,
heats.heat_pos,
tracks.name
from laps
inner join heats on laps.heat_id = heats.heat_id
inner join heats on tracks.track_id = heats.track_id
ORDER BY laps.lap_id

Two problems with my query: Show null values and order by before group by

I'm having major problems with my query. I want to show all results in the source table even if there is no pricing entry in the right table.
My order by is also not working. I want to order by product_pricing.PP_CashPrice prior to grouping by.
Here is my SQL code:
SELECT * FROM source
LEFT JOIN product_pricing ON source.Source_ID = product_pricing.Source_ID
WHERE (product_pricing.Product_ID = '234'
OR product_pricing.PP_ID = NULL)
AND source.Source_Active = 'Yes'
GROUP by source.Source_ID
ORDER by PP_CashPrice desc
I basically need it to show all sources. The right column will have duplicates but I only need to show the highest one.
My right column is as follows:
CREATE TABLE product_pricing ( PP_ID int(10) NOT NULL AUTO_INCREMENT, PP_Type varchar(150) NOT NULL, PP_CashPrice decimal(10,2) NOT NULL, PP_DateObtained date NOT NULL, PP_TimeObtained time NOT NULL, PP_Active varchar(3) NOT NULL, PP_Postcode varchar(150) NOT NULL, Source_ID int(10) NOT NULL, SC_ID int(10) NOT NULL, Product_ID int(10) NOT NULL, PRIMARY KEY (PP_ID) ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
You should not use a where clause on a "Left joined" table. Put the condition in the where clause
I would also use a COALESCE operator for the ordering clause, and probably add an ordering on s.Source_ID if you want different sourceId with "inner pricing" ordering.
SELECT * FROM source s
LEFT JOIN product_pricing pp ON s.Source_ID = pp.Source_ID AND pp.PP_ID = '234'
AND s.Source_Active = 'Yes'
GROUP by s.Source_ID
ORDER by s.Source_ID, COALESCE(p.PP_CashPrice, 0) desc

Problem selecting the latest record in JOIN

These are my 2 tables:
CREATE TABLE `documents` (
`Document_ID` int(10) NOT NULL auto_increment,
`Document_FolderID` int(10) NOT NULL,
`Document_Name` varchar(150) NOT NULL,
PRIMARY KEY (`Document_ID`),
KEY `Document_FolderID` (`Document_FolderID`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=331 ;
CREATE TABLE `files` (
`File_ID` int(10) NOT NULL auto_increment,
`File_DocumentID` int(10) NOT NULL,
`File_Name` varchar(255) NOT NULL,
PRIMARY KEY (`File_ID`),
KEY `File_DocumentID` (`File_DocumentID`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=333 ;
There can be multiple files to 1 document. I am trying to SELECT all of the documents with a JOIN on the files table but I only want 1 file record which is the latest one.
Here is my query I have come up with that doesn't quite work, can anyone suggest the right way?
SELECT `documents`.*
FROM `documents`
INNER JOIN (
SELECT MAX(`File_ID`), *
FROM `files`
WHERE `File_DocumentID` = `documents`.`Document_ID`
GROUP BY `File_ID` ) AS `file1`
ON `documents`.`Document_ID` = `file1`.`File_DocumentID`
WHERE `documents`.`Document_FolderID` = 94
ORDER BY `documents`.`Document_Name`
*edit: the error is Unknown column 'documents.Document_ID' in 'where clause'
Use:
SELECT d.*, f.*
FROM DOCUMENTS d
JOIN FILES f ON f.file_document_id = d.document_id
JOIN (SELECT t.file_document_id,
MAX(t.file_id) AS max_file_id
FROM FILES t
GROUP BY t.file_document_id) x ON x.file_document_id = f.file_document_id
AND x.max_file_id = f.file_id
The derived table/inline view called "x" is a join to the same table, all it does is tweak the records coming from the FILES table to be the highest per file_document_id...
Don't group by file_id, but by File_documentid.
I think I see what's wrong... You have GROUP BY File_ID, but I guess you really want GROUP BY File_DocumentID instead.