query to find diff between two records - mysql

I have following tables
movies
- id
details
- user_id
- movie_id
- rating
users
- id
detail belongs to user and movie
I want to find the the diff between the rating of two users say id 3,10
simply I want answer of this
sum(10-(user1.rating - user2.rating))
where rating is > 0
i.e both users should have given at-least non zero rating

select
d1.movie_id
, d1.rating as user1Rating
, d2.rating as user2Rating
, abs(d1.rating - d2.rating)
from
details d1
inner join details d2 on d1.movie_id = d2.movie_id
where d1.user_id = 1
and d2.user_id = 2
See it working live in an sqlfiddle.

CREATE TABLE ratings
( userId int, movieId int, rating int)
INSERT INTO ratings (userId, movieId, rating) VALUES
(3, 1, 5), (10, 1, 8),
(3, 2, 10), (10, 2, 3)
SELECT r1.movieId, (r1.rating - r2.rating) FROM ratings as r1
INNER JOIN ratings as r2 on r1.movieId = r2.movieId
WHERE r1.userId = 3 and r2.userId = 10

DECLARE #UserRating1 int
DECLARE #UserRating2 int
DECLARE #UserID1 int
DECLARE #UserID2 int
DECLARE #MovieID int
SET #MovieID = 99
SET #UserID1 = 3
SET #UserID2 = 10
SET #UserRating1 = (SELECT Rating FROM details WHERE movie_id = #MovieID AND user_id = #UserID1 AND Rating > 0)
SET #UserRating2 = (SELECT Rating FROM details WHERE movie_id = #MovieID AND user_id = #UserID2 AND Rating > 0)
PRINT #UserRating1 - #UserRating2

Update: I replace the syntax from MSSQL to MySql
You can check this on SQL Fiddle:
create table movies (id int);
insert into movies (id) values (1);
insert into movies (id) values (2);
insert into movies (id) values (3);
insert into movies (id) values (4);
insert into movies (id) values (5);
create table users (id int);
insert into users (id) values (1);
insert into users (id) values (2);
insert into users (id) values (3);
insert into users (id) values (4);
insert into users (id) values (5);
insert into users (id) values (10);
create table details (user_id int, movie_id int, rating int);
insert into details (user_id, movie_id, rating) values (3,1,1);
insert into details (user_id, movie_id, rating) values (3,2,2);
insert into details (user_id, movie_id, rating) values (3,3,3);
insert into details (user_id, movie_id, rating) values (3,4,1);
insert into details (user_id, movie_id, rating) values (3,5,4);
insert into details (user_id, movie_id, rating) values (10,1,3);
insert into details (user_id, movie_id, rating) values (10,2,1);
insert into details (user_id, movie_id, rating) values (10,3,2);
insert into details (user_id, movie_id, rating) values (10,4,1);
insert into details (user_id, movie_id, rating) values (10,5,5);
and
select
sum(10-(details1.rating - details2.rating))
from
movies
inner join details details1 on movies.id = details1.movie_id
inner join users user1 on details1.user_id = user1.id
inner join details details2 on movies.id = details2.movie_id
inner join users user2 on details2.user_id = user2.id
where
(user1.id=3)
and
(user2.id=10)
and
(details1.rating is not null) and (details1.rating > 0)
and
(details2.rating is not null) and (details2.rating > 0)

Related

Sum sales from different tables

