SQL Query or Table Error? - sql

So Im trying to find the total amount spent on Cuts and Products by each Customer
I don't know if my Query is Wrong or my entire Database Schema any ideas?
My Query
`Select First_Name, SUM(B.Cost), SUM(C.Cost)
FROM bookings A, cuts B, products C, customers D
Where A.Customer_ID= D.Customer_ID
AND A.Cut_ID = B.Cut_ID
AND A.Product_ID= C.Product_ID;`
My Database
`Table: bookings
Booking_N0, Customer_ID, Cut_ID, Product_ID, TimeTaken`
`Table: customers
Customre_ID, First_Name, Sex`
`Table: products
Product_ID, Products, Cost`
`Table: cuts
Cut_ID, Cut, Cost`

You should GROUP BY to SUM by each customer :
Select D.First_Name
, SUM(B.Cost)
, SUM(C.Cost)
FROM bookings A LEFT JOIN cuts B ON A.Cut_ID = B.Cut_ID
JOIN products C ON A.Product_ID = C.Product_ID
JOIN customers D ON A.Customer_ID = D.Customer_ID
GROUP BY D.First_Name;
Also, look forward using explicit join notation (FROM table1 t1 JOIN table2 t2 ON t1.field1 = t2.field2) instead of implicit join notation (FROM table1 t1, table2 t2 WHERE t1.field1 = t2.field2), because it is has more intuitive view (tables are listed near conditions on which they are joined).

Start using recommended JOIN / ON syntax for joining instead of using WHERE clause . You also need a GROUP BY clause
Select First_Name, SUM(B.Cost), SUM(C.Cost)
FROM bookings A
INNER JOIN cuts B
ON A.Cut_ID = B.Cut_ID
INNER JOIN products C
ON A.Product_ID= C.Product_ID
INNER JOIN customers D
ON A.Customer_ID= D.Customer_ID
GROUP BY First_Name

If you use aggregate function like SUM you have to add a group by clause
in your case:
...
AND A.Product_ID= C.Product_ID
GROUP BY First_Name

Related

Join in inside and outside subquery

I am working with 3 tables sales, customer & product where I want to get all columns from the sales table and customer_name, customer age from customer table and product_name, category from the product table in the same result set. There's a condition that I have to use 2 join operations where one join will be inside subquery and the other outside subquery.
Here's my query that's not working:
SELECT a.*,
b.customer_name,
b.age,
b.customer_id
FROM sales AS a
LEFT JOIN customer AS b on a.customer_id = b.customer_id
LEFT JOIN (SELECT product_id, product_name, category from product) as c
ON a.product_id = c.product_id
ORDER BY a.customer_id, b.customer_id;
Only the first join is working in this query.
select
a.*,
b.customer_name,
b.age,
b.customer_id ,
c.*
from sales as a
left join customer as b
on a.customer_id = b.customer_id
LEFT JOIN (SELECT product_id, product_name, category from product) as c
ON a.product_id = c.product_id
order by a.customer_id;
https://www.db-fiddle.com/f/wKbMaD6UF1v3a4KCXE7248/0

Multiple joins with group by (Sum)

