INNER JOIN Distinct ID - sql

I have the following code:
FROM CTE_Order cte
INNER JOIN tblOrders o
ON cte.OrderId = o.Id
INNER JOIN tblOrderUnits ou
ON o.id = ou.OrderId
INNER JOIN tblOrderServiceUnits osu
ON ou.VMSUnitID = osu.UnitId
When I join the ou I get 2 of the same unit Id's. This make the Inner Join tblOrderServiceUnits return 4 rows with 2 being duplicates. I need it to only return the 2 rows the are different. How do I use a distinct to Inner Join only distinct ou.id?
Sorry for the bad explanation but basically I am jsut trying to see how an INNER JOIN with a distinct subquery would work, If someone could give me an example of that I could figure it out from there.

INNER JOIN (SELECT DISTINCT * FROM X) Alias
ON Alias.ID = Primary.ID
For your example:
INNER JOIN (SELECT DISTINCT VMSUnitID, OrderId FROM tblOrderUnits) ou
ON o.id = ou.OrderId

Related

Find the nth highest

SELECT CONCAT(C.CUSTOMER_FNAME, ' ',C.CUSTOMER_LNAME) AS FullNAME, SUM(QTY* PRICE)AS TOTAL_SPENDINGS
FROM (SELECT DISTINCT TOP 2 TOTAL_SPENDINGS) RESULT
Customers$ C INNER JOIN Invoices$ Inv ON C.CUSTOMER_ID=Inv.CUSTOMER_ID
INNER JOIN InvDetails$ InvD ON Inv.INVOICE_ID=InvD.INVOICE_ID
INNER JOIN Products$ P ON P.PRODUCT_ID=InvD.PRODUCT_ID
GROUP BY C.CUSTOMER_FNAME,C.CUSTOMER_LNAME
ORDER BY TOTAL_SPENDINGS DESC
I am trying to prin the k-highest spending customers this is what i have done until now but i get
Incorrect syntax near 'Customers$'.
You are missing the join condition between the calculated table RESULT and the Customers table:
SELECT CONCAT(C.CUSTOMER_FNAME, ' ',C.CUSTOMER_LNAME) AS FullNAME, SUM(QTY* PRICE)AS TOTAL_SPENDINGS
FROM (SELECT DISTINCT TOP 2 TOTAL_SPENDINGS) RESULT
<missing INNER / LEFT join here>
Customers$ C <missing ON here> INNER JOIN Invoices$ Inv ON C.CUSTOMER_ID=Inv.CUSTOMER_ID
INNER JOIN InvDetails$ InvD ON Inv.INVOICE_ID=InvD.INVOICE_ID
INNER JOIN Products$ P ON P.PRODUCT_ID=InvD.PRODUCT_ID
GROUP BY C.CUSTOMER_FNAME,C.CUSTOMER_LNAME
ORDER BY TOTAL_SPENDINGS DESC

Select every value (even when it is null) with inner join sql

This is my sql select statment
SELECT k.name, c.name AS nameCustomer, o.*
FROM offertes o
INNER JOIN customers k
ON o.idCustomer= k.id
INNER JOIN contactperson c
ON o.idContact = c.id;
When o.idContact doesn't exist, then there will be no values selected. I wan't to get NULL instead of nothing. It still need to SELECT the whole row! Can anyone help me?
ps. I think it's going wrong with the inner join (ON o.idContact = c.id);
Try this:
Replace your last INNER JOIN with LEFT JOIN.
Using LEFT JOIN you tell my main table (offertes) returns always result but if in secondary table (contactperson) there's no row matches returns NULL all fields of that table
SELECT k.name, c.name AS nameCustomer, o.*
FROM offertes o
INNER JOIN customers k
ON o.idCustomer= k.id
LEFT JOIN contactperson c
ON o.idContact = c.id;
You'll need an outer join instead.
e.g.
SELECT k.name, c.name AS nameCustomer, o.*
FROM customers k LEFT OUTER JOIN
(offertes o INNER JOIN
contactperson c
ON o.idContact = c.id)
ON o.idCustomer= k.id;
(I assume there'll be a counterpart of an offerte in contactperson, else you'll need an outer join there as well)
you need to rewrite a query using outer join
SELECT k.name, c.name AS nameCustomer, o.*
FROM customers k
LEFT OUTER JOIN offertes o ON o.idCustomer = k.id
LEFT OUTER JOIN contactperson c ON o.idContact = c.id;

How to make, count row result and become a column -MS ACCESS QUERY