I have 5 tables related to sales. Three of them are like this:
Product_table_image
They are called product_a, product_b, product_c
The other tables are the time_id table which contains the reference for the date_id and the customers' table, which contains the details of customers.
time_id table
and
customer_table
The 3 tables refer to the sales of different products, but the products are not important in this context, because what I need is to sum up all the values per month per customer. There are cases when one or more customers might not have made a purchase of a certain product, which means not all customers ids will be in all products tables and that's what I can't figure out how to solve. It seems that my code is only able to fetch and sum when the clients have made purchases in all 3 tables.
So this is what I was able to come up with:
SELECT C.customer_id, ROUND((A.pa + B.pa + C.pc)* 1, 2) AS total,C.month_id
FROM (SELECT customer.customer_id, SUM(product_a.amount) AS pa , time_id.month_id FROM customer
INNER JOIN product_a on customer.customer_id = product_a.customer_id
INNER JOIN time_id on product_a.date_id = time_id.date_id
GROUP BY customer.customer_id, time_id.month_id) AS A
CROSS JOIN
(SELECT customer.customer_id, SUM(product_a.amount) AS pb , time_id.month_id FROM customer
INNER JOIN product_b on customer.customer_id = product_b.customer_id
INNER JOIN time_id on product_b.date_id = time_id.date_id
GROUP BY customer.customer_id, time_id.month_id) AS B
CROSS JOIN
(SELECT customer.customer_id, SUM(product_a.amount) AS pc , time_id.month_id FROM customer
INNER JOIN product_c on customer.customer_id = product_c.customer_id
INNER JOIN time_id on product_c.date_id = time_id.date_id
GROUP BY customer.customer_id, time_id.month_id) AS C
GROUP BY C.month_id, C.customer_id
ORDER BY C.month_id;
I've been stuck in it for a while, so any help is appreciated!
I have setup the tables and some sample data to make it more real.
create table month(id int, name varchar(20), primary key (id));
insert into month (id, name) values (1, 'January'),(2,'February'),(3,'March'),(4,'April');
create table year(id int, name varchar(4), primary key (id));
insert into year(id, name) values (2019, '2019'),(2020,'2020'),(2021,'2021');
create table time (id int, month_id int, year_id int, primary key (id));
alter table time add constraint fk_month FOREIGN KEY (month_id) REFERENCES month (id);
alter table time add constraint fk_year FOREIGN KEY (year_id) REFERENCES year (id);
insert into time (id, year_id, month_id) values
(1, 2019, 1),(2, 2019, 2),(3,2019,3),(4,2019,4),
(5, 2020, 1),(6, 2020, 2),(7,2020,3),(8,2020,4),
(9, 2021, 1),(10, 2021, 2),(11,2021,3),(12,2021,4);
create table customers (id int, name varchar(100), city varchar(100), country varchar(100), primary key (id));
insert into customers (id, name, city, country) values
(1, 'Google', 'San Francisco', 'US'),
(2, 'Ambev', 'São Paulo', 'BR'),
(3, 'Merck', 'Darmstadt', 'GE');
create table sales_of_product_a (id int, customer_id int, date_id int, amount decimal(10,2), primary key (id));
alter table sales_of_product_a add constraint fk_pa_time FOREIGN KEY (date_id) REFERENCES time (id);
-- only customer 1 - Google and 3 - Merck purchased product A
insert into sales_of_product_a (id, customer_id, date_id, amount) values
(1, 1, 1, 100.10),(2,1,2,200.20),(3,1,3,300.30),(4,1,4,400.40),
(5, 1, 5, 500.50),(6,1,6,600.60),(7,1,7,700.70),(8,1,8,800.80),
(9, 3, 1, 130.10),(10,3,2,230.20),(11,3,3,330.30),(12,3,4,430.40),
(13, 3, 5, 530.50),(14,3,6,630.60),(15,3,7,730.70),(16,3,8,830.80);
create table sales_of_product_b (id int, customer_id int, date_id int, amount decimal(10,2), primary key (id));
alter table sales_of_product_b add constraint fk_pb_time FOREIGN KEY (date_id) REFERENCES time (id);
-- only customer 1 - Google purchased product B
insert into sales_of_product_b (id, customer_id, date_id, amount) values
(1, 1, 1, 100.10),(2,1,2,200.20),(3,1,3,300.30),(4,1,4,400.40),
(5, 1, 5, 500.50),(6,1,6,600.60),(7,1,7,700.70),(8,1,8,800.80),
(9, 1, 9, 900.90),(10,1,10,1000.01),(11,1,11,1100.11),(12,1,12,1200.12);
create table sales_of_product_c (id int, customer_id int, date_id int, amount decimal(10,2), primary key (id));
alter table sales_of_product_c add constraint fk_pc_time FOREIGN KEY (date_id) REFERENCES time (id);
-- only customer 3 - Merck purchased product C
insert into sales_of_product_c (id, customer_id, date_id, amount) values
(1, 3, 1, 130.10),(2,3,2,230.20),(3,3,3,330.30),(4,3,4,430.40),
(5, 3, 5, 530.50),(6,3,6,630.60),(7,3,7,730.70),(8,3,8,830.80),
(9, 3, 9, 930.90),(10,3,10,1030.01),(11,3,11,1130.11),(12,3,12,1230.12);
The SQL you might be looking for would be something like.
with all_sales as (
select pa.customer_id, tt.month_id, sum(pa.amount) as amount from sales_of_product_a pa inner join time tt on (pa.date_id = tt.id) where tt.year_id = 2019 group by pa.customer_id, tt.month_id
union all
select pb.customer_id, tt.month_id, sum(pb.amount) as amount from sales_of_product_b pb inner join time tt on (pb.date_id = tt.id) where tt.year_id = 2019 group by pb.customer_id, tt.month_id
union all
select pc.customer_id, tt.month_id, sum(pc.amount) as amount from sales_of_product_c pc inner join time tt on (pc.date_id = tt.id) where tt.year_id = 2019 group by pc.customer_id, tt.month_id
),
sales_per_customer_per_month as (
-- summary of all sales of all products per customer per month
select customer_id, month_id, sum(amount) as amount from all_sales group by customer_id, month_id
),
customers_month as (
select c.id, c.name, c.city, c.country, m.id as month_id, m.name as month_name from customers c inner join month m on true
)
select c.id,c.name,c.city,c.country,c.month_id, coalesce(s.amount,0) as amount, sum(coalesce(s.amount,0)) over (partition by c.id order by c.id,c.month_id) as total
from customers_month c
left join sales_per_customer_per_month s on (s.customer_id = c.id and s.month_id = c.month_id)
order by c.id,c.month_id;
The result of query above is following.
The concepts used are linked below.
Window Functions:
https://dev.mysql.com/doc/refman/8.0/en/window-functions.html
Common Table Expressions (CTE):
https://www.mysqltutorial.org/mysql-cte/

