CASE not working in oracle - oracle

For one of my column in query I want a CASE statement.
The scenario is
If my column name ltt.f_complete task_completed value is 0 then the value shd be NO and if 1 then YES.
I tried like below but getting as
ORA-00905: missing keyword
here is query
SELECT flv.property_name project_name, flv.building building_name,
flv.flat_no unit_no,
ldet.customer_fname
|| ' '
|| ldet.customer_mname
|| ' '
|| ldet.customer_lname customer_name,
la.booking_date holding_date, ltt.start_date task_date,
tid.task_desc task_type, fol.next_follow_up_date,
ltt.remarks task_comment, ltt.f_complete task_completed,
CASE TASK_VALUE WHEN '0' THEN 'NO'
WHEN '1' THEN 'YES',
act.act_desc activity_description, flv.follow_type followup_type,
fol.remarks followup_comment,
fol.next_follow_up_date next_followup_date,
actt.act_desc next_todo_activity
FROM xxcus.xxacl_pn_leases_all la,
(SELECT *
FROM xxcus.xxacl_pn_lease_det
WHERE sr_no = 1) ldet,
xxcus.xxacl_pn_lease_task_trl ltt,
xxcus.xxacl_pn_customer_followup fol,
xxacl_pn_flat_det_v flv,
xxcus.xxacl_pn_hold_task_v tid,
xxcus.xxacl_pn_hold_act_v act,
xxcus.xxacl_pn_followup_type_v flv,
xxcus.xxacl_pn_hold_act_v actt
WHERE la.booking_no = ltt.draft_form_no(+)
AND ltt.draft_form_no = fol.booking_no(+)
AND ltt.task_id = fol.task_id(+)
AND ltt.task_sr_no = fol.task_sr_no(+)
AND la.delete_flag = 'N'
AND la.booking_no = ldet.booking_no
AND fol.followup_date = TO_DATE (SYSDATE - 1)
AND la.flat_id = flv.flat_id
AND ltt.task_id = tid.task_id
AND fol.activity_id = act.act_id
AND fol.followup_type_id = flv.follow_type_id
AND fol.next_activity_id = actt.act_id
kindly suggest what is wrong
UPDATE
SELECT flv.property_name project_name, flv.building building_name,
flv.flat_no unit_no,
ldet.customer_fname
|| ' '
|| ldet.customer_mname
|| ' '
|| ldet.customer_lname customer_name,
la.booking_date holding_date, ltt.start_date task_date,
tid.task_desc task_type, fol.next_follow_up_date,
ltt.remarks task_comment, ltt.f_complete task_completed,
act.act_desc activity_description, flv.follow_type followup_type,
fol.remarks followup_comment,
fol.next_follow_up_date next_followup_date,
actt.act_desc next_todo_activity
FROM xxcus.xxacl_pn_leases_all la,
(SELECT *
FROM xxcus.xxacl_pn_lease_det
WHERE sr_no = 1) ldet,
xxcus.xxacl_pn_lease_task_trl ltt,
xxcus.xxacl_pn_customer_followup fol,
xxacl_pn_flat_det_v flv,
xxcus.xxacl_pn_hold_task_v tid,
xxcus.xxacl_pn_hold_act_v act,
xxcus.xxacl_pn_followup_type_v flv,
xxcus.xxacl_pn_hold_act_v actt
WHERE la.booking_no = ltt.draft_form_no(+)
AND ltt.draft_form_no = fol.booking_no(+)
AND ltt.task_id = fol.task_id(+)
AND ltt.task_sr_no = fol.task_sr_no(+)
AND la.delete_flag = 'N'
AND la.booking_no = ldet.booking_no
AND fol.followup_date = TO_DATE (SYSDATE - 1)
AND la.flat_id = flv.flat_id
AND ltt.task_id = tid.task_id
AND fol.activity_id = act.act_id
AND fol.followup_type_id = flv.follow_type_id
AND fol.next_activity_id = actt.act_id

