Sql query to retrieve st number
Hi,
I would like to query a street number column(varchar2) from address table.
st number could be any one of following.
1.1234
2.AHh-500
3.1700/2
4.15-A or 15A
in this case, i should get only values with numberic not any alpha or special character.
Query should return case 1, not case 2.
in case of case 3 or 4, query should return 1700 or 15(i.eif st num starts with number and has non numberic characters, it shud return only numerics)
Could anyone please post the query for this scenario?
Never fear, the regex man is here ... scnr. ;-) In 10g1, how about this:
WITH t AS (SELECT '1234' street
FROM dual
UNION
SELECT 'AHh-500'
FROM dual
UNION
SELECT '1700/2'
FROM dual
UNION
SELECT '15-A'
FROM dual
UNION
SELECT '15A'
FROM dual
SELECT t.*, REGEXP_SUBSTR(t.street, '^[[:digit:]]+') street_number
FROM t; Using this as filter:
SELECT *
FROM (SELECT t.*, REGEXP_SUBSTR(t.street, '^[[:digit:]]+') street_number
FROM t
WHERE street_number IS NOT NULL
;C.
Similar Messages
-
SQL query to retrieve totals from "supporting Details"
We've had a strange issue where the totals from the supporting details aren't adding up to the value submitted in essbase.
In effect our RDMS is out of sync with Planning.
In order to remedy this, I'm trying to build a SQL query to retrieve the value of the supporting details.
Here is my Sql statement so far :
SELECT
HO1.OBJECT_NAME SCENARIO,
HO2.OBJECT_NAME ACCOUNT,
HO3.OBJECT_NAME ENTITY,
HO4.OBJECT_NAME Month,
HO5.OBJECT_NAME Version,
HO6.OBJECT_NAME Currency,
HO7.OBJECT_NAME Year,
HO8.OBJECT_NAME Project,
HO10.OBJECT_NAME CC,
HO12.OBJECT_NAME Grplcl,
HO13.OBJECT_NAME Product,
HO15.OBJECT_NAME SalesRegion,
HO16.OBJECT_NAME ContractAnalysis,
HO17.OBJECT_NAME IC,
SUM(NVL(HCDI.VALUE,0)) AMOUNT
FROM hsp_column_detail HCD,
hsp_column_detail_ITEM HCDI,
HSP_OBJECT HO1,
HSP_OBJECT HO2,
HSP_OBJECT HO3,
HSP_OBJECT HO4,
HSP_OBJECT HO5,
HSP_OBJECT HO6,
HSP_OBJECT HO7,
HSP_OBJECT HO8,
HSP_OBJECT HO10,
HSP_OBJECT HO12,
HSP_OBJECT HO13,
HSP_OBJECT HO15,
HSP_OBJECT HO16,
HSP_OBJECT HO17
WHERE hcd.detail_id = hcdi.detail_id
AND hcd.dim1 = ho1.object_id
AND hcd.dim2 = ho2.object_id
AND hcd.dim3 = ho3.object_id
AND hcd.dim4 = ho4.object_id
AND hcd.dim5 = ho5.object_id
AND hcd.dim6 = ho6.object_id
AND hcd.dim7 = ho7.object_id
AND hcd.dim8 = ho8.object_id
AND hcd.dim10 = ho10.object_id
AND hcd.dim12 = ho12.object_id
AND hcd.dim13 = ho13.object_id
AND hcd.dim15 = ho15.object_id
AND hcd.dim16 = ho16.object_id
AND hcd.dim17 = ho17.object_id
and hcdi.generation = 0
GROUP BY HO1.OBJECT_NAME, HO2.OBJECT_NAME, HO3.OBJECT_NAME, HO4.OBJECT_NAME,
HO5.OBJECT_NAME,
HO6.OBJECT_NAME,
HO7.OBJECT_NAME,
HO8.OBJECT_NAME,
HO10.OBJECT_NAME,
HO12.OBJECT_NAME,
HO13.OBJECT_NAME,
HO15.OBJECT_NAME,
HO16.OBJECT_NAME,
HO17.OBJECT_NAME
ORDER BY HO1.OBJECT_NAME, HO3.OBJECT_NAME, HO2.OBJECT_NAMEThis query works using PL SQL and has been used on Oracle 9.2.3.
Cheers,
Jeremie
Here is the Query:
set serveroutput on;
set serveroutput on size 1000000;
--set buffer_size 10000000;
--set line_size 2000;
--DBMS_OUTPUT.size = 2000000;
DECLARE
CURSOR BUDGET_HEADERS_CU IS
SELECT
hcd.detail_id,
HO1.OBJECT_NAME SCENARIO,
HO2.OBJECT_NAME ACCOUNT,
HO3.OBJECT_NAME ENTITY,
HO4.OBJECT_NAME Month,
HO5.OBJECT_NAME Version,
HO6.OBJECT_NAME Currency,
HO7.OBJECT_NAME Year,
HO8.OBJECT_NAME Project,
HO10.OBJECT_NAME CC,
HO12.OBJECT_NAME Grplcl,
HO13.OBJECT_NAME Product,
HO15.OBJECT_NAME SalesRegion,
HO16.OBJECT_NAME ContractAnalysis,
HO17.OBJECT_NAME IC,
hcd.dim1 SCENARIO_id,
hcd.dim2 ACCOUNT_id,
hcd.dim3 ENTITY_id,
hcd.dim4 Month_id,
hcd.dim5 Version_id,
hcd.dim6 Currency_ID,
hcd.dim7 Year_ID,
hcd.dim8 Project_ID,
hcd.dim10 CC_ID,
hcd.dim12 Grplcl_ID,
hcd.dim13 Product_ID,
hcd.dim15 SalesRegion_ID,
hcd.dim16 ContractAnalysis_ID,
hcd.dim17 IC_ID
FROM hsp_column_detail HCD, /*hsp_column_detail_item HCDi,*/
HSP_OBJECT HO1,
HSP_OBJECT HO2,
HSP_OBJECT HO3,
HSP_OBJECT HO4,
HSP_OBJECT HO5,
HSP_OBJECT HO6,
HSP_OBJECT HO7,
HSP_OBJECT HO8,
HSP_OBJECT HO10,
HSP_OBJECT HO12,
HSP_OBJECT HO13,
HSP_OBJECT HO15,
HSP_OBJECT HO16,
HSP_OBJECT HO17
WHERE /*hcd.detail_id = hcdi.detail_id
AND*/ hcd.dim1 = ho1.object_id
AND hcd.dim2 = ho2.object_id
AND hcd.dim3 = ho3.object_id
AND hcd.dim4 = ho4.object_id
AND hcd.dim5 = ho5.object_id
AND hcd.dim6 = ho6.object_id
AND hcd.dim7 = ho7.object_id
AND hcd.dim8 = ho8.object_id
AND hcd.dim10 = ho10.object_id
AND hcd.dim12 = ho12.object_id
AND hcd.dim13 = ho13.object_id
AND hcd.dim15 = ho15.object_id
AND hcd.dim16 = ho16.object_id
AND hcd.dim17 = ho17.object_id
and HO5.OBJECT_NAME = 'Working'
--and hcdi.generation != 0
--and hcdi.label like 'JRTEST-%'
--and hcd.detail_id = 253102
/*GROUP BY HCD.Dim1, HCD.Dim2, HCD.Dim3, HCD.Dim4,
HCD.Dim5,
HCD.Dim6,
HCD.Dim7,
HCD.Dim8,
HCD.Dim10,
HCD.Dim12,
HCD.Dim13,
HCD.Dim15,
HCD.Dim16,
HCD.Dim17*/;
CURSOR BUDGET_DETAILS_CU(
/*scenario_p in varchar2,
account_p in varchar2,
entity_p in varchar2,
month_p in varchar2,
version_p in varchar2,
currency_p in varchar2,
year_p in varchar2,
project_p in varchar2,
cc_p in varchar2,
grplcl_p in varchar2,
product_p in varchar2,
salesregion_p in varchar2,
contractanalysis_p in varchar2,
ic_p in varchar2*/
detail_id_p in number
IS
SELECT
/* HO1.OBJECT_NAME SCENARIO,
HO2.OBJECT_NAME ACCOUNT,
HO3.OBJECT_NAME ENTITY,
HO4.OBJECT_NAME Month,
HO5.OBJECT_NAME Version,
HO6.OBJECT_NAME Currency,
HO7.OBJECT_NAME Year,
HO8.OBJECT_NAME Project,
HO10.OBJECT_NAME CC,
HO12.OBJECT_NAME Grplcl,
HO13.OBJECT_NAME Product,
HO15.OBJECT_NAME SalesRegion,
HO16.OBJECT_NAME ContractAnalysis,
HO17.OBJECT_NAME IC, */
label,
NVL(HCDI.VALUE,0) AMOUNT,
HCDI.Position,
HCDI.operator,
HCDI.generation
FROM /*hsp_column_detail HCD,*/
hsp_column_detail_ITEM HCDI
/* HSP_OBJECT HO1,
HSP_OBJECT HO2,
HSP_OBJECT HO3,
HSP_OBJECT HO4,
HSP_OBJECT HO5,
HSP_OBJECT HO6,
HSP_OBJECT HO7,
HSP_OBJECT HO8,
HSP_OBJECT HO10,
HSP_OBJECT HO12,
HSP_OBJECT HO13,
HSP_OBJECT HO15,
HSP_OBJECT HO16,
HSP_OBJECT HO17*/
--WHERE hcd.detail_id = hcdi.detail_id
WHERE hcdi.detail_id = detail_id_p
/*AND hcd.dim1 = ho1.object_id
AND hcd.dim2 = ho2.object_id
AND hcd.dim3 = ho3.object_id
AND hcd.dim4 = ho4.object_id
AND hcd.dim5 = ho5.object_id
AND hcd.dim6 = ho6.object_id
AND hcd.dim7 = ho7.object_id
AND hcd.dim8 = ho8.object_id
AND hcd.dim10 = ho10.object_id
AND hcd.dim12 = ho12.object_id
AND hcd.dim13 = ho13.object_id
AND hcd.dim15 = ho15.object_id
AND hcd.dim16 = ho16.object_id
AND hcd.dim17 = ho17.object_id*/
--and hcdi.generation != 0
--and label like 'JRTEST-%'
/*AND HCD.Dim1 = scenario_p
AND HCD.Dim2 = account_p
AND HCD.Dim3 = entity_p
AND HCD.Dim4 = month_p
AND HCD.Dim5 = version_p
AND HCD.Dim6 = currency_p
AND HCD.Dim7 = year_p
AND HCD.Dim8 = project_p
AND HCD.Dim10 = cc_p
AND HCD.Dim12 = grplcl_p
AND HCD.Dim13 = product_p
AND HCD.Dim15 = salesregion_p
AND HCD.Dim16 = contractanalysis_p
AND HCD.Dim17 = ic_p*/
ORDER BY hcdi.position;
-- Variable Declaration
running_total_ln number :=0;
prev_running_total_ln number :=0;
add_amount number :=0;
multiply_amount number :=1;
prev_generation number :=0;
prev_add_amount number :=0;
prev_multiply_amount number :=1;
running_total_gen0 number :=0;
running_gen0_op number :=0;
running_total_gen1 number :=0;
running_gen1_op number :=0;
running_total_gen2 number :=0;
running_gen2_op number :=0;
running_total_gen3 number :=0;
running_gen3_op number :=0;
running_total_gen4 number :=0;
running_gen4_op number :=0;
running_total_gen5 number :=0;
running_gen5_op number :=0;
output_file utl_file.file_type;
-- Begin PL/SQL processing
BEGIN
--DBMS_OUTPUT.
DBMS_OUTPUT.ENABLE(1000000);
DBMS_OUTPUT.PUT_LINE('Begin Processing');
output_file := utl_file.fopen ('C:\temp', 'test.txt', 'W');
-- Begin Header For Loop
FOR BUDGET_HEADERS_CV IN BUDGET_HEADERS_CU
LOOP
--Reset Running Totals
running_total_ln:=0;
prev_running_total_ln:=0;
add_amount:=0;
multiply_amount:=1;
prev_generation:=0;
prev_add_amount:=0;
prev_multiply_amount:=1;
running_total_gen0:=0;
running_gen0_op:=0;
running_total_gen1:=0;
running_gen1_op:=0;
running_total_gen2:=0;
running_gen2_op:=0;
running_total_gen3:=0;
running_gen3_op:=0;
running_total_gen4:=0;
running_gen4_op:=0;
running_total_gen5:=0;
running_gen5_op:=0;
-- Begin Detail For Loop
FOR BUDGET_DETAILS_CV IN BUDGET_DETAILS_CU(
BUDGET_HEADERS_CV.Detail_ID
/*BUDGET_HEADERS_CV.SCENARIO_ID,
BUDGET_HEADERS_CV.ACCOUNT_ID,
BUDGET_HEADERS_CV.ENTITY_ID,
BUDGET_HEADERS_CV.Month_ID,
BUDGET_HEADERS_CV.Version_ID,
BUDGET_HEADERS_CV.Currency_ID,
BUDGET_HEADERS_CV.Year_ID,
BUDGET_HEADERS_CV.Project_ID,
BUDGET_HEADERS_CV.CC_ID,
BUDGET_HEADERS_CV.Grplcl_ID,
BUDGET_HEADERS_CV.Product_ID,
BUDGET_HEADERS_CV.SalesRegion_ID,
BUDGET_HEADERS_CV.ContractAnalysis_ID,
BUDGET_HEADERS_CV.IC_ID*/
LOOP
-- Null;
add_amount :=0;
multiply_amount :=1;
IF BUDGET_DETAILS_CV.OPERATOR = 1 THEN
add_amount := BUDGET_DETAILS_CV.Amount;
End if;
IF BUDGET_DETAILS_CV.OPERATOR = 2 THEN
add_amount := BUDGET_DETAILS_CV.Amount * -1;
End if;
IF BUDGET_DETAILS_CV.OPERATOR = 3 THEN
multiply_amount := BUDGET_DETAILS_CV.Amount;
End if;
IF BUDGET_DETAILS_CV.OPERATOR = 4 THEN
multiply_amount := 1/BUDGET_DETAILS_CV.Amount;
End if;
IF BUDGET_DETAILS_CV.Position = 0 then
running_total_gen0 := add_amount*multiply_amount;-- we are dealing with the first line
Else
if BUDGET_DETAILS_CV.GENERATION = prev_generation then
-- run whatever total we are on up
if BUDGET_DETAILS_CV.GENERATION = 0 then
running_total_gen0 := (running_total_gen0 + add_amount)*multiply_amount;
end if;
if BUDGET_DETAILS_CV.GENERATION = 1 then
running_total_gen1 := (running_total_gen1 + add_amount)*multiply_amount;
end if;
if BUDGET_DETAILS_CV.GENERATION = 2 then
running_total_gen2 := (running_total_gen2 + add_amount)*multiply_amount;
end if;
if BUDGET_DETAILS_CV.GENERATION = 3 then
running_total_gen3 := (running_total_gen3 + add_amount)*multiply_amount;
end if;
end if;
if BUDGET_DETAILS_CV.GENERATION = prev_generation +1 then
-- we are going up a generation
-- (we cannot go up to gen0
if BUDGET_DETAILS_CV.GENERATION = 1 then
running_total_gen1:=0;-- reset gen1 counter
running_gen0_op:=BUDGET_DETAILS_CV.OPERATOR; -- Store the sign for later operation
running_total_gen0 := running_total_gen0/prev_multiply_amount - prev_add_amount; -- Remove parent from Gen 0 total
running_total_gen1 := (running_total_gen1 + add_amount)*multiply_amount;
end if;
if BUDGET_DETAILS_CV.GENERATION = 2 then
running_total_gen2:=0;-- reset gen1 counter
running_gen1_op:=BUDGET_DETAILS_CV.OPERATOR; -- Store the sign for later operation
running_total_gen1 := running_total_gen1/prev_multiply_amount - prev_add_amount; -- Remove parent from Gen 1 total
running_total_gen2 := (running_total_gen2 + add_amount)*multiply_amount;
end if;
if BUDGET_DETAILS_CV.GENERATION = 3 then
running_total_gen3:=0;-- reset gen1 counter
running_gen2_op:=BUDGET_DETAILS_CV.OPERATOR; -- Store the sign for later operation
running_total_gen2 := running_total_gen2/prev_multiply_amount - prev_add_amount; -- Remove parent from Gen 2 total
running_total_gen3 := (running_total_gen3 + add_amount)*multiply_amount;
end if;
end if;
if BUDGET_DETAILS_CV.GENERATION = prev_generation - 1 then
-- we are Down a generation
if BUDGET_DETAILS_CV.GENERATION = 0 then
--we need to "add" back the previous gen 1 parent
IF running_gen0_op = 1 THEN
running_total_gen0 := running_total_gen0 + running_total_gen1;
End if;
IF running_gen0_op = 2 THEN
running_total_gen0 := running_total_gen0 - running_total_gen1;
End if;
IF running_gen0_op = 3 THEN
running_total_gen0 := running_total_gen0 * running_total_gen1;
End if;
IF running_gen0_op = 4 THEN
running_total_gen0 := running_total_gen0 / running_total_gen1;
End if;
-- we need to add the current member to the gen0
running_total_gen0 := (running_total_gen0 + add_amount)*multiply_amount;
END IF;
if BUDGET_DETAILS_CV.GENERATION = 1 then
--we need to "add" back the previous gen 2 parent
IF running_gen1_op = 1 THEN
running_total_gen1 := running_total_gen1 + running_total_gen2;
End if;
IF running_gen1_op = 2 THEN
running_total_gen1 := running_total_gen1 - running_total_gen2;
End if;
IF running_gen1_op = 3 THEN
running_total_gen1 := running_total_gen1 * running_total_gen2;
End if;
IF running_gen1_op = 4 THEN
running_total_gen1 := running_total_gen1 / running_total_gen2;
End if;
-- we need to add the current member to the gen1
running_total_gen1 := (running_total_gen1 + add_amount)*multiply_amount;
end if;
if BUDGET_DETAILS_CV.GENERATION = 2 then
--we need to "add" back the previous gen 3 parent
IF running_gen1_op = 1 THEN
running_total_gen2 := running_total_gen2 + running_total_gen3;
End if;
IF running_gen1_op = 2 THEN
running_total_gen2 := running_total_gen2 - running_total_gen3;
End if;
IF running_gen1_op = 3 THEN
running_total_gen2 := running_total_gen2 * running_total_gen3;
End if;
IF running_gen1_op = 4 THEN
running_total_gen2 := running_total_gen2 / running_total_gen3;
End if;
-- we need to add the current member to the gen2
running_total_gen2 := (running_total_gen2 + add_amount)*multiply_amount;
end if;
end if;
if BUDGET_DETAILS_CV.GENERATION = prev_generation - 2 then
-- we are Down 2 generations
if BUDGET_DETAILS_CV.GENERATION = 0 then
--we need to "add" back the previous gen 2 parent
IF running_gen1_op = 1 THEN
running_total_gen1 := running_total_gen1 + running_total_gen2;
End if;
IF running_gen1_op = 2 THEN
running_total_gen1 := running_total_gen1 - running_total_gen2;
End if;
IF running_gen1_op = 3 THEN
running_total_gen1 := running_total_gen1 * running_total_gen2;
End if;
IF running_gen1_op = 4 THEN
running_total_gen1 := running_total_gen1 / running_total_gen2;
End if;
--we need to "add" back the previous gen 1 parent
IF running_gen0_op = 1 THEN
running_total_gen0 := running_total_gen0 + running_total_gen1;
End if;
IF running_gen0_op = 2 THEN
running_total_gen0 := running_total_gen0 - running_total_gen1;
End if;
IF running_gen0_op = 3 THEN
running_total_gen0 := running_total_gen0 * running_total_gen1;
End if;
IF running_gen0_op = 4 THEN
running_total_gen0 := running_total_gen0 / running_total_gen1;
End if;
-- we need to add the current member to the gen0
running_total_gen0 := (running_total_gen0 + add_amount)*multiply_amount;
END IF;
if BUDGET_DETAILS_CV.GENERATION = 1 then
--we need to "add" back the previous gen 3 parent
IF running_gen1_op = 1 THEN
running_total_gen2 := running_total_gen2 + running_total_gen3;
End if;
IF running_gen1_op = 2 THEN
running_total_gen2 := running_total_gen2 - running_total_gen3;
End if;
IF running_gen1_op = 3 THEN
running_total_gen2 := running_total_gen2 * running_total_gen3;
End if;
IF running_gen1_op = 4 THEN
running_total_gen2 := running_total_gen2 / running_total_gen3;
End if;
--we need to "add" back the previous gen 2 parent
IF running_gen1_op = 1 THEN
running_total_gen1 := running_total_gen1 + running_total_gen2;
End if;
IF running_gen1_op = 2 THEN
running_total_gen1 := running_total_gen1 - running_total_gen2;
End if;
IF running_gen1_op = 3 THEN
running_total_gen1 := running_total_gen1 * running_total_gen2;
End if;
IF running_gen1_op = 4 THEN
running_total_gen1 := running_total_gen1 / running_total_gen2;
End if;
-- we need to add the current member to the gen1
running_total_gen1 := (running_total_gen1 + add_amount)*multiply_amount;
END IF;
if BUDGET_DETAILS_CV.GENERATION = prev_generation - 3 then
-- we are Down 3 generations
if BUDGET_DETAILS_CV.GENERATION = 0 then
--we need to "add" back the previous gen 3 parent
IF running_gen1_op = 1 THEN
running_total_gen2 := running_total_gen2 + running_total_gen3;
End if;
IF running_gen1_op = 2 THEN
running_total_gen2 := running_total_gen2 - running_total_gen3;
End if;
IF running_gen1_op = 3 THEN
running_total_gen2 := running_total_gen2 * running_total_gen3;
End if;
IF running_gen1_op = 4 THEN
running_total_gen2 := running_total_gen2 / running_total_gen3;
End if;
--we need to "add" back the previous gen 2 parent
IF running_gen1_op = 1 THEN
running_total_gen1 := running_total_gen1 + running_total_gen2;
End if;
IF running_gen1_op = 2 THEN
running_total_gen1 := running_total_gen1 - running_total_gen2;
End if;
IF running_gen1_op = 3 THEN
running_total_gen1 := running_total_gen1 * running_total_gen2;
End if;
IF running_gen1_op = 4 THEN
running_total_gen1 := running_total_gen1 / running_total_gen2;
End if;
--we need to "add" back the previous gen 1 parent
IF running_gen0_op = 1 THEN
running_total_gen0 := running_total_gen0 + running_total_gen1;
End if;
IF running_gen0_op = 2 THEN
running_total_gen0 := running_total_gen0 - running_total_gen1;
End if;
IF running_gen0_op = 3 THEN
running_total_gen0 := running_total_gen0 * running_total_gen1;
End if;
IF running_gen0_op = 4 THEN
running_total_gen0 := running_total_gen0 / running_total_gen1;
End if;
-- we need to add the current member to the gen0
running_total_gen0 := (running_total_gen0 + add_amount)*multiply_amount;
END IF;
end if;
END IF;
End IF;
END LOOP; -- End of Detail Loop
--DBMS_OUTPUT.PUT_LINE(BUDGET_HEADERS_CV.SCENARIO||';'||BUDGET_HEADERS_CV.ACCOUNT||';'||BUDGET_HEADERS_CV.ENTITY||';'||BUDGET_HEADERS_CV.Month||';'||BUDGET_HEADERS_CV.Version||';'||BUDGET_HEADERS_CV.Currency||';'||BUDGET_HEADERS_CV.Year||';'||BUDGET_HEADERS_CV.Project||';'||BUDGET_HEADERS_CV.CC||';'||BUDGET_HEADERS_CV.Grplcl||';'||BUDGET_HEADERS_CV.Product||';'||BUDGET_HEADERS_CV.SalesRegion||';'||BUDGET_HEADERS_CV.ContractAnalysis||';'||BUDGET_HEADERS_CV.IC||';'||running_total_gen0);
utl_file.put_line(output_file,BUDGET_HEADERS_CV.SCENARIO||';'||BUDGET_HEADERS_CV.ACCOUNT||';'||BUDGET_HEADERS_CV.ENTITY||';'||BUDGET_HEADERS_CV.Month||';'||BUDGET_HEADERS_CV.Version||';'||BUDGET_HEADERS_CV.Currency||';'||BUDGET_HEADERS_CV.Year||';'||BUDGET_HEADERS_CV.Project||';'||BUDGET_HEADERS_CV.CC||';'||BUDGET_HEADERS_CV.Grplcl||';'||BUDGET_HEADERS_CV.Product||';'||BUDGET_HEADERS_CV.SalesRegion||';'||BUDGET_HEADERS_CV.ContractAnalysis||';'||BUDGET_HEADERS_CV.IC||';'||running_total_gen0);
/*BUDGET_HEADERS_CV.SCENARIO, BUDGET_HEADERS_CV.ACCOUNT, BUDGET_HEADERS_CV.ENTITY, BUDGET_HEADERS_CV.Month, BUDGET_HEADERS_CV.Version,BUDGET_HEADERS_CV.Currency, BUDGET_HEADERS_CV.Year, BUDGET_HEADERS_CV.Project, BUDGET_HEADERS_CV.CC, BUDGET_HEADERS_CV.Grplcl, BUDGET_HEADERS_CV.Product, BUDGET_HEADERS_CV.SalesRegion, BUDGET_HEADERS_CV.ContractAnalysis, BUDGET_HEADERS_CV.IC */
END LOOP; -- End of Header Loop
--DBMS_OUTPUT.PUT_LINE('Total');
-- DBMS_OUTPUT.PUT_LINE('End Processing');
utl_file.fclose(output_file);
END;
Edited by: JeremieR on Apr 18, 2011 5:02 AM -
Query to retrieve the number of transactions done in every 1 hour for last
Hi,
Could anyone help in writing a query to retrieve the number of transactions done in every 1 hour for last month.
Case:
I/P
Cases Timestamp1
case1 01-01-2008 00:00:01
case2 01-01-2008 00:01:01
case3 01-01-2008 01:00:01
case1 01-01-2008 01:02:01
case4 01-01-2008 01:10:01
case5 02-01-2008 02:00:01
case6 02-01-2008 02:10:01
case7 02-01-2008 23:00:01
case.. 31-01-2008 24:00:00
O/P
from time to_time cases
01-01-2008 00:00:00 01-01-2008 01:00:00 2
01-01-2008 01:00:01 01-01-2008 02:00:00 3
etc
Any help really appreciatedWe can do this using analytical functions
Following is what I did:
create table timestamp1 (ts date)
select *from timestamp1
30/10/2008 15:41:13
30/10/2008 15:41:05
30/10/2008 15:40:03
30/10/2008 14:58:26
30/10/2008 14:29:45
30/10/2008 13:17:48
30/10/2008 08:29:50
30/10/2008 06:05:51
30/10/2008 03:41:52
30/10/2008 02:29:54
select distinct to_char(ts,'hh24') frmhrs,
to_char(ts,'hh24')+1 tohrs, count(ts) OVER (order by to_number(to_char(ts,'hh24')) RANGE (1/24) PRECEDING )
from timestamp1
where trunc(ts)=trunc(sysdate) -- I added this just to make sure I get for today's data
order by frmhrs
FRMHRS TOHRS CNT
02 3 1
03 4 1
06 7 1
08 9 1
13 14 1
14 15 2
15 16 3
You can customizeas per ur need. -
Need efficient SQL query to retrieve data for MLM website
Table cd_members
MemberID LeftID RightID
1 2 3
2 4 5
3 6 7
4 8 -
5 - -
6 - -
7 - -
8 9 10
9 - -
10 - -
i want to execute a query to retrieve downline of a member say MemberID 2
o/p should be Records with MemberID 4,5,8,9,10
i am using this query but don't know how to start from MembeID=2
select a.memberid, a.tradingid, a.refid, a.parentid, a.node, a.leftid, a.rightid, b.memberid from cd_members a, cd_members b
where (a.leftid=b.memberid or a.rightid=b.memberid)Again, assuming that the number of levels is known, you can just do N self joins (note that I intentionally go one level further than necessary in this case
SQL> ed
Wrote file afiedt.buf
1 with t
2 as
3 (
4 select 1 person, 2 left, 3 right from dual union all
5 select 2, 4, 5 from dual union all
6 select 3, 6, 7 from dual union all
7 select 4, 8, null from dual union all
8 select 5, null, null from dual union all
9 select 6, null, null from dual union all
10 select 7, null, null from dual union all
11 select 8, 9, 10 from dual union all
12 select 9, null, null from dual union all
13 select 10, null, null from dual
14 )
15 select t1.left, t1.right, t2.left, t2.right, t3.left, t3.right, t4.left, t
4.right
16 from t t1,
17 t t2,
18 t t3,
19 t t4
20 where t1.person = 2
21 and t1.left = t2.person(+)
22 and t2.left = t3.person(+)
23* and t3.left = t4.person(+)
SQL> /
LEFT RIGHT LEFT RIGHT LEFT RIGHT LEFT
RIGHT
4 5 8 9 10Justin -
SQL Query to retrieve the All records based on the Max Dates.
Hello all,
I am trying to retrieve the newest record based on the date field ( nextDate ).
Currently there are only 4 records in the MC_Maintenance table and two in the Machine table.
Machine table
MC_id EquipID
1 0227
MC_id EquipID
2 0228
MC_Maintenance table
Maint_id MC_id Next_maint
1 2 08/25/2010
2 2 07/01/2010
3 1 06/11/2010
4 1 07/11/2010
What I am trying to accomplish is,
list the two machines from the Machine table with the MAX(Next_maint) controlling the MC_Maintenance output list
These are the records that I would like to Display.
Maint_id MC_id Next_maint
1 2 08/25/2010
4 1 07/11/2010
Below is the SQL Query
SELECT
MC.MC_ID as ID,
MC.complete_Date as completed,
MC.next_maint as nextDate,
MC.maint_notes as Notes,
MC.facility as Facility,
M.EquipId,
M.name as name,
M.SerialNumber as SN,
M.dept as dept,
M.Freq as freq
From MC_Maintenance MC, Machine M
where MC.MC_ID = M.MC_ID
' USING MAX(nextDate )
Any ideas would help.
TJI would have thought that was a simple group by problem?
SELECT M.EquipID, MC.MC_ID, Max(MC.next_maint)
FROM MC_Maintenance MC INNER JOIN Machine M ON MC.MC_ID = M.MC_ID
GROUP BY M.EquipID, MC.MC_ID -
Discoverer Report - SQL Query to retrieve list of parameters
Hi
We have around 100 Discoverer reports and I'm trying to find the list of parameters used in each report.
Is there any SQL query that I can use to find the parameters used for each report
or
should I open the reports one by one to fetch the list of parameters used.
I checked the tables in EUL4_US schema but no luck.
- Amarnath.Kgive this a try for starters. It looks at the custom folders defined in the business area, which is where any LOV's defined for you parameters would be. As far as looking at the workbooks themselves, no, there is no way to do that in sql.
select
decode(eb.ba_created_by,'ORACLE_APPS','Seeded','Custom') Origin,
eb.ba_name business_area,
eo.OBJ_TYPE,
decode(eo.obj_type, 'SOBJ','From Database', 'CUO', 'Custom Query', 'COBJ', 'Complex Folder', eo.obj_type) obj_type_desc,
eo.OBJ_NAME folder,
eo.OBJ_DESCRIPTION folder_description,
decode(eo.sobj_ext_table, NULL, NULL, eo.obj_ext_owner || '.' || eo.sobj_ext_table) base_object,
decode(es.seg_obj_id, null, UPPER(trim(eo.obj_object_sql1)||trim(eo.obj_object_sql2)||trim(eo.obj_object_sql3))
, UPPER(trim(es.seg_chunk1) || trim(es.seg_chunk2) || trim(es.seg_chunk3) || trim(es.seg_chunk4))) SQL
from
eul4_us.EUL5_BAS eb,
eul4_us.EUL5_objs eo,
eul4_us.EUL5_BA_OBJ_LINKS ebol,
eul4_us.eul5_segments es
where
eb.BA_ID = ebol.BOL_BA_ID and
ebol.BOL_OBJ_ID = eo.OBJ_ID and
eo.obj_id = es.seg_obj_id (+) and
decode(eb.ba_created_by,'ORACLE_APPS','Seeded','Custom') = 'Custom'
order by
/*decode(eb.ba_created_by,'ORACLE_APPS','Seeded','Custom'), */eb.ba_name, ebol.BOL_SEQUENCE, es.seg_sequence -
Sql query to retrieve records in parent sibling relationship in a table
I need help to write this Sql query on a table where it has the primary_id, parent_id, and sibling_id. A new row can be created with or without the parent_id and sibling_id. However most of the new rows are created from an existing row and the primary_id
of the existing row is inserted to the parent_id of the new row. This can go on for many many rows such as (B's parent_id has A primary_id, A's sibling_id has B primary_id), (C's parent_id has B primary_id, B's sibling_id has C primary_id), (D's
parent_id has C primary_id, C's sibling_id has D primary_id), What I want is when user pass on a primary_id on a row, the query will give me back all the related parent_id or sibling_id records. For example User pass the A's primary_id the query will
give me back the Row B and Row C as a sibling rows or user pass the C's primary_id the query should give me back row B and Row A as parent rows. Thanks
KahluaCheck http://social.technet.microsoft.com/wiki/contents/articles/21062.t-sql-hierarchical-table-sorting-with-a-parent-child-relation.aspx
For every expert, there is an equal and opposite expert. - Becker's Law
My blog
My TechNet articles -
SQL query to retrieve families with only one generation from a family db
Hi,
I need to write an SQL query that would select families with only one generation ((which means a person - or a married couple - with kids) or (a person - or a married couple - with parents but no kids)) from a database containing families. The simplified db structure is:
person:
id - int, primary key
siblings:
id
parent_id
kid_id
marriage
id:
husband_id
wife_id
Could anyone please help me? I dont really have any idea how to accomplish that...Hi,
Welcome to the forum!
Assuming that siblings (despite its name) contains only parent-child relationships, and that the parent_id could be either the mother or the father:
WITH got_tree AS
SELECT CONNECT_BY_ROOT s.parent_id AS root_id
, LEVEL AS lvl
FROM siblings s
LEFT OUTER JOIN marriage m ON s.parent_id IN ( m.husband_id
, m.wife_id
START WITH parent_id NOT IN ( SELECT kid_id
FROM siblings
CONNECT BY s.parent_id IN ( PRIOR m.husband_id
, PRIOR m.wife_id
, PRIOR kid_id
SELECT root_id
FROM got_tree
GROUP BY root_id
HAVING MAX (lvl) <= 2
;This will get a list of parents who may or may not have children, but who do not have either parents or grandchildren.
To get their spouses and children (if any) you will have to join this result set to all the other tables. At that point, you can eliminate poeple who do not have ancestors themselves, but are married to people who have more than one generation of ancestors.
The details of how to do that depend on the details of your tables. If you'd like help, then post a little sample data (CREATE TABLE and INSERT statements for all tables) and the results you want from that data.
Edited by: Frank Kulash on Oct 14, 2009 4:05 PM
I'd need sample data in order to test this, of course. -
SQL Query to retrieve one line from duplicate records
Hi
I have one table which contains duplicate records in multiple column but the difference is in one column which contains the value 0 or positive. The query i want is to retrieve only the line with the positive value for only the duplicated records.
here below a sample data for your reference:
CREATE TABLE TRANS
CALLTRANSTYPE NVARCHAR2(6),
ORIGANI NVARCHAR2(40),
TERMANI NVARCHAR2(40),
STARTTIME DATE,
STOPTIME DATE,
CELLID NVARCHAR2(10),
CONNECTSECONDS NUMBER,
SWITCHCALLCHARGE NUMBER
INSERT INTO TRANS VALUES ('REC','555988801','222242850',to_date('05/15/2012 09:15:00','mm/dd/yyyy hh24:mi:ss'),to_date('05/15/2012 09:15:25','mm/dd/yyyy hh24:mi:ss'),null,25,0)
INSERT INTO TRANS VALUES ('REC','555988801','222242850',to_date('05/15/2012 09:15:00','mm/dd/yyyy hh24:mi:ss'),to_date('05/15/2012 09:15:25','mm/dd/yyyy hh24:mi:ss'),null,25,18000)
INSERT INTO TRANS VALUES ('REC','555988801','222242850',to_date('05/15/2012 09:18:03','mm/dd/yyyy hh24:mi:ss'),to_date('05/15/2012 09:18:20','mm/dd/yyyy hh24:mi:ss'),null,17,0)
The output i want to have is:
CALLTRANSTYPE ORIGANI TERMANI STARTTIME STOPTIME CELLID CONNECTSECONDS SWITCHCALLCHARGE
REC 555988801 222242850 05/15/2012 09:15:00 05/15/2012 09:15:25 25 18000
REC 555988801 222242850 05/15/2012 09:18:03 05/15/2012 09:18:20 17 0 Thank you.Hi ekh
this is the query i want to have, thank you for the help:
SQL> Select *from
select CALLTRANSTYPE,ORIGANI,TERMANI,STARTTIME,STOPTIME,CELLID,CONNECTSECONDS,SWITCHCALLCHARGE
,row_number() over( partition by STARTTIME ,STOPTIME order by SWITCHCALLCHARGE DESC ) rn from TRANS
where rn=1;
CALLTR ORIGANI TERMANI STARTTIME STOPTIME CELLID CONNECTSECONDS SWITCHCALLCHARGE RN
REC 555988801 222242850 15-MAY-12 15-MAY-12 25 18000 1
REC 555988801 222242850 15-MAY-12 15-MAY-12 17 0 1Regrads
Lucienot. -
SQL query to retrieve a Rich Text Memo field
What will be the select command to retrieve a field [ToxPatMemo] which is in RTF format? I just want to retrieve the Text contents without all the characters for underline, bold, etc.
Thanks in advance,
Raul
Raul RegoHi jjkgr,
We can create a function to convert RTF in a text column to plain text in SQL Server.
As I have tested in my local environment, the following query is for your reference:
CREATE FUNCTION dbo.fnParseRTF
@rtf VARCHAR(8000)
RETURNS VARCHAR(8000)
AS
BEGIN
DECLARE @Stage TABLE
Chr CHAR(1),
Pos INT
INSERT @Stage
Chr,
Pos
SELECT SUBSTRING(@rtf, Number, 1),
Number
FROM master..spt_values
WHERE Type = 'p'
AND SUBSTRING(@rtf, Number, 1) IN ('{', '}')
DECLARE @Pos1 INT,
@Pos2 INT
SELECT @Pos1 = MIN(Pos),
@Pos2 = MAX(Pos)
FROM @Stage
DELETE
FROM @Stage
WHERE Pos IN (@Pos1, @Pos2)
WHILE 1 = 1
BEGIN
SELECT TOP 1 @Pos1 = s1.Pos,
@Pos2 = s2.Pos
FROM @Stage AS s1
INNER JOIN @Stage AS s2 ON s2.Pos > s1.Pos
WHERE s1.Chr = '{'
AND s2.Chr = '}'
ORDER BY s2.Pos - s1.Pos
IF @@ROWCOUNT = 0
BREAK
DELETE
FROM @Stage
WHERE Pos IN (@Pos1, @Pos2)
UPDATE @Stage
SET Pos = Pos - @Pos2 + @Pos1 - 1
WHERE Pos > @Pos2
SET @rtf = STUFF(@rtf, @Pos1, @Pos2 - @Pos1 + 1, '')
END
SET @Pos1 = PATINDEX('%\cf[0123456789][0123456789 ]%', @rtf)
WHILE @Pos1 > 0
SELECT @Pos2 = CHARINDEX(' ', @rtf, @Pos1 + 1),
@rtf = STUFF(@rtf, @Pos1, @Pos2 - @Pos1 + 1, ''),
@Pos1 = PATINDEX('%\cf[0123456789][0123456789 ]%', @rtf)
SELECT @rtf = REPLACE(@rtf, '\pard', ''),
@rtf = REPLACE(@rtf, '\par', ''),
@rtf = LEFT(@rtf, LEN(@rtf) - 1)
SELECT @rtf = REPLACE(@rtf, '\b0 ', ''),
@rtf = REPLACE(@rtf, '\b ', '')
SELECT @rtf = STUFF(@rtf, 1, CHARINDEX(' ', @rtf), '')
RETURN @rtf
END
DECLARE @Sample TABLE (ID INT, data VARCHAR(8000))
INSERT @Sample
SELECT 1, '{\rtf1\ansi\ansicpg1252\deff0\deflang1053{\fonttbl{\f0\fswiss\fcharset0 Arial;}}
{\colortbl ;\red255\green0\blue0;\red0\green128\blue0;\red0\green0\blue255;}
{\*\generator Msftedit 5.41.15.1507;}\viewkind4\uc1\pard\f0\fs20 P\cf1 e\cf0 ter\par
\cf2 L\cf0 ar\b s\b0 s\cf3 o\cf0 n\par
}' UNION ALL
SELECT 2, '{\rtf1\ansi\deff0{\fonttbl{\f0\fnil\fcharset0 Tahoma;}}
{\colortbl ;\red0\green0\blue0;}
{\*\generator Riched20 5.50.99.2014;}\viewkind4\uc1\pard\cf1\f0\fs18\lang1033 ~200 LF streambank stabilization to provide structural protection\par}'
SELECT ID,
dbo.fnParseRTF(data)
FROM @Sample
The result of this query is:
1 Peter Larsson
2 ~200 LF streambank stabilization to provide structural protection
If you have any question, please feel free to let me know.
Regards,
Jerry Li -
SQL query not retrieving special characters in like O', M÷ and û
Hi,
I have a sql script to query database. The output is a string. When I execute this script through a Korn shell script to send the query result to a .csv file, it is showing some characters of the output as below.
The characters O’ is displayed as ?
The characters M÷ is displayed as ?
The characters û is displayed as u
I am able to retrieve the data as it is while running the script through SQL * Plus and spooling the output to a .csv file.
If anybody is having solutions for this please reply.
Thanks in advance for your kind help
Regards,
Sreesha
Edited by: user530278 on Oct 18, 2008 2:56 PMuser530278 wrote:
Hi,
I have a sql script to query database. The output is a string. When I execute this script through a Korn shell script to send the query result to a .csv file, it is showing some charcters of the output as below.
The characters O’ is displayed as ?
The characters M÷ is displayed as ?
The characters û is displayed as uThis sounds like an NLS client setting issue.
Note that there are scenarios where the data is "incorrectly" stored in the database but can be retrieved "successfully" from some clients. This is the case if you specify the same character set at your client as you have as database character set and this client character set setting is wrong, which means you ought to have specified a different client character set than you have in the database.
Specifying the same character set on client and server prevents any conversion from taking place, so as long as the database can store the data (e.g. multi-byte characters won't be stored correctly in single-byte character set database no matter what you do) it will be stored "as is". If you now retrieve the data using the same setting, it looks correct, but if you use a client with a different character set setting that requires conversion the data will be mangled.
The database character set can be identified using the dictionary view NLS_DATABASE_PARAMETERS, parameter "NLS_CHARACTERSET".
The client character set can be identified by issuing the following at the SQL*Plus prompt:
SQL> @[%NLS_LANG%]You'll get a message similar to:
"Unable to open file [AMERICAN_AMERICA.WE8MSWIN1252]"
The string after the dot represents the client character set.
If you get this:
Unable to open file "[%NLS_LANG%]"
then the NLS_LANG client setting is undefined.
Please follow this very useful link regarding client NLS issues and how to resolve them, in particular to determine what is your correct client NLS_LANG setting:
http://www.oracle.com/technology/tech/globalization/htdocs/nls_lang%20faq.htm
I am able to retreive the data as it is while running the script through SQL * Plus and spooling the output to a .csv file.You mean that when using SQL*Plus interactively the data is shown/spooled correctly, whereas if you run a script non-interactively then the data is mangled?
Note that if you e.g. use "cron" to schedule/execute your script that you need to take care of the Oracle specific environment variables yourself because no logon scripts are executed when cron forks the process to run. May be you need to define the NLS_LANG variable explicitly or call/source the corresponding script to define your default Oracle environment that includes the NLS_LANG setting.
It probably depends on your environment, operating system, etc. and how you set the Oracle specific settings, e.g. environment variables under Unix, registry settings under Windows etc.
Use the above mentioned check regarding NLS_LANG setting in the different cases that you encounter to get started.
Regards,
Randolf
Oracle related stuff blog:
http://oracle-randolf.blogspot.com/
SQLTools++ for Oracle (Open source Oracle GUI for Windows):
http://www.sqltools-plusplus.org:7676/
http://sourceforge.net/projects/sqlt-pp/ -
Sql query parsing (retrieve tables and columns used in query)
Hi,
1. Is there any view in Oracle which can tell me, which tables and columns were used in the last ran query, no matter how complex the query is.
2. Secondly I can get table names, used in stored procedures and views, using USER_DEPENDENCIES VIEW, however this System View does not return column names, used in that procedure / view, any idea please .
Thanks and Regards,
Luqmanluqman123 wrote:
Hi,
1. Is there any view in Oracle which can tell me, which tables and columns were used in the last ran query, no matter how complex the query is.I don't believe so. You can look at V$SQL and retrieve the actual SQL text from there.
2. Secondly I can get table names, used in stored procedures and views, using USER_DEPENDENCIES VIEW, however this System View does not return column names, used in that procedure / view, any idea please .Oracle added fine grained dependency tracking in 11g, but didn't expose any views to query this information. However, it has been reverse engineered a bit: About Oracle: DBA_DEPENDENCY_COLUMNS -
How to write a sql query to retrieve data entered in the past 2 weeks
Hi,
I have file names and last accessed date(java.sql.Date format) stored in my database table, I would like to know how I can write a query to get the name of files accessed in the past 2 weeks,I use open sql server at the back end.
Thanks in advance.This has essentially nothing to do with JDBC. JDBC is just an API to execute the SQL language using Java and thus interact with the databases.
Your problem is related to the SQL language, you don't know how to write the SQL language. I suggest you to go through a SQL tutorial (there is one at w3schools.com) and to read the SQL documentation which come along with the database in question. A decent database manfacturer has a website and probably also a discussion forum / mailinglist as well.
I'll give you a hint: you can just use equality operators in SQL like everywhere. For example: "WHERE date < somedate". -
Sql query to know the number of triggers and cursors present in a schema?
friends,is there any way to know the number of triggers and cursors preseent in a schema.
thanks in advance..what do you mean by triggers in a schema? is it the triggers owned by that schema? or the triggers applied on tables owned by that schema? remember you may own triggers applied on the tables belong to other schemas. Anyways, all this information can be extracted from oracle dictionary using a combination of all_triggers and all_tables.
As far as cursors, please elaborate. Do you mean cursors declared in PL/SQL blocks? -
SQL Query to retrieve the records
Hi All,
I have one table. It contains millions of records.I gave the query as follows.
select * from emp where empnob in (1,1000)
it displays empnob
1 A 300000 Manager
6 B 120000 Analyst
87 C 32980 salsman
Now I want to retrieve remaining records. Pelase let me know the query which are not available in emp table between the given limit.
Thank you.Hi ,
For example there are 10 records only.
I gave
select * from emp where empno in (1,10)
t displays the records having the empnos 1,3,4,6,8
Now I want to display the records 2,5,7,9,10 also...
Please let me know the query.Are use using an oracle database? My Oracle database would never return such a result for your query.
Please post some output from an sqlplus session where you show us exactly what you do.
Message was edited by:
Sven W.
Maybe you are looking for
-
Word template with web service - style and font not considered
Hi experts. I'm using a word template with a web services. So i've added my xml tags in the template and i've tried to change the font and style but when i launch the document, what i have defined is never considered. For example if i set in a table
-
Hi , I am facing this particular error while creating billing document in VF01 and VF02. "UPDATE WAS TERMINATED". The program and script are executing fine in Dev System but it is giving this error in Test System. The Billing type created was F2
-
SSL Setup in a load balanced portal
Hi, We are implementing a portal landscape and also we are using a hardware based (Cisco ACE) load balancer for load balancing purposes. So the configuration would be: Portal requests --> Load Balancer --> Portal --> Backend We are trying to implemen
-
SAPScript :Header or Window ?
Dear all, I would like to create a header with three types of paragraph formats standing at the same level. Shall I create three different windows and line them up with graphic editors or is it possible to do it with the header? Ex: name1(format1)
-
Hi, I am doing and automated TOC, and it works well. For it to number stuff correctly, I divided my document in sections, with new section in every chapter, even if the layout is the same (for titles). My goal was to be able to do a TOC with chapters