How to retrieve data based on this criteria?

I have the following tables:
Payment Table
CREATE TABLE Payment (
PayID INTEGER,
PayName CHAR (55),
PaymentDescription VARCHAR (100),
Primary Key (PayID)
);
Users Table
CREATE TABLE Users (
UserID INTEGER,
UserFirst CHAR (40),
UserLast CHAR (40),
UserName VARCHAR (40),
UserAddress VARCHAR (35),
UserBirthdate DATE,
UserGender CHAR (1),
UserEmail VARCHAR (35),
PRIMARY KEY (UserID)
);
Genre Table
CREATE TABLE GENRE (
GenreID Integer,
Name CHAR(45),
Description VARCHAR (250),
PRIMARY KEY (GenreID)
);
Song Table
CREATE TABLE SONG (
SongID Integer,
GenreID Integer,
Title VARCHAR (50),
Album VARCHAR (40),
Duration TIME,
ReleaseYear DATE,
PRIMARY KEY (SongID),
CONSTRAINT foreignkey_holds_GenreID FOREIGN KEY (GenreID)
REFERENCES Genre(GenreID)
);
Song_Users Table
CREATE TABLE Song_Users (
UserID INTEGER,
SongID Integer,
PRIMARY KEY (UserID,SongID),
CONSTRAINT fk_users_song_user FOREIGN KEY (UserID)
REFERENCES Users(UserID),
CONSTRAINT fk_users_song_song FOREIGN KEY (SongID)
REFERENCES SONG(SongID)
);
Payment_Users Table
CREATE TABLE Payment_Users (
UserID INTEGER,
PayID INTEGER,
PRIMARY KEY (UserID,PayID),
CONSTRAINT fk_users_payment_user FOREIGN KEY (UserID)
REFERENCES Users(UserID),
CONSTRAINT fk_users_payment_pay FOREIGN KEY (PayID)
REFERENCES Payment(PayID)
);
And I have also populated the tables:
Payments
INSERT INTO Payment (PayID,PayName,PaymentDescription) VALUES (1, 'Visa', 'Applies to VISA Debit, Electron, Credit')
INSERT INTO Payment (PayID,PayName,PaymentDescription) VALUES (2, 'Mastercard', 'Applies across all types')
INSERT INTO Payment (PayID,PayName,PaymentDescription) VALUES (3, 'PayPal', 'Payment made using PayPal')
INSERT INTO Payment (PayID,PayName,PaymentDescription) VALUES (4, 'ApplePay', 'Payment method for IOS users')
Users
INSERT INTO Users (UserID, UserFirst, UserLast, UserName, UserAddress, UserBirthdate, UserGender, UserEmail) VALUES (1, 'Jane', 'Johnson', 'jjsweet135', '10 House Wood Street',19970321, 'F', 'jj1997#gmail.com')
INSERT INTO Users (UserID, UserFirst, UserLast, UserName, UserAddress, UserBirthdate, UserGender, UserEmail) VALUES (2, 'Joe', 'Watson', 'joewatson98', '21 House Red Street',19980414, 'M', 'joewatson1998#gmail.com')
INSERT INTO Users (UserID, UserFirst, UserLast, UserName, UserAddress, UserBirthdate, UserGender, UserEmail) VALUES (3, 'Katie', 'Davies', 'katiethebest425', '17 House Fun Street',20000318, 'F', 'katiebest452#hotmail.com')
INSERT INTO Users (UserID, UserFirst, UserLast, UserName, UserAddress, UserBirthdate, UserGender, UserEmail) VALUES (4, 'Tom', 'Branson', 'tommylad100', '27 House Church Street',20010719, 'M', 'tommylad35#orange.com')
Genre Table
INSERT INTO Genre (GenreID, Name, Description) VALUES
(1, 'Electro Swing', 'A musical journey set in the 90s')
INSERT INTO Genre (GenreID, Name, Description) VALUES
(2, 'House', 'When the beat shakes your house')
INSERT INTO Genre (GenreID, Name, Description) VALUES
(3, 'Classical', 'To stimulate neural activity in your brain')
INSERT INTO Genre (GenreID, Name, Description) VALUES
(4, 'Rap', 'Experience the streets from the comfort of your own home')
INSERT INTO Genre (GenreID, Name, Description) VALUES
(5, 'Pop', 'Follow the status quo')
Song Table
INSERT INTO SONG (SongID, GenreID, Title, Album, Duration, ReleaseYear) VALUES (1, 1, 'Swing the Electro', 'Electro Swing Vol 1', 11537, 19970316)
INSERT INTO SONG (SongID, GenreID, Title, Album, Duration, ReleaseYear) VALUES (2, 2, 'This is my House', 'UK House Anthem', 11454, 19990419)
INSERT INTO SONG (SongID, GenreID, Title, Album, Duration, ReleaseYear) VALUES (3, 3, 'Candy Shop', '50 Cent Classics', 2254, 20010324)
INSERT INTO SONG (SongID, GenreID, Title, Album, Duration, ReleaseYear) VALUES (4, 4, 'Mozart', 'Classical strokes', 44524, 18000225)
INSERT INTO SONG (SongID, GenreID, Title, Album, Duration, ReleaseYear) VALUES (5, 5, 'Katy Perry', 'Baby you are a Firework', 13524, 20050324)
INSERT INTO SONG (SongID, GenreID, Title, Album, Duration, ReleaseYear) VALUES (6, 5, 'One Direction', 'We are the one', 013204, 20060211)
INSERT INTO SONG (SongID, GenreID, Title, Album, Duration, ReleaseYear) VALUES (7, 5, 'Chainsmokers', 'In the back seat of your rover', 33544, 20160123)
INSERT INTO SONG (SongID, GenreID, Title, Album, Duration, ReleaseYear) VALUES (8, 5, 'Justin Bieber', 'Teenage dream', 22222, 20150325)
Song_Users Table
INSERT INTO Song_Users (UserID, SongID) VALUES (1,1)
INSERT INTO Song_Users (UserID, SongID) VALUES (2,2)
INSERT INTO Song_Users (UserID, SongID) VALUES (3,3)
INSERT INTO Song_Users (UserID, SongID) VALUES (4,4)
INSERT INTO Song_Users (UserID, SongID) VALUES (4,1)
INSERT INTO Song_Users (UserID, SongID) VALUES (4,2)
INSERT INTO Song_Users (UserID, SongID) VALUES (4,3)
INSERT INTO Song_Users (UserID, SongID) VALUES (4,5)
INSERT INTO Song_Users (UserID, SongID) VALUES (4,6)
INSERT INTO Song_Users (UserID, SongID) VALUES (4,7)
INSERT INTO Song_Users (UserID, SongID) VALUES (4,8)
Payment_Users Table
INSERT INTO Payment_Users VALUES (1,1)
INSERT INTO Payment_Users VALUES (2,2)
INSERT INTO Payment_Users VALUES (3,3)
INSERT INTO Payment_Users VALUES (4,3)
I need to find the most popular genre of music for Females (F). I can't seem to retrieve this information, because there is no corresponding Foreign Key for Genre(GenreID).
How could I create the query to get the most popular genre amongst females?
EDIT
SELECT Users.UserID, UserGender, SongID, GenreID FROM Users, Song_Users, Song
WHERE Users.UserID = Song_Users.UserID
AND
WHERE Genre.GenreID = Song.GenreID;
I am trying to link GenreID with SongID, but I am not sure how to do it. There is a link with a foreign key, but not in the Genre table, but only in the Song Table. My logic was to select the user, their gender, their songs and then the corresponding genre. Then I could use something like a HAVING CLAUSE to select stricly females and using a COUNT (Genre) and the appropriate GROUPBY function
As per the comments above, there is a huge amount of unnecessary information in this question. A set of sample data and evidence of effort on your part should be included, but the commands used to populate the tables are irrelevant.
I am assuming the following definition of popularity: The greatest number of users that have purchased songs in a given genre.
From what you have above, then you can accomplish what you are looking for with inner joins across four tables:
SELECT
count(u.UserID) as 'popularity',
g.Name as 'genre'
FROM
Users u INNER JOIN
Song_Users su INNER JOIN
SONG s INNER JOIN
GENRE g
ON
u.UserID = su.UserID,
su.SongID = s.SongID,
s.GenreID = g.GenreID
WHERE
u.UserGender = 'F'
GROUP BY
g.Name
ORDER BY
count(u.UserID) DESC
You will Inner join In MYSQL,
Select Field names from Songs Innerjoin users.user_id ON songs.user_id
Innerjoin Genre.Genre_id ON songs.Genre_id
You see type your table names and columns names correctly . You will get the correct result.
SELECT COUNT(g.`GenreID`) AS popularity, g.`Name` FROM genre g, song_users su, song s, users u WHERE
u.`UserGender` = 'F'
AND su.`UserID` = u.`UserID`
AND s.`SongID` = su.`SongID`
AND g.`GenreID` = s.`GenreID`
GROUP BY g.`GenreID`
The popularity would give you the genres name and their count as popularity, the highest count is the most famous.