The keyword you are missing is END. It should read like this:
CASE TASK_VALUE WHEN '0' THEN 'NO'
WHEN '1' THEN 'YES' END

Related

Not getting multiple data for binding parameters in select statement in oracle [duplicate]

I have this query that I'm passing 2 parameters, COUNTRY_REGION parameter and COST_CENTER parameter
I have the possibility to pass both parameters at the same time COST_CENTER and COUNTRY_REGION..... Or pass one or the other... This part is OK.
You can see in the first image below
SELECT
dwg.GEOGRAPHY_ID as geographyId
,INITCAP (lower (dwc.COUNTRY_REGION)) as countryRegion
,INITCAP (lower (dwc.COUNTRY_NAME)) as countryName
,dp.PROJECT_ID as projectId
FROM
DATALAKE.DWL_GEOGRAPHIES dwg
,DATALAKE.DWB_PROJECT dp
,DATALAKE.DWL_GEOGRAPHY_COUNTRIES dwgc
,DATALAKE.DWL_COUNTRY dwc
,DATALAKE.DWB_PROJECT_FINANCIAL dpf
,DATALAKE.DWL_GOLIVE dgl
where dwg.geography_id = dp.project_geography_id
and dwg.geography_id = dwgc.geography_id
and dwc.country_id = dwgc.country_id
and dp.PROJECT_ID = dpf.PROJECT_ID
and dpf.PROJECT_ID = dgl.PROJECT_ID
and dpf.FLAG_ACTIVE = 1
and ((dp.cost_center = (:costCenter) and INITCAP (lower (dwc.COUNTRY_REGION)) = (:countryRegion))
or (:costCenter IS null and INITCAP (lower (dwc.COUNTRY_REGION)) = (:countryRegion))
or (dp.cost_center = (:costCenter) and :countryRegion IS null)
)
order by dwc.COUNTRY_REGION
But I WANT TO HAVE THE OPTION TO PASSA TWO OR MORE IN THE SAME PARAMETER as in the image below...
example:
COUNTRY_REGION: Peru, Chile, Argentina
or
COST_CENTER : 10500, 1000, ... , ....
I would like help, I've tried several things and I can't proceed, thank you very much.
You cannot pass multiple values with a single bind variable.
What you can do is pass in a single string that contains a delimited list and match a sub-string of the list to the value:
SELECT *
FROM table_name
WHERE ', ' || :country_region_list || ', ' LIKE '%, ' || country_region || ', %'
OR ', ' || :cost_centre_list || ', ' LIKE '%, ' || cost_centre || ', %'
Which would make your query:
SELECT dwg.GEOGRAPHY_ID as geographyId
, INITCAP(dwc.COUNTRY_REGION) as countryRegion
, INITCAP(dwc.COUNTRY_NAME) as countryName
, dp.PROJECT_ID as projectId
FROM DATALAKE.DWL_GEOGRAPHIES dwg
INNER JOIN DATALAKE.DWB_PROJECT dp
ON (dwg.geography_id = dp.project_geography_id)
INNER JOIN DATALAKE.DWL_GEOGRAPHY_COUNTRIES dwgc
ON (dwg.geography_id = dwgc.geography_id)
INNER JOIN DATALAKE.DWL_COUNTRY dwc
ON (dwc.country_id = dwgc.country_id)
INNER JOIN DATALAKE.DWB_PROJECT_FINANCIAL dpf
ON (dp.PROJECT_ID = dpf.PROJECT_ID)
INNER JOIN DATALAKE.DWL_GOLIVE dgl
ON (dpf.PROJECT_ID = dgl.PROJECT_ID)
WHERE dpf.FLAG_ACTIVE = 1
AND (
', ' || :costCenter || ', ' LIKE '%, ' || dp.cost_center || ', %'
OR :costCenter IS null
)
AND (
', ' || :countryRegion || ', '
LIKE '%, ' || INITCAP(dwc.COUNTRY_REGION) || ', %'
OR :countryRegion IS null
)
AND (:costCenter IS NOT NULL OR :countryRegion IS NOT NULL)
order by dwc.COUNTRY_REGION

