SQL joining tables issue - sql

So I am trying to run a statement in SSMS like:
SELECT Project.PROJNAME FROM PROJECT
JOIN SHIPMENT ON SHIPMENT.SNUM = SUPPLIERS.SNUM
JOIN PARTS ON PARTS.PNUM = SHIPMENT.PNUM
JOIN SUPPLIERS ON PROJECT.PROJNUM = SHIPMENT.PROJNUM
WHERE SUPPLIERS.SNAME='S1' AND SUPPLIERS.SNAME='S2'
However, when I do, I have an issue with the suppliers.snum portion on line 2 of the query. It tells me the multi-part identifier cannot be bound. I have looked at several ways to rectify the problem, but for some reason its just not sinking in for the understanding on the how and why. Could someone please explain how to fix this and why exactly the current way does not work? Thanks guys, cheers.

your query looks very strange for me, try this version with proper order:
SELECT Project.PROJNAME
FROM
PROJECT
JOIN
SHIPMENT
ON PROJECT.PROJNUM = SHIPMENT.PROJNUM
JOIN
PARTS
ON SHIPMENT.PNUM = PARTS.PNUM
JOIN
SUPPLIERS
ON SHIPMENT.SNUM = SUPPLIERS.SNUM
WHERE
SUPPLIERS.SNAME IN ('S1', 'S2')

Related

SQL join 4 tables with WHERE query

I am trying to join 4 tables together as shown in this diagram here:
https://imgur.com/a/jukJvSw
The SQL query I have written returns all fields except TExpiryDate and I have not come across any examples online that can help me understand this. Please help.
SELECT tbPurchaseHeader.PurchaseDate,
tbSupplier.CompanyName,
tbPurchaseDetails.UnitCost,
tbPurchaseDetails.Quantity,
tbPurchaseDetails.Bonus,
tbpurchasedetails.BatchID,
tbBatch.TExpiryDate
FROM ((tbPurchaseDetails
INNER JOIN tbPurchaseHeader
ON tbPurchaseDetails.PurchaseID = tbPurchaseHeader.PurchaseID)
LEFT JOIN tbBatch
ON tbPurchaseDetails.BID = tbBatch.BID)
INNER JOIN tbSupplier
ON tbPurchaseHeader.SupplierID = tbSupplier.SupplierID
WHERE tbPurchaseDetails.ProductID = ?
ORDER BY tbPurchaseHeader.PurchaseDate
Turns out BID contains empty values in the database. I have decided to make links elsewhere to get the data. Thanks everyone for making me realise this.

The multi-part identifier "t.tag_instrument_id_fk" could not be bound

I'm new here specifically for this problem...I already search every topic and none has helped me so far.
I am trying to create a little report but the error that "could not be bound" is hunting me.
select i.ins_serial_number AS "Serial",
t.tag_tag AS "TAG",
tg.tcv_strategy_id_ck AS "Estrategia",
tg.tcv_cal_value AS "Valor",
tg.tcv_error_tolerance as "Crit Aceit",
un.un_unit as "Unidade"
from tag_cal_values as tg,
units as un,
tags as t,
inst_scales as sc
left outer join instruments as i
on i.ins_instrument_id_pk = t.tag_instrument_id_fk
where t.tag_tag_id_pk = tg.tcv_tag_id_ck
and un.un_unit_id_pk = sc.isc_input_unit_id_fk
message 4104, level 16, state 1, line 1
The multi-part identifier "t.tag_instrument_id_fk" could not be bound.
The column name, table, etc is all okay.
obs: actuall I'm not expert in SQL, so...
I'm running this query in SQL Server Management Studio 2008.
Thanks :)
Never use commas in the FROM clause. Always use proper, explicit, standard JOIN syntax:
from tags t JOIN
tag_cal_values tg
on t.tag_tag_id_pk = tg.tcv_tag_id_ck cross join
units un join
inst_scales as sc
un.un_unit_id_pk = sc.isc_input_unit_id_fk left join
instruments as i
on i.ins_instrument_id_pk = t.tag_instrument_id_fk
This will fix the syntax problem. I doubt this does anything useful, though. It looks like you are missing one or more join conditions.
From what I can tell tags is trying to use the fk from instruments to join in with the rest of the tables. Since instruments comes later in the query it is messing it up. For future reference you really should not be mixing your join styles, I personally like doing INNER JOIN, LEFT OUTER JOIN etc for every table I am using.