mysql select not does not work

I want to only select student IDs that do not have a row where product ID is 11. But I can't. What should I do?
BEGIN TRANSACTION;
CREATE TABLE STUDENTS(Id integer PRIMARY KEY, name text,year_born integer
);
CREATE TABLE PROJECT(project_id integer,title text,project_owner
text,year_written integer );
CREATE TABLE PROJECTWORKS(student_id integer,project_id integer);
INSERT INTO STUDENTS VALUES(1598,'james',1996);
INSERT INTO STUDENTS VALUES(2479,'andre',1996);
INSERT INTO STUDENTS VALUES(3682,'pierre',1997);
INSERT INTO PROJECT VALUES(10,'A','ABC',2008);
INSERT INTO PROJECT VALUES(11,'B','ABC',2010);
INSERT INTO PROJECT VALUES(12,'C','ABC',2016);
INSERT INTO PROJECT VALUES(13,'D','CBA',2014);
INSERT INTO PROJECTWORKS VALUES(1598,10);
INSERT INTO PROJECTWORKS VALUES(1598,11);
INSERT INTO PROJECTWORKS VALUES(1598,12);
INSERT INTO PROJECTWORKS VALUES(3682,12);
INSERT INTO PROJECTWORKS VALUES(3682,13);
INSERT INTO PROJECTWORKS VALUES(2479,12);
SELECT * FROM STUDENTS;
SELECT * FROM PROJECT;
SELECT * FROM PROJECTWORKS;
SELECT DISTINCT student_id FROM PROJECTWORKS
WHERE not project_id=11 and (project_id=10 OR project_id=12 OR
project_id=13);
I just want 3682 and 2479 Because 1598 has 11.
NOT EXISTS is perfect for this:
SELECT DISTINCT student_id
FROM PROJECTWORKS p
WHERE project_id IN (10, 12, 13)
AND NOT EXISTS
(SELECT 1
FROM PROJECTWORKS p2
WHERE p.student_id = p2.student_id
AND p2.project_id = 11);
Also, I assume you meant you don't want ID 11, as I didn't see 14 in your example.
If I understood you right, this is what you want
SELECT DISTINCT student_id FROM PROJECTWORKS
WHERE student_id not in
(select student_id from PROJECTWORKS where project_id=11)
You could use a join between the two result
select distinct student_id
from PROJECTWORKS p
where project_id NOT IN (11)
INNER JOIN (
Select distinct student_id
from PROJECTWORKS
where project_id IN ( 10, 12, 13)
) T on t.student_id = p.student_id
SELECT DISTINCT student_id FROM PROJECTWORKS
WHERE student_id not in
(select student_id from PROJECTWORKS where project_id=11)
Your question is not clear. above code snippet is for distinct student_id which does not have project_id as 11

