SQL syntax error using joins - sql

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.

Related

SQL Query for Reporting

I am trying to join two tables and getting the data for my report. Is there any best way to get the details as per below report?
I have tried below query and getting the results. Please find my query.
SELECT wl.trasaction_id
,wl.supplier_id
,wl.sp_patientpid
,wl.dm_id AS original_dm_id
,NVL(cr.new_dm_id,wl.dm_id) AS out_dm_id
FROM weekly_load wl
LEFT JOIN cross_ref cr
ON wl.sp_patientpid = cr.sp_patient_id
AND wl.supplier_id = cr.ims_supplier_id
Kindly help me.
Many thanks.
You may need an additional join condition on the dm ids:
SELECT wl.trasaction_id, wl.supplier_id, wl.sp_patientpid, wl.dm_id AS original_dm_id,
COALESCE(cr.new_dm_id, wl.dm_id) AS out_dm_id
FROM weekly_load wl LEFT JOIN
cross_ref cr
ON wl.sp_patientpid = cr.sp_patient_id AND
wl.supplier_id = cr.ims_supplier_id AND
wl.dm_id = cr.old_dm_id;

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.

Error Trying to do Left Outer Join BigQuery

I'm trying to do a relatively simple outer join but quite new to Big Query and I'm getting the following Error: internal error: missing closing bracket at: 2.3 - 2.39
SELECT
([130493328.ga_sessions_20170312].date),
([130493328.ga_sessions_20170312].total.visits),
([130493328.social].engagedUsers)
FROM ([130493328.ga_sessions_20170312]),
LEFT OUTER JOIN
[130493328.social]
ON
([130493328.ga_sessions_20170312].date) = ([130493328.social].date);
Could someone let me know where I'm going wrong?
Thanks
Try writing this with table aliases:
SELECT ga.date, ga.total.visits, s.engagedUsers
FROM [130493328.ga_sessions_20170312] ga LEFT OUTER JOIN
[130493328.social] s
ON ga.date = s.date;
You should also check if you are using Legacy SQL or Standard SQL. The square braces would not be appropriate in Standard SQL.

SQL joining tables issue

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')

Query is too complex error in ms access?

I tried to bind 3 queries into single query, using this code but when i click "Datasheet View" it showing error "QUERY IS TOO COMPLEX".
This is my code
SELECT
RPT_Invoice_Less.InvoiceNumber,
RPT_Invoice_Less.Terms,
RPT_Invoice_Less.Invoicedate,
RPT_Invoice_Less.OurQuote,
RPT_Invoice_Less.SalesPerson,
RPT_Customer.CustomerName,
RPT_Customer.CustomerId,
RPT_Customer.ContactPerson,
RPT_Customer.BillingAddress,
RPT_Customer.DeliveryAddress,
RPT_Invoice_Less.OrderNumber,
RPT_Invoice_Less.ShippingBy,
RPT_Invoice_Less.ShipReferenceNo,
RPT_Invoice_Less.Notes,
RPT_Invoice_Less.Price,
RPT_Invoice_Less.Discount,
RPT_Invoice_Less.Shipping,
RPT_Invoice_Less.Tax,
RPT_Invoice_Less.GrandTotal,
RPT_Company.CompanyName,
RPT_Company.CompanyId,
RPT_Company.RegistrationNumber,
RPT_Company.Address,
RPT_Company.MobileNumber,
RPT_Company.FaxNumber,
RPT_Company.CompanyEmail,
RPT_Company.CompanyWebsite,
RPT_Company.VatTinNumber
FROM
(RPT_Invoice_Less
INNER JOIN RPT_Customer
ON RPT_Invoice_Less.CustomerId=RPT_Customer.CustomerId)
INNER JOIN
RPT_Company
ON RPT_Invoice_Less.CompanyId=RPT_Company.CompanyId;
Try to use the built in designer to reproduce as close as you can, if not replicate the query, I get the impression looking at that there maybe an issue around the FROM part of that query
Thank you guys finally I solved with your ideas and my current code i pasted below
SELECT RPT_Invoice_Less.InvoiceNumber, RPT_Invoice_Less.Terms, RPT_Invoice_Less.Invoicedate, RPT_Invoice_Less.OurQuote, RPT_Invoice_Less.SalesPerson,
RPT_Customer.CustomerName, RPT_Customer.CustomerId, RPT_Customer.ContactPerson, RPT_Customer_Address.BillingAddress, RPT_Customer_Address.DeliveryAddress, RPT_Invoice_Less.OrderNumber, RPT_Invoice_Less.ShippingBy, RPT_Invoice_Less.ShipReferenceNo, RPT_Invoice_Less.Notes, RPT_Invoice_Less.Price, RPT_Invoice_Less.Discount, RPT_Invoice_Less.Shipping, RPT_Invoice_Less.Tax, RPT_Invoice_Less.GrandTotal,
RPT_Company.CompanyName, RPT_Company.CompanyId, RPT_Company.RegistrationNumber, RPT_Company_Address.Address, RPT_Company.MobileNumber, RPT_Company.FaxNumber, RPT_Company.CompanyEmail, RPT_Company.CompanyWebsite, RPT_Company.VatTinNumber
FROM (((RPT_Invoice_Less INNER JOIN RPT_Customer ON RPT_Invoice_Less.CustomerId = RPT_Customer.CustomerId) INNER JOIN RPT_Company ON RPT_Invoice_Less.CompanyId = RPT_Company.CompanyId) INNER JOIN RPT_Company_Address ON RPT_Invoice_Less.CompanyId = RPT_Company_Address.AddressId) INNER JOIN RPT_Customer_Address ON RPT_Invoice_Less.CustomerId = RPT_Customer_Address.CustomerId;
This code working successfull.