Each line in individual columns in a SQL query
Hi Experts,BANNER
--------------------------------------------------------------------------------
Oracle Database 11g Enterprise Edition Release 11.1.0.7.0 - Production
PL/SQL Release 11.1.0.7.0 - Production
CORE 11.1.0.7.0 Production
TNS for 32-bit Windows: Version 11.1.0.7.0 - Production
NLSRTL Version 11.1.0.7.0 - Production
-- DROP TABLE RENDER_VALUES;
CREATE TABLE RENDER_VALUES
(
ID INTEGER PRIMARY KEY,
FIELD_NAME VARCHAR2(1000),
FIELD_VALUE VARCHAR2(1000)
);
INSERT INTO RENDER_VALUES(ID,FIELD_NAME,FIELD_VALUE)VALUES(1,'CropX','10.31234');
INSERT INTO RENDER_VALUES(ID,FIELD_NAME,FIELD_VALUE)VALUES(2,'CropY','20.31234');
INSERT INTO RENDER_VALUES(ID,FIELD_NAME,FIELD_VALUE)VALUES(3,'Height','100');
INSERT INTO RENDER_VALUES(ID,FIELD_NAME,FIELD_VALUE)VALUES(4,'Width','200');
COMMIT;
SELECT FIELD_NAME,FIELD_VALUE FROM RENDER_VALUES;
FIELD_NAME FIELD_VALUE
CropX 10.31234
CropY 20.31234
Height 100
Width 200
I need the output voltage:CropX CropY Height Width
10.31234 20.31234 100 200
Just for example... I gave four values in the column field name... There may be values of number (n) to that and to get each linein individual columns... How can I do something like that?
V dia
Hello
Here is an example of a VARCHAR2 variable usage in PL/SQL.
VARIABLE dj REFCURSOR
CREATE OR REPLACE FUNCTION dept_by_job
RETURN SYS_REFCURSOR
IS
-- dept_by_job returns a SYS_REFCURSOR that has
-- one row for every department in scott.emp and one
-- column for every job, showing the number of employees
-- in that department with that job.
return_csr SYS_REFCURSOR;
select_txt VARCHAR2 (5000);
BEGIN
-- 1st part of query is fixed
select_txt := 'SELECT deptno ';
-- 2nd part of query depends on what's in the table
FOR r IN ( SELECT DISTINCT job
FROM scott.emp
ORDER BY job
)
LOOP
select_txt := select_txt || ', COUNT (CASE WHEN job = '''
|| r.job
|| ''' THEN 1 END) AS '
|| r.job
|| '_cnt ';
END LOOP;
-- Last part of query is fixed
select_txt := select_txt || ' FROM scott.emp'
|| ' GROUP BY deptno'
|| ' ORDER BY deptno';
OPEN return_csr
FOR select_txt;
RETURN return_csr;
END dept_by_job
;
/
SHOW ERRORS
EXEC :dj := dept_by_job;
PRINT :dj
Otherwise, as you have provided a test table, here's an example suitable for your test table:
CREATE OR REPLACE FUNCTION pivot_rv
RETURN SYS_REFCURSOR
IS
return_csr SYS_REFCURSOR;
select_txt VARCHAR2 (5000);
BEGIN
-- 1st part of query is fixed
select_txt := 'SELECT ';
-- 2nd part of query depends on what's in the table
FOR r IN ( SELECT DISTINCT field_name
, ROW_NUMBER () OVER (ORDER BY id) AS rnum
FROM render_values
ORDER BY rnum
)
LOOP
select_txt := select_txt || CASE
WHEN r.rnum > 1
THEN ','
END
|| 'MAX (CASE WHEN field_name = '''
|| r.field_name
|| ''' THEN field_value END) AS "'
|| r.field_name
|| '"';
END LOOP;
-- Last part of query is fixed
select_txt := select_txt || ' FROM render_values';
OPEN return_csr
FOR select_txt;
RETURN return_csr;
END pivot_rv
;
Published by: Frank Kulash, January 15, 2010 10:42
Tags: Database
Similar Questions
-
SQL Analysis: Extract column expression and SQL query tags
Hi all
I have a requirement and posted earlier reg. this.
An SQL statement must be parsed and column names must be extracted separately.
I can't use dbms_sql. I need to extract the names of columns through manipulating strings in PL/SQL only.
I don't have to validate the SQL code.
for example.
SELECT EMPNO, Upper (ENAME), DEPTNO Emp_Name, DECODE (Deptno, 10, 'ACCOUNTING', 20, 'HR', 'OTHER') Dept_Name OF double
Output should be,.
Column names
--------------------
(1) EMPNO
(2) Emp_Name Upper (ENAME)
(3) DEPTNO
(4) Dept_Name DECODE(Deptno,10,'ACCOUNTING', 20,'HR','OTHERS')
How can I retrieve the names of columns using PL/SQL for the above format?
Pls help.
Kind regards
SamIf the requirement is to have them come forward...
SQL> ed Wrote file afiedt.buf 1 declare 2 v_sql VARCHAR2(4000) := q'[SELECT EMPNO, Upper(ENAME) Emp_Name, DEPTNO, DECODE(Deptno,10,'ACCOUNTING', 20,'HR','OTHERS') Dept_Name FROM Dual]'; 3 v_cols VARCHAR2(4000) := regexp_replace(v_sql, 'SELECT (.*) FROM .*', '\1'); 4 PROCEDURE parse_col(p_cols IN VARCHAR2, lvl NUMBER) IS 5 v_str VARCHAR2(4000); 6 v_br NUMBER := 0; 7 v_pos NUMBER := 1; 8 begin 9 LOOP 10 EXIT WHEN v_pos > length(p_cols); 11 CASE SUBSTR(p_cols,v_pos,1) 12 WHEN '(' THEN 13 v_br := v_br + 1; 14 WHEN ')' THEN 15 v_br := v_br - 1; 16 WHEN ',' THEN 17 IF v_br = 0 THEN 18 DBMS_OUTPUT.PUT_LINE(to_char(lvl,'fm99')||': '||trim(v_str)); 19 parse_col(SUBSTR(p_cols,v_pos+1),lvl+1); 20 v_str := NULL; 21 EXIT; 22 END IF; 23 ELSE NULL; 24 END CASE; 25 v_str := v_str || SUBSTR(p_cols,v_pos,1); 26 v_pos := v_pos + 1; 27 END LOOP; 28 IF v_str IS NOT NULL THEN 29 DBMS_OUTPUT.PUT_LINE(to_char(lvl,'fm99')||': '||trim(v_str)); 30 END IF; 31 end; 32 begin 33 parse_col(v_cols,1); 34* end; SQL> / 1: EMPNO 2: Upper(ENAME) Emp_Name 3: DEPTNO 4: DECODE(Deptno,10,'ACCOUNTING', 20,'HR','OTHERS') Dept_Name PL/SQL procedure successfully completed. SQL>
-
Each line in a column to an XML conversion
Hello world
With the help of Oracle Database 10 g Enterprise Edition Release 10.2.0.4.0 - 64 bit
I have a table where there is a comments column which is VARCHAR2. This column contains data of type string in the form of XML tags.
As for example. * < tag_name attr_name = "10" / > * (cant sorry give the exact values that I'm not allowed to)
There are as many such lines. Now, I want to use these channels and operated as a XML like the use of XPath to find some attributes etc.
I tried to use the extract function and to use it, I need an XMLType object. So I tried to create one using the XMLType() constructor.
But it gives me error ORA-19032: tag expected, XML had no content
When I looked on the net, I found that this error number does not match the error message I get. The msg of error found on internet, it is should tag XML string as string
I used this query
But when I changed (based on the intuition of someone)SELECT XMLType (<column>) FROM <table>;
The application worked well. That it may take only one line.SELECT XMLType (<column>) FROM <table> WHERE ROWNUM <= 1;
I also tried like this
This worked too well.SELECT XMLType ('<tag_name attr_name="10"/>') FROM DUAL;
So finally he left me completely confused. Is there a way to convert every row in a column to an XMLType object so that I can use the extract function to collect information on this topic.
For now, I used REGEXes to write code. However, with the flexibility of xml is preferable.
Any help would be appreciated.
Kind regards
Aalami
Published by: aalami Kanrar on May 23, 2013 05:27Aalami,
The error message is correct. What you have found, it is simply the generic message with placeholders in the string .
The error message is also pretty explicit: you cannot pass an empty string (NULL) to the XMLType constructor.
SQL> select xmltype('') from dual; ERROR: ORA-19032: Expected XML tag , got no content ORA-06512: at "SYS.XMLTYPE", line 310 ORA-06512: at line 1 no rows selected
You must either add a WHERE clause to filter the columns NULL or use a CASE statement to only convert strings that are not empty:
SELECT XMLType (
) FROM WHERE
IS NOT NULL ; SELECT CASE WHEN
IS NOT NULL THEN XMLType ( ) END FROM ... And do not use EXTRACT if you want access to scalar values, use EXTRACTVALUE instead:
SQL> select extractvalue(xmltype('
'), '/tag_name/@attr_name') from dual; EXTRACTVALUE(XMLTYPE(' '),'/TAG_NAME/@ATTR_NAME') -------------------------------------------------------------------------------- 10 update of each line in a column
I have to update only the first letter of all the names in the last_name column used table... Please help me with this issue?You must be more precise, that is what you need:
update mytable set last_name = 'A' || Substr(Last_name,2);
that's really interesting.
Published by: Mustafa KALAYCI on August 10, 2012 10:35
- Hello
I post this question for clarification. I have a report that is based on a SQL query area.
Place automatically APEX sql in the tables, all THE results of a query if it is in a region of report? Or y at - it a step that the developer must do to retrieve the columns of the sql query in a table.
I'm working with checkboxes, and I read up on top of the api APEX_APPLICATION. This part is still unclear.
Your comments would be appreciated!
KeishaHi Keisha,
You should have a read of: [http://download-uk.oracle.com/docs/cd/B31036_01/doc/appdev.22/b28839/check_box.htm#CHDHFACG] that explains the use of checkboxes on the reports.
I guess the 'picture' you refer to are APEX_APPLICATION. G_F01 up APEX_APPLICATION. Paintings of G_F50? If so, they are built when you submit the page and are based on elements with attributes of 'name' of 'f01' up to 'f50' in the HTML tags. You don't need to do anything beforehand to create these, which is done automatically for you by the Apex. But, notes, however, that only the checked boxes are presented with a page - so if your username is checked, say, 3 of the 10 boxes, table will contain only 3 points not 10 (with each element of the table that contains the attribute 'value' of a checkbox).
Andy
Is possible to write the INSERT statement that fills two columns: 'word' and 'sense' of the file text with multiple lines - in each line is followed word that is the meaning?
Hello
2796614 wrote:
Is possible to write the INSERT statement that fills two columns: 'word' and 'sense' of the file text with multiple lines - in each line is followed word that is the meaning?
Of course, it is possible. According to what the text file looks like to, you can create an external table that treats the text file as if it were a table. Otherwise, you can always read the file in PL/SQL, using the utl_file package and INSERT of PL/SQL commands.
You have problems whatever you wantt? If so, your zip code and explain what the problem is.
Whenever you have any questions, please post a small example of data (CREATE TABLE and only relevant columns, INSERT statements) for all of the tables involved and the exact results you want from these data, so that people who want to help you can recreate the problem and test their ideas. In this case, also post a small sample of the text involved file.
If you ask about a DML operation, such as INSERT, then INSERT statements, you post should show what looks like the tables before the DML, and the results will be the content of the table changed after the DML.
Explain, using specific examples, how you get these results from these data.
Always say what version of Oracle you are using (for example, 11.2.0.2.0).
See the FAQ forum: Re: 2. How can I ask a question on the forums?
Get only one line for each type of a column
Hello!
I have a little problem.
I have a table with 10 columns. One of them has repeated values. My goal is to get all the lines with this column only once for each values he holds.
Ex:
Col1 Col2 Col3
A A A A
B B B
C A C
D C D
Column 2 has repeated values. My goal is to get:
A A A A
B B B
D C D
Only once each value in column 2.
Thank you guys! ;)SQL> CREATE TABLE Tbl (col1 VARCHAR2(1),col2 VARCHAR2(1),col3 VARCHAR2(1)); Table created. SQL> INSERT INTO Tbl VALUES ('A','A','A'); 1 row created. SQL> INSERT INTO Tbl VALUES ('B','B','B'); 1 row created. SQL> INSERT INTO Tbl VALUES ('C','A','C'); 1 row created. SQL> INSERT INTO Tbl VALUES ('D','C','D'); 1 row created. SQL> commit; Commit complete. SQL> SELECT * FROM tbl t1 2 WHERE 3 rowid=(SELECT min(rowid) 4 FROM tbl t2 5 WHERE t1.col2=t2.col2); C C C - - - A A A B B B D C D SQL> SELECT * FROM tbl t1 2 WHERE 3 rowid=(SELECT max(rowid) 4 FROM tbl t2 5 WHERE t1.col2=t2.col2); C C C - - - B B B C A C D C D
Published by: Johan August 3, 2010 01:31
Adding column which gives the serial number for each line in the Bulletin Board
I use the update version 2 Jdev 11 g.
I'm the filling of all employees from the emp table ADF table.
My requirement is to add a column more as the first column and header line which displays the serial number for each serial number of row.the is not available in the table emp in database .i want to generate it dynamically when the data are filled in to ADF table.i don't want not to use the method to get the SQL query rownum. I want to get custom code (in the Managed Bean) or set all of the properties in the subject entity or any other object.
Published by: sj0609 on September 8, 2009 09:43Hello
Give an id to the table (say 'currRow') varStatus property. Add a column to the table (for example with a text output), then set it to the value of the output text #{currRow.index}.
Arun-
I have a column with two values, separated by a space, in each line. How do I create 2 new columns with the first value in one column and the second value in another column?
Add two new columns after than the original with space separated values column.
Select cell B1 and type (or copy and paste it here) the formula:
= IF (Len (a1) > 0, LEFT (A1, FIND ("", A1) −1), ' ')
shortcut for this is:
B1 = if (Len (a1) > 0, LEFT (A1, FIND ("", A1) −1), ' ')
C1 = if (Len (a1) > 0, Member SUBSTITUTE (A1, B1 & "", ""), "")
or
the formula of the C1 could also be:
= IF (Len (a1) > 0, RIGHT (A1, LEN (A1) −FIND ("", A1)), "")
Select cells B1 and C1, copy
Select cells B1 at the end of the C column, paste
Write a SQL query with lines in columns
All the
I need help in writing a SQL query with lines in columns, let give u an example...
drop table activity;
CREATE TABLE 'ACTIVITY '.
(
"PROJECT_WID" NUMBER (22.0) NOT NULL,
VARCHAR2 (150 CHAR) "PROJECT_NO."
VARCHAR2 (800 CHAR) 'NAME '.
);
Insert in the ACTIVITY (PROJECT_WID, PROJECT_NO, NAME) values (1683691, '10007', 12-121');
Insert in the ACTIVITY (PROJECT_WID, PROJECT_NO, NAME) values (1684994, '10008', 12-122');
Insert in the ACTIVITY (PROJECT_WID, PROJECT_NO, NAME) values (1686296, '10009', 12-123');
Insert in the ACTIVITY (PROJECT_WID, PROJECT_NO, NAME) values (2225222, '9040', 12-124');
drop table lonet;
CREATE TABLE 'LONET.
(
VARCHAR2 (150 CHAR) "NAME."
NUMBER OF THE "ROOT."
VARCHAR2 (150 CHAR) "ENTRYVALUE".
);
INSERT INTO LONET (NAME, ROOT, ENTRYVALUE) VALUES ("GAC", 1683691, "LDE");
INSERT INTO LONET (NAME, ROOT, ENTRYVALUE) VALUES ('NAM', 1683691, 'LME');
INSERT INTO LONET (NAME, ROOT, ENTRYVALUE) VALUES ('BAG', 1683691, 'ICE');
INSERT INTO LONET (NAME, ROOT, ENTRYVALUE) VALUES ('PAP', 1683691, 'IKE');
INSERT INTO LONET (NAME, ROOT, ENTRYVALUE) VALUES ('NAM', 1686291, "QTY");
INSERT INTO LONET (NAME, ROOT, ENTRYVALUE) VALUES ('PAP', 1686291, 'MAX');
INSERT INTO LONET (NAME, ROOT, ENTRYVALUE) VALUES ("GAC", 1684994, "MTE");
INSERT INTO LONET (NAME, ROOT, ENTRYVALUE) VALUES ('PAP', 1684994, 'MAC');
INSERT INTO LONET (NAME, ROOT, ENTRYVALUE) VALUES ('FMT', 1684994, 'NICE');
INSERT INTO LONET (NAME, ROOT, ENTRYVALUE) VALUES ('FMR', 1684994, 'RAY');
INSERT INTO LONET (NAME, ROOT, ENTRYVALUE) VALUES ('BAG', 1686296, "CAQ");
INSERT INTO LONET (NAME, ROOT, ENTRYVALUE) VALUES ("PAP", 1686296, "QAQ");
INSERT INTO LONET (NAME, ROOT, ENTRYVALUE) VALUES ("VANESSA", 1686296, "THEW");
INSERT INTO LONET (NAME, ROOT, ENTRYVALUE) VALUES ("ANDR", 1686296, "REYL");
commit;
Link: activity.project_wid = lonet.root
look like output
Project_wid Project_no NAME GAC NAM BAG RAC 1683691 10007 12-121 LDE LME LCE LKE 1684994 10008 12-122 MTE null null MAC 1686296 10009 12-123 null null CAQ QAQ 2225222 9040 12-124 null null null null two problems, in that I am running
1. I dono how simply we can convert rows to columns
2. for root = 1683691, there are double NAM and RAC in lonet table... ideally these data should not be there, but since its here, we can take a MAX so that it returns a value
3. There are undesirables who should be ignored
Once again my thought process is that we join the activity and 4 alias table lonet.
ask for your help in this
Thank you
Hello
This is called pivoting.
Here's a way to do it:
WITH relevant_data AS
(
SELECT a.project_wid, a.project_no, b.SID
, l.name AS lonet_name, l.entryvalue
Activity one
LEFT OUTER JOIN lonet l.root = a.project_wid l
)
SELECT *.
OF relevant_data
PIVOT (MAX (entryvalue)
FOR lonet_name IN ("GAC" IN the gac
"NAM" AS nam
'BAG' IN the bag
"RAC" AS cars
)
)
ORDER BY project_wid
;
Output:
PROJECT_WID PROJECT_NO GAC NAM BAG RAC NAME
----------- ---------- ---------- ---------- ---------- ---------- ----------
1683691 12 - 10007 121 LDE LME LCE LKE
1684994 MAC MTE 10008 12-122
1686296 12 - 10009 123 QAC QAQ
2225222 9040 12 - 124
To learn more about swivel, see the FAQ in the Forum: Re: 4. How can I convert rows to columns?
Thanks for posting the CREATE TABLE and INSERT statements; It's very useful!
How to access a column value in a Select list in a tabular presentation for each line
Hello
Environment: Using Oracle APEX v4.2.1 on an Oracle 11 g 2 DB
In a tabular presentation using the following example query, i.e.:
select "EMPNO", "EMPNO" EMPNO_DISPLAY, "ENAME", "HIREDATE", "SAL", "DEPTNO", "MY_LOV" from"#OWNER#"."EMP"
I need access to every line of my sub form of table, the EMPNO of this line value, within a list of selection (query based LOV) in the column "MY_LOV."
So, basically, the MY_LOV column in my table presentation, would be a list of selection (query based LOV) where MY_EMPNO =: EMPNO (first column in the select statement above), for each line of the tabular presentation.
With the help of a picture ( Tom's Blog ) and assuming 'Deptno' column here is actually my new column, i.e. "MY_LOV, I want to know how this list of selection based on lov query would be able to access each EMPNO down in the form of tables, i.e. 7839, 7698, 7782 etc.?
Any help would be appreciated.
Thank you.
Tony.
Hi Tony,.
Take a look at the API APEX_ITEM for LOV
http://docs.Oracle.com/CD/E37097_01/doc/doc.42/e35127/apex_item.htm#AEAPI208
Kind regards
Brad
Lines of columns within PL/SQL or sql.
Hi I need to get this data, which are retrieved using a query sql with a prerequisite by clause and are displayed as lines. for display in a separate on one... rank column, the values must be in their own column on the same line...
tabular presentationLevel 1 Level2 Level3 Level4 Level5
So, I need to enter these lines in the columns of the table... But the lines can be up to 11 and beyond so I'll create a table with many columns, but for nowcolumn1 column2 column3 270767 197851 1 64631 230693 2 33115 230481 3 229148 209655 4
just using 5, but I want the script, so it can just keep going through the columns not only limited to 5...This query you posted could be reduced up to...
select max(decode(hierarchy_level,1,subordinate_position_id)) as number1 ,max(decode(hierarchy_level,2,subordinate_position_id)) as number2 ,max(decode(hierarchy_level,3,subordinate_position_id)) as number3 ,max(decode(hierarchy_level,4,subordinate_position_id)) as number4 ,max(decode(hierarchy_level,5,subordinate_position_id)) as number5 ,max(decode(hierarchy_level,6,subordinate_position_id)) as number6 ,max(decode(hierarchy_level,7,subordinate_position_id)) as number7 ,max(decode(hierarchy_level,8,subordinate_position_id)) as number8 ,max(decode(hierarchy_level,9,subordinate_position_id)) as number9 ,max(decode(hierarchy_level,10,subordinate_position_id)) as number10 ,max(decode(hierarchy_level,11,subordinate_position_id)) as number11 ,max(decode(hierarchy_level,12,subordinate_position_id)) as number12 ,max(decode(hierarchy_level,13,subordinate_position_id)) as number13 ,max(decode(hierarchy_level,14,subordinate_position_id)) as number14 ,max(decode(hierarchy_level,15,subordinate_position_id)) as number15 from (SELECT pos.subordinate_position_id ,LEVEL AS hierarchy_level FROM apps.per_pos_structure_elements pos, (select paf.position_id,paf.person_id from per_all_assignments_f paf where sysdate between paf.effective_start_date and paf.effective_end_date and paf.assignment_type = 'E') people_data WHERE pos.pos_structure_version_id = 103 and pos.subordinate_position_id = people_data.position_id (+) CONNECT BY PRIOR pos.parent_position_id = pos.subordinate_position_id AND pos.pos_structure_version_id = 103 START WITH pos.subordinate_position_id = 197851 )
Add dynamic buttons to each line of the report and bind the data line to this line-specific buttons
I have a page with a form in the upper part and a report at the bottom that is bound to a table. I need to add a pair of buttons for each row in the table and add dynamic actions for these buttons. I need to submit the data corresponding to the line to a REST service and update the table/line. I know that it is possible to add ajax call and refresh the table after receipt of the response. But I don't know how can I dynamically include a pair of buttons on each line and access the data corresponding to the record when a particular button is clicked. I was not able to find such a feature using the help page. Can you please let me know if this is possible. Thanks in advance.
Here is the representation of how I need to the page to look like.
Col 1
Col 2
Col 3
data 1
data 21
data 31
Button 1
Button 2
data 2
data 22
data 32
Button 1
Button 2
data 3
data 23
data 33
Button 1
Button 2
data 4
data 24
data 34
Button 1
Button 2
I should be able to access the data 1, data21, data button 31 11 and button21 etc.
Select data1,
data2,
data3,
......,
Button1 null,
Button2 null,
ROWID r_id
Of...
If you change the column for button1 and navigate to the area of column formatting you can create your button here, in several different ways. You will need to play with the way you prefer. See below:
SQl query to find out time between the different lines of transactions
(See both images from an attachment to get the clear picture of the data and understand the question correctly.)
I have a set of data like this in one of my paintings. (This is a simple representation of the original data.)
Reference table1.jpg
Id | Type | Value | Start_date | End_date
----------------------------------------------------------------------------------------------------------------------
ZTR0098 | ALLOW | 0 | 1 JUN | 2 JUN |
ZTR0098 | ADTAX | 0 | 1 JUN | 2 JUN |
ZTR0098 | MXTAX | 0 | 1 JUN | 9 JUN |
ZTR0098 | ALLOW | 4. 3 JUN | 15 JUN |
ZTR0098 | ADTAX | 44.00 | 3 JUN | 17-JUNE |
ZTR0098 | MXTAX | 2. 10 JUN | 17-JUNE |
ZTR0098 | ALLOW | 5. 16-JUNE | 20 JUN |
ZTR0098 | ADTAX | 55,34 | 18 JUN | 22 JUN |
ZTR0098 | MXTAX | 1. 18 JUN | 25 JUN |
ZTR0098 | MXTAX | 6. 26 JUN | 31 AUG |
ZTR0098 | ADTAX | 20.09. 23 JUN | 23 JUL |
ZTR0098 | ALLOW | 8. 21 JUN | 31 AUG |
ZTR0098 | ADTAX | 45. 24 JUL | 31 AUG |
each line has a type and a rasthaus id to it. ID belongs to other parent tables. the value of each type is given, and the validity of each value is followed by a field start_date and end_date.
All values start from 1 - JUN and expires on 31 - AUG. Now my requirement is to obtain a report that gives three columns for three different types (ALLOW, ADTAX and MXTAX) with combination of unique values in the effective time interval. Let me put the result below.
Reference table2.jpg
Id | ALLOW | ADTAX | MXTAX | Start_date | End_date
--------------------------------------------------------------------------------------------------------------------------------------------------
ZTR0098 | 0 | 0 | 0 | 1 JUN | 2 JUN |
ZTR0098 | 4. 44.00 | 0 | 3 JUN | 9 JUN |
ZTR0098 | 4. 44.00 | 2. 10 JUN | 15 JUN |
ZTR0098 | 5. 44.00 | 2. 16-JUNE | 17-JUNE |
ZTR0098 | 5. 55,34 | 1. 18 JUN | 20 JUN |
ZTR0098 | 8. 55,34 | 1. 21 JUN | 22 JUN |
ZTR0098 | 8. 20.09. 1. 23 JUN | 25 JUN |
ZTR0098 | 8. 20.09. 6. 26 JUN | 23 JUL |
ZTR0098 | 8. 45. 6. 23 JUL | 31 AUG |
As you can see there are no duplicate rows for a combination of (ALLOW, ADTAX and MXTAX) with their respective dates in force. resulting in the above table. the first step is to convert lines to the column which is pretty obvious to do that by grouping on start_date and end_date colum, but the real deal is the time interval during which the combination of the values (ALLOW, ADTAX, and MXTAX) has remained constant.
I wrote under query using Group by.
Select
ID,
NVL (max (decode (type, "ALLOW", value)), 0) as ALLOW
NVL (max (decode (type, 'ADTAX', value)), 0) as ADTAX
NVL (max (decode (type, 'MXTAX', value)), 0) as MXTAX
Start_date,
End_date
from my_table
Group of start_date, end_date, id
start_date, end_date
the results it gives are like this:
Reference table3.jpg
Id | ALLOW | ADTAX | MXTAX | Start_date | End_date
------------------------------------------------------------------------------------------------------------------------------------------------
ZTR0098 | 0 | 0 | 0 | 1 JUN | 2 JUN |
ZTR0098 | 0 | 0 | 2. 1 JUN | 9 JUN |
ZTR0098 | 4. 0 | 0 | 3 JUN | 15 JUN |
ZTR0098 | 0 | 44.00 | 0 | 3 JUN | 17-JUNE |
ZTR0098 | 0 | 0 | 2. 10 JUN | 17-JUNE |
ZTR0098 | 5. 0 | 0 | 16-JUNE | 20 JUN |
ZTR0098 | 0 | 55,34 | 0 | 18 JUN | 22 JUN |
. .
. .
like wise
but I'm not able to determine the time intervals by using the SQL query.
with
Table1 as
(select the id 'ZTR0098', 'ALLOW' type, 0 val, to_date('1-JUN','dd-MON') start_date, end_date Union to_date('2-JUN','dd-MON') double all the)
Select 'ZTR0098', 'ADTAX', 0, to_date('1-JUN','dd-MON'), to_date('2-JUN','dd-MON') of all the double union
Select 'ZTR0098', 'MXTAX', 0, to_date('1-JUN','dd-MON'), to_date('9-JUN','dd-MON') of all the double union
Select 'ZTR0098', 'ALLOW', 4, to_date('3-JUN','dd-MON'), to_date('15-JUN','dd-MON') of all the double union
Select 'ZTR0098', 'ADTAX', 44.00, to_date('3-JUN','dd-MON'), to_date('17-JUN','dd-MON') of all the double union
Select 'ZTR0098', 'MXTAX', 2, to_date('10-JUN','dd-MON'), to_date('17-JUN','dd-MON') of all the double union
Select 'ZTR0098', 'ALLOW', 5, to_date('16-JUN','dd-MON'), to_date('20-JUN','dd-MON') of all the double union
Select 'ZTR0098', 'ADTAX', 55.34, to_date('18-JUN','dd-MON'), to_date('22-JUN','dd-MON') of all the double union
Select 'ZTR0098', 'MXTAX', 1, to_date('18-JUN','dd-MON'), to_date('25-JUN','dd-MON') of all the double union
Select 'ZTR0098', 'MXTAX', 6, to_date('26-JUN','dd-MON'), to_date('31-AUG','dd-MON') of all the double union
Select 'ZTR0098', 'ADTAX', 20.09, to_date('23-JUN','dd-MON'), to_date('23-JUL','dd-MON') of all the double union
Select 'ZTR0098', 'ALLOW', 8, to_date('21-JUN','dd-MON'), to_date('31-AUG','dd-MON') of all the double union
Select 'ZTR0098', 'ADTAX', 45, to_date('24-JUL','dd-MON'), to_date('31-AUG','dd-MON') of the double
),
days like
(select level - 1 dte + to_date('1-JUN','dd-MON')
of the double
connect by level<= to_date('31-aug','dd-mon')="" -="" to_date('1-jun','dd-mon')="" +="">=>
)
Select id, allow, adtax, mxtax, min (dte) start_date, max (dte) end_date
(select ID, dte, max (allow) allow, max (adtax) adtax, max (mxtax) mxtax,
ROW_NUMBER() over (order by dte) row_number() - courses (partition by order max (allow), max (adtax), max (mxtax) by dte) gr
go (select id, dte,
-case when type = 'ALLOW' and dte between start_date and end_date then end val 0 otherwise allow.
-case when type = "ADTAX" and dte between start_date and end_date then val 0 otherwise end adtax.
-case when type = "MXTAX" and dte between start_date and end_date then val 0 otherwise end mxtax
Table 1 t,
days d
where d.dte between t.start_date and t.end_date
)
Group by id, dte
)
Group by id, gr, allow, adtax, mxtax
order by id, gr
ID ALLOW ADTAX MXTAX START_DATE END_DATE ZTR0098 0 0 0 01/06/2015 02/06/2015 ZTR0098 4 44 0 03/06/2015 09/06/2015 ZTR0098 4 44 2 10/06/2015 15/06/2015 ZTR0098 5 44 2 16/06/2015 17/06/2015 ZTR0098 5 55,34 1 18/06/2015 20/06/2015 ZTR0098 8 55,34 1 21/06/2015 22/06/2015 ZTR0098 8 20.09 1 23/06/2015 25/06/2015 ZTR0098 8 20.09 6 26/06/2015 23/07/2015 ZTR0098 8 45 6 24/07/2015 31/08/2015 Concerning
Etbin
SQL query to retrieve a single record for each employee of the following table?
Hi all
Help me on the writing of SQL query to retrieve a single record for each employee of the following table? preferably a standard SQL.
CREATE TABLE xxc_contact)
empnum NUMBER,
alternatecontact VARCHAR2 (100),
relationship VARCHAR2 (10),
phtype VARCHAR2 (10),
Phone NUMBER
);
insert into xxc_contact values (123456, 'Rick Grimes', 'SP', 'Cell', 9999999999)
insert into xxc_contact values (123456, 'Rick Grimes', 'SP', 'Work', 8888888888)
insert into xxc_contact values (123457, 'Daryl Dixon', 'EN', 'Work', 7777777777)
insert into xxc_contact values (123457, 'Daryl Dixon', 'EN', 'Home', 3333333333)
insert into xxc_contact values (123456, 'Maggie Greene', 'CH', 'Cell', 9999999999)
insert into xxc_contact values (123456, 'Maggie Greene', 'CH', 'Home', 9999999999)
expected result:
EmpNum AlternateContact Relationship PhType Phone
123456 rick Grimes SP cell 9999999999
Daryl Dixon EN work 7777777777 123457
Home 123458 Maggie Greene CH 6666666666
Thanks in advance.
994122 wrote:
Thank you all, that I got a result
http://www.orafaq.com/Forum/m/620305/#msg_620305
By Lalit Kumar B...
Specifically, the two simple solutions provided were:
1 using the row_number, entitled Oracle ranking based on descending order of the inside telephone each empnum group. And finally selects the lines which has least rank (of least since that order is descending for phone).
SQL > column alternatecontact format A20;
SQL >
SQL > SELECT empnum, alternatecontact, relationship, phtype, phone
2 from (SELECT a.*, row_number() over r (PARTITION BY empnum ORDER BY phone / / DESC))
3 FROM xxc_contact one)
4. WHEN r = 1
/
EMPNUM ALTERNATECONTACT RELATIONSHIP PHTYPE PHONE
---------- -------------------- ---------- ---------- ----------
123456 rick Grimes SP cell 9999999999
Daryl Dixon EN work 7777777777 123457
Home 123458 Maggie Greene CH 6666666666
2. with the help of MAX, Oracle automatically assigns the maximum phone for all the rows in each group of empnum. And finally selects the rows with the maximum phone. Order by clause is omitted here intentionally. You can find out why.
SQL > SELECT empnum, alternatecontact, relationship, phtype, phone
2 (SELECT a.*, MAX (phone) over (PARTITION BY empnum) rn FROM xxc_contact one)
3 WHERE phone = rn
4.
EMPNUM ALTERNATECONTACT RELATIONSHIP PHTYPE PHONE
---------- -------------------- ---------- ---------- ----------
123456 rick Grimes SP cell 9999999999
Daryl Dixon EN work 7777777777 123457
Home 123458 Maggie Greene CH 6666666666
Kind regards
Lalit
Maybe you are looking for
-
How to activate an add-on we Temp (Java). ?
I when prompted "allow and remember", but I still get it out look for the safety block. Help!
-
How to reset the map of IO on a Tecra 9100
Hello I would like to know how I can reset my tecra 9100 IO card, because I ve found just below the Mini PCI slot a reset option, why am I doing this? Well, this is just a test...! because my mini PCI slot does not work for a few days and I think a r
-
Is it possible to limit the number of connections to the remote panel to a vi?
Hello I have an application that runs on a PXI RT system and I use the remote control to access. My license allows me to have several remote connections and it's ok that I sometimes need to open several remote panels at the same time. However, I wou
-
How can I TAKE pictures on my laptop...?
How to make a new photos on my new laptop. I don't know where to go. I don't know what to do. the answers here are not useful. WHERE I'M GOING AND HOW I'M TAKING NEW PHOTOS ON MY LAPTOP?
-
WiFi guard turn off (DESPERATE for a SOLUTION)
Hello, I'm desperate for a solution right now... I almost tried everything. Whenever I connect to my router (linksys) it connects, but stops immediately! I have to turn off wifi, turn it on again and when I try to connect to my router... it connects