MS Access 2016 Error: "Multi-level group by not allowed"

i'm stuck at another point in my little Access 2016 Database. My code looks like the following and i know it probably isn't the cleanest solution but i'm kinda new to this and i tried to educate myself and get some help here already.
I'm trying to play around with the reports now a little bit and i am using this test query which returns all entries of two tables joined together.
As far as i could find out I have this one subquery included that returns the prvious day inventory for each record and that is most likely the cause of my error. I found a possible solution with adding SELECT * FROM at the beginning of my code but i get a Syntax error when i do that and i'm not sure how to solve this problem.
here's my code
SELECT Stations.StationName, Product.ProductName, GasInventoryTransactions.TransactionDate, (SELECT TOP 1 Dupe.ActualInventory FROM GasInventory AS Dupe WHERE Dupe.StationID = Stations.StationID AND Dupe.ProductID = Product.ProductID AND Dupe.InventoryDate < GasInventory.InventoryDate ORDER BY Dupe.InventoryDate DESC) AS PreviousDayInventory, GasInventory.ActualInventory, GasInventoryTransactions.GasSales, GasInventoryTransactions.GasDelivery, [PreviousDayInventory]+[GasDelivery]-[GasSales] AS BookBalance, GasInventory.ActualInventory, [ActualInventory]-[BookBalance] AS OverShort
FROM (Stations INNER JOIN (Product INNER JOIN GasInventory ON Product.[ProductID] = GasInventory.[ProductID]) ON Stations.[StationID] = GasInventory.[StationID]) INNER JOIN GasInventoryTransactions ON GasInventory.[InventoryDate] = GasInventoryTransactions.[TransactionDate];
thanks for your help!

SQL syntax error using joins

Hoping I can get some help with my SQL syntax. I haven't been able to fix the problem on my own. I used a syntax checker which it says my code is good but I'm getting an error. Any help is greatly appreciated!
SELECT DATALIVE.CO_ALLOCATION_TAIL.PO_KEY,
DATALIVE.CO_ALLOCATION_TAIL.SO_KEY,
DATALIVE.CO_PICK_LOTS_DETAIL.SO_KEY,
Sum(DATALIVE.CO_ALLOCATION_TAIL.QTY_ALLOC) AS SumOfQTY_ALLOC,
Sum(DATALIVE.CO_ALLOCATION_TAIL.PO_ALLOC_QTY) AS SumOfPO_ALLOC_QTY,
Sum(DATALIVE.CO_PICK_LOTS_DETAIL.QTY) AS Picked_Qty,
Min(DATALIVE.CO_ALLOCATION_TAIL.ALLOC_DATE) AS MinOfALLOC_DATE,
Max(DATALIVE.CO_ALLOCATION_TAIL.ALLOC_DATE) AS MaxOfALLOC_DATE,
DATALIVE.CO_SORDER.STATUS
FROM (DATALIVE.CO_ALLOCATION_TAIL
INNER JOIN DATALIVE.CO_SORDER.SO_KEY
ON DATALIVE.CO_ALLOCATION_TAIL.SO_KEY = DATALIVE.CO_SORDER.SO_KEY)
INNER JOIN DATALIVE.CO_PICK_LOTS_DETAIL
ON DATALIVE.CO_ALLOCATION_TAIL.SO_KEY = DATALIVE.CO_PICK_LOTS_DETAIL.SO_KEY
GROUP BY DATALIVE.CO_ALLOCATION_TAIL.PO_KEY,
DATALIVE.CO_ALLOCATION_TAIL.SO_KEY,
DATALIVE.CO_SORDER.STATUS,
DATALIVE.CO_PICK_LOTS_DETAIL.SO_KEY
HAVING (((DATALIVE.CO_SORDER.STATUS) = 'O'))
INNER JOIN DATALIVE.CO_SORDER.SO_KEY
That's a column, not a table. Needs a table, like DATALIVE.CO_SORDER.

