I need to execute a query that gets everything from the table Incidents based on a CustomerID. The CustomerID comes from the MasterAccounts table, which is not directly related to the Incidents table. There is a SubAccounts table that could be INNER JOIN-ed to relate the two.
Here is what I have so far:
SELECT
*
FROM
MasterAccounts AS ma
INNER JOIN
SubAccounts AS sa
INNER JOIN
Incidents AS i
WHERE
ma.CustomerID = sa.CustomerID;
AND sa.AccountID = i.AccountID
AND IncidentTypeID = 11;
This is failing with
Incorrect syntax near the keyword 'WHERE'.
Any thoughts would be greatly appreciated!
You need to define join conditions with ON after your INNER JOIN, to link the two tables:
SELECT
*
FROM
MasterAccounts AS ma
INNER JOIN
SubAccounts AS sa ON ma.CustomerID = sa.CustomerID;
INNER JOIN
Incidents AS i ON sa.AccountID = i.AccountID
WHERE
IncidentTypeID = 11;
Those columns must exist in the tables involved, and should have the same datatype (because otherwise the JOIN might cause a "hidden" data type conversion which can be costly in terms of performance)
Most of the WHERE conditions should be in ON clauses for each join:
SELECT i.*
FROM MasterAccounts ma
INNER JOIN SubAccounts sa ON sa.CustomerID = ma.CustomerID
INNER JOIN Incidents i ON i.AccountID = sa.AccountID
WHERE IncidentTypeID = 11;
The correct syntax would be:
SELECT
*
FROM
MasterAccounts AS ma
INNER JOIN
SubAccounts AS sa ON ma.CustomerID = sa.CustomerID;
INNER JOIN
Incidents AS i ON sa.AccountID = i.AccountID
WHERE
IncidentTypeID = 11;
Hope this helps!
Related
I am trying to create normalization 3 nf (normal form) in this database. However, when I execute the query, the table is empty. As you will see, this is my table and diagram.
Here is the relationship that we want to 3NF
Torder->(Customer-Food-Carrier-Waiter)-(Ternary Relationship)-(Customer_id,Food_id,Carrier_id,Waiter_id)
Here is my query
SELECT
Customers.ID AS CustomerID,
Food.ID AS FoodID,
Carrier.ID AS CarrierID,
Waiter.ID AS WaiterID,
tOrder.ID AS TORDERID
FROM
tOrder
INNER JOIN
Customers ON Customers.ID = tOrder.Customer_id
INNER JOIN
Food ON Food.ID = tOrder.Food_id
INNER JOIN
Carrier ON Carrier.ID = tOrder.Carrier_id
INNER JOIN
Waiter ON Waiter.ID = tOrder.Waiter_id
ORDER BY tOrder.ID;
Clearly, you have an issue where some of the tables are empty or the ids do not match. You can use LEFT JOIN to keep all orders and see what is happening:
FROM tOrder o LEFT JOIN
Customers c
ON c.ID = o.Customer_id LEFT JOIN
Food f
ON f.ID = o.Food_id LEFT JOIN
Carrier ca
ON ca.ID = o.Carrier_id LEFT JOIN
Waiter w
ON w.ID = o.Waiter_id
If there are no matches, then the orders are still in the results, with NULL for the values in the table with no matches.
Note that this also introduces table aliases, so the query is easier to write and to read.
SELECT agents.aname,products.pname,customers.cname
FROM products INNER JOIN
(customers INNER JOIN agents
(INNER JOIN orders ON orders.pid=products.pid)
ON orders.cid=customers.cid)
ON orders.aid=agents.aid;
I am getting an error in line 4 for a missing keyword. Any ideas?
You need to add the ON condition after each table JOIN
SELECT agents.aname,
products.pname,
customers.cname
FROM products
INNER JOIN orders
ON orders.pid = products.pid
INNER JOIN customers
ON orders.cid = customers.cid
INNER JOIN agents
ON orders.aid = agents.aid;
I'm trying to find "the number of outstanding invoices for a given contract rate".
SQL Fiddle here (updated with ujjwal's answer to add a distinct)
You'll notice I have 350 outstanding invoices which is a lot more than the 14 I expected so I guess I haven't restricted the INNER JOIN's correctly...
Any ideas would be great thanks (sorry I'm rusty on SQL), this is for the latest version of Oracle.
Wayne
Can you try selecting only distinct entries:
SELECT
r.id AS rate_id,
COUNT(distinct mi.id) AS outstanding_invoices **change here**
FROM
contract_rate r
INNER JOIN
contract c ON r.contract_id = c.id
INNER JOIN
contractor con ON con.id = c.contractor_id
INNER JOIN
maintenance_item item ON item.contract_rate_id = r.id
INNER JOIN
maintenance_invoice mi ON mi.contractor_id = c.contractor_id AND mi.status = 'Awaiting Approval'
GROUP BY
r.id
I have 3 tables.
First one:
Stores(StoreID, Name)
Second one:
Store_Items(StoreID, ItemID)
Third one:
Items(ItemID, Name, Price)
How can I write a single statement, which get all StoreID and all names and all items for each StoreID?
Should I use Join or something?
select *
from Store as S
left outer join Store_Items as SI on SI.StoreID = S.StoreID
left outer join Items as I on I.ItemID = SI.ItemID
select s.storeid, s.name, si.itemid, i.name, i.price
from store s
left join store_item si on si.storeid = s.storeid
left join item i on i.itemid = store.itemid
Yes, you should join the tables roughly as such
SELECT * FROM `firstone`
LEFT OUTER JOIN `secondone`
ON firstone.StoreId = secondone.StoreID
LEFT OUTER JOIN `thirdone`
ON secondone.ItemID = thirdone.ItemID
I need to go to two tables to get the appropriate info
exp_member_groups
-group_id
-group_title
exp_members
-member_id
-group_id
I have the appropriate member_id
So I need to check the members table, get the group_id, then go to the groups table and match up the group_id and get the group_title from that.
INNER JOIN:
SELECT exp_member_groups.group_title
FROM exp_members
INNER JOIN exp_member_groups ON exp_members.group_id = exp_member_groups.group_id
WHERE exp_members.member_id = #memberId
SELECT g.group_title
FROM exp_members m
JOIN exp_member_groups g ON m.group_id = g.group_id
WHERE m.member_id = #YourMemberId
If there is always a matching group, or you only want rows where it is, then it would be an INNER JOIN:
SELECT g.group_title
FROM exp_members m
INNER JOIN
exp_member_groups g
ON m.group_id = g.group_id
WHERE m.member_id = #member_id
If you want rows even where group_id doesn't match, then it is a LEFT JOIN - replace INNER JOIN with LEFT JOIN in the above.