Sql query to dynamically filter out records -help needed
Hi ,
I have a table with structure as below
Create table T1
(ID Number(5),
Action Varchar2(20)
Below are the table contents at differnt points in time and please help me with a query which should show the results as per the entries in the table dynamically.
Initail Table contents
1 Queued
2 Queued
Query Result should be
1 Queued
2 Queued
After an insert the Table contents
1 Queued
2 Queued
3 Ignored
Result
Nothing should be displayed
After an insert the Table contents
1 Queued
2 Queued
3 Ignored
4 Ignored
Result
Nothing should be displayed
After an insert the Table contents
1 Queued
2 Queued
3 Ignored
4 Ignored
5 Queued
Result
5 Queued
Thanks in advance!!
Best Regards,
Sridhar
Hi, Sridhar,
So, you want to display rows from t1 that come after the last row with action='Ignored', which means no output at all when the last row has action='Ignored'. (A row with a given id is considered to be "after" a row with a lower id. The row with the highest id is considered the "last" row.)
Is that right?
Here's one way:
SELECT *
FROM t1
WHERE id > (
SELECT MAX (id)
FROM t1
WHERE action = 'Ignored'
;This does not assume that id is consecutive integers.
If id is not unique, what results do you want? There's a chance that the query above already does it; if not, it can probably be modified.
Similar Messages
-
SQL Query criteria eliminates too many records
This SQL query results in zero out of 268 records due to the criteria of (((Products.ProductFamily)<=0))
SELECT images.url_img, images.alt_img, images.productid_img, Products.ProductFamily, Products.ID
FROM images LEFT JOIN Products ON images.productid_img = Products.ID
WHERE (((Products.ProductFamily)<=0));
However, if you change the <=0 to >0, the correct 140 out of 268 records show up. So if 140 records are greater than zero, shouldn't the remaining 128 records be less than or equal to zero? How do I get the results that are <=0 to show up?
FYI - Products.ProductFamily data type is numeric, field size is integer, decimal places is auto, default value is 0, required is No, indexed is NoYes, but can you still answer the question? I am trying to determine if the problem is in the join or the where clause. What happens if you remove the join and still include results from the product table? Can you give an example of data for a few rows (both tables) that are not working as expected?
-
SQL query to get last 10 records in the table?
Hi,
Can anyone tell me the SQL query to get last 10 records in the table?
Thanks!!
MCPPlease, define what "last" means. Sets are unordered by definition, so if you want to retrieve rows from a table in a specific order you need to specify what that order is - e.g. by maintaining a value in a column (or a combination of columns) that you can use in the ORDER BY clause of the SELECT statement.
If, for instance, you kept the time when the row was inserted in a special column (InsertedTime), you could use this in your query like this:
select top (10)
<column list>
from <table or view>
where <restriction(s)>
order by InsertedTime desc;
ML
Matija Lah, SQL Server MVP
http://milambda.blogspot.com -
Sql query hint - first filter , then join
hello
i have a following query (based on OWB data model views)
select /* + LEADING(all_iv_xform_map_components all_iv_xform_map_parameters) USE_NL(all_iv_xform_map_components all_iv_xform_map_parameters) */
comp.map_name,
comp.operator_type ,
param.map_component_name ,
param.parameter_name,
md.map_component_id,
md.map_component_name,
mp.MAP_COMPONENT_ID ,
mp.MAP_COMPONENT_NAME
from all_iv_xform_map_components comp,
all_iv_xform_map_parameters param,
ALL_IV_XFORM_MAP_PROPERTIES mp,
ALL_IV_XFORM_MAP_DETAILS md
where comp.map_name = nvl('&load_map_name','LOAD_MY_TABLE')
and param.map_component_id = comp.map_component_id
and param.map_component_id=md.map_component_id
and md.map_component_id=mp.map_component_id
what i want to achieve is to force the query to first filter out the table all_iv_xform_map_components and do the join with the remainign tables on the filtered rows only;
i tried using the above hint but its not working; oracle still grabs to the full table: ALL_IV_XFORM_MAP_PROPERTIES
id appreciate any tips
thanks
rgdsHI
for the sake of the case:
i ran your query and its giving the clob error
i hard coded the com id's and its not giving error anymore
WITH comp AS
( SELECT --+materialize
comp.map_name,
comp.operator_type,
comp.map_component_id,
param.map_component_name ,
param.parameter_name
FROM
all_iv_xform_map_components comp,
all_iv_xform_map_parameters param
WHERE
comp.map_name = nvl('&load_map_name','LOAD_MY_TABLE)
AND param.map_component_id = comp.map_component_id
AND param.map_component_id in ('3931622','3931625','3931624','3931623','3931626') --> without this line its giving clob error, but these are the only comp ids retured by that query
select
comp.map_name,
comp.operator_type ,
comp.map_component_name ,
comp.parameter_name,
md.map_component_id,
md.map_component_name,
mp.MAP_COMPONENT_ID ,
mp.MAP_COMPONENT_NAME
from
comp,
ALL_IV_XFORM_MAP_PROPERTIES mp,
ALL_IV_XFORM_MAP_DETAILS md
where
comp.map_component_id = md.map_component_id
and comp.map_component_id = mp.map_component_id
/ -
recording help needed. I'm a beginner, imac, logic express and helix board 12 firewire.
I have recorded my daughter voice only ok, BUT when i import audio file to track 1 as backing track and my daughter is using headphones from the helixboard to sing to she does not record to track 2 when all in enabled.
Should her headphone come from the imac and not the helix board
Regards
PaulCheck the recording channel in the channel strip.
It must be set the same as the audio interface channel.
cheers
rob -
SQL query as a filter in OBIEE
i have a list of student_ID in one table ( lets say A) and its subset in another table( table B)
i want a filter such that only the student_ID in table A which are not in table B are selected.
i am selecting operator "not equal to/ is not in" of the student_id in table A and choosing SQL query option for adding the value.
select distinct student_id from DatabaseName.B
Its not working. Its giving error after a lot of minor tweaks.
Can you help how to resolve this1.) Answers references RPD objects and NOT database objects. (ref. "select distinct student_id from DatabaseName.B"
2.)
- Create analysis (1) which retrieves all student_id's from your table B
- save
- Create analysis (2) which retrieves student_id's from table A
- Create a filter criteria on column student_id: column "student_id" -> Operator "is based on results of another analysis" -> Saved analysis = "/whereeveryoustoredthat/analysis (1)" -> Relationship "is not equal to any" -> Use values in Column "student_id"
3.) Bob's your uncle -
SQL Query to retrieve the All records based on the Max Dates.
Hello all,
I am trying to retrieve the newest record based on the date field ( nextDate ).
Currently there are only 4 records in the MC_Maintenance table and two in the Machine table.
Machine table
MC_id EquipID
1 0227
MC_id EquipID
2 0228
MC_Maintenance table
Maint_id MC_id Next_maint
1 2 08/25/2010
2 2 07/01/2010
3 1 06/11/2010
4 1 07/11/2010
What I am trying to accomplish is,
list the two machines from the Machine table with the MAX(Next_maint) controlling the MC_Maintenance output list
These are the records that I would like to Display.
Maint_id MC_id Next_maint
1 2 08/25/2010
4 1 07/11/2010
Below is the SQL Query
SELECT
MC.MC_ID as ID,
MC.complete_Date as completed,
MC.next_maint as nextDate,
MC.maint_notes as Notes,
MC.facility as Facility,
M.EquipId,
M.name as name,
M.SerialNumber as SN,
M.dept as dept,
M.Freq as freq
From MC_Maintenance MC, Machine M
where MC.MC_ID = M.MC_ID
' USING MAX(nextDate )
Any ideas would help.
TJI would have thought that was a simple group by problem?
SELECT M.EquipID, MC.MC_ID, Max(MC.next_maint)
FROM MC_Maintenance MC INNER JOIN Machine M ON MC.MC_ID = M.MC_ID
GROUP BY M.EquipID, MC.MC_ID -
Sql query with dynamic fiscal year
Hi,
I wrote this query with static fiscal year and fiscal period, I need info on making the variables dynamic
Fiscal year : starts from July1st. So this year until June'30 it is '2011' and from July'1st its '2012'
Fiscal period: July1st its '1' and June'1st its '12'
Query:
select distinct o.c_num, o.ac_num, s.sub_ac_num, o.fiscal_year, o.ac_exp_date, s.sub_ac_ind
from org_account o
left outer join sub_account s
on o.c_num = s.c_num and o.ac_num = s.ac_num
where (o.ac_exp_date >= CURRENT_DATE or o.ac_exp_date is null)
and o.fiscal_year = *'2011'* --> need to be dynamic
and o.fiscal_period = *'12'* --> need to be dynamic
thanks,
Mano
Edited by: user9332645 on Jun 2, 2011 6:55 PMHi, Mano,
Welcome to the forum!
Whenever you have a question, please post a little sample data (CREATE TABLE and INSERT statements), and the results you want from that data.
Always say which version of Oracle you're using.
Since this is your first thread, I'll post some sample data for you:
CREATE TABLE table_x
( dt DATE
INSERT INTO table_x (dt) VALUES (DATE '2010-12-31');
INSERT INTO table_x (dt) VALUES (DATE '2011-01-01');
INSERT INTO table_x (dt) VALUES (DATE '2011-06-02');
INSERT INTO table_x (dt) VALUES (DATE '2011-06-30');
INSERT INTO table_x (dt) VALUES (DATE '2011-07-01');Is this the output you would want from that data?
DT FISCAL_YEAR FISCAL_PERIOD
31-Dec-2010 2011 06
01-Jan-2011 2011 07
02-Jun-2011 2011 12
30-Jun-2011 2011 12
01-Jul-2011 2012 01If so, here's one way to get it:
SELECT dt
, TO_CHAR ( ADD_MONTHS (dt, 6)
, 'YYYY'
) AS fiscal_year
, TO_CHAR ( ADD_MONTHS (dt, 6)
, 'MM'
) AS fiscal_period
FROM table_x
ORDER BY dt
;Since your fiscal year starts 6 months before the calendar year, you need to add 6 months to the actual date to get the fiscal year and month.
The query above produces strings for fiscal_year and fiscal_period. If you'd rather have NUMBERs, then use EXTRACT instead of TO_CHAR:
SELECT dt
, EXTRACT ( YEAR
FROM ADD_MONTHS (dt, 6)
) AS fiscal_year
, EXTRACT ( MONTH
FROM ADD_MONTHS (dt, 6)
) AS fiscal_period
FROM table_x
ORDER BY dt
;The first query will work in Oracle 6 (and higher).
I'm not sure when EXTRACT was introduced. It definitely works in Oracle 10, and may be available in earlier versions, too. -
Handling sql query in jsp while extracing records from 3 tables
hi to one and all,
i want to implement the sql query given below in jsp in execute query statement . the query is working in sql but not when implemented using jsp. please help me in resolving this.
Query
SELECT e.Department , e.ETitle , s.basic , s.da, c.address1 , c.address2 from Employee e , Salary s , Contactdetails1 c where e.id = s.id and e.id =c.idIt would help if you gave us a little more info to go on like;
a) post your code
b) post the error message the compiler is giving you -
Sql query to Load only new records,or update old records
Hi,
I need a query (not stored procedure) to insert only new records otherwise update existing records.
I tried by creating "Merge statement in SQl", but the sql query is wrong,it is not updating,always inserting records(replicating..1,2,4,8,16,32..).
below is my sample query,
Here "FACT_mytbl -FCT" is my fact table.(which need to update if already records found ,otherwise insert as new records).
*Inside select ---the table E_tbl1,E_tbl2 is the business logic table ,using this two table only ,"FACT_mytbl" was created .So the "S" alias will be the source
table and "FCT" will be target table. based on this we have to insert or update records.
---Query starts-------------------------------------
MERGE INTO [FACT_mytbl] FCT
USING (
SELECT
FCT1.ID
,FCT1.PKcol1
,FCT1.FKcol1
,FCT1.col1
,FCT1.col2
, FCT1.col3
FROM [FACT_mytbl] FCT1 WITH(NOLOCK)
LEFT JOIN dbo.E_tbl1 CT WITH(NOLOCK)
ON CT.PKcol1=FCT1.PKcol1
LEFT JOIN dbo.E_tbl2 CT1 WITH(NOLOCK)
ON CT1.PKcol1=FCT1.PKcol1
) S
ON FCT.PKcol1 = S.PKcol1
WHEN MATCHED AND (FCT.PKcol1 ! = S.PKcol1 ) THEN
UPDATE SET
FCT.col1
,FCT.col2
,FCT.col3
WHEN NOT MATCHED THEN
INSERT VALUES
S.ID
,S.PKcol1
,S.FKcol1
,S.col1
,S.col2
, S.col3
--query ends----------------
any suggestions,
Thanks,
R.BHi Bhupesh_Rajasekaran,
According to your description, if you want to insert only new records which does not exist in destination and update existing records which does exist in destination. We usually accomplish these in two statement.
1.A join statement to update records.
2.A insert statement for new records.
Also we can use MERGE in SQL Server to insert, update at the same time. You specify a "Source" record set and a "Target" table, and the join between the two. You then specify the type of data modification that is to occur when the records between the two
data are matched or are not matched. For more information, there is an similar example about merge in SQL Server, you can review the following article.
http://www.codeproject.com/Tips/590085/Merge-in-SQL-Server
Regards,
Sofiya Li
Sofiya Li
TechNet Community Support -
SQL- Query that return only one record
Hello,
is it possible to execute a pure SQL-Query which returns only one row although multiple rows are found.
It is no problem in PL/SQL, but I have no idea if the same is also possible in SQL.
thanks in advance
MichaelWhy not? There are 4 records in this table, but only selected one.
SQL> with t
2 as
3 (
4 select '#%$@#$@$@##$' Addr from dual
5 union all
6 select '18 Jalan Scott' from dual
7 union all
8 select '18 Lemana St' from dual
9 union all
10 select '32-penstation' from dual
11 union all
12 select '99999999999' from dual
13 )
14 select addr
15 from t
16 where rownum =1
17 /
ADDR
#%$@#$@$@##$
SQL>Cheers
Sarma. -
Need (procedure or function or sql query )for Email filter
Hi All,
in (procedure or function or sql query ) ,if i pass the a input parameter like (anbu@y or anbu@ or anbu@yahjjj ) ,i need to get output like [email protected] .
please give me code for this ...SQL> with t
2 as
3 (
4 select 'anbu@y' email from dual union all
5 select 'anbu@' from dual union all
6 select 'anbu@yahjjj' from dual
7 )
8 select email, regexp_replace(email, '@.*','@yahoo.com') email_new
9 from t
10 /
EMAIL
EMAIL_NEW
anbu@y
[email protected]
anbu@
[email protected]
anbu@yahjjj
[email protected] -
Need help in SQL query to get only distinct records
Hi all.
I am new to oracle SQL and i want to write a query to get only distinct values from 2 tables.
for example i hade 2 tables table1(employee_id,employee_no,grade) and another table2 with same structure.
for example data in table1 is
Employeeid employeeno Grade
1 101 A
1 101 B
1 101 E
1 101 D
and data in table 2 is
Employeeid employeeno Grade
1 101 A
1 101 B
1 101 E
i want to display only the fourth row like the data that doesnt exist in table2 i,e (1 101 D)
I had too much records in both tables and i jst want to see the records that is in table 1 not in table 2 only.
employeeid and employeeno are the same in both tables
I hope u understand my query. I user left outer join and right outer join bt it displays all the results that and matching and unmatching. I want jst unmatched records.
Any help is highly Appreciated.
Thanx in Advance.
I am using oracle 10G on windowsTry MINUS:
http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/queries004.htm#i2054381
SQL> -- generating sample data:
SQL> with t1 as (
2 select 1 employeeid, 101 employeeno, 'A' grade from dual union
3 select 1, 101, 'B' from dual union
4 select 1, 101, 'E' from dual union
5 select 1, 101, 'D' from dual
6 )
7 , t2 as (
8 select 1 employeeid, 101 employeeno, 'A' grade from dual union
9 select 1, 101, 'B' from dual union
10 select 1, 101, 'E' from dual
11 )
12 --
13 -- actual query:
14 --
15 select employeeid
16 , employeeno
17 , grade
18 from t1
19 minus
20 select employeeid
21 , employeeno
22 , grade
23 from t2;
EMPLOYEEID EMPLOYEENO G
1 101 D
1 row selected. -
Comma delimited in Sql query decode function errors out
Hi All,
DB: 11.2.0.3.0
I am using the below query to generate the comma delimited output in a spool file but it errors out with the message below:
SQL> set lines 100 pages 50
SQL> col "USER_CONCURRENT_QUEUE_NAME" format a40;
SQL> set head off
SQL> spool /home/xyz/cmrequests.csv
SQL> SELECT
2 a.USER_CONCURRENT_QUEUE_NAME || ','
3 || a.MAX_PROCESSES || ','
4 || sum(decode(b.PHASE_CODE,'P',decode(b.STATUS_CODE,'Q',1,0),0)) Pending_Standby ||','
5 ||sum(decode(b.PHASE_CODE,'P',decode(b.STATUS_CODE,'I',1,0),0)) Pending_Normal ||','
6 ||sum(decode(b.PHASE_CODE,'R',decode(b.STATUS_CODE,'R',1,0),0)) Running_Normal
7 from FND_CONCURRENT_QUEUES_VL a, FND_CONCURRENT_WORKER_REQUESTS b
where a.concurrent_queue_id = b.concurrent_queue_id AND b.Requested_Start_Date <= SYSDATE
8 9 GROUP BY a.USER_CONCURRENT_QUEUE_NAME,a.MAX_PROCESSES;
|| sum(decode(b.PHASE_CODE,'P',decode(b.STATUS_CODE,'Q',1,0),0)) Pending_Standby ||','
ERROR at line 4:
ORA-00923: FROM keyword not found where expected
SQL> spool off;
SQL>
Expected output in the spool /home/xyz/cmrequests.csv
Standard Manager,10,0,1,0
Thanks for your time!
Regards,Get to work immediately on marking your previous questions ANSWERED if they have been!
>
I am using the below query to generate the comma delimited output in a spool file but it errors out with the message below:
SQL> set lines 100 pages 50
SQL> col "USER_CONCURRENT_QUEUE_NAME" format a40;
SQL> set head off
SQL> spool /home/xyz/cmrequests.csv
SQL> SELECT
2 a.USER_CONCURRENT_QUEUE_NAME || ','
3 || a.MAX_PROCESSES || ','
4 || sum(decode(b.PHASE_CODE,'P',decode(b.STATUS_CODE,'Q',1,0),0)) Pending_Standby ||','
5 ||sum(decode(b.PHASE_CODE,'P',decode(b.STATUS_CODE,'I',1,0),0)) Pending_Normal ||','
6 ||sum(decode(b.PHASE_CODE,'R',decode(b.STATUS_CODE,'R',1,0),0)) Running_Normal
7 from FND_CONCURRENT_QUEUES_VL a, FND_CONCURRENT_WORKER_REQUESTS b
where a.concurrent_queue_id = b.concurrent_queue_id AND b.Requested_Start_Date <= SYSDATE
8 9 GROUP BY a.USER_CONCURRENT_QUEUE_NAME,a.MAX_PROCESSES;
|| sum(decode(b.PHASE_CODE,'P',decode(b.STATUS_CODE,'Q',1,0),0)) Pending_Standby ||','
>
Well if you want to spool query results to a file the first thing you need to do is write a query that actually works.
Why do you think a query like this is valid?
SELECT 'this, is, my, giant, string, of, columns, with, commas, in, between, each, word'
GROUP BY this, is, my, giant, stringYou only have one column in the result set but you are trying to group by three columns and none of them are even in the result set.
What's up with that?
You can only group by columns that are actually IN the result set. -
Difference between Static SQL Query and Dynamic SQL Query.
Hi,
Please explain the basic difference between static and dynamic sql queries. Please explain with example.Static: http://download.oracle.com/docs/cd/E11882_01/appdev.112/e10472/static.htm
Dynamic: http://download.oracle.com/docs/cd/E11882_01/appdev.112/e10472/dynamic.htm
Maybe you are looking for
-
Nano no longer recognized by Windows
I have a first generation Nano. It was connecting to Windows XP machine just fine and iTunes worked great --- until yesterday. Now it's no longer seen by iTunes, or by My Computer. I've reset the iPod and tried everything else on the support site. I
-
How do you save a pdf to Adobe Document Cloud from within Acrobat Pro DC?
I have a full Creative Cloud account so I now have the updated Acrobat Pro DC for both my pc and my iPhone. On my iPhone, when I opened a pdf I was given the option to save the file to Document Cloud. But on the PC, how do I open this file or others
-
Third Party Database Support in SQL Developer
As a rule I don't (blatantly) advertise my blog on this forum, but I have just had feedback from my recent posting which made realize you might not have stumbled on this new functionality. i.e. Third Party Database Support in SQL Developer. <br> <p>I
-
[Windows 8] F-keys overrided by Fn on Satellite L850-1JG
Hello, I recently bought a Satellite L850-1JG laptop which works with Windows 8. When pressing the F-keys (F1, F2,..., F12), it's the Fn behavior which is triggered by default(like adjusting brightness or switching Wifi). So, to get the F-keys "norma
-
What is the use of Update type used with call transaction statement?
What is the difference between following option of call transaction- 'A' (asynchronous update) 'S' (synchronous update) 'L' (local update) ??? Regards, GURU