Query that selects rows depending on information in other rows?
Hello!
I have this table that contains employment information about persons. Each person can have one or several rows in this table. The problem I have is that some people who have several jobs are a little special in the way that I need to query them. A person that has several jobs can look like this:
PKEY | PERSONID | EMPTYPE | IDNO | STARTDATE | ENDDATE
1 | 730813 | 0 | 1000 | 09-01-01 | 10-01-01
2 | 730813 | 1 | 1000 | 09-05-01 | 09-09-01
3 | 730813 | 0 | 1001 | 05-03-02 | 11-07-02
The person above has 2 active employments, the first one isn't really active since the person has another temporary employment that has replaced it, it's number 2 and it has the same IDNO. The difference is the EMPTYPE, the ordinary employment has number 0 and the temporary employment has the number 1. On row 3 is the persons other active employment with IDNO 1001 and EMPTYPE 0.
This is what I need to do:
I need to do a select that only gives me the rows 2 and 3 when the employment no 2 is seen as active by looking at the dates, when the current date is between the start and end dates. The select needs to give me rows 1 and 3 when the employment no 2 is no longer active, i.e. when the end date has passed.
Trying to sum it up, when there are several rows with the same PERSONID and the same IDNO and the current date is greater or equal to the STARTDATE but less or equal to the ENDDATE give me only the row where the EMPTYPE is 1 and not the one where the EMPTYPE is 0.
Is this possible to do?
Thanks.
Hi,
Sorry, I can't follow what you're saying about dates. It's hard, not knowing what the date format is.
You probably want analytic functions to do this.
For example to chose the row with the higher emptype (when there is a choice):
WITH got_rnum AS
SELECT pkey, personid, emptype, idno, startdate, enddate
, ROW_NUMBER () OVER (PARTITION BY personid, idno
ORDER BY emptype DESC
) AS rnum
FROM table_x
SELECT pkey, personid, emptype, idno, startdate, enddate
FROM got_rnum
WHERE rnum = 1
;Whatever your criteria are for deciding which row you want to pick, make an ORDER BY clause in the ROW_NUMBR function that puts that row first.
If you want to include ties for the row chosen, use RANK instead of ROW_NUMBER.
Edited by: Frank Kulash on Aug 15, 2009 11:09 PM
Similar Messages
-
I need a query that selects the amount of records for each day in a table.
I need a query that selects the amount of records for each
day in a table.
Eg the result would be:
date 1 14
date 2 3
etc
Any ideas?sorted:
SELECT count([commentID]),convert(varchar, dateAdded, 112)
FROM COMMENTSgroup by convert(varchar, dateAdded,
112) -
Query in selecting row wise information
Hello All,
Could you please help me in getting the row wise information for the Financial Reporting Studion Report? I have a report where the row information will be Revenue and Expenses. but the revenue and expenses should only carry for specific cost centers. For example XXXXRevenue should carry revenue for GL Account XXXX.
Iam not able to do this. Please help me.
Thanks.Hi Geoff,
You can use the ROW_NUMBER and CASE Functions to acheive this. Here with , i have give u a small idea with an ex:
Qry :
B.ItemCode,SUM(B.PO1)[PO1],SUM(B.POQty1)[POQty1],SUM(B.PO2)[PO2],SUM(B.POQty2)[POQty2] From
select Itemcode
CASE when Sno=1 Then DocNum End [PO1]
CASE When Sno=1 Then Qty End [POQty1]
CASE when Sno=2 Then DocNum End [PO2]
CASE When Sno=2 Then Qty End [POQty2]
From(
Row_Number() Over (Order By Opor.Docnum) [Sno], Opor.DocNum,POr1.ItemCode,sum(POr1.Quantity)[Qty] from OPOR join Por1 on Opor.DocEntry=por1.DocEntry
ItemCode='20002' group by oPor.DocNum,por1.ItemCode
A where A.Sno<=2)B Group By B.ItemCode
Result :-
ItemCode PO1 POQty1 PO2 POQty2
20002 18 1.000000 19 10.000000
Thanks,
Karthikeyan.P -
How to query the selected row?
i have a table displayed in a gridcontrol... how would i go about binding a controlButton to this table so that when this button is clicked, it returns the value of a particular column that is currently selected in the gridcontrol??? ... currently, when the button is clicked... it returns the top of the ResultSet which is always the same value... i would like it to return the value of the row that is currenlty selected in the table??? ... using infoProducers(sessionInfo and resultSetInfo)... thanx, james
After using bellow script it works fine
xfa.resolveNode("Formname.Table1.Row1["this.parent.index"].fieldname").rawValue; -
How to develop this report/Query that selects records with MAX Value
Hi,
My requirment is to develop a report to show all emp whose policy is going to expire ith in the user selected date intreval...
Emp_ID--App_Date-Policy_Strt_Date--Policy_End_Date--App_Status
101.01.2007---BlankBlank--
NEW (New application for a Medical Insurance policy raised for employee)
101.01.2007---01.02.200730.05.2007--
COMPLETE (policy issued for emp with strt & end dates)
115.05.2007---01.02.200730.05.2007--
RENEWAL (a application is raised to renew emp policy)
115.05.2007---01.06.200731.12.2007--
COMPLETE (policy issued for emp with strt & end dates)
115.12.2007---01.06.200731.12.2007--
RENEWAL (a application is raised to renew emp policy)
115.12.2007---01.01.200831.05.2008--
COMPLETE (policy issued for emp with strt & end dates)
115.05.2008---01.01.200831.05.2008--
RENEWAL (a application is raised to renew emp policy)
115.05.2008---01.06.200831.05.2009--
COMPLETE (a application is raised to renew emp policy)
So EMP 1 got valid policy untill 31.05.2009.
When a user executes a query to show all employees whose policy expiry date intreval (01.05.2008 to 31.05.2008)
it shouldn't show Emp 1 as he got valid policy untill 31.05.2009 but currently it is showing Emp 1....
How can i achieve this
ThanksHi
Thanks for the update
I created variable on Plc End end
If user enter show all emps whose policy is expiring in MAY (01.05.2008---31.05.2008) it is displaying EMp 1 as well but emp 1 has a policy which expires in 2009
Thanks -
WAD - input locking for selected rows in planning querie!?
hello guys,
i need some ideas - in which way i can lock data entry functionality in IP query for selected rows? some rows in the query have an indicator S (sum) and some I (input). so i have a chance for selection, which rows are input rows and which rows are sum (dsiplay) rows.
but all rows are input ready in sap standard querydefiniton and i need some ideas how i could implement an row specific inputfunctionality.
example
ITEM KEYFIGURE
line 1 Input input ready
line 2 Input input ready
line 3 Sline display only/ no input
line 4 Input input ready
dataslices are no option, because i need to change the "Sline-rows" data via planning functions.
i need ideas for web, not excel.
thx for your ideas!
Edited by: David Jahn on May 4, 2010 2:03 PMHi David,
of course, it is possible to use two structures in BI-IP, e.g. one containing the key figures (may be restricted) and another one used in the rows where one uses only characteristic values. But then the number of lines in the row structure is static. It is possible to drill-down other characteristics as ususal.
One idea is to use you characteristic containing the 'S value' as a navigation attribute and to create a row structure containing two lines, one with the restriction # and one with S and the corresponding setting for input readyness (maybe a cell definition for input readyness is needed here). Then drill-down by the base characteristic of the navigation attribute. What is not so nice then that one will get a group change: drill-down characteristic by the row structure; but the system generated characteristic relationship for navigation attributes makes the non existing combinations not input ready.
Regards,
Gregor -
Changing colour of selected row to orange
Hi,
I have requirement that selected row of the table should be highlighted in orange colour.
Currently, my table selection and associated events are working fine.
Only problem is selected row is not getting highlighted.
Can anyone tell me which property of the table I should modify?
Thanks in advance,
ApurvaHi Ayyappa,
The selectionMode of the table is auto. I have created an action HighlightRow and this action is getting called on the event onLeadSelection. I have placed the code given by you in the Action.
However, the row is not getting highlighted.
What am I missing.
Nikhil -
Make columns editable in a selected row of SALV
Hello ,
I have a requirement,im using SALV for listing some records,On lead select of a row in SALV,i need to make two columns of that selected row editable and make the remaining in display mode.
Useful answers would be rewarded.Hi Reema Elsy Easow,
Changing the type of a single field in a column is i think not possible. you can make whole column editable.
I will suggest you a solution-- when a row is selected for editting , you can display that row in another alv or table with single row and provide the record for editiing.
Regards
Sarath -
How do i get the data present in the selected row at run time in Multicolumn list box?
Hi,
I am using multicolumn list box with three columns and i am having OK button.
If the user selects particular row and then press ok then that row datas(that 3 values) has to get update in the file.
Can u tell me how to get that selected row values alone and also how to append the list box value at run time?
Thanks
MeenatchiYou can simply use the Value property of the MLBox to get the row selected & wire it to the Delete from array function with the Itemnames of the MLBox as the Input & Output of the Delate from array function.
See the attached VI.
- Partha
LabVIEW - Wires that catch bugs!
Attachments:
Dbl-click Row to get Data & Delete.vi 41 KB -
Hi
In my JTable i'am hilighting the selected row with blue color and the Selected row will be changing dynamically during run time, this is working fine but the problem is:-
when selected row crosses the visible space it's Hilighted but unless i manually scroll down it's not visible, so i
Can any one tell me how to set the scrollbar to adjust so that selected row is always visible with in vissible space and i neednot scroll down to see
Thank's in Advance for any advice/suggestionsWell this method is taking Rectangle as the parameter so i tried the following :-
table.scrollRectToVisible(new Rectangle(table.getSelectedRow(),table.getSelectedColumn(),1,1));
Am not sure about it but still i'll give a try
Anyways Thank's a lot for the Replay, Atleast i got a hint to give a try
THANK AGAIN -
How to get all rows that are returned in inner sub query of select statemen
If a sub query in select statement returns more than one row than how to get all those returned rows in the final
output of the query .It will be all right if all column's value repeat and that multiple output of inner query comes
in another column .
How to get that ?As Frank said, you likely want a join, and likely an outer join to replicate the select in the projection. Something like:
SELECT id,stat, section, USER_ID concerned_person
FROM table_all,
left join table2
on room_id = sectoion and
sur_role = 'r001'
WHERE section IN (SELECT code
FROM t_area
WHERE dept= 'p002')An alternative, depending on where and how you are using the statement would be something like:
SQL> WITH t AS (
2 select 1 id from dual union all
3 select 2 id from dual),
4 t1 as (
5 select 1 id, 'One' descr from dual union all
6 select 1, 'Un' from dual union all
7 select 1, 'Une' from dual)
8 SELECT t.id, CURSOR(SELECT t1.id, t1.descr from t1
9 WHERE t1.id = t.id)
10 FROM t;
ID CURSOR(SELECTT1.ID,T
1 CURSOR STATEMENT : 2
CURSOR STATEMENT : 2
ID DESCR
1 One
1 Un
1 Une
2 CURSOR STATEMENT : 2
CURSOR STATEMENT : 2
no rows selectedJohn -
Select Query to fetch rows that contain non alphabetic characters
how to select rows from a table (tab1) where a specific column (tab1.col1) has non alphabetic characters
hi,
you didnt specify which version you are using.
if you are working on oracle 10g you can use regular expressions like this:
select * from tab1 where regexp_like(col1,'[^[[:alpha:]]]'); -
Select query that eliminate duplicates
I am struck at the below scenario. need your help in modify my query based on the below sample data. My objective is to create a EVENT AND EVENT_LOW using the data available from TEM_GT and TXN table.
TEM_GT (global temporary table)
est_id primary key, trans_id, trp_id, amount
1 111 2221 1.5
2 111 3332 2.0
3 112 4443 3.0
TXN table
trans_id, trans_type
111 type1
112 type1
EVENT table
event_id primary key, trans_id, trans_type, flag.
1000 111 type1 N
1001 112 type1 N
EVENT_LOW table
event_low_id primary key, event_id, est_id, amount.
9991 1000 1 1.5
9992 1000 1 2.0
9993 1001 2 3.0
insert into(event_low_id, event_id, est_id, amount)
select event_low_id_s.nextval e.event_id, tg.est_id, tg.amount from
from TEM_GT tg, EVENT e
WHERE
tg.trans_id = e.trans_id
AND e.flag = 'N'
Based on TEM_GT and TXN gt, populating data into EVENT table. Now when I try to populate data in the EVENT_LOW table, the above query returns 5 rows instead of 3 rows. This is due to trp_id in TEM_GT table. I do not want to add trp_id in EVENT table, and want to handle the duplicate elimination in the above select query.
I wrote the below sql. It almost resolve my problem. But sometimes it returns different value for the amount column. I wanted my event_low result as below for the est_id 1.
event_low_id primary key, event_id, est_id, amount.
9991 1000 1 1.5
9992 1000 1 2.0
But sometime it returns as below
event_low_id primary key, event_id, est_id, amount.
9991 1000 1 1.5
9992 1000 1 1.5
or
event_low_id primary key, event_id, est_id, amount.
9991 1000 1 2.0
9992 1000 1 2.0
select *
from (select x.*,
row_number() over (partition by event_id order by event_id) rn
from (seelct e.event_id, tg.est_id, tg.amount
from TEM_GT tg, EVENT e
WHERE
tg.trans_id = e.trans_id
AND e.flag = 'N'
) x
where rn = 1
I am using Oracle 11g. Any help. Thanks in advance.Hi Suresh,
Please read SQL and PL/SQL FAQ
Whenever you create a thread always post sample data as CREATE TABLE and INSERT statements.
Additionally when you put some code or output please enclose it between two lines starting with {noformat}{noformat}
i.e.:
{noformat}{noformat}
SELECT ...
{noformat}{noformat}
Regarding your problem you said:
Based on TEM_GT and TXN gt, populating data into EVENT table. Now when I try to populate data in the EVENT_LOW table, the above query returns 5 rows instead of 3 rows.
The above query cannot return 5 rows with the data you have posted. You have 3 rows in TEM_GT and 2 rows in EVENT table. The table are joined ontg.trans_id = e.trans_id AND e.flag = 'N'
And the result of the query with your posted data is *3 rows*. So try to be clear.
If your intention is to insert into EVENT_LOW only one row from TEM_GT for the same trans_id then you have to tell us which is the criteria to choose the row between the duplicates (the first by est_id order??).
Also be careful to post valid statements. The statement below is not valid as it is missing the table name after INSERT INTO and there are 2 consecutive FROM keywords:insert into(event_low_id, event_id, est_id, amount)
select event_low_id_s.nextval e.event_id, tg.est_id, tg.amount from
from TEM_GT tg, EVENT e
WHERE
tg.trans_id = e.trans_id
AND e.flag = 'N'
I will make the assumption that you want to select the first row order by est_id during insertion. If this is not what you want, please change the analytic function:INSERT INTO event_low (event_low_id, event_id, est_id, amount)
WITH got_data AS
SELECT e.event_id, tg.est_id, tg.amount
, ROW_NUMBER() OVER(PARTITION BY tg.trans_id ORDER BY tg.est_id) AS rn
FROM tem_gt tg, event e
WHERE tg.trans_id = e.trans_id
AND e.flag = 'N'
SELECT event_low_id_s.NEXTVAL
, event_id, est_id, amount
FROM got_data
WHERE rn = 1;
If this is not what you want please post CREATE TABLE and INSERT statement, and post your expected output (for insertion into EVENT_LOW table) for the data you have posted.
Regards.
Al -
SQL query that returns exclusive rows from groups
I'm using modified scott/tiger data for this.
I've got two tables
DEPT_X
DEPTNO DNAME LOC CODE_ID SUB_DEPTNO
10 ACCOUNTING NEW YORK 111 101
10 SALES ATWN 111 102
10 SALES BTWN 112 103
20 RESEARCH DALLAS 111 201
20 RESEARCH CTWN 111 202
30 SALES CHICAGO 111 301
40 OPERATIONS BOSTON 112 401and
BI_PD
CODE_ID PD_TYPE BI_TYPE
111 -1
112 -1 I want to write a query that joins the code_ids of the two tables and lists out either only the records from DEPT_X who's code_ids have a -1 in the PD_TYPE(112) column and none of the code_ids that have a -1 in the BI_TYPE(111) column for each department or if that department has code_ids for the BI_TYPE (111), list out those rows. So for Deptno 10 you'll only get the row with SUB_DEPTNO = 103. But if for that DEPTNO= 20 you'll get the rows for both SUB_DEPTNO = 201 and 202.
So the result should look like
DEPTNO SUB_DEPTNO
10 103
20 201
20 202
30 301.
Basically I just want rows from each department that have code_ids = 111 not to show up if there are code_ids = 112.
What is the query to do this?
Message was edited by:
user623359
Message was edited by:
user623359
Message was edited by:
user623359
Message was edited by:
user623359
Message was edited by:
user623359One way could be:
Connected to:
Oracle Database 10g Enterprise Edition Release 10.2.0.3.0 - 64bit Production
With the Partitioning, OLAP and Data Mining options
SQL> with dept_x as (
2 select 10 deptno, 'ACCOUNTING' dname, 'NEW YORK' loc, 111 code_id, 101 sub_deptno from dual
union all
3 select 10, 'SALES', 'ATWN', 111, 102 from dual union all
4 select 10, 'SALES', 'BTWN', 112, 103 from dual union all
5 select 20, 'RESEARCH', 'DALLAS', 111, 201 from dual union all
6 select 20, 'RESEARCH', 'CTWN', 111, 202 from dual union all
7 select 30, 'SALES', 'CHICAGO', 111, 301 from dual union all
8 select 40, 'OPERATIONS', 'BOSTON', 112, 401 from dual ),
9 --
10 bi_pd as(
11 select 111 code_id, null pd_type, -1 bi_type from dual union all
12 select 112, -1, null from dual),
13 --
14 t as(
15 select a.deptno,a.sub_deptno from dept_x a, bi_pd b
16 where a.code_id = b.code_id and b.pd_type = -1)
17 --
18 select a.deptno,a.sub_deptno
19 from dept_x a, bi_pd b
20 where a.code_id = b.code_id and b.bi_type = -1 and
21 (a.deptno) not in (select deptno from t)
22 union
23 select deptno,sub_deptno from t;
DEPTNO SUB_DEPTNO
10 103
20 201
20 202
30 301
40 401 -
Multiselect query to display selected row
HI All,
I have a requirements to do multiselect from a table region, and need to display the selected row values in the page.
so i am able to multiselect from the table but while trying to print the values it is always printing only one selected row values not multiple one.
the below code i am using to print multiple row selected values.
for (int i = 0; i < fetchedRowCount; i++) {
ABCEOViewRowImpl rowi =
(ABEOViewRowImpl)selectIter.getRowAtRangeIndex(i);
if (rowi != null) {
if ("Y".equals(rowi.getReprint())) {
for (int j = 0; j < fetchedRowCount; j++) {
String srnum = rowi.getSerialNumber();
throw new OAException("After getting Srno " +
srnum,
OAException.INFORMATION);
where fetchedrowcount --------no of rows in the table region
here srnum is printing only one value.
Please guide me where to check for this issue.
Thanks
DebThanks Sanujeet for reply,
yes, the second for loop is not required, just for to check i have added that one,
As you suggested now, i just kept one for loop and trying to print all the value through comma separated and added the Exception in the last.
for (int i = 0; i < fetchedRowCount; i++) {
ABCEOViewRowImpl rowi =
(ABCEOViewRowImpl)selectIter.getRowAtRangeIndex(i);
if (rowi != null) {
if ("Y".equals(rowi.getReprint())) {
srnum =srnum+rowi.getSerialNumber()+",";
if (srnum!="")
throw new OAException("After getting Srno " +
srnum,
OAException.INFORMATION);
Thanks
Deb
Maybe you are looking for
-
File will not open nor move to trash, and unexpectedly closes finder
I downloaded a file and saved it in my movies folder on my mac book pro. When i click on the movies folder in finer it will automatically close finder down. If i search for the folder that i downloaded it appears but will not move to trash nor open,
-
Specified font need to be changed in all paragraph styles
Hi All, Is there any option to change a font which is used in many paragarph styles to some other font. Thanks in advance,
-
Binding question i think (:
I'm working on a website (holiday homes for rent). One of the pages is contact.cfm. People visiting the site will come to that page after clicking a text link " Contact us about this house"on a result page with available holiday homes. The problem is
-
Importing m3u files in iTunes alters song name, how can I fix it?
When I import an m3u file into iTunes, that refers to music that already exists in my library, it alters the "Name" of the song to be in the format "Track Number - Name" until I double click the song. Then iTunes refreshes the "Name" information from
-
Message in Real Time???
how do i see the messages going out in real time?