Report is taking hours to show the data and it returns 0 records

I have written a query using UNION functionality , but I'm not able to fetch any records. IT returns 0 records!!!
SELECT
p_mv.created_by createby,
TO_CHAR(sf_get_local(p_con.created_date,p_con.cdate_tz_code),'DD MON YY HH24:MI') createdate,
port.port_name dest,
p_con.pipeline_ref_id consolno,
reference_numbers.ref_num refnum,
route_s.vessel_name vessel,
route_s.flight_voyage voyage,
TO_CHAR(sf_get_local(route_s.orgn_vsl_arvl_date,route_s.orgn_vsl_arvl_date_tz_code),'DD/MM/YYYY') destdate,
TO_CHAR(sf_get_local(route_s.arrival_date,route_s.arrival_date_tz_code),'DD/MM/YYYY') origindate,
TO_CHAR(sf_get_local(route_s.orgn_vsl_arvl_date,route_s.orgn_vsl_arvl_date_tz_code),'DD MON YY HH24:MI') destdate,
TO_CHAR(sf_get_local(route_s.arrival_date,route_s.arrival_date_tz_code),'DD MON YY HH24:MI') origindate,
carrier.carrier_name carrier,
p_mv.pipeline_ref_id mawb,
sf_get_local(route_s.orgn_vsl_arvl_date,route_s.orgn_vsl_arvl_date_tz_code) real_origindate ,
DECODE(p_con.pipeline_tx_status,'O','OPEN','MFT','MANIFESTED','IS','ISSUED','CNF','CONFIRMED','OI','OPEN IMPORT','ICF','CONFIRM IMPORT','C','CLOSED','VD','VOID',p_con.pipeline_tx_status) status,
cd_code.cd_description consol_type
from pipeline p_mv,
pipeline p_con,
pipeline_relations pr_cs,
route route_s ,
reference_numbers,
carrier,
carrier_segment,
mwb_header ,
consol ,
port ,
(SELECT cd_code,cd_description FROM code_detail WHERE cm_code = '436') cd_code
where (p_mv.pipeline_tx_id = pr_cs.pipeline_tx_id) AND
(p_mv.pipeline_tx_type = 'CS') AND
(pr_cs.rel_pipeline_tx_id = p_con.pipeline_tx_id(+)) AND
(pr_cs.rel_pipeline_tx_type(+) = 'CO' ) and
(p_con.pipeline_tx_id = consol.pipeline_tx_id) and
(p_mv.pipeline_tx_id = mwb_header.pipeline_tx_id(+)) and
(p_mv.pipeline_tx_id = reference_numbers.pipeline_tx_id(+)) and
(reference_numbers.ref_num_type(+) = 'CN') and
(p_mv.pipeline_tx_id = route_s.pipeline_tx_id(+)) and
(route_s.route_type(+) = 'S') and
(route_s.line_no(+) = 1) and
p_mv.pipeline_tx_id = carrier_segment.pipeline_tx_id(+) and
carrier_segment.carrier_code = carrier.carrier_code(+) and
p_con.port_dest = port.port_code(+) and
cd_code.cd_code(+) = consol.consol_service_type
AND (p_con.orig_company_id ='570')
AND (p_con.orig_terminal_id ='5705')
AND p_con.pipeline_tx_status='CNF'
AND mwb_header.transport_mode='O'
and
/*Passing dates with the case statement to pass to different date abject at a time*/
case '&psStatusType'
when 'CTA' then p_con.created_date
when 'VAD' then route_s.orgn_vsl_arvl_date
when 'ETA' then route_s.arrival_date
else null -- default anyway
end
between to_date('&1','DD-MON-YYYY:HH24:MI:SS')
AND to_date('&2','DD-MON-YYYY:HH24:MI:SS')
UNION
/*Using UNION functionality to join the query*/
select
p_con.created_by createby,
TO_CHAR(sf_get_local(p_con.created_date,p_con.cdate_tz_code),'DD MON YY HH24:MI') createdate,
port.port_name dest,
p_con.pipeline_ref_id consolno,
'' refnum,
route_s.vessel_name vessel,
route_s.flight_voyage voyage,
TO_CHAR(sf_get_local(route_s.orgn_vsl_arvl_date,route_s.orgn_vsl_arvl_date_tz_code),'DD/MM/YYYY') destdate,
TO_CHAR(sf_get_local(route_s.arrival_date,route_s.arrival_date_tz_code),'DD/MM/YYYY') origindate,
TO_CHAR(sf_get_local(route_s.orgn_vsl_arvl_date,route_s.orgn_vsl_arvl_date_tz_code),'DD MON YY HH24:MI') destdate,
TO_CHAR(sf_get_local(route_s.arrival_date,route_s.arrival_date_tz_code),'DD MON YY HH24:MI') origindate,
carrier.carrier_name carrier,
'' mawb,
sf_get_local(route_s.orgn_vsl_arvl_date,route_s.orgn_vsl_arvl_date_tz_code) real_origindate ,
DECODE(p_con.pipeline_tx_status,'O','OPEN','MFT','MANIFESTED','IS','ISSUED','CNF','CONFIRMED','OI','OPEN IMPORT','ICF','CONFIRM IMPORT','C','CLOSED','VD','VOID',p_con.pipeline_tx_status) status,
cd_code.cd_description consol_type
from pipeline p_con,
route route_s ,
carrier,
consol ,
port,
PIPELINE_RELATIONS PREL_CS,
(SELECT cd_code,cd_description FROM code_detail WHERE cm_code = '436') cd_code
where (p_con.pipeline_tx_id = consol.pipeline_tx_id) and
P_CON.PIPELINE_TX_ID = PREL_CS.PIPELINE_TX_ID AND
PREL_CS.REL_PIPELINE_TX_TYPE ='CS' AND
(PREL_CS.REL_pipeline_tx_id = route_s.pipeline_tx_id(+)) and
(route_s.route_type(+) = 'S') and
(route_s.line_no(+) = 1) and
route_s.carrier_code = carrier.carrier_code(+) and
p_con.port_dest = port.port_code(+) and
cd_code.cd_code(+) = consol.consol_service_type and
p_con.orig_company_id='570' and
p_con.orig_terminal_id='5705' and
p_con.pipeline_tx_status='CNF' and
consol.transport_mode = 'O'
and
/*Passing dates with the case statement to pass to different date abject at a time*/
case '&psStatusType'
when 'CTA' then p_con.created_date
when 'VAD' then route_s.orgn_vsl_arvl_date
when 'ETA' then route_s.arrival_date
else null -- default anyway
end
between to_date('&1','DD-MON-YYYY:HH24:MI:SS')
AND to_date('&2','DD-MON-YYYY:HH24:MI:SS')

