Issue with CASE statement in a calculated column - sql

I have a SELECT statement which works fine up until the CASE part, I think it's just a case of syntax error and the lead time bit. I am using Oracle APEX.
I get the error;
ORA-00923: FROM keyword not found where expected
select
supplier.companyname as "Supplier",
purchaseorder.orderdate as "Order Date",
purchaseorder.duedate as "Due Date",
popayment.datedelivered as "Delivered",
(popayment.paymentdate - popayment.datedelivered) - 31 as paidontime,
/* this is the same as the next one (this works fine) */
(popayment.datedelivered - purchaseorder.duedate) - 1 as deleiverylate,
/* trying to advance the previous query (this does not work) */
popayment.datedelivered - purchaseorder.duedate -1,
case when "lead time" <0 then 'early'
when "lead time" >0 then 'late'
else 'on time'
end as "lead time";
from supplier, purchaseorder, suppliercontact, popayment
where
purchaseorder.SUPPLIERCONTACTID2 = suppliercontact.suppliercontactid
and
supplier.supplierid = suppliercontact.supplierid
and
purchaseorder.purchaseorderid = popayment.purchaseorderid
order by COMPANYNAME
Thanks for any help
Dangerous Brian

Is this what you want?
SELECT supplier.companyname AS "Supplier",
purchaseorder.orderdate AS "Order Date",
purchaseorder.duedate AS "Due Date",
popayment.datedelivered AS "Delivered",
(popayment.paymentdate - popayment.datedelivered) - 31 AS paidontime,
/* this is the same as the next one (this works fine) */
(popayment.datedelivered - purchaseorder.duedate) - 1 AS deleiverylate,
/* trying to advance the previous query (this does not work) */
CASE
WHEN popayment.datedelivered - purchaseorder.duedate -1 <0
THEN 'early'
WHEN popayment.datedelivered - purchaseorder.duedate -1 >0
THEN 'late'
ELSE 'on time'
END AS "lead time"
FROM supplier,
purchaseorder,
suppliercontact,
popayment
WHERE purchaseorder.SUPPLIERCONTACTID2 = suppliercontact.suppliercontactid
AND supplier.supplierid = suppliercontact.supplierid
AND purchaseorder.purchaseorderid = popayment.purchaseorderid
ORDER BY COMPANYNAME

You should delete ; at end as "lead time";

Related

ORA-01476: Divisor is equal to zero

I have this query that I've been given to fix and I cannot understand why its causing error. I reviewed similar topics involving the max function and the null function but I can't seem to make them work without throwing another error.
select case when count(trlr_num) > 0 then 'Inbounds Need Closed'
else 'All Good'
end as "Inbounds To Be Closed"
from (select t.trlr_num,
r.invnum "Invoice",
to_char(nvl(max(rl.init_rcv_dte), sysdate), 'MM/DD/YY HH:MI AM') as "Begin receive date",
round(sum(rl.idnqty) / sum(rl.expqty) *100, 1) as "% Received%"
from trlr t
join rcvinv r
on t.trlr_num = r.trknum
join rcvlin rl
on r.trknum = rl.trknum
and r.supnum = rl.supnum
and r.invnum = rl.invnum
and r.wh_id = rl.wh_id
join rcvtrk rt
on r.trknum = rt.trknum
and t.trlr_id = rt.trlr_id
and r.wh_id = rt.wh_id
where t.yard_loc_wh_id = 'US_3218'
and rt.rcvtrk_stat <> 'C'
and r.invtyp not in ('PKG', 'XFR')
and (rl.rcvsts <> 'QI' or (rl.rcvsts = 'QI' and t.trlr_seal1 is not null and t.trlr_seal2 is not null))
group by t.trlr_num,
r.invnum
having sum(rl.idnqty) / sum(rl.expqty) = 1
and nvl(max(rl.init_rcv_dte), sysdate) < sysdate -2)

How to change status based on date in another text box on form - Access

Im making a training database.
Im looking to change a textbox value on a form based on a date in another textbox.
I have the following:
Refresher Period - TxtRef
Participation Date - TxtPart
Refresher Date - TxtRefDate
Status - TxtStatus
I want status to update to either - In Date, Expired or Expiring based on the following rules applying to the date in TxtRefDate.
Value < Now() + 60 "Expiring"
Value < Now() "Expired"
Value > Now() + 60 "In Date"
Create a small helper function:
Public Function Status(ByVal RefDate As Date) As String
Dim Description As String
Select Case DateDiff("d", Date, RefDate)
Case > 60
Description = "In date"
Case > 0
Description = "Expiring"
Case Else
Description = "Expired"
End Select
Status = Description
End Function
Now, set the ControlSource of txtStatus to:
=Status([TxtRefDate])
I think you're looking for something like that ?
If TxtRefDate.Value < Now() Then
TxtStatus.Value = "Expired"
Else:
If TxtRefDate.Value < Now() + 60 Then
TxtStatus.Value = "Expiring"
Else:
TxtStatus.Value = "In Date"
End If
End If

Data conversion or data mapping error in SQL

