Help build the query
In order to explain my problem, I'll give my data first and then explain what I am trying to accomplish.Oracle: 10g
Table = tb.xInfo
NAME STYLE YEAR AMOUNT SEC_ID
BOB BLUE 1995 6 1
BOB BLUE 1996 8 1
BOB BLUE 1997 7 1
CHRIS RED 1995 4 1
CHRIS PURPLE 1996 8 1
CHRIS RED 1997 5 1
DAN YELLOW 1995 9 1
DAN YELLOW 1996 4 1
DAN YELLOW 1997 1 1
"So what I try to do based on the above data is: summarize all specific amounts, exit it years in years like 1995-1997 column" and all styles of there in the column style, with Chris, I want out of his STYLE values separated by a comma.Desired result set
NAME STYLE YEAR AMOUNT
BOB BLUE 1995-1997 21
CHRIS RED, PURPLE 1995-1997 17
DAN YELLOW 1995-1997 14
This is the code I tried but did not have much success:select name,
(CASE
WHEN count(style) = 2 THEN
STYLE || ', ' || STYLE
ELSE
STYLE
END) as style,
'1995-1997' AS YEAR
SUM(AMOUNT) as amount
from
(
select name,
style,
year,
amount
from
tb.xInfo
where year in (1995,1996,1997)
and sec_id = 1
and name
)
Group by name, style, amount
The above doesn't seem to work.
Hello
Here is an example of SYS_CONNECT_BY_PATH, using the example of data published by Mwooldridge:
WITH got_style_num AS
(
SELECT DISTINCT
name
, style
, SUM (amount) OVER ( PARTITION BY name) AS total_amount
, DENSE_RANK () OVER ( PARTITION BY name
ORDER BY style
) AS style_num
FROM msw_test
WHERE year IN (1995, 1996, 1997)
AND sec_id = 1
)
SELECT name
, LTRIM ( SYS_CONNECT_BY_PATH (style, ', ')
, ', '
) AS styles
, '1995-1997' AS years
, total_amount
FROM got_style_num
WHERE CONNECT_BY_ISLEAF = 1
START WITH style_num = 1
CONNECT BY style_num = PRIOR style_num + 1
AND name = PRIOR name
;
Create dynamically the years column, as did Mwooldrige, is a good idea. To modify the above query, use the MIN and MAX functions analytical in the subquery got_style_num.
Alternatively, you can use the SYS_CONNECT_BY_PATH to generate a string like ' 1995, 1996, 1997. This could be very useful if some names, it was missing a few years; then you might get results like ' 1995, 1997' or '1995' dynamically.
The above query lists the styles alphabetically. In the sample data you posted, you RED prior to VIOLET. If it's important, explains what order you want.
Tags: Database
Similar Questions
-
Build the query to select date with status
Hi guys,.
Grateful if you can advise me on how to build the query on below scenario:
Table A
Date
11.44.39.000000000 12-OCT-14 AM ASIA/SINGAPORE
11.44.35.000000000 16-SEP-14 AM ASIA/SINGAPORE
11.44.42.000000000 22-SEP-14 AM ASIA/SINGAPORE
The result of the query:
Date Status
11.44.39.000000000 12-OCT-14 AM ASIA / SINGAPORE inactive
11.44.35.000000000 16-SEP-14 active AM ASIA/SINGAPORE
11.44.42.000000000 22-SEP-14 AM ASIA/SINGAPORE inactive
Basically, the logic, I wanted is
Active principles: today date or max (date) < sysdate
Otherwise will be inactive
And there is 1 active date in time.
Thanks in advance
Hello
2753165 wrote:
Hi guys,.
Grateful if you can advise me on how to build the query on below scenario:
Table A
Date
11.44.39.000000000 12-OCT-14 AM ASIA/SINGAPORE
11.44.35.000000000 16-SEP-14 AM ASIA/SINGAPORE
11.44.42.000000000 22-SEP-14 AM ASIA/SINGAPORE
The result of the query:
Date Status
11.44.39.000000000 12-OCT-14 AM ASIA / SINGAPORE inactive
11.44.35.000000000 16-SEP-14 active AM ASIA/SINGAPORE
11.44.42.000000000 22-SEP-14 AM ASIA/SINGAPORE inactive
Basically, the logic, I wanted is
Active principles: today date or max (date)<>
Otherwise will be inactive
And there is 1 active date in time.
Thanks in advance
Sorry, we don't know what you want.
Are you saying that 1 row (maximum) can be assigned status = 'Active' and if 2 or more lines are eligible, the last of them will be called 'Active'?
If so:
CASE
WHEN tmstmp<=>=>
OR ROW_NUMBER () OVER (PARTITION BY CASE
WHEN tmstmp<=>=>
THEN "could be."
ANOTHER 'No Way'
END
ORDER BY tmstmp DESC
) = 1
THEN "active."
ELSE 'inactive '.
END
If you would care to post CREATE TABLE and instructions INSERT for some samples, so I could test it.
DATE is not a very good name for a column, especially if the column is a TIMESTAMP, not a DATE. I called the TMSTMP instead of the DATE column.
-
Help with the query to select only one record from the result set in double
Hello
Please help with the query. Version of Oracle database we use is 10g R2.
I have a vision that is duplicated IDS, but they are used across the different functions. See below examples of data. Please help me with a query to select only one record (based on ID regardless of the area) from the bottom of the result set of duplicate records. For what is the point of view is there unique records, given the combination of the fields ID, Org, DF, dry, Sub-Sec
ID Org DF Sec Sub-Sec (163) CQ CPDM CPDM HD (163) PC ENG ENG ENG (163) CQ ASIC ASIC IS 8888 TS TAC TAC TAC (163) TS HE HE SW 6789 CQ INFO INFO FOS 6789 PC SECSYS SECSYS INFO1 6789 TS SECSYS SECSYS INFO2 9009 PC BMS BMS BMS1 My result set must eliminate the duplicate identifiers regardless of whoever we choose of the result set. (I mean without distinction Org, DF, s, Sub-s). My expected result set should be.
ID DSB DF SEC Sub-Sec (163) CQ CPDM CPDM HD 8888 TS TAC TAC TAC 6789 CQ INFO INFO FOS 9009 PC BMS BMS BMS1 Thank you
Orton
Hello
This sounds like a job for ROW_NUMBER:
WITH got_r_num AS
(
SELECT id, DSB, df, s, sub_sec org
ROW_NUMBER () OVER (PARTITION BY ID.
ORDER BY org
) AS r_num
OF view_x
)
SELECT id, DSB, df, sub_sec s,
OF got_r_num
WHERE r_num = 1
;
He is a Top - N query example, where you choose the elements of N (N = 1 in this case) from the top of an ordered list.
I hope that answers your question.
If not, post a small example of data (CREATE TABLE and INSERT, only relevant columns instructions) to your sample data and the results desired from these data. (I know that you said that you were a view selection. Just for this thread, pretending it is a picture and post simple CREATE TABLE and INSERT statements to simulate your point of view).
Point where the above query is to produce erroneous results, and explain, using specific examples, how you get the right results from data provided in these places. (I didn't quite understand the explanation above. I don't know why you want toID ORG DF DRY SUB_SEC
-- --- -- --- -------
1234 CQ DPRK DPRK HD
and is not
1234 IS CQ ASIC, ASIC
or
TS 1234 IT IT SW
or
1234 CQ ASIC ASIC HD
)
If you change the query at all, post your modified version.
Always say what version of Oracle you are using (for example, 11.2.0.2.0).See the FAQ forum: https://forums.oracle.com/message/9362002
-
procedure that will dynamically build the query data and table Medallion
Hi people,
I write a procedure that dynamically build the query data and insert in the table "dq_summary".
enforcement procedure with success and data not inserted into the table 'dq_summary '.
I have thin problem in code attached below
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------FOR rep IN cur_di_attr LOOP dbms_output.put_line ('d: '); BEGIN EXECUTE IMMEDIATE 'SELECT table_name FROM ' || sum_tab || ' WHERE id = ' || rep.attribute_id INTO rep_tab; dbms_output.put_line ('rep_tab: '||rep_tab); run_query := run_query || ' ' || rep_tab || ' WHERE ' || nvl(wh_cond, '1 = 1'); EXECUTE IMMEDIATE run_query INTO end_rslt; EXECUTE IMMEDIATE 'UPDATE dq_summary SET ' || prop || '_' || p_code || ' = ' || end_rslt || ' WHERE attribute_id = ' || rep.attribute_id; dbms_output.put_line ('e: '); dbms_output.put_line ('rep_tab: '||rep_tab); dbms_output.put_line ('end_rslt: '||end_rslt); dbms_output.put_line ('f: '); EXCEPTION WHEN no_data_found THEN rep_tab := ''; sum_tab := ''; END; END LOOP;
but in the procedure below must be run several times
Published by: BluShadow on February 7, 2012 12:04create or replace PROCEDURE DQ_REPORT_PROC AS prop di_proposition.pro_name%type; col_var VARCHAR2(100); p_code dq_parameter.para_code%type; sum_tab di_proposition.summary_table%type; run_query dq_parameter.run_query%type; wh_cond dq_parameter.where_cond%type; end_rslt VARCHAR2(20); rep_tab VARCHAR2(50); v_error_msg VARCHAR2(200); v_error_code VARCHAR2(200); v_object_name VARCHAR2(50) DEFAULT 'DQ_REPORT_PROC'; v_iss_no VARCHAR2(20) DEFAULT NULL; CURSOR cur_di_prop IS SELECT upper(replace(replace(pro_name, ' '),'-')) pro_name FROM di_proposition; CURSOR cur_di_para IS SELECT upper(para_code) para_code, run_query, where_cond FROM dq_parameter; CURSOR cur_di_attr IS SELECT attribute_id FROM dq_summary; BEGIN DELETE FROM dq_summary; INSERT INTO dq_summary (attribute_id, entity_name, attribute_name, data_champ) SELECT a.attribute_id, b.entity_name, a.attribute_name, a.data_champ FROM di_attribute_master a, di_entity_master b WHERE a.entity_id = b.entity_id; FOR c_prop IN cur_di_prop LOOP prop := c_prop.pro_name; BEGIN SELECT distinct SUBSTR(column_name, 1, INSTR(column_name, '_')-1), summary_table INTO col_var, sum_tab FROM user_tab_cols a, di_proposition b WHERE a.table_name = 'DQ_SUMMARY' AND upper(replace(replace(b.pro_name, ' '),'-')) = prop AND SUBSTR(a.column_name, 1, INSTR(a.column_name, '_')-1) = upper(replace(replace(b.pro_name, ' '),'-')) AND upper(b.status) = 'Y'; dbms_output.put_line ('col_var: '||col_var); dbms_output.put_line ('sum_tab: '||sum_tab); EXCEPTION WHEN no_data_found THEN col_var := ''; sum_tab := ''; END; dbms_output.put_line ('a: '); FOR para IN cur_di_para LOOP dbms_output.put_line ('b: '); p_code := para.para_code; run_query := para.run_query; wh_cond := para.where_cond; dbms_output.put_line ('c: '); FOR rep IN cur_di_attr LOOP dbms_output.put_line ('d: '); BEGIN EXECUTE IMMEDIATE 'SELECT table_name FROM ' || sum_tab || ' WHERE id = ' || rep.attribute_id INTO rep_tab; dbms_output.put_line ('rep_tab: '||rep_tab); run_query := run_query || ' ' || rep_tab || ' WHERE ' || nvl(wh_cond, '1 = 1'); EXECUTE IMMEDIATE run_query INTO end_rslt; EXECUTE IMMEDIATE 'UPDATE dq_summary SET ' || prop || '_' || p_code || ' = ' || end_rslt || ' WHERE attribute_id = ' || rep.attribute_id; dbms_output.put_line ('e: '); dbms_output.put_line ('rep_tab: '||rep_tab); dbms_output.put_line ('end_rslt: '||end_rslt); dbms_output.put_line ('f: '); EXCEPTION WHEN no_data_found THEN rep_tab := ''; sum_tab := ''; END; END LOOP; END LOOP; END LOOP; COMMIT; EXCEPTION WHEN OTHERS THEN v_error_msg := SQLERRM; v_error_code := SQLCODE; TRACKER_LOG_EXECEPTION(v_iss_no, v_object_name, CURRENT_TIMESTAMP, v_error_msg, v_error_code); COMMIT; END DQ_REPORT_PROC;
addition of {noformat}{noformat} tags. Please read {message:id=9360002} and learn to do this yourself in future.
903830 wrote:
I write a procedure that dynamically build the query data and insert in the table "dq_summary".
enforcement procedure with success and data not inserted into the table 'dq_summary '.I'm sorry. But there is no kind of say that way. The code is undesirable. The approach is wrong. This will not happen. This will cause the fragmentation of memory in the shared Pool. This will lead to another session being impossible to analyze the sliders because of the fragmented memory.
Not only that. The underlying data model is questionable.
All this seems a candidate perfect as an example of how NOT to design and code and use Oracle.
-
need help with the query, thx
Hello
I need a little help with the query I have to write;
the table has 4 columns:
what I am asking is the following result: salary according to the latest available yearcol1 col2 col3 col4 emp_name empl_id salary year content of data: col1 col2 col3 col4 smith 12 1200 1999 smith 12 1340 2000 smith 12 1500 2001 jones 13 1550 1999 jones 13 1600 2000 aron 14 1200 2002
i.e.
ID appreciate some guidance on how to achievesmith 12 1500 2001 jones 13 1600 2000 aron 14 1200 2002
Thank you
Rgdsselect *from (select col1, col2,col3, col4,row_number() over(partition by col1 order by col4 desc) rn from
) where rn=1 -
small request I have given as below,
Month ProductNo CustomerNo units
9001 1001 Jan - 09 100
9002 1002 Jan - 09 200
9003 1003 Jan - 09 300
Jan 9001 400 ABCCustomer
9002 1004 Jan - 09 500
for all record - if column - customerNo starts with digital it must show as CN_ * other wise of the same name.
Result must be
9001 100 CN_1001-09 Jan
9002 200 CN_1002-09 Jan
9003 300 CN_1003-09 Jan
Jan 9001 400 ABCCustomer
9002 500 CN_1004-09 Jan
Can help get the queryA logic that is easier to implement this is as below
SQL> with t as (select 'jan' Month, 9001 ProductNo,'1001-09' CustomerNo,100 Units from dual union all 2 select 'jan', 9002, '1002-09', 200 from dual union all 3 select 'jan', 9003, '1003-09', 300 from dual union all 4 select 'jan', 9001, 'ABCCustomer', 400 from dual union all 5 select 'Jan', 9002, '1004-09', 500 from dual) 6 select month,productno,case when upper(substr(customerno,1,1))=lower(substr(customerno,1,1)) 7 then 'CN_'||customerno else customerno end customerno,units from t; MON PRODUCTNO CUSTOMERNO UNITS --- ---------- -------------- ---------- jan 9001 CN_1001-09 100 jan 9002 CN_1002-09 200 jan 9003 CN_1003-09 300 jan 9001 ABCCustomer 400 Jan 9002 CN_1004-09 500
TRY WITH THIS upper (substr(customerno,1,1)) = lower (substr(customerno,1,1))
-
Hello
I got the info from a table named X.
Col1 Col2 Col3 Col4
IT01 20 01 X
IT01 20 50 X
IT01 30 01 X
IT01 30 50 X
If the value of "X" all 4 combinations of Col2 and Col3, Col4 the result should be displayed as
Col1, Col4
IT01 X
If the actual values are as
Col1 Col2 Col3 Col4
IT01 20 01 X
IT01 20 50 X
IT01 30 01 X
30 50 IT01 Y
The result should be displayed as
Col1, Col4
IT01 Y
How will generate the query for this?
Thank you
Thomas Titus
Published by: Titus Thomas on April 20, 2010 14:48Try this:
with test_tab as (select 'ITO1' Col1, 20 Col2, 01 Col3, 'X' Col4 from dual union all select 'ITO1' Col1, 20 Col2, 50 Col3, 'X' Col4 from dual union all select 'ITO1' Col1, 30 Col2, 01 Col3, 'X' Col4 from dual union all select 'ITO1' Col1, 30 Col2, 50 Col3, 'X' Col4 from dual union all select 'ITO2' Col1, 20 Col2, 01 Col3, 'X' Col4 from dual union all select 'ITO2' Col1, 20 Col2, 50 Col3, 'X' Col4 from dual union all select 'ITO2' Col1, 30 Col2, 01 Col3, 'X' Col4 from dual union all select 'ITO2' Col1, 30 Col2, 50 Col3, 'Y' Col4 from dual) --- end of mimicking your data; use SQL below: select col1, max(col4) col4 from test_tab group by col1; COL1 COL4 ---- ---- ITO2 Y ITO1 X
-
There are two tables. One is composed of members with fresh and then fixed percentile. I need to build a single query based on the following
1. need to know the number of members who report to a beach fixed percent 10%, 40%, 50%. Suppose, total non-member = 50 then
10%-> 5
40%--> 20
50%--> 25
2. now, I need to order the members in accordance with their costs. Thus, if he follows the expenses of members sorted by cost
A1 100
A2 99
A3 98
A4 97
A5 96
A6 95
.....
.....
A50 50
then
the total expenses should be discovered as
10% [sum of the first 5: A1 to A5] = 100 + 99 + 98 + 97 + 96 = 490
40% [sum of the next 20: A6 to A26]
50% [sum of the next 25: of the A27 to A50]
Help, please!
Published by: Fanny on January 11, 2010 07:50Fanny says:
I can try, follow these steps. But I need to improvise the query.
No problem:
with t as ( select 'A' || level member,101 - level expenses from dual connect by level < 51 ) select b.label, sum(a.expenses) total_expenses from ( select row_number() over(order by expenses desc) rn, count(*) over() cnt, expenses from t ) a, ( select seq_id, label, cumulative_percent, lag(cumulative_percent,1,0) over(order by seq_id) prev_cumulative_percent from groups ) b where a.rn > a.cnt * b.prev_cumulative_percent / 100 -- you can wrap it with round and a.rn <= a.cnt * b.cumulative_percent / 100 -- depending on your requirements group by b.label, b.seq_id order by b.seq_id / LABEL TOTAL_EXPENSES -------------------- -------------- 10% 490 40% 1710 50% 1575 SQL>
Table A calculates the number of members and each sequential member the number of current Member in descending order of their spending. Table B calculates, for each cumulative percentage of the previous sequence id id. So now, table B, each line defines a bucket. Now, all we need is to join A and B send each Member to bucket appropriate.
SY.
-
Hi, can someone help me with the query. I try to display comm based on calculation below
SAL < 5000 then 10%
5000 - 10000 then 15%
> 10000 then 20%
This is the query
DECLARE
v_comm number (7.2);
v_Name emp.ename%type;
v_empno emp.empno%type;
v_sal emp.sal%type;
BEGIN
SELECT ename, empno, sal IN
v_Name, v_empno, v_sal
EMP;
v_comm: = (IF v_sal < v_sal*.10 then 5000;)
ELSIF v_sal between 5001 AND 10000 THEN v_sal*.15;
Of ANOTHER v_sal > 10001 THEN v_sal*.20;
END IF ;);
dbms_output.put_line(v_Name||) e with empno' | v_empno | "win a comm' | v_comm);
END; /
also tried
DECLARE
v_comm number (7.2);
v_Name emp.ename%type;
v_empno emp.empno%type;
v_sal emp.sal%type;
BEGIN
SELECT ename, empno, sal IN
v_Name, v_empno, v_sal
EMP;
IF v_sal < v_comm then 5000: = v_sal*.10;
ELSIF v_sal between 5001 AND 10000 THEN v_comm: = v_sal*.15;
Of ANOTHER v_sal > 10001 THEN v_comm: = v_sal*.20;
END IF ;);
dbms_output.put_line(v_Name||) e with empno' | v_empno | "win a comm' | v_comm);
END;
/
Hello
Look at the syntax of the IF in the PL/SQL manual. Comapre to the instruction BOX and also the expression BOX.
Perhaps, instead of an IF statement
v_comm: = (IF v_sal<5000 then="">5000>
ELSIF v_sal between 5001 AND 10000 THEN v_sal*.15;
ELSE v_sal > 10001 THEN v_sal*.20;
END IF ;);
you wanted to write an expression BOX, like this:
v_comm: = BOX
WHEN v_sal< 5000 ="" then="" v_sal="" *="">
WHEN v_sal BETWEEN 5001 AND 10000 THEN v_sal *.15
WHEN v_sal > 10001 THEN v_sal *.20
END;
Or perhaps cela:
v_comm: = v_sal * CASE
WHEN v_sal<= 5000="" then="">= >
WHEN v_sal<= 10000="" then="">=>
OF AUTRE.20
END;
You can also use a CASE expression in the SELECT statement.
-
Need help with the query. Help, please
Hey everyone, need your help. Thank you in advance. In my view, there is function Pivot. Just do not know how to use this function. I have the query that works. The result is:
11-111-1111 Vlad 16505 01/04/2013 5 August 13 dental plan pre-tax amount 29,65
11-111-1111 Vlad 16505 01/04/2013 dental pre-tax 5 August 13 Plan level EE + SP
11-111-1111 16505 Vlad 01/04/2013 5 August 13 pre-tax Option TOP dental plan
11-111-1111 Vlad 16505 01/04/2013 5 August 13 pre-tax dental care plan pay the value
11-111-1111 16505 Vlad 01/04/2013 dental pre-tax 5 August 13 Plan period Type
11-111-1111 Vlad 16505 01/04/2013 amount pre-tax medical Plan of 5 August 13 149
11-111-1111 Vlad 16505 01/04/2013 5 August 13 pre-tax Medical Plan level EE + SP
11-111-1111 Vlad 16505 01/04/2013 5 August 13 pre-tax Plan medical Option MED
11-111-1111 Vlad 16505 01/04/2013 5 August 13 plan pre-tax pay value
11-111-1111 16505 Vlad 01/04/2013 5 August 13 pre-tax Medical Plan period Type
11-111-1111 Vlad 16505 01/04/2013 5 August 13 pre-tax Plan PPO medical Plan
11-111-1111 Vlad 16505 01/04/2013 5 August 13 Vision Plan amount 5.94 pre-tax
But I need the result to be
Amount of SSN ID name item level Option PayValue period Type
11-111-1111 Vlad 16505 01/04/2013 null null high of 5 August 13 pre-tax Dental Plan 29,65 EE + SP
11-111-1111 Vlad 16505 01/04/2013 null null MED 5 August 13 149 medical plan pre-tax EE + SP
11-111-1111 Vlad 16505 01/04/2013 Vision Plan before taxes of 5 August 13
Select distinct
' 11-111-1111 "as ssn,
WOMEN'S WEAR. Employee_number,
"Vlad" as EMPLOYEE_FULL_NAME,
TO_CHAR (papf.start_date, "MM/DD/YYYY") as Date_Of_Hire
a.effective_start_date,
PETF.element_name,
pivf. Name,
peevf.screen_entry_valueOf
PER_all_PEOPLE_F women's wear
per_assignments_f A
pay_element_types_f petf
pay_element_links_f pelf
PAY_ELEMENT_ENTRIES_F penf
PAY_ELEMENT_ENTRY_VALUES_F peevf
pay_input_values_f pivf
WHERE
PETF.element_name ('Dental Plan before taxes', 'Medical Plan before taxes', "Vision Plan before taxes")
and petf. ELEMENT_TYPE_ID = pelf. ELEMENT_TYPE_ID
and (trunc (sysdate) BETWEEN pelf. EFFECTIVE_START_DATE AND pelf. EFFECTIVE_END_DATE)
and (pelf. ELEMENT_LINK_ID = penf. ELEMENT_LINK_ID and a.assignment_id = penf. ASSIGNMENT_ID)
and (trunc (sysdate) BETWEEN penf. EFFECTIVE_START_DATE AND penf. EFFECTIVE_END_DATE)
and penf. ELEMENT_ENTRY_ID = peevf. ELEMENT_ENTRY_ID
and peevf. INPUT_VALUE_ID = pivf. INPUT_VALUE_ID
AND papf.employee_number IS NOT NULL
AND A.assignment_type = 'E '.
AND A.person_id = papf.person_id
and papf.effective_end_date > sysdate
and a.effective_end_date > sysdate
and (trunc (sysdate) BETWEEN women's wear. EFFECTIVE_START_DATE AND women's wear. EFFECTIVE_END_DATE)
and a.effective_start_date = (select MAX (effective_start_date) from PER_ASSIGNMENTS_f where assignment_id = a.assignment_id)
and a.assignment_id = 42643
and a.assignment_status_type_id = '1'
order of petf.element_name;Change with your query
SELECT * FROM (select distinct)
' 11-111-1111 "as ssn,
WOMEN'S WEAR. Employee_number,
"Vlad" as employee_full_name,
TO_CHAR (papf.start_date, "MM/DD/YYYY") as date_of_hire
a.effective_start_date,
PETF.element_name,
pivf. Name,
peevf.screen_entry_value
Of
PER_all_PEOPLE_F women's wear
per_assignments_f A
pay_element_types_f petf
pay_element_links_f pelf
PAY_ELEMENT_ENTRIES_F penf
PAY_ELEMENT_ENTRY_VALUES_F peevf
pay_input_values_f pivf
WHERE
PETF.element_name ('Dental Plan before taxes', 'Medical Plan before taxes', "Vision Plan before taxes")
and petf. ELEMENT_TYPE_ID = pelf. ELEMENT_TYPE_ID
and (trunc (sysdate) BETWEEN pelf. EFFECTIVE_START_DATE AND pelf. EFFECTIVE_END_DATE)
and (pelf. ELEMENT_LINK_ID = penf. ELEMENT_LINK_ID and a.assignment_id = penf. ASSIGNMENT_ID)
and (trunc (sysdate) BETWEEN penf. EFFECTIVE_START_DATE AND penf. EFFECTIVE_END_DATE)
and penf. ELEMENT_ENTRY_ID = peevf. ELEMENT_ENTRY_ID
and peevf. INPUT_VALUE_ID = pivf. INPUT_VALUE_ID
AND papf.employee_number IS NOT NULL
AND A.assignment_type = 'E '.
AND A.person_id = papf.person_id
and papf.effective_end_date > sysdate
and a.effective_end_date > sysdate
and (trunc (sysdate) BETWEEN women's wear. EFFECTIVE_START_DATE AND women's wear. EFFECTIVE_END_DATE)
and a.effective_start_date = (select MAX (effective_start_date) from PER_ASSIGNMENTS_f where assignment_id = a.assignment_id)
and a.assignment_id = 42643
and a.assignment_status_type_id = '1')
PIVOT (MAX (screen_entry_value) FOR (name) TO ("Amount" AS 'Amount', 'level' AS 'level', 'Option High' AS 'High Option', 'Pay the value' AS 'Value to pay', 'Period of Type' AS 'Type period'))
order by element_name;
(GOLD)
SELECT ssn,
Employee_number,
employee_full_name,
date_of_hire,
effective_start_date,
element_name,
Max (decode (Name, 'Amount', screen_entry_value)) 'amount. "
Max (decode (Name, 'Level', screen_entry_value)) 'level ',.
MAX (DECODE (name, "High Option", screen_entry_value)) "High Option",
MAX (DECODE (name, 'Value of pay', screen_entry_value)) 'value of pay. "
MAX (DECODE (name, 'Period Type', screen_entry_value)) 'period of Type '.
FROM (select distinct)
' 11-111-1111 "as ssn,
WOMEN'S WEAR. Employee_number,
"Vlad" as employee_full_name,
TO_CHAR (papf.start_date, "MM/DD/YYYY") as date_of_hire
a.effective_start_date,
PETF.element_name,
pivf. Name,
peevf.screen_entry_value
Of
PER_all_PEOPLE_F women's wear
per_assignments_f A
pay_element_types_f petf
pay_element_links_f pelf
PAY_ELEMENT_ENTRIES_F penf
PAY_ELEMENT_ENTRY_VALUES_F peevf
pay_input_values_f pivf
WHERE
PETF.element_name ('Dental Plan before taxes', 'Medical Plan before taxes', "Vision Plan before taxes")
and petf. ELEMENT_TYPE_ID = pelf. ELEMENT_TYPE_ID
and (trunc (sysdate) BETWEEN pelf. EFFECTIVE_START_DATE AND pelf. EFFECTIVE_END_DATE)
and (pelf. ELEMENT_LINK_ID = penf. ELEMENT_LINK_ID and a.assignment_id = penf. ASSIGNMENT_ID)
and (trunc (sysdate) BETWEEN penf. EFFECTIVE_START_DATE AND penf. EFFECTIVE_END_DATE)
and penf. ELEMENT_ENTRY_ID = peevf. ELEMENT_ENTRY_ID
and peevf. INPUT_VALUE_ID = pivf. INPUT_VALUE_ID
AND papf.employee_number IS NOT NULL
AND A.assignment_type = 'E '.
AND A.person_id = papf.person_id
and papf.effective_end_date > sysdate
and a.effective_end_date > sysdate
and (trunc (sysdate) BETWEEN women's wear. EFFECTIVE_START_DATE AND women's wear. EFFECTIVE_END_DATE)
and a.effective_start_date = (select MAX (effective_start_date) from PER_ASSIGNMENTS_f where assignment_id = a.assignment_id)
and a.assignment_id = 42643
and a.assignment_status_type_id = '1')
GROUP BY ssn, employee_number, employee_full_name, date_of_hire, effective_start_date, NOM_ELEMENT
order by element_name;
-
Need help with the query string manipulation
Hello
With the help of 10.1.0.4.2
Given a table called PREFIX_CODES that contains a column called PREFIX_CODE
with the following data:
PREFIX_CODE - VARCHAR2 (20)
-------------------
AAA
BENAMER
CARTER
and another table called SUBSCRIBERS with a column called SUBSCRIBER_ID
with the following data:
SUBSCRIBER_ID - VARCHAR2 (30)
---------------------
BBBB-123456
AAA-444444
DD-2222222
EEEE-888888
Is there a query that will pull all the SUBSCRIBER_ID that begin with
each of the PREFIX_CODES? The following query is not valid, but it
This will give you an idea of what I'm trying to do:
SELECT SUBSCRIBER_ID
SUBSCRIBERS
WHERE AS SUBSCRIBER_ID (SELECT PREFIX_CODE |) » %'
OF PREFIX_CODES)
Using the data from above, I would like that the query to return:
BBBB-123456
AAA-444444
Thanks for your help!Hello
Welcome to the forum!
You were on the right track. To find if a given subscriber corresponds to any prefix_code, you can do an EXISTS subquery:
SELECT subscriber_id FROM subsribers s WHERE EXISTS ( SELECT NULL FROM prefix_codes WHERE s.subscriber_id LIKE prefix_code || '%' ) ;
This will tell you if at least a prefix code. It won't tell you exactly how many, or what they were.
Your message, I'm guessing that there may be more than one, and you can deduct the subscriber_id itself, then the above query should work for you. -
How to build the query SQL for indexes work based on data - dict.?
Hello
I want to build the index 'create' - statement to all indexes have worked-based on the data dictionary.
For example:
create indexes tab1.ind1
on tab1 (lower (col3), lower (col1), col2, col4)
Tablespace "index."
I know I'll find all needed information views all_indexes, all_ind_columns and all_ind_expressions. During the generation of the create statement for the index of the order of the indexed columns must be correct (the order of the indexed column can be achieved using the "position_colonne" column of seen all_ind_columns and all_ind_expressions). But what would be the best way to provide all the necessary information in the right order?
One way would be a PLSQL procedure that puts it all together - but is it possible to use a single SQL statement?
Any help will be appreciated.
Rgds
JHYou can try using DBMS_METADATA. GET_DDL:
SQL> show user; USER is "TEST" SQL> drop table t; Table dropped. SQL> create table t(x varchar2(10)); Table created. SQL> create index i on t(upper(x)); Index created. SQL> select dbms_metadata.get_ddl('INDEX','I','TEST') from dual; DBMS_METADATA.GET_DDL('INDEX','I','TEST') -------------------------------------------------------------------------------- CREATE INDEX "TEST"."I" ON "TEST"."T" (UPPER("X")) PCTFREE 10 INITRANS 2 MAXTRANS 255 STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645 PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT) TABLESPACE "USERS"
-
Help with the query to return the last possible value
Can someone please help me to create a query for the situation below?
Table: TABLEA
Columns:
FACID VARCHAR2 (10),
DEPTID VARCHAR2 (10),
CHARGENUMBER NUMBER (10)
I have the following data:
A, B, 1
A, B, 2
C, D, 3
C, D, 4
I will return the following:
A, B, 2
C, D, 4
In other words, I would return the last possible CHARGENUMBER for FACID and DEPTID.
The table has no index, and it is responsible for a worksheet in that order.
Thank you very much!Hello
If you have a TIMESTAMP column, called entry_dt, you can use a Top - N query like this to find the last x entries.
WITH got_rnum AS ( SELECT my_table.* , RANK () OVER (ORDER BY entry_dt DESC) AS rnum FROM my_table ) SELECT * -- or list all columns except rnum FROM got_rnum WHERE rnum <= x ;
It is very common to have a trigger to ensure that columns like entry_dt are met.
If you have only one statement that inserts with hundreds of lines, they can all have the same entry_dt. (The value of SYSTIMESTAMP is constant throughout a statement, even if it takes a few seconds).
The above query uses RANK, so if you tell him you want the last 10 entries, it can return more than 10, because it includes all lines with exactly the same entry_dt as the 10th. If you want to exactly 10 rows returned, even if there is a tie for 10th place, then use ROW_NUMBER instead of RANK; the rest of the query is the same. -
Hi all
I have the below requirement.
I'm looking for a way to identify products with the exact same specifications.Master table: Product ID Product Name 1 TV 2 Cellphone 3 Laptop 4 DVD Player Details table: Product ID Spec type Spec Detail 1 Color Blue 1 Weight 20 2 Color Blue 2 Weight 20 3 Color Blue 3 Weight 25 4 Color Blue 4 Weight 20 4 Height 10
That is to say, in the example TV and cell phone above has exact specifications.
So the result of the query must be TV and phone cell phone here.
Is it possible to do in a query, or we need a sql block to perform this action.
Any help would be greatly appreciated.
Thank you
AgathyaHi agathya,
We do not know how you want to handle the situation where more than two products have the same specifications, so I extended your sample data slightly to take account of this situation as well:
SQL> create table products (id,name) 2 as 3 select 1, 'TV' from dual union all 4 select 2, 'Cellphone' from dual union all 5 select 3, 'Laptop' from dual union all 6 select 4, 'DVD Player' from dual union all 7 select 5, 'Radio' from dual union all 8 select 6, 'VCR' from dual 9 / Tabel is aangemaakt. SQL> create table product_specifications (product_id,spec_type,spec_detail) 2 as 3 select 1, 'Color' , 'Blue' from dual union all 4 select 1, 'Weight', '20' from dual union all 5 select 2, 'Color' , 'Blue' from dual union all 6 select 2, 'Weight', '20' from dual union all 7 select 3, 'Color' , 'Blue' from dual union all 8 select 3, 'Weight', '25' from dual union all 9 select 4, 'Color' , 'Blue' from dual union all 10 select 4, 'Weight', '20' from dual union all 11 select 4, 'Height', '10' from dual union all 12 select 5, 'Color' , 'Blue' from dual union all 13 select 5, 'Weight', '20' from dual union all 14 select 6, 'Color' , 'Blue' from dual union all 15 select 6, 'Weight', '20' from dual union all 16 select 6, 'Height', '10' from dual 17 / Tabel is aangemaakt. SQL> select p1.name 2 , p2.name 3 from products p1 4 , products p2 5 , product_specifications ps1 6 , product_specifications ps2 7 where p1.id = ps1.product_id 8 and p2.id = ps2.product_id 9 and p1.id < p2.id 10 group by p1.id 11 , p1.name 12 , p2.id 13 , p2.name 14 having sqrt(count(*)) = 15 count(case when ps1.spec_type = ps2.spec_type and ps1.spec_detail = ps2.spec_detail then 1 end) 16 / NAME NAME ---------- ---------- TV Cellphone TV Radio Cellphone Radio DVD Player VCR 4 rijen zijn geselecteerd.
But why devil you or your specifications product model predecessor like that? You are much more complicated that it should be. And the data type of size and weight should be not the same as those of color. But now they are. And how do limit you the allowed values for the colors? Color, weight and size are characteristic of your products so that they should have been modeled like this.
See how easily your questions becomes after remodeling it:
SQL> drop table product_specifications purge 2 / Tabel is verwijderd. SQL> drop table products purge 2 / Tabel is verwijderd. SQL> create table products (id,name,color,weight,height) 2 as 3 select 1, 'TV', 'Blue', 20, null from dual union all 4 select 2, 'Cellphone', 'Blue', 20, null from dual union all 5 select 3, 'Laptop', 'Blue', 25, null from dual union all 6 select 4, 'DVD Player', 'Blue', 20, 10 from dual union all 7 select 5, 'Radio', 'Blue', 20, null from dual union all 8 select 6, 'VCR', 'Blue', 20, 10 from dual 9 / Tabel is aangemaakt. SQL> select * from products 2 / ID NAME COLO WEIGHT HEIGHT ---------- ---------- ---- ---------- ---------- 1 TV Blue 20 2 Cellphone Blue 20 3 Laptop Blue 25 4 DVD Player Blue 20 10 5 Radio Blue 20 6 VCR Blue 20 10 6 rijen zijn geselecteerd. SQL> select p1.name 2 , p2.name 3 from products p1 4 , products p2 5 where p1.id < p2.id 6 and ( p1.color = p2.color or (p1.color is null and p2.color is null)) 7 and ( p1.weight = p2.weight or (p1.weight is null and p2.weight is null)) 8 and ( p1.height = p2.height or (p1.height is null and p2.height is null)) 9 / NAME NAME ---------- ---------- TV Cellphone TV Radio Cellphone Radio DVD Player VCR 4 rijen zijn geselecteerd.
I hope this helps.
Kind regards
Rob. -
Need help with the query to get the County
Hello
Oracle 10 g 2 10.2.0.3 - 64 bit
I want back the number of accounts with two different types of funds (say A and B). Some accounts hold only one of the two funds, and some support both. I want to get the counts like this:
- account held funds - has only
- accounts holding funds-B only
- accounts holding the Fund-A and B funds
Here is what I started with but need assistance to meet the requirement above:
select count(distinct acct.bkoff_acct_no ) accounts_holding_fund_A from xe_account acct, xec_tal_investment_mandate iman, xec_tal_asset_allocation alloc, xe_benchmark bmark, xe_benchmark_usage bu, xe_object_description xod, xec_asset_class cls where iman.mandate_status_cd='A' and cls.asset_class_cd = alloc.asset_class_cd and iman.mandate_id = alloc.mandate_id and acct.account_id = iman.object_id and iman.object_type_cd = 'ACCT' and iman.mandate_id = xod.object_id and xod.field_nm='XEC_TAL_INVESTMENT_MANDATE.COMMENT_TXT' and xod.language_cd = 'E' and acct.acct_status_cd = 'O' and bu.object_type_cd(+) = 'TMAA' and bu.object_id(+) = alloc.asset_allocation_id and bmark.benchmark_id(+) = bu.benchmark_id and alloc.resp_txt like '%fund-A%'
And suppose that the Fund-B has resp_txt like ' % of Fund-B»
Please suggest.
Concerning
Hello
Here is another way, it is easier to adapt to different jobs and different numbers of jobs:
WITH got_distinct_jobs AS
(
SELECT DISTINCT deptno, job
FROM scott.emp
WHERE job IN ("ANALYST", "CLERKS") - or what
)
got_job_list AS
(
SELECT LISTAGG (job, ",") THE Group (ORDER BY work) AS job_list
OF got_distinct_jobs
GROUP BY deptno
)
SELECT job_list
COUNT (*) AS num_departments
OF got_job_list
GROUP BY job_list
;
This shows all the combinations of the jobs listed in the WHERE clause of got_distinct_jobs. You don't need to change anything else in the query. There may be any number of jobs.
Output:
JOB_LIST NUM_DEPARTMENTS
------------------------------ ---------------
CLERK 2
ANALYST, CLERK 1
Maybe you are looking for
-
bookmarks lost after firefox default reset
HelloI received a pop up for Advisor Firefox soon to load and I should go to the troubleshooting and click on restore by default. I did, but now I can't get back my bookmarks toolbars. I right click on the box and I only get more visited and getting
-
Change the path of the directory for a song
I have a song in iTunes for which the directory path is wrong. Therefore, it gets placed in my iTunes in the wrong spot. I cannot seem to edit the directory path. This song "merely" has a folder of an underscore (which needs to be deleted) just be
-
Satellite P875-102 - touch pad / cursor freezes regularly for 2-3 sec
Very happy to get very new P75-102 Toshiba (Windows 7) - £1. 1 k - WAITED so it works well immediately the day 1 - from the box. Unfortunately, the cursor on a regular basis (either 5 - 6 times in a surf web minutes) hangs for 2 to 4 seconds. Everyon
-
Came across this problem trying to put an AVI in powerpoint presentation for the school. I don't know how this happened or how to fix the error. The error indicates that windows media player is not the right codec to play the video. However, when I s
-
Wipe Handheld Smartphones blackBerry
Is recommended that it be executed before all upgrades or damage besides?