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;
Related
Column 'company_id' in field list is ambiguous
It does not seem "ambiguous", I have no idea where I should fix it:
SELECT company_id, companies.name
FROM company_contracts AS contracts
LEFT OUTER JOIN companies ON companies.id = contracts.company_id
LEFT OUTER JOIN order_logs AS logs ON companies.id = logs.company_id;
Because company_id appears on both table company_contracts and order_logs,you need to specify it
SELECT contracts.company_id,c1.name
FROM company_contracts as contracts
LEFT OUTER JOIN companies as c1 on c1.id = contracts.company_id
LEFT OUTER JOIN order_logs as logs on c1.id = logs.company_id;
You should qualify all columns names in such a query. In addition, if you really want outer joins, the second join condition should refer to the first table, not the second:
SELECT cc.company_id, c.name
FROM company_contracts cc LEFT OUTER JOIN
companies c
ON c.id = cc.company_id LEFT OUTER JOIN
order_logs ol
ON cc.company_id = ol.company_id;
Or, more likely, you want to keep all companies and the query should look like:
SELECT c.id, c.name
FROM companies c LEFT OUTER JOIN
company_contracts cc
ON c.id = cc.company_id LEFT OUTER JOIN
order_logs ol
ON c.id = ol.company_id;
I have a query with a not in clause like this:
select *
FROM COMPANY c
where c.company_id not In (SELECT SenderId
FROM CrossRef)
and c.id not in (select company_id
FROM USER)
I am wondering if there is a way to re-write that query using a left join in SQL Server 2008.
I tried the following one but it's not giving the correct result
select c.id, c.company_id
from COMPANY c
left join CrossRef cr on c.company_id != cr.senderid, COMPANY c1
left join USER u on c1.id != u.company_id
SELECT *
FROM Company C
LEFT JOIN CrossRef R ON R.SenderID = C.CompanyID
LEFT JOIN [User] U ON U.company_id = C.id
WHERE R.SenderID IS NULL
AND U.company_id IS NULL
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
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 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