This is my WinSQL query that I try and execute.
SELECT ezait6 AS "MO No",
vhitno AS "Item No",
vhrefd AS "Finish Date",
vhmaqa AS "Manuf Qty",
vhmaun AS "U/M"
FROM m3edbtest.mwohed,
m3edbtest.cinacc
WHERE ezcono = 1
AND vhcono = ezcono
AND vhrefd >= '20170801'
AND vhrefd <= '20170831'
AND vhmfno = ezait6;
--Different Data Types
--vhmfno ==> Integer
--ezait6 ==> nchar(8)
After I run my query, I got below errors:
-- Error : SQL0802 - Data conversion or data mapping error.
I suspect that it is beclose of different data type
I have a nvarchar column in one of my table "ezait6".
So how should I convert that values to INT type.
You can check first if the content of the varchar field is numeric and query only on those:
SELECT ezait6 AS "MO No",
vhitno AS "Item No",
vhrefd AS "Finish Date",
vhmaqa AS "Manuf Qty",
vhmaun AS "U/M"
FROM m3edbtest.mwohed,
m3edbtest.cinacc
WHERE IsNumeric(ezait6)=1
AND ezcono = 1
AND vhcono = ezcono
AND vhrefd >= '20170801'
AND vhrefd <= '20170831'
AND vhmfno = CONVERT(INT,ezait6);

Teradata SQL Update Table "Query Error 3802 Database 'oo' does not exist"

I need help with the following query code. Currently it is giving me the 3802 query error "Database does not exist". Well I did my "from" selection query as 'oo', so that definitely exists. This worked yesterday with some minor changes to the "from" part of the query. The assistance is appreciated.
UPDATE snd_bqa.open_order_all_test
FROM (
SELECT
CASE WHEN m.order_status_desc = 'Completed' AND NVL(c.supptype, 'NULL') <> 'CAN'
THEN 'YES'
ELSE 'NO'
END AS "Order Complete",
CASE WHEN m.order_status_desc = 'Completed' AND c.supptype = 'CAN'
THEN 'YES'
ELSE 'NO'
END AS "Order Cancelled",
CASE WHEN pih.effective_start_date IS NOT NULL AND pih.product_instance_status_code = 3
THEN 'YES'
ELSE 'NO'
END AS "Service Active in PB",
c.ordernumber,
c1.order_requested_due_date,
c.creationdate AS "Order Creation Date",
c.ordersubmitdate AS "Order Submit Date",
c.lastupdateddate,
c.supptype,
m.order_status_desc,
m.phase_stage,
cus.sub_nasp_id,
cus.gch_id,
b.work_order_no,
b.itemcode,
b.instance_id,
COALESCE(b.specific_date, b.standard_date) AS "Service Req Due Date",
m.milestone,
m.milestone_desc,
cus.custlegalname,
a.account_number,
vle.vle_id,
vle.currency_cd,
cus.duns_number,
a.cle_vle_id,
CURRENT_TIMESTAMP(2) AS "Originally Loaded",
CURRENT_TIMESTAMP(2) AS "Last Upd",
CAST((CASE WHEN m.order_status_desc = 'Completed' AND NVL(c.supptype, 'NULL') <> 'CAN'
THEN c.lastupdateddate
ELSE NULL
END) AS TIMESTAMP(2)) "Order Completed Date",
CAST((CASE WHEN m.order_status_desc = 'Completed' AND c.supptype = 'CAN'
THEN c.lastupdateddate
ELSE NULL
end) AS TIMESTAMP(2)) "Order Cancelled Date",
pih.last_modified AS "Service Last Modified Date",
pih.effective_start_date AS "Service Effective Date"
FROM (
SELECT *
FROM edw_stg_ord_cw_vw.cwpc_basketitem
WHERE itemcode LIKE 'PR%'
AND (instance_id, lastupdateddate) IN (
SELECT instance_id, MAX(lastupdateddate)
FROM edw_stg_ord_cw_vw.cwpc_basketitem
GROUP BY instance_id
)) AS b
INNER JOIN edw_stg_ord_cw_vw.cworderinstance AS c
ON c.basket_id = b.basketid
AND c.isactive = 1
AND c.request_type = 'ORD'
LEFT JOIN (
SELECT c.ordernumber,
MAX(COALESCE(b.specific_date, b.standard_date)) order_requested_due_date
FROM edw_stg_ord_cw_vw.cworderinstance AS c
INNER JOIN edw_stg_ord_cw_vw.cwpc_basketitem AS b
ON b.basketid = c.basket_id
AND c.request_type = 'ORD'
AND c.isactive = 1
GROUP BY c.ordernumber
) AS c1
ON c1.ordernumber = c.ordernumber
LEFT JOIN edw_stg_ord_cw_vw.uno_customer AS cus
ON cus.cworderid = c.cwdocid
AND cus.oi_customer_id = c.ordering_customer_id
LEFT JOIN edw_stg_ord_cw_vw.uno_milestone AS m
ON m.milestone_id = c.milestone_id
LEFT JOIN edw_stg_ord_cw_vw.uno_account AS a
ON a.cworderid = c.cwdocid
AND b.account_id = a.oi_account_id
LEFT JOIN edw_stg_ord_cw_vw.uno_cle_vle AS vle
ON vle.cle_vle_id = a.cle_vle_id
AND c.cwdocid = vle.cworderid
INNER JOIN snd_bqa.open_order_all_test tst
ON tst."Service Instance ID" = b.instance_id
AND tst."Milestone" <> m.milestone
LEFT JOIN edw_pb_stg_vw.product_instance_history AS pih
ON pih.general_5 = tst."Service Instance ID"
WHERE cus.sub_nasp_id NOT IN ('19HNYO', '23MAXA', '19HNYR', '10INTD')
AND COALESCE(b.specific_date, b.standard_date, c1.order_requested_due_date) BETWEEN CURRENT_DATE - 60 AND CURRENT_DATE - 30
) oo
SET "Order Complete" = oo."Order Complete",
"Order Cancelled" = oo."Order Cancelled",
"Service Active in PB" = oo."Service Active in PB",
"Service Order Number" = oo.ordernumber,
"Order Requested Due Date" = oo.order_requested_due_date,
"Order Last Updated" = oo.lastupdateddate,
"Order Creation Date" = oo."Order Creation Date",
"Order Submit Date" = oo."Order Submit Date",
"Order Supp Type" = oo.c.supptype,
"Order Status" = oo.order_status_desc,
"Phase Stage" = oo.phase_stage,
"Milestone" = oo.milestone,
"Milestone Description" = oo.milestone_desc,
"NASP ID" = oo.sub_nasp_id,
"GCH ID" = oo.gch_id,
"Work Order Numbers" = oo.work_order_no,
"Product Code" = oo.itemcode,
"Service Requested Due Date" = oo."Service Req Due Date",
"Customer Name" = oo.custlegalname,
"Account Number" = oo.account_number,
"VLE ID" = oo.vle_id,
"Currency Code" = oo.currency_cd,
"DUNs Number" = oo.duns_number,
"CLE VLE ID" = oo.cle_vle_id,
"Last Record Update" = oo."Last Upd",
"Order Completed Date" = oo."Order Completed Date",
"Order Cancelled Date" = oo."Order Cancelled Date",
"Service Last Modified Date" = oo."Service Last Modified Date",
"Service Effective Date" = oo."Service Effective Date"
WHERE open_order_all_test."Service Instance ID" = oo.instance_id
;
I found the issue. I had oo.c.supptype, when it should have just been oo.supptype
This caused Teradata to think 'oo' was referring to a database. case closed.

