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_NAME

This 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

Similar Messages

  • SQL query to fetch records  from  26 onwards

    Want to know SQL query to fetch records from 26 onwards.

    See the fifth entry on the FAQ page:
    Re: How to attach a java bean in forms6i

  • How will write SQL query to fetch data from  each Sub-partition..

    Hi All,
    Anyone does have any idea about How to write SQL query to fetch data from Sub-partition.
    Actually i have one table having composite paritition(Range+list)
    Now if i want to fetch data from main partition(Range) the query will be
    SELECT * FROM emp PARTITION(q1_2005);
    Now i want to fetch data at sub-partition level(List) .But i am not able to get any SQL query for that.
    Pls help me to sort out.
    Thanks in Advance.
    Anwar

    SELECT * FROM emp SUBPARTITION(sp1);

  • Sql query to get numbers from 0 to 99

    How can we write an sql query to get values from 0 to 99 ... this shouldn't be coming from any table

    Bawer wrote:
    Result: (quickly replied by oracle)
    SQL-Error: ORA-30009 (Not enough memory for connect by - 1GB reserved for oracle on VirtualBox)Oracle version? Takes about 8 seconds on my:
    SQL> select * from v$version
      2  /
    BANNER
    Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - Prod
    PL/SQL Release 10.2.0.4.0 - Production
    CORE    10.2.0.4.0      Production
    TNS for 32-bit Windows: Version 10.2.0.4.0 - Production
    NLSRTL Version 10.2.0.4.0 - Production
    Elapsed: 00:00:00.14
    SQL> select count(*) from (
      2  SELECT LEVEL - 1
      3  FROM DUAL
      4  CONNECT BY LEVEL <= 10000000
      5  )
      6  /
      COUNT(*)
      10000000
    Elapsed: 00:00:07.76
    SQL> SY.

  • 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 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 PM

    user530278 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 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.
    TJ

    I 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

  • Extract Sql Query with Actual Parameters from Report

    Hi
    I am able to extract query from Crystal Report using the following code :
    ReportDocument.ReportClientDocument.RowSetController.GetSqlStatement(new GroupPath, out tmp);
    But the sql query retrieve comes in the following format :
    select name , trans_code, account_code from command_query.accounts
    where account_code = {?Command_query_Prompt0}  and effective_date < '{?Command_query_prompt1 }'
    The parameters which I m using in the reports are :
    Account_Code and Effective_Date .
    Why does my extracted sql translates it into {?Command_query_Prompt0} and '{?Command_query_prompt1 }' .
    Is there any way to map this to the actual parameter values ?
    OR
    Can we extract the query after assigning the values ?
    Any help is appreciated ... 
    Thanks
    Sanchet

    hi,
    You can create nested sql query with conditional parameters,
    For example
    Select Code From OITT Where Code IN (Select ItemCode From OITM
    Where ItemName LIKE '[%0]' + '%%')
    Edited by: Jeyakanthan A on Jun 9, 2009 12:31 PM

  • 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

  • Passing entire SQL query to BIP server from Forms

    Hi
    I want to pass the entire SQL statement or only a WHERE clause dynamically constructed on Forms side to BI Publisher server at runtime.
    I have come across this article:
    http://blogs.oracle.com/BIDeveloper/2009/12/dynamic_sql_query_in_data_template.html
    It mentions about using pwhereclause which could be constructed dynamically.
    Is it possible to pass the entire SELECT statement and has anyone done this before? May I have some tips on this?
    Thanks and kind regards,
    Aparna

    My requirement is as follows:
    1) I have a customised application which contains the definition of the report (report id, report name, the screen id on which the report is to appear as a record in the list and so on). This is where I would like to record the SQL statement which will be used to retrieve data for the report.
    2) Once I launch the report from my application, it will pass SQL statement and other data (template, format, output file name, path where the output file will be saved, URL from which the output file will be launched after saving).
    Thus, on BI Publisher server side, I shall only create a report, and will create a data template that will reference the SQL query that has been passed from application. I don't want to even specify parameters in Bi Publisher and want to specify them only in the application.
    Is this possible?
    Currently, I'm using a web service called 'PublicReportService' of BI Publisher to pass template, format, output file name, report path etc.). However, I am not sure how to pass an SQL query from Oracle Forms via this web service to BI Publisher.
    Can someone please guide me?
    Thanks and kind regards,
    Aparna

  • Help Required -- Can we use SQL Query to READ data from SAP MDM Tables

    Hi All,
    Please help.........
    Can we use SQL Query to READ(No Creation/Updation/Deletion  just Read) Data from SAP MDM tables directly, without using MDM Syndicator.
    Or direct SQL access to SAP MDM tables is not possible. Only through MDM Syndicator can we export data.
    Thanks in Advance
    Regards

    All the tables you create in Repository comes under A2i_CM_Tables in Database named as your repository name. So the tables names are fields of table A2i_CM_Tables. Now i tried it but cant make it.
    Now, I dont think its possible to extract all fields in tables and there values using select query. May be pure sql guy can do that or not.
    But there is no relation of data extraction and syndicator. Data is viewed in Data Manager. and you can also store data in a file from DM also.
    BR,
    Alok

  • 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.K

    give 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
    Kahlua

    Check 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

  • Executing sql query(not through sqlfile) from batchfile

    Hi,
    I have written a batchfile below which should execute the sql query. Can someone please help me in modifying the below code. I know we can make use of sql file ,but wanted it to be more robust and hence everything in a single batch file instead of having combination of sqlfile and batchfile.
    @echo off
    set ORACLE_HOME=%RAMBOH%
    set ORACLE_SID=%RAMBSID%
    set path=%RAMBOH%\bin;%RAMBOH%\admin\Ramb_Scripts;%path%
    sqlplus sys/XXX as sysdba
    @echo SET SERVEROUTPUT ON;
    @echo DECLARE
    @echo     v1   DATE;
    @echo BEGIN
    @echo     SELECT SYSDATE INTO V1 FROM DUAL;
    @echo     DBMS_OUTPUT.PUT_LINE(v1);
    @echo END;
    @echo /

    I used the below syntax to execute a procedure
    set ORACLE_SID=orcl
    echo exec MyProcedure;
    echo exit
    )|sqlplus system/manager >MyProcedure.log
    Try making modifications to this and see if this helps you in executing the SQL from the batch file

  • 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

  • Advance payment to be reduced

    Hello Frds, we paid to vendor 10000 advance but invoice received only 5000 remaining 5000 vendor has given back.Now i want to reduce the advance payment from vendor.Can any body explain how can we post this. I appreciate your valuable inputs and adva

  • Needs rows to be listed in BEX in a particular order(not asc desc)

    See the thread. Needs rows to be listed in a particular order(not ascending or descending)

  • Solaris 9, V210, bge0 network performance issue

    Setup: Solaris 9, V210, single network (bge0). ndd shows: 100FD LAN Switch set to 100FD Network performance bad, in fact very bad. System shows upwards of 19% collisions with netstat -- odd since FD should have no collisions. Network folks have check

  • Premiere Pro CC audio corruption issues

    Hello Adobe community, I've been editing happily on Adobe for several years now, and I'm not sure if it's CC, Mavericks updating and/or getting a brand new top-of-the-line 16GB/ram MacBook Pro to edit on, but every time I close and re-open premiere p

  • Task execution Log

    Hi Experts, In consolidation monitor for a data collection task we have activated "last log" funtionality at Task level. Please help us to find out the table where the last log stored. Not only the status of the log but the messages that comes in the