When I using multiple JOIN, I hope to get the sum of some column in joined tables.
SELECT
A.*,
SUM(C.purchase_price) AS purcchase_total,
SUM(D.sales_price) AS sales_total,
B.user_name
FROM
PROJECT AS A
LEFT JOIN
USER AS B ON A.user_idx = B.user_idx
LEFT JOIN
PURCHASE AS C ON A.project_idx = C.project_idx
LEFT JOIN
SALES AS D ON A.project_idx = D.project_idx
GROUP BY
????
You need to use subquery as follows:
SELECT A.project_idx,
a.project_name,
A.project_category,
sum(C.purchase_price) AS purcchase_total,
sum(D.sales_price) as sales_total,
B.user_name
FROM PROJECT AS A
LEFT JOIN USER AS B ON A.user_idx = B.user_idx
LEFT JOIN (select project_idx, sum(purchase_price) as purchase_price
from PURCHASE group by project_idx ) AS C ON A.project_idx = C.project_idx
LEFT JOIN (select project_idx, sum(sale_price) as sale_price
from SALES group by project_idx) AS D ON A.project_idx = D.project_idx
I am not sure but you can use inner join of project with user instead of left join.
SELECT A.project_idx,
a.project_name,
A.project_category,
purcchase_total,
sales_total,
B.user_name
FROM PROJECT AS A
LEFT JOIN USER AS B ON A.user_idx = B.user_idx
LEFT JOIN (select project_idx, sum(purchase_price) as purchase_total
from PURCHASE group by project_idx ) AS C ON A.project_idx = C.project_idx
LEFT JOIN (select project_idx, sum(sale_price) as sale_total
from SALES group by project_idx) AS D ON A.project_idx = D.project_idx
This is working correctly on MS-SQL Server.
Thanks to Popeye
You are attempting to aggregate over two unrelated dimensions, and that throws off all the calculations.
Correlated subqueries are an alternative:
SELECT p.*,
(SELECT SUM(pu.purchase_price)
FROM PURCHASE pu
WHERE p.project_idx = pu.project_idx
) as purchase_total,
(SELECT SUM(s.sales_price)
FROM SALES s
WHERE p.project_idx = s.project_idx
) as sales_total,
u.user_name
FROM PROJECT p LEFT JOIN
USER u
ON p.user_idx = u.user_idx ;
Note that this uses meaningful table aliases so the query is easier to read. Arbitrary letters are really no better (and perhaps worse) than using the entire table name.
Correlated subqueries avoid the outer aggregation as well -- and let you select all the columns from the first table, which is what you want. They also often have better performance with the right indexes.

How to make it in one query

I have the following relational table schema
Customer(customer_id, customer_name, customer_city)
Branch(branch_id, branch_name, branch_city)
Account(account_id, balance, customer_id, branch_id)
Question name every customer who has accounts in branches at least two different cities.
It is working for me the next query but only if I create a view first. Is there any other option to do it together?
My solution which works:
Create view Cust as select c.Customer_Name, c.customer_ID, b.branch_name, b.branch_city from Customer as c inner Join Account as a on c.customer_ID=a.customer_ID join Branch as b on b.branch_id=a.branch_id
SELECT * FROM Cust as c inner join Cust as c1 on c.CustomerID=c1.CustomerID and c.branch_city <> c1.branch_city
Here is one option using exists with an aggregate query
select c.*
from customer c
where exists (
select 1
from account a
inner join branch b on b.branch_id = a.branch_id
where a.customer_id = c.customer_id
group by c.customer_id
having min(b.branch_city) <> max(b.branch_city)
)

Display 2 columns from one table having max count in column 3 and display computed sum of values from another table

I've a customer table and purchases table,
need to show cname, cid with max(customer_visits) from customer table
and sum of total_purchases by customer in purchases table.
I'm doing something like this
select p.cid, c.cname, sum(p.total_price)
from customers c where exists
(select max(visits_made) from customers having visits_made=max(visits_made)
and cid=p.cid)
inner join purchases p on p.cid=c.cid
group by p.cid,c.cname
and
select p.cid, c.cname, sum(p.total_price)
(select max(visits_made) from customers c where c.cid=p.cid)
from purchases p
inner join customers c on c.cid=p.cid
group by p.cid,c.cname
What's going wrong with these queries?
Found the solution, had to include where clause after inner join :D
I think this is just an aggregation query:
select p.cid, c.cname, sum(p.total_price) as total_price,
max(visits_made) as visits_made
from purchases p inner join
customers c
on c.cid = p.cid
group by p.cid, c.cname;

Get one to many relation data

Hi everyone I am having three tables.
Customer(list of customers)
Payments(list of customer payment)
Orders(list of customer orders)
customer can have more than one payment and order that is one to many relation.
I tried following query but it is not showing proper result.
select a.name, b.job_date as JobDate, c.order_date as OrderDate from Customers a
inner join Jobs b on a.id = b.customer_id
inner join Orders c on a.id = c.customer_id
where a.id = 1;
What i need is to show a customer's orders and jobs.
Try left join instead of inner join. child tables might not have records assosiated with Customers.id =1.
select a.name, b.job_date as JobDate, c.order_date as OrderDate from Customers a
left join Jobs b on a.id = b.customer_id
left join Orders c on a.id = c.customer_id
where a.id = 1;
If you want to get all the orders you can use the following :
select a.name, c.order_date as OrderDate from Orders c
inner join Customers a on a.id = c.customer_id
where a.id = 1;
You get build a similar query to get all the employee's jobs:
select a.name, b.job_date as JobDate from Jobs b
inner join Customers a on a.id = b.customer_id
where a.id = 1;