SELECT query problem
Hi allNeed select records less where Rownum < = 100, a table in which the records are classified by a field in a query, but after stopped in the ordering of rownum gets changes.
with t as (select 1 a,'d' b from dual
union select 8 a,'t' b from dual
union select 3 a,'u' b from dual
union select 7 a,'k' b from dual )
select t.*,rownum from t order by 2 ;
A B ROWNUM
1 d 1
7 k 3
8 t 4
3 u 2
Now I want to select records where ronum < = 2 as below, this can be done by changing only in where clause and do not touch the query select, as can do a nested query I but need to do it by changing only in where clause.
A B ROWNUM
1 d 1
7 k 3
Thanks in advanceThanks and greetings
Sanjay
Published by: Sanjay March 29, 2011 06:26
Hi, Sanjay
Sanjay wrote:
Hi allNeed select records less where Rownum<= 100,="" from="" a="" table="" in="" which="" records="" are="" ordered="" by="" a="" field="" in="" a="" query="" but="" after="" doing="" order="" by="" the="" rownum="" ordering="" gets="">=>
with t as (select 1 a,'d' b from dual union select 8 a,'t' b from dual union select 3 a,'u' b from dual union select 7 a,'k' b from dual ) select t.*,rownum from t order by 2 ; A B ROWNUM 1 d 1 7 k 3 8 t 4 3 u 2
Now I want to select records where ronum<= 2="" like="" below,="" can="" this="" be="" done="" by="" changing="" only="" in="" where="" clause="" and="" not="" touching="" the="" select="" query="" as="" it="" can="" be="" done="" by="" nested="" query="" i="" but="" need="" to="" do="" it="" by="" changing="" only="" in="" where="">=>
A B ROWNUM 1 d 1 7 k 3
No, there is no way to get these results by changing only the WHERE clause. As it is, you have no column or expression in the main query that indicates how the lines are classified according to b, which is exactly what to test in the WHERE clause. (Remember, ROWNUM is assigned before ORDER BY is done in the same auxiliary request). You must add a using ROWNUM or ROW_NUMBER analytic function. either way requires some preparation to be done in a subquery.
For example, the following query has an ORDER BY clause, added to the request slot, so that ROWNUM in the main query will be more arbitrary:
with t as (select 1 a,'d' b from dual
union select 8 a,'t' b from dual
union select 3 a,'u' b from dual
union select 7 a,'k' b from dual
ORDER BY b -- Added
)
SELECT t.*
, ROWNUM
FROM t
WHERE ROWNUM <= 2 -- Added
ORDER BY b
;
Output:
` A B ROWNUM
---------- - ----------
1 d 1
7 k 2
It is no coincidence that the numbers in the column ROWNUM are 1 and 2. If you want really to arbitrary numbers (such as 1 and 3), you will need an another subquery.
Tags: Database
Similar Questions
-
Hi friends,
I have a view called 'risk_efforts' with the field user_id, user_name, wknd_dt, Etiquettemois, prod_efforts, unprod_efforts.
Name Type
-------------- -------------
ROW_ID NUMBER
USER_ID VARCHAR2 (14)
VARCHAR2 (50) USER_NAME
WKND_DT VARCHAR2 (8)
ETIQUETTEMOIS VARCHAR2 (250)
NUMBER OF PROD_EFFORTS
NUMBER OF UNPROD_EFFORTS
data are like this:
When there is some data in prod_efforts, unprod_efforts will be null
When there is some data in unprod_efforts, prod_efforts will be null
for example:
USER_ID, USER_NAME WKND_DT ETIQUETTEMOIS PROD_EFFORTS UNPROD_EFFORTS
G666999 20100403 GTest KILLS null 3
G666999 GTest 20100403 Mar 14 null
now I want to combine these 2 rows in i.e 1 row o/p should be like this
USER_ID, USER_NAME WKND_DT ETIQUETTEMOIS PROD_EFFORTS UNPROD_EFFORTS
G666999 20100403 GTest KILLS 14 3
I tried all combinations but couldn't make the query. Please help me with the exact SQL select query.
Thank you
GirishWelcome to the forum.
First read this:
Second, it is always helpful to provide the following information:
1. oracle version (SELECT * FROM V$ VERSION)
2. examples of data in the form to CREATE / INSERT commands.
3. expected results
4 explanation of the expected results (alias "business logic")
5. use.tags for #2 and #3. See FAQ (Link on top right side) for details. You have provided #3 and #4. However with no usable form of sample data forum members will often not respond as quickly as they could if you provided #2. I'm just wagering a guess here but what about this:
SELECT ROW_ID
USER_ID
WKND_DT
ETIQUETTEMOIS
MAX (PROD_EFFORTS) AS PROD_EFFORTS
MAX (UNPROD_EFFORTS) AS UNPROD_EFFORTS
OF RISK_EFFORTS
ROW_ID GROUP
USER_ID
WKND_DT
ETIQUETTEMOIS -
Hello
Oracle version: 11g R2 (11.2.0.2)
I get the following error message (I'm using TOAD for Oracle last version 12.1):
ORA-12801: error reported in the parallel query P032 Server
ORA-01114: block write IO file error (block #)
ORA-01114: IO error to the file 201 (block # 2165248) writing block
ORA-27072: IO file error
Linux-x86_64 error: 28: no space is available on the device
Additional information: 4
When you run a SELECT query:
SELECT field1, Field2...
OF (REVENUE_SOURCE b
INNER JOIN ACCOUNTS_CLASSIFICATION one
WE b.SUB_ACCOUNT = a.SUB_ACCOUNT)
INNER JOIN REPORT_PRODUCT_ID c
ON b.REPORT_PRODUCT_ID = c.REPORT_PRODUCT_ID
WHERE a.UC_BUSINESS_UNIT = 'ANX.
AND b.report_mm > =.
(SELECT ADD_MONTHS (MAX (report_mm),-13))
OF REVENUE_SOURCE)
AND b.REVENUE_FLAG = - 1
AND (b.CALLS <>0 b.MINUTES <>0 b.CHARGE_AMOUNT GOLD <>GOLD 0);
Please notify.
NOTE: the query worked well for a while, no errors, then all suddenly it became much accentsr and then I started to get the error message above.
Kind regards
M.R.
Hello
ORA-27072: IO file error
Linux-x86_64 error: 28: no space is available on the device
Looks like a system or OS, no SQL or PL/SQL problem problem. I think you will answer sooner if you check this issue as 'Response' and start a new question in the forum of "General Question":
https://forums.Oracle.com/community/Developer/English/oracle_database/general_questions/content
My guess is that Oracle tries to allocate space in the tablespace temp, but cannot because the disk is full. If you have a full disk, then create the space to this topic.
I see nothing wrong with your code; Certainly, nothing which would cause an error like that. The fact that the query has worked well for some time confirms that there is noting wrong with the request itself; It is just triggering a mistake elsewhere.
-
Sinlge select query in the diff for the same table (same Structure) diagrams
Scenario:
Table XYZ is created in detail a.
After a year, the old data of the previous year could be moved to another schema. However in the other schema of the same table name would be used.
For example
A schema contains XYZ table with data from the year 2012
Schema B contains XYZ table with data for the year 2011
Table XYZ in the two schemas have an identical structure.
So we can draw a single select query to read the data from the tables in an effective way.
For example select * from XYZ so including date between October 15, 2011 to March 15, 2012.
However, the data resides in 2 different schema altogether.
Creating a view is an option.
But my problem, there are ORM (Hibernate or Eclipse Top Link) layer between the application and the database.
If the queries would be constituted by the ORM layer and are not generated by hand.
So I can't use the view.
So is there any option that would allow me to use only query on different scheme?970773 wrote:
Scenario:Table XYZ is created in detail a.
After a year, the old data of the previous year could be moved to another schema. However in the other schema of the same table name would be used.For example
A schema contains XYZ table with data from the year 2012
Schema B contains XYZ table with data for the year 2011
Table XYZ in the two schemas have an identical structure.So we can draw a single select query to read the data from the tables in an effective way.
For example select * from XYZ so including date between October 15, 2011 to March 15, 2012.
However, the data resides in 2 different schema altogether.Creating a view is an option.
But my problem, there are ORM (Hibernate or Eclipse Top Link) layer between the application and the database.
If the queries would be constituted by the ORM layer and are not generated by hand.
So I can't use the view.Why not make the ORM as below?
SELECT * FROM VIEW_BOTH;
-VIEW_BOTH is a real VIEW of Oracle -
How to store results of the select query in the tables.
I created a variable varray type and now want to assign some data of output of the select query in pl/SQL, as well as in reports 6i.You are in the wrong forum (this is for problems with the SQL Developer tool). You were the one where you have published first on the right, but not to reuse independent threads as you did.
Kind regards
K. -
What is the syntax for creating a global temporary table using a select query
HII
I create a global temporary table using a select query... How to speak of "on commit preserve rows ' who?
create a table temporary global t1 select * from trn_ordbase on the lines of commit preserve;
but this is an invalid syntax, then how to talk on commit preserve rows in this? If I don't mention, by default its recital on the validation of deleted rows.
Please help me on this problem.create global temporary table t1 on commit preserve rows 2 as select * from dual; Table created. TUBBY_TUBBZ?
-
Hi guys
I get problem in my select query that creates a long string, it works very well without a prescription but when I run this order that she be hanged. This request for help on the function to concatenate multiple strings into one string as well
Please help me what I'm doing wroing.
Select
regexp_substr(PE.emp_id,'[0-9]+$') |' | ' ||
p.title |' | ' ||
CDT. Text: ' |' |
j.tit | staff_pcb.host_pub_tit |' | ' ||
jou.vol | PCB.vol | PCP.vol |' | ' ||
jou.journal_number |'| ' |
substr (jou.pages |) PCB.pages | PCP.pages, 1, instr (jou.pages |) PCB.pages | PCP.pages, '-')-1) |' | ' ||
substr (jou.pages |) PCB.pages | PCP.pages, instr (jou.pages |) PCB.pages | PCP.pages, '-') + 1) |' | ' ||
PCB.St | jou. St | PCP.St |'| ' |
concat_aut (p.ID)
Pub p, j, caa c, pe, cdt class_t,.
(select pcj.pub_id, pcj.vol, pcj.pages, pcja.st
Of pub_C_j_a airports, JCP pc_j
where pcja.pub_c_j_id = pcj.pub_id) played.
(select pcb.pub_id, pcb.host_pub_tit, pcb.pages, pcb.vol, pcba.st
Pub_c_b pcb, printed circuit board pub_c_b_a
where pcba.pub_c_b_id = pcb.pub_id) pcb.
(select pcp.pub_id, pcp.vol, pcp.pages, pcpa.st
Of pcp, pub_c_p_a LPA pub_c_p
where pcpa.pub_c_p_id = pcp.pub_id) CFP,.
where
p.ID = c.pub_id
and c.person_id = pe.id
and p.t_c_id = cdt.c_id
and p.journal_id = j.id (+)
and p.id = jou.publication_id (+)
and p.id = pcb.publication_id (+)
and p.id = pcp.publication_id (+)
and substr (regexp_substr(pe.emp_id,'[0-9]+$'), 1, 2) in('77','78')
and pcb.st. jou. St | PCP.St = '1' order of pe.emp_id
;Here's something
regexp_like(emp_id,'(^|[^0-9])(77|78)[0-9]*$')
work instead?
Not sure if this should use an index no longer however.
Kind regards
BobPublished by: BobLilly on March 25, 2010 14:56
Published by: BobLilly on March 25, 2010 14:58
-
SELECT query time-out - huge table
DBA dear friends,
DB version 11.1.0.7. I have a SELECT query that is running long and wedging on. Query joins the 2 tables. It is partitioned hash (16 sheets) and others are not partitioned, each table is with the same volume of data - 230 million lines.
> Optimizer stats are not outdated
SELECT only 1 row (as indicated in the PLAN of EXPLAIN) should be fast.
-
Can I use * and the list of the column names in a select query
PLSQL again. Can I use * and the list of the column names in a select query, i.e. Select *, col1, col2 from Mytable. When I do that my questions tend to the bomb. Can do in other databases.
Thank you
Mike
Hi, Mike,.
If there is something else in the more SELECT clause *, then * must be qualified with a table name or alias.
For example:
SELECT Mytable. *, col1, col2
FROM MyTable;
-
place a select query calculation in a different column in the same table
How can I put my calculation result in a column named within the same table?
I have a table called: dgpercentagedatachart
I use the columns of this dgpercentagedatachart: totalcecrating divided by lowestfeederrating times 100 to get the percentage
In the query, I gave the result the Alias of the cal
What I want is to put this result in my application or my calculation (in percentage) in my column "percent" on my table of dgpercentagedatachart vacuum.
How can I configure this syntax?
This is the select query, I came with:
Select dgpercentagedatachart.totalcecrating, dgpercentagedatachart.lowestfeederrating,.
100.00*dgpercentagedatachart.totalcecrating/dgpercentagedatachart.lowestfeederrating as cal
of dgpercentagedatachart;
Here are the results:
CAL lowestfeederrating Totalcecrating
8,978 7.48166666666666666666666666666666666667 120
30.951 25.7925 120
5.04 120 4.2
Hello
2685870 wrote:
How can I put my calculation result in a column named within the same table?
I have a table called: dgpercentagedatachart
I use the columns of this dgpercentagedatachart: totalcecrating divided by lowestfeederrating times 100 to get the percentage
In the query, I gave the result the Alias of the cal
What I want is to put this result in my application or my calculation (in percentage) in my empty column '%' on my table of dgpercentagedatachart .
How can I configure this syntax?
This is the select query, I came with:
Select dgpercentagedatachart.totalcecrating, dgpercentagedatachart.lowestfeederrating,.
100.00*dgpercentagedatachart.totalcecrating/dgpercentagedatachart.lowestfeederrating as cal
of dgpercentagedatachart;
Here are the results:
CAL lowestfeederrating Totalcecrating
8,978 7.48166666666666666666666666666666666667 120
30.951 25.7925 120
5.04 120 4.2
To change an existing column in a table, you can use the UPDATE or MERGE, instructions like this:
UPDATE dgpercentagedatachart
Percentage of VALUE = 100,00 * totalcecrating
/ lowestfeederrating
;
Noramlly, tables is not redundant columns like this. If percent can always be calculated from other columns, then it is probably better to calculate at run time and store it in the database, where you will have to be updated each time the columns it depends on change. You can use a view to avoid having to encode them the calculation.
If you really don't want a column that can be calculated in the same lines other columns, then use a virtual column (if you use Oracle 11 or higher).
I hope that answers your question.
If this isn't the case, please post a small example of data (CREATE TABLE and only relevant columns, INSERT statements) for all of the tables involved and also publish outcomes from these data.
If you ask on a DML statement, such as UPDATE, the sample data will be the content of the or the tables before the DML, and the results will be the State of the or the tables changed when it's all over.
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?
-
Hello
First of all, I had two tables. The first PARAMETER called that inculdes three columns parameter_id (number), the parameter_desc (varchar2 (20) and (number) parameter_value.) The parameter_id is a PK. The INSERT statement might like this:
Enter the parameter (parameter_id, parameter_desc, parameter_value) values (1, 'TEST1', 233);
Enter the parameter (parameter_id, parameter_desc, parameter_value) values (2, 'TEST2', 456);
Insert in the parameter (parameter_id, parameter_desc, parameter_value) values(3,'TEST3',678);
And a second table, called TOPO_PARAM, which includes three columns (number) topo_id, parameter_id (number) and topo_param_value (varchar2 (10).) The PK is on topo_id and parameter_id. The insert for this one is
insert into topo_param (topo_id, parameter_id, topo_param_value) values (349.1, 'FALSE');
insert into topo_param (topo_id, parameter_id, topo_param_value) values (568,2, 'TRUE');
I want a select query that links the two tables and returns this:
If topo_id = 349
I wish to have this:
parameter_id parameter_value topo_param_value
1 233 'FALSE'
2 456 NULL
3 678 NULL
If topo_id = 568,
parameter_id parameter_value topo_param_value
1 233 NULL
2 456 'TRUE'
3 678 NULL
And if topo_id = 500,
parameter_id parameter_value topo_param_value
1 233 NULL
2 456 NULL
3 678 NULL
Thanks for your response!
SELECT T1. PARAMETER_ID,
T1. PARAMETER_VALUE,
T2. TOPO_PARAM_VALUE
THE PARAMETER T1 LEFT OUTER JOIN
TOPO_PARAM T2
ON T1. PARAMETER_ID = T2. PARAMETER_ID
TOPO_ID = & INP
ORDER OF T1. PARAMETER_ID;
OUTPUT:
Enter the value of the inp: 349
7 old: AND TOPO_ID = & INP
7 new: AND TOPO_ID = 349PARAMETER_ID PARAMETER_VALUE TOPO_
------------ --------------- -----
1 233 FALSE
2 456
3 678 -
Formatting of the Select query
The following select query returns the data in green. How do I format to return the path that it is formatted in red?
Basically, if FIELD1 is "Customer A" and FIELD2 is "Gas" then I back to 'New customer' Field1 and Field2 'gas '. If 'Customer E' FIELD1 and FIELD2 is 'Total' then it should return 'E rejection' in Field1 and 'Prod_Discharge' in the Field2.
Please let me know if my question is not sensible or need to be clarified.
SELECT
DECODE (Field1, 'Nine', ' A', 'Old', 'E', 'Term', 'F', Field1)
DECODE (Field2, 'Prod', 'Gas', Field2)
Customers
FIELD1 FIELD2
Customer A gas
Customer has oil
Total customers E
Customer F Total
Need to format as follows:
Field1 Field2
New gas customer
New customer oil
E Prod_Discharged releases
F Prod_Discharged releases
Try this:
SELECT CustomerID
, CASE WHEN field1 = 'Customer A' THEN 'new customer '.
WHEN field1 = 'Customer E' AND field2 = 'Total' THEN 'E releases '.
WHEN field1 = ' customer F ' AND field2 = 'Total' THEN 'F performs ".
Of ANOTHER Field1
FIELD1 AS END
, CASE WHEN Field1 IN ('E of customer', ' customer F ') AND field2 = 'Total' THEN 'Prod_Discharged '.
Of OTHER Field2
END AS FIELD2
Status
Customers
;
-
Hello
I have a query something like...
Select...
....
where
b.ProdName as 'GEAR of the TREE %' or b.prodname like '% FRONT GEAR %' or b.prodname like '% BREAK 4 %k '.
and
b.ProdName as
(select the name of the society of companyms
where < some comdition >
)
order of 3.6
I have blocked in:
b.ProdName as
(select the name of the society of companyms
where < some comdition >
)
How to give a select query with like operator.
In above query I want to b.prodname as above and the name of the company for which I give condition. Company names are stored in another table and I want to select and use them with like operator.
Thank you.
Hello
It is not clear what you want to do.
One possibility is an EXISTS subquery, like this:
...
WHERE (b.prodname LIKE "% of the CYCLE of the TREE" - begins with this text
OR b.prodname LIKE '% FRONT GEAR %' - contains this text
OR b.prodname LIKE '% BREAK 4 %k' - contains this text
)
AND THERE ARE)
SELECT 1
OF companynms
WHERE--a condition
AND b.prodname AS business
)
I hope that answers your question.
If not, post a small example of data (CREATE TABLE and only relevant columns, INSERT statements) for all of the tables involved and the results desired from these data.
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: https://forums.oracle.com/message/9362002
-
How to run a Select query stored in a Variable
Hello
I have a following requirement:
Select a query result on Var1, result of another request selection in Var2,
If Va2 = "value11" or Var2 = "Value2" then Var1 = "select query. Now how can I run this SQL at the end of the Pl/SQL query?
so I write request for the same thing:
DECLARE
qry nvarchar2 (500);
result nvarchar2 (500);
BEGIN
Select "choose TEXTVAL as 'CHARGER' FROM TABLE1 WHERE LOC =" [ParameterValue] "and KEYNAME =" < < REPLACE > > "' double INTO qry ;"
SELECT CASE County WHEN (RW." CountofBATCH') > 1 then "mixture."
WHEN count (RW." CountofMAT') = 0 then 'None '.
ANOTHER 'other '.
END
AS a result
TABLENAME2 TT, XMLTable ('/ sets of lines/lines/lines ' PASSAGE TT.) XMLCOL
COLUMNS
"CountofBATCH" PATH "/ row [CLABS > 0] / LOADED ',"
"CountofMAT" PATH "/ row [MATNR = '[parameter value]'] / MAST '"
) AS RW
where
TT.PL = '[parameter value]' and
TT. TANK = "[value settings]";
IF result = 'Mixture' result GOLD = 'None' THEN
qry: replace = (qry, "< < REPLACE > >", result);
on the other
qry: = 'nothing. '
END IF;
Thus the qry variable will have select statement. Now how can I use this to get the select result of this statement in the same query qry?You can use execute immediately if the output of the query is in the same query.
It's very simple.
a query string signle and then go like thisdeclare
qry VARCHAR2 (255);
result varcharf2 (2500);
number of vempid: = 1;Start
qry: =' select empname emp where empid =: empid ";run immediately qry in the result using vempid;
-now the result of data is as a result
end; -
Please suggest a select query / under query with using any routines or
source table: three columns ORIGIN, DESTINATION, MILES
Origin Destination Miles
Sydney Melbourne 1000
Perth, Adelaide 3000
Canberra Melbounre 700
Melbourne Sydney 1000
Brisbane Sydney 1000
Perth, Darwin 4000
Sydney, Brisbane 1000
out put: three columns ORIGIN, DESTINATION, MILES
Duplicate routes should be ignored, so the output is
Origin Destination Miles
Sydney Melbourne 1000
Perth, Adelaide 3000
Canberra Melbounre 700
Brisbane Sydney 1000
Perth, Darwin 4000
Please suggest a select query / under query with using subroutines or functions/pkgs to get put out table.Hello
user9368047 wrote:
... Please suggest a select query / under query with using subroutines or functions/pkgs to get put out table.Why? If the most effective way to achieve the desired results is to use a function, why would you not use it?
Here's a way, without all the features:
SELECT a.* FROM source_table a LEFT OUTER JOIN source_table b ON a.origin = b.destination AND a.destination = b.origin AND a.miles = b.miles WHERE b.origin > a.origin -- Not b.origin > b.origin OR b.origin IS NULL ;
If you would care to post CREATE TABLE and INSERT statements for your sample data, and then I could test this.
Published by: Frank Kulash, November 6, 2012 19:39
Fixed WHERE after MLVrown clause (see below)
Maybe you are looking for
-
HP Probook 470 G1 wireless problem
Hi, I just bought a HP Probook 470 G1. I installed Windows 7 Professional 64 bit and I cannot get my wireless to work card. The problem appears after an amount all or random that long, it can be 2 minutes or 1 hour 5 minutes. The problem arises when
-
Cannot get photosmart 5520 to print wireless from my Macbook pro
Hi- I tried for more than a month to get my computer to print wireless, and I can't make it work. It will print the test pages, but when I try to print anything else I get the message "Printing - the printer is not responding. I deleted and reinsta
-
connect a samsung SGH-A886 to computer
I'm trying to connect my Samsung SGH-A886 to my computer to download pictures from the Samsung phone. The PC studio software seems to be involved. I don't seem to be able to get the two pieces of material to talk... Any suggestions? Thanks RG
-
How to access the function of magnification?
How to access the magnifcation function?
-
Personal files changed to the hidden file system
Hi all I have a prroblem of 2 days. my personal files changed for the file system and hidden type and disappeared from the D partition and then uncheck the option that is (Hide protected files (recommended) operating system) files have appeared, but