Shipping Report - Distinct Order with Two Dates - Status Missing from Table THEN - sql

Links to the tables in the screenshot can be found at the link below:
https://www.dropbox.com/l/scl/AAAX85i7QE4zr1S-zr-_Lo00VUnIkE02XWo
First Issue:
Normally this table (See Load Tracking Table) would have a Field in Check Call Name that is "Pickup - Actual" signalling that this has been picked up. However in certain circumstances the CheckCallName will bypass the pickup and move to Delivered status. In cases like this I want to pull the query to treat the "Delivered" status in the same manner that Pickup Actual was being used.
Said another way, where kpo.loadtracking does not have Pickup - Actual in the Check Call Name field for any one order, I want to pull the Check Call Name for Delivered and the KeypointStatusDate.
Second Issue:
The DISTINCT clause does not work perfectly in my case, as an order can have the Pickup - Actual status updated multiple times. Here, we should take the oldest Keypoint Status Date. I've tried to use the MIN(KeypointStatusDate) and grouping by Order ID but I'm getting the following message
Msg 8120, Level 16, State 1, Line 14
Column 'KPO.LOAD.LoadId' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
The code I have currently is below. I'm new to SQL (about a week in) so apologies if this is simple.
SELECT
Distinct LOAD.LoadNumber
,LOAD.LoadId
,CustomerName
,OrderStatus
,CheckCallName
,PickedUpOn
,LoadTracking.KeypointStatusDate
,LOAD.OriginEarliest
,LOAD.DestinationEarliest
,CustomerRate
,CarrierCost
,Miles
FROM KPO.LOAD
Inner JOIN kpo.LoadExtension ON LOAD.LoadId = LoadExtension.LoadId
Inner JOIN kpo.Customer ON customer.CustomerId = LOAD.CustomerID
Inner JOIN kpo.LoadTracking ON LoadTracking.LoadId = LOAD.LoadId
WHERE
DATEDIFF(MONTH, GETDATE(), LoadTracking.KeypointStatusDate) = - 2
AND OrderStatus <> 'VOID'
AND CheckCallName = 'Pickup - Actual'
ORDER BY CustomerName, LoadNumber, LoadTracking.KeypointStatusDate
Load Tracking Table with Question Inside:
Data Result from Query:

Related

SQL query not working properly (not sure about it)