What is CHR code for single quote ' in oracle?

What is CHR code for single quote (') in oracle?
I am generating update statements ATM for tables without PKs, and Regex replacing some special characters.
select 'UPDATE QUESTION SET questiontypeid = '||questiontypeid||', questiontext = '''||questiontext||''', questiondescription = '''||questiondescription||''', productid = '||productid||', mandatory = '||CASE WHEN mandatory IS NULL THEN 'NULL' ELSE to_char(mandatory) END ||', displayorder = '||displayorder||', minvalue = '||CASE WHEN minvalue IS NULL THEN 'NULL' ELSE to_char(minvalue) END||', maxvalue = '||CASE WHEN maxvalue IS NULL THEN 'NULL' ELSE to_char(maxvalue) END||', parentid = '||CASE WHEN parentid IS NULL THEN 'NULL' ELSE to_char(parentid) END||', questioncategoryid = '||questioncategoryid||', isasset = '||CASE WHEN isasset IS NULL THEN 'NULL' ELSE to_char(isasset) END||', ISTHUNDERHEADONLY = '||CASE WHEN ISTHUNDERHEADONLY IS NULL THEN 'NULL' ELSE to_char(ISTHUNDERHEADONLY) END||', QCODE = '''||QCODE||''' WHERE QUESTIONID = '||QUESTIONID from (
(select * from question where questionid not in
(select t.questionid from question t
full join question#somecomp.co.uk d on t.questionid = d.questionid
where t.questiontypeid <> d.questiontypeid
or t.questiontext <> d.questiontext
or t.questiondescription <> d.questiondescription
or t.productid <> d.productid
or t.mandatory <> d.mandatory
or t.displayorder <> d.displayorder
or t.minvalue <> d.minvalue
or t.maxvalue <> d.maxvalue
or t.parentid <> d.parentid
or t.questioncategoryid <> d.questioncategoryid
or t.isasset <> d.isasset))
)
Let's see the ASCII value :
SQL> select ascii('''') from dual;
ASCII('''')
-----------
39
Or, to avoid multiple single-quotation marks, using literal quote technique :
SQL> select ascii(q'[']') from dual;
ASCII(Q'[']')
-------------
39
Let's confirm :
SQL> select chr(39) from dual;
C
-
'
SQL>
Thanks to #a_horse_with_no_name:
There is nothing special about ' in Oracle. It's the standard ASCII value.
Answer is : CHR(39)

Oracle SUM function not working in Select clause

I have the attached code below in which the sum(slice) column is giving me same results even if I don't use the SUM function. Can somebody help me fix the code?
Also need the following:
Currently, it is giving output as:
Task Name Resource ID Slice Date Slice Hours
abc 123 4/1 8.50
abc 123 4/2 7.50
abc 123 4/3 8.50
I need output as:
Task Name Resource ID Slice Date Slice Hours
abc 123 4/1 – 4/30 <Total for the month>
xyz 123 4/1 – 4/30 <Total for the month>
select distinct PCF.project_code as project_id,
PCF.project_name as project_name,
RCF.resource_id,
RCF.last_name || ' ' || RCF.first_name as resource_name,
Tsh.slice_date as fact_date,
sum(tsh.slice) "Total Slice",
T.prname as TaskName,
task_info.phase_code as phase_code,
task_info.phase_name as phase_name,
task_info.task_sequence as task_outline
from prj_blb_slicerequests tsr
JOIN prj_blb_slices tsh ON tsr.id = tsh.slice_request_id
JOIN prtimeentry TE ON tsh.prj_object_id = te.prid
JOIN prtimesheet TS ON TS.prid = TE.prtimesheetid
JOIN prAssignment A ON TE.prassignmentid = A.prid
JOIN prtask T ON A.prtaskid = T.prid
JOIN nbi_resource_current_facts RCF ON TS.prresourceid = RCF.resource_id
JOIN nbi_project_current_facts PCF ON T.prprojectid = PCF.project_id
JOIN prj_projects PP ON PCF.project_id = PP.prid
JOIN (select task_id, phase_code, phase_name, task_name, task_sequence from cust_phase_rollup_v) task_info ON T.prid = task_info.task_id
where tsr.request_name = 'Daily Timeentry Actuals'
and tsh.slice > 0
and ts.prstatus < 5
and tsh.slice_date >= to_date('1-MAY-14')
and tsh.slice_date <= to_date('31-MAY-14')
group by project_code, project_name, resource_id, RCF.last_name || ' ' || RCF.first_name, Tsh.slice_date, T.prname, task_info.phase_code,
task_info.phase_name, task_info.task_sequence
order by project_id
2 problems:
You want results per month, not per day, so instead of just selecting the slice_date, you should convert that into month/year format.
To use an aggregate function, you need a corresponding group by in your query. You should group by all of the other columns you're selecting.
This should work (untested, though):
select distinct PCF.project_code as project_id,
PCF.project_name as project_name,
RCF.resource_id,
RCF.last_name || ' ' || RCF.first_name as resource_name,
to_char(Tsh.slice_date, 'mm/yyyy') as fact_date,
sum(tsh.slice) "Total Slice",
T.prname as TaskName,
task_info.phase_code as phase_code,
task_info.phase_name as phase_name,
task_info.task_sequence as task_outline
from prj_blb_slicerequests tsr
JOIN prj_blb_slices tsh ON tsr.id = tsh.slice_request_id
JOIN prtimeentry TE ON tsh.prj_object_id = te.prid
JOIN prtimesheet TS ON TS.prid = TE.prtimesheetid
JOIN prAssignment A ON TE.prassignmentid = A.prid
JOIN prtask T ON A.prtaskid = T.prid
JOIN nbi_resource_current_facts RCF ON TS.prresourceid = RCF.resource_id
JOIN nbi_project_current_facts PCF ON T.prprojectid = PCF.project_id
JOIN prj_projects PP ON PCF.project_id = PP.prid
JOIN (select task_id, phase_code, phase_name, task_name, task_sequence from cust_phase_rollup_v) task_info ON T.prid = task_info.task_id
where tsr.request_name = 'Daily Timeentry Actuals'
and tsh.slice > 0
and ts.prstatus < 5
and tsh.slice_date >= to_date('1-MAY-14')
and tsh.slice_date <= to_date('31-MAY-14')
group by PCF.project_code,
PCF.project_name,
RCF.resource_id,
RCF.last_name || ' ' || RCF.first_name,
to_char(Tsh.slice_date, 'mm/yyyy'),
T.prname,
task_info.phase_code,
task_info.phase_name,
task_info.task_sequence
order by PCF.project_code

How do I pass a parameter into the Query.CommandText in SQL Server Reporting Services?

I have the following query against an Oracle database:
select * from (
select Person.pId, Person.lastName as patLast, Person.firstName as patFirst
, Person.dateOfBirth
, Medicate.mId, Medicate.startDate as medStart, Medicate.description
, cast(substr(Medicate.instructions, 1, 50) as char(50)) as instruct
, ml.convert_id_to_date(Prescrib.pubTime) as scripSigned
, max(ml.convert_id_to_date(Prescrib.pubTime)) over (partition by Prescrib.pId, Prescrib.mId) as LastScrip
, UsrInfo.pvId, UsrInfo.lastName as provLast, UsrInfo.firstName as provFirst
from ml.Medicate
join ml.Prescrib on Medicate.mId = Prescrib.mId
join ml.UsrInfo on Prescrib.pvId = UsrInfo.pvId
join ml.Person on Medicate.pId = Person.pId
where Person.isPatient = 'Y'
and Person.pStatus = 'A'
and Medicate.xId = 1.e+035
and Medicate.change = 2
and Medicate.stopDate > sysdate
and REGEXP_LIKE(Medicate.instructions
, ' [qtb]\.?[oi]?\.?[dw][^ayieo]'
|| '|[^a-z]mg?s'
|| '|ij'
|| '|[^a-z]iu[^a-z]'
|| '|[0-9 ]u '
|| '|[^tu]hs'
, 'i')
order by ScripSigned desc
) where scripSigned = lastScrip and scripSigned > date '2011-01-01'
I have a Report Parameter defined, DateBegin, defined as a DateTime, and I've associated it with a Query Parameter also called DateBegin. I just can't figure out how to replace "date '2011-01-01'" with "DateBegin" so that the blinkin' thing actually works. Thanks!
Use the Oracle format for parameters - so use the following:
) where scripSigned = lastScrip and scripSigned > :DateBegin
(The # sign is used in SQLServer to identify SQLServer variables.)

Resources