I was tasked to create a new report on a legacy program. I needed to LEFT JOIN a table but I am getting Syntax error on JOIN Operation.
My SQL query as follows:
SELECT
SUM(b.qty) as qty,
b.price,
c.item_desc,
a.cust_name,
e.curr_symbol
FROM (
tran_hdr a,
tran_dtl b,
items c ,
tailoring e
(
LEFT JOIN
customers d
ON a.cust_name = d.name
)
)
WHERE a.tran_id = b.tran_id
AND b.item_no = c.item_no
GROUP BY b.price,
c.item_desc,
a.cust_name,
e.curr_symbol
I am joining the tran_hdr to customers. Because not all customers in Tran Header is maintained in customer table, but report requirest to show all Data in Transaction table.
You're messing up your JOINs.
And your Tailoring table has no relation whatsoever to other table.
So, just try this one out:
SELECT
b.price,
c.item_desc,
a.cust_name,
e.curr_symbol,
SUM(b.qty) as qty
FROM
tran_hdr a
INNER JOIN tran_dtl b
ON a.tran_id = b.tran_id
INNER JOIN items c
ON b.item_no = c.item_no
LEFT JOIN
customers d
ON a.cust_name = d.name
,tailoring e
GROUP BY b.price,
c.item_desc,
a.cust_name,
e.curr_symbol
Related
I am trying to select and print customer_name(customer table), room_number(room table), room_rate (room table),rate_type(reservation table), checkin_date(reservation table), checkout_date(reservation table), billing_amount(billing table) from multiple tables. I tried to do an inner join but the error is 'column ambiguously defined '
SELECT *
FROM Customer c
INNER JOIN Reservation r
ON c.customer_id = r.customer_id
INNER JOIN Room s
ON s.room_id = r.room_id
INNER JOIN billing b
ON reservation_id = b.reservation_id
WHERE c.customer_name = 'John Scott';
You're missing the table alias on the left side of the last JOIN. Try this:
SELECT *
FROM Customer c
INNER JOIN Reservation r
ON c.customer_id = r.customer_id
INNER JOIN Room s
ON s.room_id = r.room_id
INNER JOIN billing b
ON r.reservation_id = b.reservation_id
WHERE c.customer_name = 'John Scott';
One problem is in your join and one in your select:
SELECT *
-------^ duplicate column names
FROM Customer c INNER JOIN
Reservation r
ON c.customer_id = r.customer_id INNER JOIN
Room s
ON s.room_id = r.room_id INNER JOIN
billing b
ON reservation_id = b.reservation_id
--------^ missing table alias
WHERE c.customer_name = 'John Scott';
If the only duplicate columns are the JOIN keys, then the USING clause (standard but not supported by all databases) is a handy way to get all the columns:
SELECT *
FROM Customer c INNER JOIN
Reservation r
USING (customer_id) INNER JOIN
Room s
USING (room_id) INNER JOIN
billing b
USING (reservation_id)
WHERE c.customer_name = 'John Scott';
When you using USING, the key columns are not duplicated with SELECT *.
I'm trying to join three tables.
Tables:
HumanResources.Employees: Employee_ID(Primary Key), First_Name, Title -- also known as Employee_Title,
ProjectDetails.TimeCards: Employee_ID(Foreignkey), Project_ID (Foreign Key)
ProjectDetails.Projects: Project_Name, Project_ID(Primary Key)
I tried joining them using a temporary table.
select b.First_Name, b.Title, c.Project_ID from HumanResources.Employees b -- select statement
inner join ProjectDetails.TimeCards c on b.Employee_ID = c.Employee_ID -- first join seems to be the only one working.
inner join (select d.Project_Name as Project_Name, Project_ID from ProjectDetails.Projects d) as d on d.Project_ID=c.Project_ID -- second join doesn't seem to work.
The use of the subquery is redundunt at best, and also the common alias of "d" may be a source of error.
Just do:
select b.First_Name, b.Title, c.Project_ID
from HumanResources.Employees b
inner join ProjectDetails.TimeCards c on b.Employee_ID = c.Employee_ID
inner join ProjectDetails.Projects d on d.Project_ID=c.Project_ID
In the inner query for Project_ID use column alias name and then join on the alias column name.also try to have a different alias name for the sub query.
I don't see the benefit of using a subquery in your query. Could you try the below query?
Make sure in your table Projects has matching Project_ID otherwise of course nothing would come up.
SELECT b.First_Name
,b.Title
,c.Project_ID
FROM HumanResources.Employees b
INNER JOIN ProjectDetails.TimeCards c ON b.Employee_ID = c.Employee_ID
INNER JOIN ProjectDetails.Projects d ON d.Project_ID = c.Project_ID
I have 4 tables, in that I want to fetch records from all 4 and aggregate the values
I have these tables
I am expecting this output
but getting this output as a Cartesian product
It is multiplying the expenses and allocation
Here is my query
select
a.NAME, b.P_NAME,
sum(a.DURATION) DURATION,
sum(b.[EXP]) EXPEN
from
(select
e.ID, a.P_ID, e.NAME, a.DURATION DURATION
from
EMPLOYEE e
inner join
ALLOCATION a ON e.ID = a.E_ID) a
inner join
(select
p.P_ID, e.E_ID, p.P_NAME, e.amt [EXP]
from
PROJECT p
inner join
EXPENSES e ON p.P_ID = e.P_ID) b ON a.ID = b.E_ID
and a.P_ID = b.P_ID
group by
a.NAME, b.P_NAME
Can anyone suggest something about this.
The following should work:
SELECT e.Name,p.Name,COALESCE(d.Duration,0),COALESCE(exp.Expen,0)
FROM
Employee e
CROSS JOIN
Project p
LEFT JOIN
(SELECT E_ID,P_ID,SUM(Duration) as Duration FROM Allocation
GROUP BY E_ID,P_ID) d
ON
e.E_ID = d.E_ID and
p.P_ID = d.P_ID
LEFT JOIN
(SELECT E_ID,P_ID,SUM(AMT) as Expen FROM Expenses
GROUP BY E_ID,P_ID) exp
ON
e.E_ID = exp.E_ID and
p.P_ID = exp.P_ID
WHERE
d.E_ID is not null or
exp.E_ID is not null
I've tried to write a query that will produce results where e.g. there are rows in Expenses but no rows in Allocations (or vice versa) for some particular E_ID,P_ID combination.
Use left join in select query by passing common id for all table
Hi I got the answer what I want from some modification in the query
The above query is also working like a charm and have done some modification to the original query and got the answer
Just have to group by the inner queries and then join the queries it will then not showing Cartesian product
Here is the updated one
select a.NAME,b.P_NAME,sum(a.DURATION) DURATION,sum(b.[EXP]) EXPEN from
(select e.ID,a.P_ID, e.NAME,sum(a.DURATION) DURATION from EMPLOYEE e inner join ALLOCATION a
ON e.ID=a.E_ID group by e.ID,e.NAME,a.P_ID) a
inner join
(select p.P_ID,e.E_ID, p.P_NAME,sum(e.amt) [EXP] from PROJECT p inner join EXPENSES e
ON p.P_ID=e.P_ID group by p.P_ID,p.P_NAME,e.E_ID) b
ON a.ID=b.e_ID and a.P_ID=b.P_ID group by a.NAME,b.P_NAME
Showing the correct output
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
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;