How to return multiple rows when using Aggregate functions?

SELECT StudentID, Fname, LName, S_LessonNumber, LessonName, Date, Cost
FROM STUDENT_2
JOIN LESSON ON S_LessonNumber = LessonNumber
NATURAL JOIN STUDENT_1
WHERE StudentID = '1001'
The resulting table I get with this query is as follows,
When attempting to display the total amount paid, and the total number of lessons taken, using the following query, I was only able return one row.
SELECT StudentID, Fname, LName, S_LessonNumber, LessonName, Date,
Cost,COUNT( DISTINCT S_LessonNumber ) , SUM( Cost )
FROM STUDENT_2
JOIN LESSON ON S_LessonNumber = LessonNumber
NATURAL JOIN STUDENT_1
WHERE StudentID = '1001'
Is there a way that I can return all 4 rows with the values for COUNT(DISTINCT S_LessonNumber) and SUM(Cost) repeated.
The desired output is as follows:
StudentID FName LName S_LessonNumber LessonName Date Cost COUNT SUM
1001 Hannibal Lecter 7 C--- --- 15 4 60
1001 Hannibal Lecter 6 Wa-- --- 15 4 60
1001 Hannibal Lecter 5 Tri-- --- 15 4 60
1001 Hannibal Lecter 1 Cha- --- 15 4 60
Aggregate functions will always return 1 row. If using subqueries is not a problem, you can do:
SELECT StudentID, Fname, LName, S_LessonNumber, LessonName, Date, Cost,
(SELECT COUNT( DISTINCT S_LessonNumber ) FROM STUDENT_2 JOIN LESSON ON S_LessonNumber = LessonNumber NATURAL JOIN STUDENT_1 WHERE StudentID = '1001') AS COUNT,
(SELECT SUM( Cost ) FROM STUDENT_2 JOIN LESSON ON S_LessonNumber = LessonNumber NATURAL JOIN STUDENT_1 WHERE StudentID = '1001') AS SUM
FROM STUDENT_2
JOIN LESSON ON S_LessonNumber = LessonNumber
NATURAL JOIN STUDENT_1
WHERE StudentID = '1001'
Check it here: http://rextester.com/SBUQ82088
create table if not exists fstudents (id int, fname text, lname text);
create table if not exists fstudents2 (student_id int, lesson_number int, lesson_date date, cost int);
create table if not exists flessons (number int, name text);
insert into fstudents values (1001, 'Anibal', 'Lecter');
insert into flessons values (1, 'Cha Cha');
insert into flessons values (2, 'Waltz');
insert into flessons values (3, 'Country');
insert into flessons values (4, 'Triple2');
insert into fstudents2 values (1001, 1, '2016-10-06', 15);
insert into fstudents2 values (1001, 2, '2016-10-07', 15);
insert into fstudents2 values (1001, 3, '2016-10-08', 15);
insert into fstudents2 values (1001, 4, '2016-10-09', 15);
Use a subquery to count an sum, and then join it to main query.
select st2.student_id, st.fname, st.lname, ls.name, st2.lesson_date, st2.cost, agg.courses, agg.total_cost
from fstudents2 st2
join fstudents st on st2.student_id = st.id
join flessons ls on st2.lesson_number = ls.number
join (select st3.student_id, count(st3.lesson_number) courses, sum(st3.cost) total_cost
from fstudents2 st3
group by st3.student_id) agg on agg.student_id = st2.student_id
where
st2.student_id = 1001;

