Amplify the query lines
G ' Day in the SQL forum,.I am looking for an elegant solution to this problem, and so far nothing comes to mind (except maybe something scary like the clause type)
In light of this test scenario:
create table my_table
(id number
,name varchar2(100)
,qty number
);
insert into my_table values (1, 'Scott', 3);
insert into my_table values (2, 'Penny', 2);
insert into my_table values (3, 'Eddie', 1);
insert into my_table values (4, 'Ray' , 1);
I want my output to beID NAME
--- -----
1 Scott
1 Scott
1 Scott
2 Penny
2 Penny
3 Eddie
4 Ray
That's why amplify each line by the number in the field.Just as I posted one solution I came with implied analytical, but I think it could be better.
select * from (
select id, name, qty
,row_number() over (partition by name order by null) rn
from my_table
connect by level <= 2)
where qty >= rn
Any ideas for something cleaner?Scott.
select t.*
from my_table t,(select rownum no from dual connect by rownum <= (select max(qty) from my_table)) x
where no <= qty
order by id
Tags: Database
Similar Questions
-
I have two tables pecourses and teachcourses with the following desc
pecourses
Name Null? Type
----------------------------------------- -------- ----------------------------
CSTDT NOT NULL DATE
COURSCOD NOT NULL VARCHAR2 (5)
COURSDOCNO NOT NULL VARCHAR2 (15)
COURSDOCDT NOT NULL DATE
TRSTYPE NOT NULL VARCHAR2 (1)
DATE OF CENDDT
QUALIFYING VARCHAR2 (2)
PARTNO VARCHAR2 (3)
VARCHAR2 (6) MAJOR
HOURNO NUMBER (3)
teachcourses
Name Null? Type
----------------------------------------- -------- ----------------------------
EMPNO NOT NULL VARCHAR2 (5)
CSTDT NOT NULL DATE
COURSCOD NOT NULL VARCHAR2 (5)
COURSDOCNO NOT NULL VARCHAR2 (15)
COURSDOCDT NOT NULL DATE
CHAR (1) PARTICIPANT
HOURSNO NUMBER (3)
TEACHER NOT NULL NUMBER (3)
I will execute a query as follows:
SQL > select p1.courscod, p1.cstdt, p1.cenddt and p2.cenddt in the p1, p2 pecourses pecourses where
P1.courscod = P2.courscod and p1.cstdt = p2.cstdt and p1.cenddt <>p2.cenddt and
P1.courscod in (select courscod from the teachcourses where empno = '3056')
the object of the application is to retrieve the rows that have the same code of classes and the start of the course, but differ by the
date this employee 3056 has taught for the courses.
the issue: the query works very well but there are recurrences as follows:
the line
P1.courscod = 016900
P1.cstdt = 30-6-2008
P1.cenddt = 4-7-2008
P2.cenddt = 5-7-2008
would be repeated in the output as follows
P1.courscod = 016900
P1.cstdt = 30-6-2008
P1.cenddt = 5-7-2008
P2.cenddt = 4-7-2008
is there a way to eliminate this repetition?
Thank you in advance.Perhaps you could add a condition of p1.cenddt< p2.cenddt="">
don't know if that's exactly what you want, but it will eliminate the duplicate output example you provided -
Hallo,
Sometimes, in the code for my application, I need to run smaller queries to retrieve data from the database.
I wonder if for these queries, it is necessary to define a View object or if I can avoid this for example creation and execution of a VO on the fly.
In this case I would not define a VO, given that queries will be never used to display data, but only for the internal logic.
A use case, I want to solve it next.
I would like to create a function to be used after validation for last inserted record by a user in a TableVO.
For example
Select the Id of the Table where UserId =? and Type =?
The query should be run in a managed bean...
public String okButton_action() {} OperationBinding operationBinding is BindingsUtils.getBindings () .getOperationBinding ("Commit");.
Object result = operationBinding.execute ();
If (! operationBinding.getErrors () .isEmpty ()) {}
Error handling
return;
}
Here, I need to:
1. run the query to get the Id of the last inserted record
2 call the setCurrentRowWithKey on the TableVO to update the display
and get the Id (identity/auto-increment) than the database
assigned to the field
return "return";
}
If I can create a function that creates / runs the VO on the fly and return the value ID... It is best to create it in a class of public services or in the implementation class TableVO?
I need to get the Id of the last line inserted since the ADF is not able to manage MS SQL Server identity fields (it seems that REFRESH AFTER INSERT cannot be used with non - Oracle DB).
Any notice will be welocome.
Thank you
Federico
The query should be run in a managed bean...
Much better to execute the query in the template project.
Example query: http://amit-adf-work.blogspot.com/2013/09/how-to-execute-sql-using-adf-application.html
If I can create a function that creates / runs the VO on the fly and return the value ID... It is best to create it in a class of public services or in the implementation class TableVO?
If you have the function that accepts the SQL as a parameter, then this should go to class util.
Dario
-
Is it possible to change the number of lines to display in the query of the adf?
Hello
is it possible to change the number of lines that appear in a query of the adf that is similar to a form of the adf?
I need about 5 lines per column rather than display all the fields in a single column to display? Thank you.
Hello
What do you mean that it didn't work? Attributes of how you have in the Panel of the query? Maybe you have fewer number than the combination or lines and the properties of the argument maxColumns attributes.
Try again with
and see if you are able to get everything under a single column. If you get, play with these two properties to get the desired result.
Arun-
-
Select the query with the level line list where the clause
Hi all
I am creating a tabular presentation based on a SQL query that has a list of selection based on a query with a where clause clause that refers to a column in the original SQL query.
The situation is, I have a table that stores the client_id, source_id, and build_id, lets call it client_source. I have a second table, build_source, containing source_id and build_id, one to many relations between the two (1 source_id could have build_id 1-7).
Using a tabular presentation, I want to select the correspondent build_id to use in client_source, but the selection list should contain only the build_id for this particular source_id of lines.
Here is an example of the SQL source of tabular presentation;
Select
s.ROWID,
s.CLIENT_ID,
s.SOURCE_ID,
APEX_ITEM. SELECT_LIST_FROM_QUERY (1, s.BUILD_ID,)
"Select display b.build_id, b.build_id return.
b build_source where b.source_id = s.SOURCE_ID ') lst
of s client_source
what I want to achieve, it's as source_id "BOLD" match fields. When the query is built this way, I get an error of "invalid identifier" Oracle on s.SOURCE_ID during execution.
Is there some special tags to be used to refer to the external column? I must be missing something because this looks like a pretty mundane problem.
I am running on 4.1.0.00.32, on an Oracle 10 g release 10.2.0.4.0 Server Express request.
I look forward to useful responses!
See you soon,.
Jason
Published by: 1005131 on May 9, 2013 19:02Your selection by query list receives a static SQL. That SQL can't "see" the value of your s.source_id.
But it would work like this:
where b.source_id = ' | s.SOURCE_ID)You would be the value for the SQL concatenation. It is not ideal, but it will work.
Jorge
-
Insert a blank line in the query output
Is it possible to insert a blank line between two rows of the query output?
In the EMP table, the column LINES is as follows:
LINES
1
2
3
4
1
2
3
1
2
3
4
5
I want to insert a blank line after every 1
Please suggest.
SanjayI'm quite puzzled why you need rows in the dataset returned by a select statement that is empty.
If you are using Oracle reports to the release date, you define the presentation of the report to have spaces between the rows returned by the select and have do not need to have the select statement returns a blank line after each line of data. If the output should be obtained by using another client, I think that the customer will have facilities for output formatting.Admit that you really need "blank lines" between data lines, I see only one way: use a temporary table and do an insert into select temporary_table (...) ... from... where... Then, in this temporary table, you can insert 'empty rows', which is actually not really empty, because they must have some fields that give the order of the rows. And then select in temporary_table, you place your order by the columns that are used to give the order for the release.
Thusinsert into temporary_table ( .... ) select ... from ... where ... ; for r in ( select col1, col2, col3 -- columns used for order by from temporary_table order by col1, col2, col3 ) loop insert into temporary_table (col1, col2,col3, col4) values (r.col1,r.col2,r.col3, null); end loop; -- this way, a select * from temporary_tabel order by col1, col2, col3, col4 will return a blank row after each data row
-
Problems with lines due double date/time in the query
Hello
I'm trying to draw a list of the counties of the test sample. I want only 1 rank by the test result so that I can build a report in Cognos showing the rate of an outcome determined as a percentage of the total causes a range of dates.
My problem is that I get duplicate for the same result rows: in the example below, I have 2 to 5 lines for each result instead of only 1 for each result. I have this reduced up to caused by the date - the date is the same, but the times are different, if several lines are drawn. I tried to use tronque(), among other things, but although the output now shows the same date without the time part, duplicate rows are always drawn.
Any help would be appreciated!
This is the code for the query:
SELECT DISTINCT io.lab_id AS "LAB ID", l.name AS "LAB NAME", mtr.marketingtest_id AS "TEST ID", mtl.name AS "TEST NAME", mtr.marketingresult AS "MARKETING RESULT", COUNT(DISTINCT mtr.sample_id) AS "NO OF SAMPLES", TRUNC(os.statusdate) AS "STATUS DATE", os.orderstatuscodes_id AS "ORDER STATUS", io.redoorderid AS "REDOORDERID", mtl.locale_id AS "LOCALE" FROM igen_sample s INNER JOIN igen_igenityorder io ON s.igenityorder_id = io.igenityorder_id INNER JOIN igen_orderstatus os ON io.igenityorder_id = os.igenityorder_id INNER JOIN igen_marketingtestresult mtr ON s.sample_id = mtr.sample_id INNER JOIN igen_lab l ON io.lab_id = l.lab_id INNER JOIN igen_marketingtestlocale mtl ON mtr.marketingtest_id = mtl.marketingtest_id WHERE mtl.locale_id = 1 AND os.orderstatuscodes_id IN (8, 9) AND mtr.marketingtest_id = 84 AND os.statusdate BETWEEN '24-OCT-2011' AND '31-OCT-2011' GROUP BY mtr.marketingtest_id, mtl.name, mtr.marketingresult, os.statusdate, os.orderstatuscodes_id, io.lab_id, l.name, io.redoorderid, mtl.locale_id ORDER BY mtl.name
Hello
893078 wrote:
HelloI'm trying to draw a list of the counties of the test sample. I want only 1 rank by the test result so that I can build a report in Cognos showing the rate of an outcome determined as a percentage of the total causes a range of dates.
My problem is that I get duplicate for the same result rows: in the example below, I have 2 to 5 lines for each result instead of only 1 for each result. I have this reduced up to caused by the date - the date is the same, but the times are different, if several lines are drawn. I tried to use tronque() among others...
TRUNC looks like what you want. If you want a single line per calendar day, or need to GROUP BY TRUNC (os.statusdate).
AND os.statusdate >= TO_DATE ( '24-OCT-2011' , 'DD-MON-YYYY' ) AND os.statusdate < TO_DATE ( '31-OCT-2011' , 'DD-MON-YYYY' ) + 1 GROUP BY mtr.marketingtest_id , mtl.name , mtr.marketingresult , TRUNC (os.statusdate) -- <== CHANGED , os.orderstatuscodes_id , io.lab_id , l.name , io.redoorderid , mtl.locale_id ORDER BY mtl.name
If os.statusdate is a DATE, don't try to compare it to a VARCHAR2, as 24 October 2011 "." Compare DATEs to other DATEs.
Furthermore, GROUP BY product automatically separate results. If all GROUP expressions in the SELECT clause, there is not need to say SELECT DISTINCT .
Published by: Frank Kulash, November 23, 2011 09:34
-
How to automatically add the line number in the query
Hello
Can anyone please glimpses me the path to achieve this?
I want to write a query that will search the record based on the line number in the result set.
Let's say I have 500 rows in the employee table, but I want to put a condition on request where will see record only between 100 to 200 line number in the result set.
Thank you
Madam.It is wrong to say that look for the lines between ROWNUM 100 to 200 unless you define a column of lines must be ordered.
Oracle does not guarantee that the lines will be be returned in the same order if you run the query several times without defining the ORDER BY clause.
So first decide on what columns you want to order the files and then use the rownum to assign the line number and then get the records you want.
something like that
SQL>SELECT * 2 FROM ( SELECT e.* 3 ,ROW_NUMBER() OVER (ORDER BY sal) row_num 4 FROM emp e 5 ) 6 WHERE row_num BETWEEN 5 AND 10; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ROW_NUM ---------- ---------- --------- ---------- --------- ---------- ---------- ---------- ---------- 7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30 5 7934 MILLER CLERK 7782 23-JAN-82 1300 10 6 7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30 7 7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30 8 7782 CLARK MANAGER 7839 09-JUN-81 2450 10 9 7698 BLAKE MANAGER 7839 01-MAY-81 2850 30 10 6 rows selected. SQL>
Concerning
Arun -
The query results: extract all lines?
I was looking for a feature that will allow me to choose to return "all records" in the query results window. The current behavior is fetching 50 files at a time (for example fetch 50 records when you scroll the results = s-l-o-w). What I would do, that is 'Recover all lines' and then use for the scroll bar to scroll "smoothly" to the last record. For example, the 8.0.6 Query Builder version has this feature and SQL Developer 3.0, if I right click on the results. I choose 'Count lines... ". ", looks like it would be a perfect place to put an option"retrieve all rows.If your application is to be able to quickly reach the last record in a query you can press Ctrl + Page Down (with emphasis in the grid of results) and you will automatically position your view to the last record in the query automatically go get all the results in the process.
You have no need to do anything with the mouse scroll is actually the fastest way to reach the desired line.
-
Query running Discoverer Desktop... via the command line
Hello world
Hope all is well,
I learned how to operate the discoverer by using the command line.
My question is:
===========
When you connect manually in Discoverer, the values of parameters that were used last in the last series display when
the query opens...
When I run the command of discoverer line, I have to mention explicitly all parameter names and their values
If I do not mention the settings, I get an error message: unresolve parameter...
I would really like to run the command-line style WITHOUT mentioning query parameters and wish the discoverer of run
the application with the default settings...
Is this possible...
Thx for your ideas and your advice, sandraHello
How parameters could be made optional in the Office?
It can be made using desktop only, there is no functionality of optional parameters.
If you have a single desktop, you will need to set the parameters in the command line.Tamir
-
Number of lines on the query and export are different?
Hello
I'm kinda new to SQL Developer, but so far it has been very well. I came across a problem, but with a certain query I count the lines and get about 11 000 (which is what I expected), and then export the data to a csv file to download on another system, and somehow the number of lines is increased to 30,000? I tried several times and still get the same results, and I know that the extra lines should not be returned to request bythe they fail when uploaded to the other system.
Does anyone have any idea why this could be? The query is a union query, but it was fine before. Looks like a bug to me, but then again that I am a newbie.
Thank you
SteveThe recordtypeextract r table is not related to any other table. Link with another table and avoid the Cartesian join.
-
Get the "selected lines" value after sql custom query in the database adapter
We execute a custom query in a database adapter. With other technologies (JDBC, etc.) the result of a query produces not only a game but the number of lines selected results. For example, the output of the script of Oracle SQL Developer will display something like:
9999 selected lines
at the end of the output of the script.
I would like to obtain this value for a BPEL process. Of course, I could run a 2nd interview whose where clause is identical to the first, but select count (*) instead. However, it is not only ineffective but (in rare circumstances in an active database) could be the problem.
It is not obvious to the novice BPEL how to get the selected value and the result set row in an Invoke. Is this possible?Maybe it's not the only solution - but this is how I did in the past.
After step to invoke the adapter, use a step of the having-right/copy to fill a variable of integer type BPEL using the xpath count function...
That should affect the number of records returned by the query.
I hope this helps.
Rod.
Published by: Tunnels of Rod on December 15, 2009 13:16
-
How to find total lines read by the query
HII all
I'm trying to find out the number of records their by read by my request.
I also tried count (*) but it gives me correct result
I want to use the number of lines in my procedure then, how do I use it.
Can I use number of lines in the loop.
so that I can do the math on each result of a line
Thanking you in advance
Concerning
GauravSIf you want to know the total number of rows returned by a query, and you want to know it at the time that the first line is read so that you can use it in a kind of calculation, you would be better using the analytical function of the account...
SQL> select empno, sal, count(*) over () as cnt 2 from emp; EMPNO SAL CNT ---------- ---------- ---------- 7369 800 14 7499 1600 14 7521 1250 14 7566 2975 14 7654 1250 14 7698 2850 14 7782 2450 14 7788 3000 14 7839 5000 14 7844 1500 14 7876 1100 14 7900 950 14 7902 3000 14 7934 1300 14 14 rows selected. SQL>
If you try to use rowcount % while in a cursor loop, then you will find that it will only return how many rows have been retrieved at that time here...
SQL> ed Wrote file afiedt.buf 1 declare 2 cursor cur_e is 3 select empno, sal, count(*) over () as cnt 4 from emp; 5 begin 6 for e in cur_e 7 loop 8 dbms_output.put_line(to_char(e.empno)||','||to_char(e.sal)||','||to_char(e.cnt)||','||to_char(cur_e%rowcount)); 9 end loop; 10* end; SQL> / 7369,800,14,1 7499,1600,14,2 7521,1250,14,3 7566,2975,14,4 7654,1250,14,5 7698,2850,14,6 7782,2450,14,7 7788,3000,14,8 7839,5000,14,9 7844,1500,14,10 7876,1100,14,11 7900,950,14,12 7902,3000,14,13 7934,1300,14,14 PL/SQL procedure successfully completed. SQL>
-
Add lines to the query result?
Is it possible to add lines to the result of a shutdown of the SQL query, similar to the CALCULATION command in SQL * more?
In other words, for example, when A column value changes, add a line after the previous line (the last one before changing the column A) to display the sum of the values in column B.
for example
< PRE > A and B
- --
1 3
1 4
1 5
12
2 9
2 1
2-7
1 < / PRE >SQL> create table sales (person,city,sales) 2 as 3 select 'Alice', 'Phoenix', 19 from dual union all 4 select 'Alice', 'Tulsa', 11 from dual union all 5 select 'Bob', 'Phoenix', 17 from dual union all 6 select 'Bob', 'Tulsa', 9 from dual union all 7 select 'Tony', 'Miami', 5 from dual union all 8 select 'Tony', 'San Francisco', 4 from dual union all 9 select 'Tony', 'San Francisco', 3 from dual 10 / Tabel is aangemaakt. SQL> select case grouping_id(person,city,rowid) 2 when 1 then 'Sub-Total for' 3 when 3 then 'Sub-Total for' 4 when 7 then 'Grand Total' 5 end notes 6 , person 7 , city 8 , sum(sales) sales 9 from sales 10 group by rollup(person,city,rowid) 11 having grouping_id(city,rowid) != 1 12 or count(*) > 1 13 order by person 14 , city 15 , grouping(rowid) 16 / NOTES PERSO CITY SALES ------------- ----- ------------- ---------- Alice Phoenix 19 Alice Tulsa 11 Sub-Total for Alice 30 Bob Phoenix 17 Bob Tulsa 9 Sub-Total for Bob 26 Tony Miami 5 Tony San Francisco 4 Tony San Francisco 3 Sub-Total for Tony San Francisco 7 Sub-Total for Tony 12 Grand Total 68 12 rijen zijn geselecteerd.
Kind regards
Rob. -
Run the query to separate lines
Hi all
I have a button with the code "Execute_query", but the table contains duplicate, and I want to retrieve only the separate lines?
Thank you
Try this in trigger WBP:
GO_BLOCK ("dept1");
CLEAR_FORM;
SET_BLOCK_PROPERTY ("dept1', QUERY_DATA_SOURCE_NAME, ' (SELECT DISTINCT * FROM dept1)');
EXECUTE_QUERY;
Kind regards
Zlatko
Maybe you are looking for
-
Hello We can use for loop to run the code. How the channel names can be assigned to the variable? Call ChnConcat("[2]/[2]","[1]/[2]") Call ChnConcat("[2]/[6]","[1]/[2]") Call ChnConcat("[2]/[10]","[1]/[2]") Call ChnConcat("[2]/[14]","[1]/[2]") Call C
-
my product key is wrong according to microsoft word, help
Im trying to enter my product key for microsoft word and he always tells me that it is evil. I got it on the label under my laptop... Please help
-
I have Windows Vista and when you try to check the security updates "found error code 0xc8000222 happens. How can I fix in order to check and download updates of security? Thank you.
-
I have a Dungeon problem on my desktop screen. "Dell Data Safe Local back upward, an Unhandled Exception has occurred, Application will exit.
-
computer HP mini 110 won't boot recovery disc
HP mini 110 dosnt have embedded dvd player and will not load the dvd collection I have orderd from hp through the external dvd player. is it possible to change the file system of the dvd and put it on a USB key? or is it possible to get the recovery