DB2 SQL Select Statement & Group By

I am having trouble getting my query to group correctly. I need to include the TELLERACTIVITY.ENTRY_DATE date column so I can filter by the date later on. However, I do not want it to group by the TELLERACTIVITY.ENTRY_DATE date because it gives me more results than I want.
SELECT DISTINCT ACTIVITYINFO.MEMBER_NBR AS "Member Number"
,ACCOUNT.ACCOUNT_NBR AS "Account Number"
,TELLERACTIVITY.FOCUS_TELLER_ID AS "Teller ID"
,TELLERACTIVITY.ENTRY_DATE AS "Date"
,SUM(( CASE WHEN OPERATOR.BRANCH_NBR = 1 THEN 1
ELSE 0
END )) AS "Branch 1"
,SUM(( CASE WHEN OPERATOR.BRANCH_NBR = 2 THEN 1
ELSE 0
END )) AS "Branch 2"
,SUM(( CASE WHEN OPERATOR.BRANCH_NBR = 3 THEN 1
ELSE 0
END )) AS "Branch 3"
,SUM(( CASE WHEN OPERATOR.BRANCH_NBR = 4 THEN 1
ELSE 0
END )) AS "Branch 4"
FROM TELLERACTIVITY
JOIN OPERATOR
ON TELLERACTIVITY.FOCUS_TELLER_ID = OPERATOR.OPR_NBR
JOIN ACTIVITY
ON TELLERACTIVITY.ACTIVITY_ID = ACTIVITY.ACTIVITY_ID
AND TELLERACTIVITY.FOCUS_TELLER_ID = ACTIVITY.FOCUS_TELLER_ID
AND TELLERACTIVITY.ENTRY_DATE = ACTIVITY.ENTRY_DATE
JOIN ACTIVITYINFO
ON ACTIVITY.ACTIVITY_ID = ACTIVITYINFO.ACTIVITY_ID
JOIN ACCOUNT
ON ACTIVITYINFO.MEMBER_NBR = ACCOUNT.MEMBER_NBR
AND ACTIVITYINFO.ACCOUNT_NBR = ACCOUNT.ACCOUNT_NBR
WHERE TELLERACTIVITY.FOCUS_TELLER_ID < 6000
GROUP BY ACTIVITYINFO.MEMBER_NBR
,ACCOUNT.ACCOUNT_NBR
,OPERATOR.BRANCH_NBR
,TELLERACTIVITY.FOCUS_TELLER_ID
,TELLERACTIVITY.ENTRY_DATE
ORDER BY ACTIVITYINFO.MEMBER_NBR