Equations in SQL (MySQL)

I have a table with a cost_maintence column that has cost for the entire year(52) weeks. I also have a table of renters, and a table of renter_units where there is a week_owned column that has the week number the renter rented. I am trying to figure out how I could calculate the cost for each renter. The equation I came up with is:
what each person owes = (cost_maintence/52) * #weeks each renter
rented
Is there any way I could get the value from a query?
create table renters(
id,
lname,
fname,
phone_num);
create table unit(
id,
unit_name,
unit_num,
cost_maintence);
create table renters_unit(
renters_id,
unit_id,
week_owned);
This is the query I came up with but I have no way of testing it out
select r.lname, r.fname, count(ru.week_owned),
sum(u.cost_maintence/52*count(ru.week_owned))
from renters r, renters_units ru, units u
where r.id = ru.renter_id
and ru.unit_id = u.id
and u.unit_name =unitname
and u.unit_num = unitnum
group by lname
order by lname,fname asc;
Here's an example. The inner query will get you amount owed per item, and the outer query sums that to find the total owed per person.
SELECT fname, SUM(owes) AS total_due
FROM (
SELECT r.fname,
r.id,
u.unit_name,
u.cost_maintence/52*COUNT(ru.week_owned) AS owes
FROM renters AS r
INNER JOIN renters_unit AS ru ON r.id = ru.renters_id
INNER JOIN unit AS u ON u.id = ru.unit_id
GROUP BY r.id, u.id
) AS t
GROUP BY id
Try it out with a SQLFiddle demo
Example Schema:
create table renters(
id int,
lname varchar(20),
fname varchar(20),
phone_num varchar(20));
create table unit(
id int,
unit_name varchar(30),
unit_num int,
cost_maintence int);
create table renters_unit(
renters_id int,
unit_id int,
week_owned int);
INSERT INTO renters VALUES (1, 'Peterson', 'Chaz', '8675309');
INSERT INTO unit VALUES (1, 'Skateboard', 1337, 52);
INSERT INTO unit VALUES (2, 'Flamethrower', 5432, 104);
INSERT INTO renters_unit VALUES (1, 1, 1);
INSERT INTO renters_unit VALUES (1, 1, 2);
INSERT INTO renters_unit VALUES (1, 1, 4);
INSERT INTO renters_unit VALUES (1, 2, 4);
INSERT INTO renters_unit VALUES (1, 2, 5);
By this, we can see that Chaz should owe $7 for the year (had a skateboard for 3 weeks at $1 per week, and a flamethrower for 2 weeks at $2 per week).
The inner query gives the following:
FNAME UNIT_NAME OWES
Chaz Skateboard 3
Chaz Flamethrower 4
And the outer:
FNAME TOTAL_DUE
Chaz 7
SELECT t.renters_id, SUM(u.cost_maintence)/52
FROM unit u JOIN renters_unit t ON t.unit_id = u.id
GROUP BY t.renters_id