I am trying to select the total number of each value in column paxtype with the following letters m,f,i,c but my error is sql not ended properly
(select b.PAXTYPE from xxxx b, xxx a)
(case b.PAXTYPE
when 'M' then count('M')
when 'F' then count('F')
when 'I' then count('I')
when 'C' then count('C')
END)
where a.date_key=to_char(b.FLIGHTDATE,'RRRRMMDD')
and a.FLTNUM_KEY= trim(substr(b.flightnumber,3))
and a.origin=b.frm
and a.destination=b.too
--and a.date_key=20170801
--and fightnumber = '100'
and trim(a.cancelled) is null
and rownum = 1
)
Firstly You are not using the correct sql syntax. Also, there are several other problems with your query.
Table names should come after columns in select .
You need to use group by since you need count
There is no need for CASE block
why is ROWNUM = 1 required?
This query should work fine for your requirement.
SELECT b.PAXTYPE, COUNT (b.PAXTYPE)
FROM xxxx b, xxx a
WHERE a.date_key = TO_CHAR (b.FLIGHTDATE, 'RRRRMMDD')
AND a.FLTNUM_KEY = TRIM (SUBSTR (b.flightnumber, 3))
AND a.origin = b.frm
AND a.destination = b.too
--and a.date_key=20170801
--and fightnumber = '100'
AND TRIM (a.cancelled) IS NULL
--and rownum = 1 # Why was it required?
GROUP BY b.PAXTYPE;
Additionally, If you need only the counts for M,F,I,C then add AND b.PAXTYPE IN ('M','F','I','C') before group by
I have code that is randomly getting "ORA-22814: attribute or element value is larger than specified in type" with a bulk collect into.
I suspect this is a data issue since it's random but PL/SQL is not my strong suit but I maintain an application that has a great deal of it in processing logic.
This is an Oracle 12c database.
Any help would be appreciated!
This is the block of code:
SELECT mxpv_activityupdate_O (
projectid,
task_id,
task_code,
pvtask_code,
task_name,
CASE
WHEN wbs_t0_id IS NOT NULL AND scgemergent_flag = 'Y'
THEN
wbs_t0_id
ELSE
wbs_pv_id
END,
act_start_date,
CASE
WHEN act_end_date IS NOT NULL AND (invalidactfinish_flag = 'Y')
THEN
NULL
ELSE
act_end_date
END,
NULL,
CASE WHEN invalidactuals_flag = 'Y' THEN NULL ELSE act_work_qty END,
MAX (oid),
scgemergent_flag,
invalidactuals_flag,
invalidactfinish_flag)
BULK COLLECT INTO u_all_tab
FROM (SELECT projectorganization,
projecttype,
projectid,
task_id,
task_code,
pvtask_code,
pvact_start_date,
task_name,
act_start_date,
act_end_date,
CASE
WHEN pvact_start_date IS NULL
AND act_start_date IS NULL
AND NVL (act_work_qty, 0) <> 0
THEN
'Y'
ELSE
'N'
END
invalidactuals_flag,
act_work_qty,
oid,
wbs_t0_id,
wbs_pv_id,
CASE
WHEN pvact_start_date IS NULL
AND (act_start_date IS NOT NULL --or nvl(act_work_qty,0)<>0
)
AND projectorganization = 'SCG'
AND projecttype = 'Daily'
AND pvtarget_start_date > v_t0_start_midnight + 7
THEN
'Y'
ELSE
'N'
END
scgemergent_flag,
CASE
WHEN act_end_date IS NOT NULL
AND NVL (act_start_date, pvact_start_date) IS NOT NULL
AND act_end_date <
NVL (act_start_date, pvact_start_date)
THEN
'Y'
ELSE
'N'
END
invalidactfinish_flag
FROM (SELECT oid,
projecttype,
wbs_t0.wbs_id wbs_t0_id,
--emergentwork,
projectorganization,
projectid,
task_id,
pvwbs_id wbs_pv_id,
activityid task_code,
task_code pvtask_code,
pvact_start_date,
pvtarget_start_date,
CASE
WHEN pvact_end_date IS NULL THEN activityname
ELSE NULL
END
task_name,
CASE
WHEN actualstart IS NOT NULL
AND ( pvact_start_date IS NULL
OR actualfinish IS NOT NULL)
THEN
actualstart
ELSE
NULL
END
act_start_date,
--actualstart act_start_date,
CASE
WHEN pvact_end_date IS NULL
AND ( pvact_start_date IS NOT NULL
OR actualstart IS NOT NULL)
THEN
actualfinish
ELSE
NULL
END
act_end_date,
NULL target_start_date,
CASE WHEN mxactualunits.proj_catg_short_name = 'Y' --and nvl(pvact_work_qty, 0) <> nvl(actuallaborhours,0)
THEN NVL (actuallaborhours, 0) ELSE NULL END
act_work_qty
FROM --1) activityid and proj id matches
(SELECT oid,
pv.task_id,
mx.proj_id,
mx.projectid,
pv.task_code,
pv.task_name,
pv.wbs_id pvwbs_id,
pv.act_start_date pvact_start_date,
pv.act_end_date pvact_end_date,
pv.target_start_date pvtarget_start_date,
mx.activityid,
mx.activityname,
mx.actualstart,
mx.actualfinish,
pv.act_work_qty pvact_work_qty,
mx.actuallaborhours,
mx.projectorganization,
mx.projecttype,
mx.process_start_ts --,
FROM mxpv_activity_tmp mx, mxpv_task_tmp pv
WHERE mx.task_id = pv.task_id
UNION
--3) PM Number/Job Plan number matches
SELECT oid,
jp.task_id,
jp.proj_id,
jp.proj_short_name,
jp.task_code,
jp.task_name,
jp.wbs_id,
jp.act_start_date pvtarget_start_date,
jp.act_end_date pvtarget_end_date,
jp.target_start_date pvtarget_start_date,
jp.activityid,
jp.activityname,
jp.actualstart,
jp.actualfinish,
jp.act_work_qty pvact_work_qty,
actuallaborhours,
projectorganization,
projecttype,
p_process_start_ts --,
FROM --3a) PM Number matches
(SELECT * FROM TABLE (pm_tab)
UNION
--3a) Job Plan Number matches
SELECT * FROM TABLE (jp_tab)) jp)
update_activity,
(SELECT projpcat.PROJ_ID, pcval.PROJ_CATG_SHORT_NAME
FROM privuser_PROJPCAT projpcat,
privuser_PCATTYPE pctype,
privuser_PCATVAL pcval
WHERE projpcat.PROJ_CATG_ID = pcval.PROJ_CATG_ID
AND pcval.PROJ_CATG_TYPE_ID =
pctype.PROJ_CATG_TYPE_ID
AND pctype.PROJ_CATG_TYPE = 'MX Actual Units')
mxactualunits,
(SELECT w.wbs_id, w.proj_id
FROM privuser_projwbs w, privuser_phase ph
WHERE w.phase_id = ph.phase_id
AND ph.phase_name = '0') wbs_t0
WHERE --update_activity.proj_id = actstrtoffset.proj_id (+)
update_activity.proj_id = wbs_t0.proj_id(+)
AND update_activity.proj_id = mxactualunits.proj_id(+)
AND update_activity.process_start_ts =
p_process_start_ts--and delete_ts is null
))
GROUP BY projectid,
task_id,
task_code,
pvtask_code,
task_name,
wbs_t0_id,
wbs_pv_id,
act_start_date,
act_end_date,
act_work_qty,
invalidactuals_flag,
invalidactfinish_flag,
scgemergent_flag,
pvact_start_date;
The type is defined as follows:
TYPE PV_APP_DATA.MXPV_ACTIVITYUPDATE_O AS OBJECT(
proj_short_name VARCHAR2 (100),
task_id NUMBER,
task_code VARCHAR2 (120 CHAR),
pvtask_code VARCHAR2 (120 CHAR),
task_name VARCHAR2 (120 CHAR),
wbs_id NUMBER,
act_start_date DATE,
act_end_date DATE,
target_start_date DATE,
act_work_qty NUMBER,
oid NUMBER,
scgemergent_flag VARCHAR2 (10),
invalidactuals_flag VARCHAR2 (100),
invalidactfinish_flag VARCHAR2 (100)
)
Please post if you need more information I wasn't sure what could be useful.
update 1 7/18/17
I modified the query with substr with no luck in fixing the problem:
select mxpv_activityupdate_O(
substr(projectid,0,99),
task_id,
substr(task_code,0,119),
substr(pvtask_code,0,119),
substr(task_name,0,119),
case when wbs_t0_id is not null and scgemergent_flag='Y' then wbs_t0_id
else wbs_pv_id end,
act_start_date,
case when act_end_date is not null and
(invalidactfinish_flag='Y'
)
then null else act_end_date end,
null,
case when invalidactuals_flag = 'Y' then null else act_work_qty end,
max(oid),
substr(scgemergent_flag,0,9),
substr(invalidactuals_flag,0,99),
substr(invalidactfinish_flag,0,99)
)
bulk collect into u_all_tab
from
(SELECT
projectorganization,
projecttype,
substr(projectid,0,99) as projectid,
task_id,
substr(task_code,0,119) as task_code,
substr(pvtask_code,0,119) as pvtask_code,
pvact_start_date,
substr(task_name,0,119) as task_name,
act_start_date,
act_end_date,
case when pvact_start_date is null and act_start_date is null
and nvl(act_work_qty, 0) <> 0 then 'Y' else 'N' end invalidactuals_flag,
act_work_qty,
oid,
wbs_t0_id,
wbs_pv_id,
case when pvact_start_date is null and
(act_start_date is not null --or nvl(act_work_qty,0)<>0
)
and projectorganization='SCG' and projecttype='Daily'
and pvtarget_start_date>v_t0_start_midnight+7
then 'Y' else 'N' end scgemergent_flag,
case when act_end_date is not null and nvl(act_start_date, pvact_start_date) is not null
and act_end_date<nvl(act_start_date, pvact_start_date) then 'Y' else 'N' end
invalidactfinish_flag
FROM
"if I want to force to correct length what's the best approach?"
You need to compare the length of the Tables' columns with the length of the Type's target attributes, and see which attributes are shorter than the columns you're trying to fit into them. Use the data dictionary.
For the Type Attributes use USER_TYPE_ATTRS (or ALL_TYPE_ATTRS if it's a different schema):
select attribute_name, attr_type_name, length, scale, precision
from user_type_attrs
where type_name = 'MXPV_ACTIVITY_UPDATE_O'
/
For the Table Columns it will be a similar query based on USER_TAB_COLS.
Once you have established the errant columns you will need to alter the type to enlarge the attributes. Alternatively apply SUBSTR() function to the columns in the query projection, to cut them down to size. Which is actually better depends on the circumstances of your application, although usually we would prefer the approach which doesn't lose data.
I want to use case statement for a column like below
SELECT sr_no, TYPE, stage, party_name, amount, remarks, exp_type, exp_id,
voucher_no, cheque_no, cheque_dt, chq_favr_name, attachment,
CASE NVL (amount, 0) = 0 THEN checkVal = 0
ELSE
checkVal = 1
FROM xxcus.xxacl_pn_expense_info
WHERE mkey = '354'
AND ((NVL (amount, 0) <> 0) OR (party_name IS NOT NULL))
ORDER BY sr_no
But I am getting error as
ORA-00923: FROM keyword not found where expected
SELECT sr_no, TYPE, stage, party_name, amount, remarks, exp_type, exp_id,
voucher_no, cheque_no, cheque_dt, chq_favr_name, attachment,
CASE WHEN NVL(amount, 0) = 0 THEN 0 ELSE 1 END AS checkVal
FROM xxcus.xxacl_pn_expense_info
WHERE mkey = '354'
AND ((NVL (amount, 0) <> 0) OR (party_name IS NOT NULL))
ORDER BY sr_no
you're missing an END
SELECT sr_no, TYPE, stage, party_name, amount, remarks, exp_type, exp_id,
voucher_no, cheque_no, cheque_dt, chq_favr_name, attachment,
CASE WHEN NVL (amount, 0) = 0 THEN checkVal = 0
ELSE
checkVal = 1
END
FROM xxcus.xxacl_pn_expense_info
WHERE mkey = '354'
AND ((NVL (amount, 0) <> 0) OR (party_name IS NOT NULL))
ORDER BY sr_no
I have a query as part of a larger function and having trouble making DECODE work with null dates. This portion of my query is in the WHERE condition of the query:
NVL(datRunDate,SYSDATE)
BETWEEN NVL(EFFECTIVE_DATE,NVL(datRunDate,SYSDATE-1))
AND DECODE(STOP_DATE, NULL, NVL(datRunDate,SYSDATE + 1), STOP_DATE + (59/86400))
Where:
datRunDate DATE
EFFECTIVE_DATE DATE
STOP_DATE DATE
My issue is that the STOP_DATE is typically NULL and the DECODE doesn't work. Any ideas / help to work around this is appreciated.
EDIT
Adding some sample data as suggested:
datRunDate 2016-01-14 06:41:54
EFFECTIVE_DATE 2013-04-01 09:53:00
STOP_DATE NULL
EDIT2
Adding the entire query text here as the problem probably lies here. Note: I have replaced the variables manually. These variables are populated before this query with simple SELECT INTO statements.
datRunDate DATE;
SELECT COMP_DATE INTO datRunDate where IDL_SEQ = 2320;
SELECT EFFECTIVE_DATE, STOP_DATE from IDLS where IDL_SEQ = 2320;
DESC IDL_TABLE
DESC SCHEDULES COMP_DATE = datRunDate
...
SELECT I.IDL_SEQ
FROM IDLS I, IDL_CMPS IC
WHERE I.RECORD_TYPE = 'M'
AND IC.IDL_SEQ = I.IDL_SEQ
AND IC.CMP = '71-43-2'
AND I.METHOD = 'N0'
AND ((I.RUN_INSTRU = '') OR (I.RUN_INSTRU IS NULL))
AND ((I.PREP_METHOD = 'K9') OR (I.PREP_METHOD IS NULL))
AND ((I.MATRIX = 'SO') OR (I.MATRIX IS NULL))
AND ((I.COLUMN_ID = '') OR (I.COLUMN_ID IS NULL))
AND COALESCE(datRunDate, SYSDATE)
BETWEEN COALESCE(I.EFFECTIVE_DATE, datRunDate, SYSDATE - 1)
AND COALESCE(I.STOP_DATE + (59/86400), datRunDate, SYSDATE + 1)
AND ((I.SAMPLE_TYPE = 'SAMPLE') OR (SAMPLE_TYPE IS NULL))
AND ((I.CUST_SAMPLE_ID = 'SB-7') OR (CUST_SAMPLE_ID IS NULL))
AND ((I.LOCATION = '') OR (LOCATION IS NULL))
AND (
(OTHER_CRITERIA IS NOT NULL AND 404324 IS NOT NULL AND OTHER_CRITERIA = 'P|'||404324) OR
(OTHER_CRITERIA IS NOT NULL AND 28936 IS NOT NULL AND OTHER_CRITERIA = 'R|'||28936) OR
(OTHER_CRITERIA IS NOT NULL AND 'ECO' IS NOT NULL AND OTHER_CRITERIA = 'C|'||'ECO') OR
(OTHER_CRITERIA IS NULL));
Now when I run this I get: ORA-00932: inconsistent datatypes: expected CHAR got DATE
I would write this as:
COALESCE(datRunDate, SYSDATE) BETWEEN COALESCE(EFFECTIVE_DATE, datRunDate, SYSDATE - 1) AND
COALESCE(STOP_DATE + (59/86400), dateRunDate, SYSDATE + 1)
The ANSI standard function COALESCE() is simpler than using NVL() and DECODE() (which should be obsoleted anyway).
Overall Task :- I need to retrieve data from 45 fields in system A and dump that data into a temp table which is then picked up by a unix process which produces an xml data file to be imported into system B.
Specific Question : What would be the best way of retrieving the data to be written into the 45 fields. Majority of the data is independent and can't be retrieved using a single statement. The way i currently retrieve this data is as follows (example below)
My temp tables hold the affected properties ID that i need to extract data for. i.e PROP_LIST_TEMP and ASSOC_PROP_TEMP.
SELECT SUBSTR (pro.pro_propref, 1, 25) UPRN,
(SELECT SUBSTR (adr_building, 1, 100)
FROM addresses, address_usages
WHERE aus_adr_refno = adr_refno
AND aus_aut_far_code = 'PHYSICAL'
AND aus_aut_fao_code = 'PRO'
AND (aus_end_date IS NULL OR aus_end_date > SYSDATE)
AND aus_pro_refno = pro.pro_refno)
BUILDING_NAME,
(SELECT CASE
WHEN (adr_street_number like 'BLOC%'
OR adr_street_number like '%-%'
OR adr_street_number like '%/%')
THEN NULL
ELSE regexp_replace (adr_street_number, '[^[:digit:]]+')
END
FROM addresses, address_usages
WHERE aus_adr_refno = adr_refno
AND aus_aut_far_code = 'PHYSICAL'
AND aus_aut_fao_code = 'PRO'
AND (aus_end_date IS NULL OR aus_end_date > SYSDATE)
AND aus_pro_refno = pro.pro_refno)
STREET_NUMBER,
(SELECT CASE
WHEN (adr_street_number like 'BLOC%'
OR adr_street_number like '%-%'
OR adr_street_number like '%/%')
THEN SUBSTR (adr_street_number, 1, 20)
ELSE REGEXP_REPLACE (adr_street_number, '[^[:alpha:]]+', '')
END
FROM addresses, address_usages
WHERE aus_adr_refno = adr_refno
AND aus_aut_far_code = 'PHYSICAL'
AND aus_aut_fao_code = 'PRO'
AND (aus_end_date IS NULL OR aus_end_date > SYSDATE)
AND aus_pro_refno = pro.pro_refno)
STREET_NUMBER_SUFFIX,
(SELECT SUBSTR (ptv_pty_code, 1, 3)
FROM prop_type_values
WHERE ptv_refno = pro.pro_hou_ptv_refno)
HOUSE_TYPE
FROM properties pro
WHERE pro_refno IN (select * from PIMSS_PROP_LIST_TEMP
UNION
select * from PIMSS_ASSOC_PROP_TEMP)
AND pro.pro_hou_hrv_hot_code IN
(SELECT frv_code
FROM first_ref_values
WHERE frv_frd_domain IN ('ASS_OWN_REF')
AND frv_current_ind = 'Y');
Since the where clauses of the subqueries in the select statement are identical, you could simply pull that out into the where clause, like so:
SELECT SUBSTR (pro.pro_propref, 1, 25) UPRN,
SUBSTR (addr.adr_building, 1, 100) BUILDING_NAME,
CASE WHEN (addr.adr_street_number like 'BLOC%'
OR addr.adr_street_number like '%-%'
OR addr.adr_street_number like '%/%')
THEN NULL
ELSE regexp_replace (addr.adr_street_number, '[^[:digit:]]+')
END STREET_NUMBER,
CASE WHEN (addr.adr_street_number like 'BLOC%'
OR addr.adr_street_number like '%-%'
OR addr.adr_street_number like '%/%')
THEN SUBSTR (addr.adr_street_number, 1, 20)
ELSE REGEXP_REPLACE (addr.adr_street_number, '[^[:alpha:]]+', '')
END STREET_NUMBER_SUFFIX,
(SELECT SUBSTR (ptv_pty_code, 1, 3)
FROM prop_type_values
WHERE ptv_refno = pro.pro_hou_ptv_refno) HOUSE_TYPE
FROM properties pro,
(select adr_building,
adr_street_number
FROM addresses, address_usages
WHERE aus_adr_refno = adr_refno
AND aus_aut_far_code = 'PHYSICAL'
AND aus_aut_fao_code = 'PRO'
AND (aus_end_date IS NULL OR aus_end_date > SYSDATE)) addr
WHERE pro.pro_refno = aus_pro_refno
and pro_refno IN (select * from PIMSS_PROP_LIST_TEMP
UNION
select * from PIMSS_ASSOC_PROP_TEMP)
AND pro.pro_hou_hrv_hot_code IN (SELECT frv_code
FROM first_ref_values
WHERE frv_frd_domain IN ('ASS_OWN_REF')
AND frv_current_ind = 'Y');
You might possibly need an outer join if there's a chance that no rows could be returned from the addr subquery.