I have 2 tables: ORDER_INFO_TABLE and APPROVE_TABLE
The first one contains the information related to "orders".. DO NOTE that a single order may contain different "order lines" (thats why you'll see the order number repeated in the image below).Also, there exist a field called "PRICE" (this field is not SHOWN in my image) and CANCELLED (order line status)
The second table contains the information related to the "approvers" for the different order lines. Inside this table you will see a field called "APPROVERID", ITEMID (order line id) and "APPROVED" (order line status). The approvers must check if the price is okay or not. If the order line is okay, the approver will put a number 1 in the field "APPROVED". If the price is not correct, he will put a number 1 in the field CANCELLED in the other table.
Take a look at these images:
I have tried without success to obtain all the cancelled orders (an order is cancelled when all ITS order lines are cancelled) AND the approved order (an approved order may contain cancelled order lines) for an specific approver
I tried so many times, using count operator, left join but i am completely lost :(
A guy suggested me to use this query:
SELECT web_order_id
FROM ORDER_INFO_TABLE oif
INNER JOIN APPROVE_TABLE apt
ON (oif.item_id = apt.item_id)
GROUP BY web_order_id
HAVING SUM(cancelled) = COUNT(*);
The problem is that it is not woking properly (it shows this error message: operand data type is invalid for sum operator) AND using this query i will not be able to obtain the CANCELLED and APPROVED orders for a specific approver.
EDIT:
SELECT web_order_id
FROM ORDER_INFO_TABLE oif
INNER JOIN APPROVE_TABLE apt
ON (oif.item_id = apt.item_id **and apt.APPROVERID = 'RANDOM#MAILNATOR.COM'**)
GROUP BY web_order_id
HAVING SUM(cancelled) = COUNT(*);
Edited: If you just want to select order from specific approver then just use simple select with join:
SELECT oif.web_order_id, oif.item_id, apt.approverid,
oif.cancelled, apt.approved
FROM ORDER_INFO_TABLE oif
INNER JOIN APPROVE_TABLE apt
ON oif.item_id = apt.item_id
WHERE apt.approverid = 'RANDOM#MAILNATOR.COM'
ORDER BY oif.web_order_id, oif.item_id;
Seems that your cancelled column datatype is VARCHAR. You should convert it to number before using SUM function
SELECT oif.web_order_id
FROM ORDER_INFO_TABLE oif
INNER JOIN APPROVE_TABLE apt
ON oif.item_id = apt.item_id
GROUP BY oif.web_order_id
HAVING SUM(CAST(oif.cancelled AS int)) = COUNT(*);

How to count the number of instances within a table (Countifs)

Thank you so much for looking!
Here is a link to the tables.
Normally this table would have a Field in Check Call name that is "Pickup - Actual" signalling that this has been picked up. However in certain circumstances the CheckCallName will bypass the pickup and move to delivered status. In cases like this I want to pull the query to treat the "Delivered" status in the same manner that Pickup Actual was being used.
So where kpo.loadtracking does not have Pickup - Actual in the Check Call Name field for any one order, I want to pull the date (KeypointStatusDate) the status changed to 'Delivered'.
I figured this would be something like "where the status count of Pickup-Actual is greater than 0 then pull the earliest status update date for pickup actual.
However when the count is = 0, I'd want it to pull the date relevant to the Delivered CheckCallName.
Table 1 should Pull 10/5 -
Table 2 should Pull 10/2
My current code is below:
SELECT DISTINCT
Load.LoadNumber
,CustomerName
,OrderStatus
,PickedUpOn
,CheckCallName
,Min(LoadTracking.KeypointStatusDate) AS Earliest_Check_Call
,CustomerRate
,CarrierCost
,Miles
FROM kpo.load
INNER JOIN kpo.LoadExtension ON LOAD.LoadId = LoadExtension.LoadId
INNER JOIN kpo.Customer ON customer.CustomerId = LOAD.CustomerID
INNER JOIN kpo.LoadTracking ON LoadTracking.LoadId = LOAD.LoadId
WHERE
DATEDIFF(MONTH, GETDATE(), LoadTracking.KeypointStatusDate) = - 2
AND OrderStatus <> 'VOID'
AND CheckCallName = 'Pickup - Actual'
--- WE NEED TO THEN LOOK AT EACH LOAD
--- AND IF THERE IS NO PICKUP-ACTUAL THEN IT PULLS IN THE DELIVERED DATE.
--- THERE ARE SOME ORDERS THAT GO STRAIGHT TO DELIVERED STATUS. (453653)
GROUP BY
Load.LoadNumber
,OrderStatus
,CustomerName
,CheckCallName
,PickedUpOn
,CustomerRate
,CarrierCost
,Miles
ORDER BY
Earliest_Check_Call

Type conversion failure in update query

I'm fairly new to Access so this is driving me a little crazy.
I'm creating an inventory database and want to count the number of items in stock to update an ordering form. Received items are assigned an order code, and I want to count the number of instances of each order code found within the master table. I have a make table query which does this just fine:
SELECT PrimerList.PrimerName
, First(Primer_Master.FR) AS FR
, Primer_Master.OrderCode
, Count(Primer_Master.OrderCode) AS InStock
INTO PrimerOrder
FROM PrimerList
LEFT JOIN Primer_Master ON PrimerList.ID = Primer_Master.PrimerName
GROUP BY PrimerList.PrimerName
, Primer_Master.OrderCode
, Primer_Master.PrimerName
, Primer_Master.FR
, Primer_Master.Finished
HAVING ((([Primer_Master]![Finished])=No));
I want to use PrimerOrder to update an order list table PrimerOrderList which has all of the different possible order codes, updating the InStock value for records with matching OrderCode:
UPDATE PrimerOrderList
SET PrimerOrderList.InStock = PrimerOrder.InStock
WHERE (((PrimerOrderList.OrderCode)=[PrimerOrder].[OrderCode]));
However, when I try to run it I get parameter boxes which pop-up asking for PrimerOrder.OrderCode and PrimerOrderList.OrderCode. Even if I put in a valid value for each, I get a type conversion failure. I've checked the data types for both tables and don't see how there could be a type conversion failure - both are set to text.
Any insight would be greatly appreciated! Thanks in advance!
You haven't included the PrimerOrder table in your query. Should be:
UPDATE PrimerOrderList INNER JOIN PrimerOrder
ON PrimerOrderList.OrderCode = PrimerOrder.OrderCode
PrimerOrderList.InStock = PrimerOrder.InStock

Access Update not working

I have an Access Database which links into Excel for a college database project. The goal is to fix incorrect/incomplete data before importation through the use of Update/SQL queries. There is one item in particular which is a field containing the order status. If an order is complete = Complete, if an order is missing a few parts = Backorder and nothing at all = Open. The issue I have is there is multiple parts on one PO ID# which makes it difficult to determine if an order is on backorder as 2/5 parts may be complete while 3/5 may be on backorder. Any ideas on how I can force access to automatically set a default order status or is this a long sql query?
Thanks
With SQL you can return a query to find the order status, using a CASE statement. I'm not sure of the table/column names in the parent table you have but you can probably tweak it from here (I'm just assuming identity columns):
SELECT P.Id, P.Name, "Status" =
CASE
WHEN R.QuantityReceived < I.QuantityOrdered THEN 'Backorder'
WHEN R.QuantityReceived = I.QuantityOrdered THEN 'Complete'
WHEN R.QuantityReceived > I.QuantityOrdered THEN 'Open'
END
FROM PurchaseOrders P INNER JOIN POItem I on P.PurchaseOrderId = I.PurchaseOrderId
INNER JOIN POReceipt R on I.ItemId = R.ItemId
So first you need to join your tables to get the related records (this is also assuming that there is only one of each), then use a CASE statement to return the value you are looking for based on a comparison between fields.

Need to pull only last date in table that stores change dates SQL / ODBC

Hope somebody can help me with this. I'm trying to pull a list of forthcoming titles (I work in publishing) via ODBC/ms query. I want (amongst other things) to show their internal status (approved, prepress etc.). The database stores the change dates for the status'. I seem to be getting one line per status per title. So if the title has changed status 6 times, I will get 6 lines. But I only want to show the latest status...
The date is in BL_PROJECT_TO_STATUS.STATUS_DATE (I've inserted a date criteria beneath, just to make it more visible).
How can this be done? I'm very new to ODBC and would appreciate it a lot.
SELECT DISTINCT
BL_PROJECT.EXP_PUB_DATE, BL_PROJECT.EAN, BL_PROJECT.TITEL,
MEDIATYPE.DESCRIPTION, BL_PROJECT_STATUS.DESCRIPTION
FROM
FIRMA1.BL_PROJECT BL_PROJECT, FIRMA1.BL_PROJECT_STATUS BL_PROJECT_STATUS,
FIRMA1.BL_PROJECT_TO_STATUS BL_PROJECT_TO_STATUS, FIRMA1.MEDIATYPE MEDIATYPE
WHERE
BL_PROJECT.PROJECT_ID = BL_PROJECT_TO_STATUS.PROJECT_ID AND
BL_PROJECT_TO_STATUS.STATUS_ID = BL_PROJECT_STATUS.CODE AND
BL_PROJECT.MEDIATYPE = MEDIATYPE.ID AND
((BL_PROJECT.PROJECT_TYPE = 2) AND
(BL_PROJECT.EXP_PUB_DATE Between SYSDATE AND (SYSDATE+90)) AND
(BL_PROJECT_TO_STATUS.STATUS_DATE = {ts '2013-11-20 00:00:00'}))
ORDER BY
BL_PROJECT.EXP_PUB_DATE, BL_PROJECT.EAN, BL_PROJECT.TITEL
Here is the general idea. You can adapt it with your table and field names.
select somefields
from sometables
join
(select something, max(datetimefield) maxdt
from table1
where whatever
group by something ) temp on table1.datetimefield = maxdt
etc