SELECT DISTINCT Customers.Name, Customers.Stall_Number, Animal_Types.Code, Count(Animal_Types.Code) AS CountOfCode, Sum(Transaction_Details.Weight) AS TotalWeight
FROM (Customers INNER JOIN Transactions ON Customers.ID = Transactions.Customer) INNER JOIN ((Animal_Types INNER JOIN Carcass ON Animal_Types.ID = Carcass.Animal_Type) INNER JOIN Transaction_Details ON Carcass.ID = Transaction_Details.Carcass) ON Transactions.ID = Transaction_Details.Transaction
GROUP BY Customers.Name, Customers.Stall_Number, Animal_Types.Code;
Please refer to images below for more details.
To get rows to become columns, you will need to use a crosstab query:
TRANSFORM Count(Animal_Types.Code)
SELECT Customers.Name, Customers.Stall_Number, Sum(Transaction_Details.Weight) AS TotalWeight
FROM (Customers INNER JOIN Transactions ON Customers.ID = Transactions.Customer) INNER JOIN ((Animal_Types INNER JOIN Carcass ON Animal_Types.ID = Carcass.Animal_Type) INNER JOIN Transaction_Details ON Carcass.ID = Transaction_Details.Carcass) ON Transactions.ID = Transaction_Details.Transaction
GROUP BY Customers.Name, Customers.Stall_Number, Animal_Types.Code
PIVOT Animal_Types.Code

Select 3 rows in SQL missing keyword

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;

LEFT OUTER JOIN EQUIVALENT

I have a tables contains null values. In ORDER table i have 2 null in PART_ID section and 2 null values in CUSTOMER_ID.
And i have that kind of query:
SELECT O.ORDER_ID , O.ORDER_DATE , O.CUST_ID, O.QUANTITY ,O.PART_ID ,
C.CUST_NAME, C.CUST_CODE, P.PART_NAME, P.PART_CODE
FROM [ORDER] O
LEFT OUTER JOIN PART P ON P.PART_ID = O.PART_ID
LEFT OUTER JOIN CUSTOMER C ON C.CUST_ID = O.CUST_ID
So here is my question. How can i do it without using outer join ?
I tried too many things including where not exists or this ;
SELECT *
FROM [ORDER] O ,CUSTOMER C, PART P
WHERE C.CUST_ID = (
SELECT CUST_ID FROM CUSTOMER C WHERE O.CUST_ID = C.CUST_ID
) AND P.PART_ID = (SELECT PART_ID FROM PART P WHERE O.PART_ID = P.PART_ID)
but i couldn't find solution. If there is a solution how it will be ?
(Note: this is homework.)
I have that kind of table :
and left outer join gives that :
the hw said do it without using outer join and get same table as left outer join gives. But like a said i coulnd't. I'm also using MSSQL.
Outer join produces super-set over inner join. Indeed, from Wikipedia: A left outer join returns all the values from an inner join plus all values in the left table that do not match to the right table.
So to model left outer join using inner join one could use UNION of inner join SELECT between same tables with same join condition and another SELECT from 1st table that returns all rows without a match from the right table (I reduced your case to a single left join):
SELECT O.ORDER_ID , O.ORDER_DATE , O.CUST_ID, O.QUANTITY ,O.PART_ID ,
P.PART_NAME, P.PART_CODE
FROM [ORDER] O JOIN PART P ON P.PART_ID = O.PART_ID
UNION
SELECT O.ORDER_ID , O.ORDER_DATE , O.CUST_ID, O.QUANTITY ,O.PART_ID ,
NULL, NULL
FROM [ORDER] O
WHERE NOT EXISTS (SELECT 'found' FROM PART P WHERE P.PART_ID = O.PART_ID)
Presumably, you want to get matches to the columns with NULL values, instead of having them fail. If so, just modify the join conditions:
FROM [ORDER] O
LEFT OUTER JOIN PART P
ON P.PART_ID = O.PART_ID or (p.Part_id is NULL and o.Part_id is null)
LEFT OUTER JOIN CUSTOMER C
ON C.CUST_ID = O.CUST_ID or (c.cust_id is null and o.cust_id is null)
The major issue with this approach is that many (most?) SQL engines will not use indexes for the join.
Is there a specific reason why you don't want to use outer join? Isn't this the result you want? :
FROM [ORDER] O
LEFT JOIN PART P
ON P.PART_ID = O.PART_ID and P.PARTID is not null
LEFT JOIN CUSTOMER C
ON C.CUST_ID = O.CUST_ID and C.CUSTID is not null
So complete answer should be like this (after my teacher gave results) :
SELECT O.ORDER_ID,O.ORDER_DATE,O.CUST_ID,
(SELECT C.CUST_CODE FROM CUSTOMER C WHERE C.CUST_ID=O.CUST_ID) AS CUST_CODE,
(SELECT C.CUST_NAME FROM CUSTOMER C WHERE C.CUST_ID=O.CUST_ID) AS CUST_NAME,
O.PART_ID,
(SELECT P.PART_CODE FROM PART P WHERE P.PART_ID = O.PART_ID ) AS PART_CODE,
(SELECT P.PART_NAME FROM PART P WHERE P.PART_ID = O.PART_ID ) AS PART_NAME,
O.QUANTITY
FROM [ORDER] O