SQL Query for Reporting - sql

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;

Related

Use group by with sum in query

These 3 tables that you see in the image are related
Course table and coaching table and sales table
I want to make a report from this table on how much each coach has sold by each course period.
The query I created is as follows, but unfortunately it has a problem and I do not know where the problem is.
Please help me fix the problem
Thank you
SELECT
dbo.tblCustomersOrders.id, dbo.tblCustomersOrders.pid, dbo.tblPost.postTitle,
dbo.tblArticleAuthor.authorName, SUM(dbo.tblCustomersOrders.prodPrice) AS TotalBuys
FROM
dbo.tblPost
INNER JOIN
dbo.tblArticleAuthor ON dbo.tblPost.id = dbo.tblArticleAuthor.articleID
INNER JOIN
dbo.tblCustomersOrders ON dbo.tblPost.id = dbo.tblCustomersOrders.pid
GROUP BY dbo.tblCustomersOrders.pid
For this use, SUM() is an Aggregate Function, so you need to refer all the
fields that you want to get in your result set.
Example:
SELECT
dbo.tblCustomersOrders.id, dbo.tblCustomersOrders.pid, dbo.tblPost.postTitle,
dbo.tblArticleAuthor.authorName, SUM(dbo.tblCustomersOrders.prodPrice) AS TotalBuys
FROM dbo.tblPost
INNER JOIN
dbo.tblArticleAuthor ON dbo.tblPost.id = dbo.tblArticleAuthor.articleID
INNER JOIN
dbo.tblCustomersOrders ON dbo.tblPost.id = dbo.tblCustomersOrders.pid
GROUP BY dbo.tblCustomersOrders.id, dbo.tblCustomersOrders.pid,
dbo.tblPost.postTitle, dbo.tblArticleAuthor.authorName
But this query does not solve the need for your report.
If you just need to get "how much each coach has sold by each course" , you can try the query bellow.
SELECT
dbo.tblArticleAuthor.authorName, dbo.tblPost.postTitle,
SUM(dbo.tblCustomersOrders.prodPrice) AS TotalBuys
FROM dbo.tblPost
INNER JOIN
dbo.tblArticleAuthor ON dbo.tblPost.id = dbo.tblArticleAuthor.articleID
INNER JOIN
dbo.tblCustomersOrders ON dbo.tblPost.id = dbo.tblCustomersOrders.pid
GROUP BY dbo.tblArticleAuthor.authorName, dbo.tblPost.postTitle
If you need, send more details regarding the desired result.
Here you can find more information about SQL SERVER Aggregate Functions:
https://learn.microsoft.com/en-us/sql/t-sql/functions/aggregate-functions-transact-sql?view=sql-server-ver15
And here a quick example regarding SQL Aliases to build queries with a simple
and effective way:
https://www.w3schools.com/sql/trysql.asp?filename=trysql_select_alias_table
Per your description of the task, the problem is that you only GROUPed BY dbo.tblCustomersOrders.pid, which is the period's id I guess, but you also need to GROUP BY the coach, which is dbo.tblArticleAuthor.authorName, I guess again. Plus in the SELECT field list you can not use more columns only that are aggregated + GROUPed.

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.

MS Access Subquery

The sql queries below need to be combined so that it further reduces the results. One of these will need to be a subquery. I am newbie with Access and am only getting errors. The end result should further filter the results to only show the encounters meeting all criteria in both of the querys. Both of these result in the correct result individually...any help you could provide would be greatly appreciated.
SELECT encounters.encounter_id, medications.encounter_id,
medications.medication_id, medication_types.medication_id,
medication_types.name, medication_types.class
FROM medication_types
INNER JOIN (encounters
INNER JOIN medications ON encounters.encounter_id = medications.encounter_id)
ON medication_types.medication_id = medications.medication_id
WHERE medication_types.class LIKE '*Antibiotic*';
SELECT encounters.encounter_id, encounters.admit_year,
diseases.encounter_id, diseases.disease_id,
disease_types.disease_id, disease_types.icd9cm
FROM encounters
INNER JOIN (disease_types
INNER JOIN diseases ON disease_types.disease_id = diseases.disease_id)
ON encounters.encounter_iD = diseases.encounter_id
WHERE disease_types.icd9cm IN ('041.3','480.0','480.1','480.2','480.3','480.8','480.9','481','482.1','482.2','482.9','486','V03.82','V12.61')
AND admit_week BETWEEN 5 and 9
AND encounters.admit_year = 2014
ORDER BY encounters.admit_week;
If you don't need to display the medications and diseases, just return the encounter info, consider:
SELECT DISTINCT encounters.encounter_id, admit_year FROM Query2 WHERE encounters.encounter_id IN (SELECT encounters.encounter_id FROM Query1);

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.

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.