relationships produce duplicate records on query, DISTINCT does not work, any other solutions available?

I am new to this portal. I have a very simple problem to be solved. It is related to the ANSI SQL. I am writing a reports using BIRT and I am fetching the data from several tables. I understand how the SQL joins work but maybe not fully. I researched google for hours and I could not find relevant answer.
My problem is that one of the relationships in the code produce a duplicate result (the same row is copied - duplicated). I was so determined to solve it I used every type of join available. Some of this SQL was produced already. I shall post my code below. I know that one of the solutions to my problem is use of the 'DISTINCT' keyword. I have used it and it does not solve my problem.
Can anyone propose any solution to that?
Sample code:
SELECT DISTINCT
partmaster.partdesc,
partmaster.uom,
traders.name AS tradername,
worksorders.id AS worksorderno,
worksorders.partid,
worksorders.quantity,
worksorders.duedate,
worksorders.traderid,
worksorders.orderid,
routingoperations.partid,
routingoperations.methodid,
routingoperations.operationnumber,
routingoperations.workcentreid,
routingoperations.settime,
routingoperations.runtime,
routingoperations.perquantity,
routingoperations.description,
routingoperations.alternativeoperation,
routingoperations.alternativeoperationpreference,
machines.macdesc,
machines.msection,
allpartmaster.partnum,
allpartmaster.nbq,
allpartmaster.partdesc,
routingoperationtools.toolid,
tools.tooldesc,
CAST (emediadetails.data as VARCHAR(MAX)) AS cplandata
FROM worksorders
INNER JOIN partmaster ON worksorders.partid = partmaster.partnum
INNER JOIN traders traders ON worksorders.traderid = traders.id
INNER JOIN routingoperations routingoperations ON worksorders.partid = routingoperations.partid
AND worksorders.routingmethod = routingoperations.methodid
INNER JOIN allpartmaster allpartmaster ON routingoperations.partid = allpartmaster.partnum
LEFT OUTER JOIN machines machines ON routingoperations.workcentreid = machines.macid
LEFT OUTER JOIN routingoperationtools routingoperationtools ON routingoperationtools.partid = routingoperations.partid
AND routingoperationtools.routingmethod = routingoperations.methodid
AND routingoperationtools.operationnumber = routingoperations.operationnumber
LEFT OUTER JOIN tools tools ON tools.toolid = routingoperationtools.toolid
LEFT OUTER JOIN emediadetails ON emediadetails.keyvalue1 = worksorders.id
AND emediadetails.keyvalue2 = routingoperations.operationnumber
AND emediadetails.emediaid = 'worksorderoperation'
I do not have too much of the test data but I know that one row is copied twice as the result of the query below even tho I used DISTINCT keyword. I know that my problem is rather specific and not general but the solution that someone will propose may help others with the similar problem.
I can't solve your problem for you without some test data, but I have some helpful hints.
In principle, you should be really careful with DISTINCT - its a great way of hiding bugs in your query. Only use DISTINCT if you are confident that the underlying data contains legitimate duplicates. If your joins are wrong, and you're getting a cartesian product, you can remove the duplicates from the results with DISTINCT - but that doesn't stop the cartesian product being generated. You'll get very poor performance, and possibly incorrect data.
Secondly, I am pretty sure that DISTINCT works properly - you are almost certainly not getting duplicates, but it may be hard to spot the difference between two rows. Leading or trailing spaces in text columns, for instance could be to blame.
Finally, to work through this problem, I'd recommend building the query up join by join, and seeing where you get the duplicate - that's the join that's to blame.
So, start with:
SELECT
traders.name AS tradername,
worksorders.id AS worksorderno,
worksorders.partid,
worksorders.quantity,
worksorders.duedate,
worksorders.traderid,
worksorders.orderid
FROM worksorders
INNER JOIN traders traders ON
worksorders.traderid = traders.id
and build up to the next join.
Are you sure the results are exact duplicates? Makes sure there isn't one column that actually has a different value.