How to select records based on Max/Min on different columns and group by
I have a table with 5 columns(a,b,c,d,e), i need to select records based on MAX(c),Max(D) and Min(e) group by a,b. i am trying using : select max(c),max(d),min(e) from table group by a,b. this is not working. its giving me 1 6 1
a b c d e
1 1 1 2 1
1 1 1 6 4
1 1 1 6 3
when i group by a,b i am expecting the record 1 6 3
Please help me with this.. Thanks in advance....
Hi,
Welcome to the forum!
962163 wrote:
I have a table with 5 columns(a,b,c,d,e), i need to select records based on MAX(c),Max(D) and Min(e) group by a,b. i am trying using : select max(c),max(d),min(e) from table group by a,b. this is not working. its giving me 1 6 1
a b c d e
1 1 1 2 1
1 1 1 6 4
1 1 1 6 3
when i group by a,b i am expecting the record 1 6 3It looks to me like "1 6 1" is the correct answer. You're asking for the lowest value of e, and 1 is lower than 3.
Maybe you don't want MIN (e). Explain why you want 3 (that is, how you decided that 3 is the correct value for the last column) and someone will help you code it.
Edited by: Frank Kulash on Sep 28, 2012 6:17 PM
Whenever you have a problem, you should psot CREATE TABLE and INSERT statements for your sample data. That way, the people who want to help you can re-create the problem and test their ideas. It often helps to clarify the problem, too. since this is your first message, I'll do it for you:
CREATE TABLE table_x
( a NUMBER
, b NUMBER
, c NUMBER
, d NUMBER
, e NUMBER
INSERT INTO table_x (a, b, c, d, e) VALUES (1, 1, 1, 2, 1);
INSERT INTO table_x (a, b, c, d, e) VALUES (1, 1, 1, 6, 4);
INSERT INTO table_x (a, b, c, d, e) VALUES (1, 1, 1, 6, 3);
COMMIT;
Similar Messages
-
Select records based on max(date)
Hi everyone,
I have a table (tbl_training) with training information-such as who took what training, when they took it, scores they received and other various stuff.
In this table, a person could have taken multiple training within the year. what i need to see in my results is, the last training the person took. I assume i am looking to query something that produces max(date_of_training) and maybe grouping my id_number. I have tried various combinations of sub-queries to no avail. Any help is welcomed.
So my data may look something like this:
id_number date_of_training score last_name first_name instructor rank
1234 01/01/09 50 doe john mr. hank sgt
1234 02/13/09 72 doe john mr. hank sgt
1234 01/31/09 60 doe john mr. hank sgt
5678 02/03/09 80 smith lisa mr. hank cpl
What i need returned in the query is:
1234 02/13/09 72 doe john mr. hank sgt
5678 02/03/09 80 smith lisa mr. hank cpl
Select id_number, date_of_training, score, last_name, first_name, instructor, rank
from tbl_training;
Thanks for the help in advance.Try this code
select * from (
Select id_number, date_of_training, score, last_name, first_name, instructor, rank, row_number()over(partition by id_number order by date_of_training desc ) rn
from tbl_training)
where rn =1; -
Select records based on the closest given time
Dear SQL gurus,
I have a table T1:
Name Null? Type
ID NOT NULL NUMBER(5)
MOMENT NOT NULL DATE [DD.MM.YYYY HH24:MI]
MEASUREMENT NOT NULL NUMBER(8,3)
Example (ID, MOMENT, MEASUREMENT)
-- START OF EXAMPLE --
9380 18.11.2000 03:45 17.6
9380 18.11.2000 04:30 17.3
9380 18.11.2000 05:45 16.8
9380 18.11.2000 06:15 16.8
9380 18.11.2000 07:00 16.2
9380 18.11.2000 07:30 16.2
9380 18.11.2000 08:15 16
9380 18.11.2000 08:45 15.7
9380 18.11.2000 09:30 15.4
9380 18.11.2000 10:00 15.4
9380 18.11.2000 11:15 15.4
9380 18.11.2000 11:45 15.4
9380 18.11.2000 12:30 15.4
9380 18.11.2000 13:00 15.4
9380 18.11.2000 13:45 15.4
--- END OF EXAMPLE --
How to select records based on the:
- time period specified by the day only [DD.MM.YYYY] - CONDITION 1
- with values for 6AM only, and if not available, with values closest to 6AM - CONDITION 2
(if the time gap in MOMENT field is too big, lets say > 5h then choose the average between the value before 6AM (ex. 4:15AM) and the value after the 6AM (ex. 9:45AM))
CONDITION 1 (something like): moment between '01.01.2005' and '31.12.2004' - this is OK
CONDITION 2: I do not know how to formulate, especially if 6AM value is not availabe, and I have to find the closest available value, or get the avergae from the two adjacent values.
Maybe cursor magic??? Thanks a lot for your help.
RadoAbout condition two, would the following select be of use to you? Picking the first record could be achived by rownum, analytic function, etc.
WITH t1 AS (SELECT 9380 id, TO_DATE('18.11.2000 03:45', 'dd.mm.yyyy hh24:mi') moment, 17.6 measurement
FROM dual
UNION
SELECT 9380 id, TO_DATE('18.11.2000 04:30', 'dd.mm.yyyy hh24:mi') moment, 17.3 measurement
FROM dual
UNION
SELECT 9380 id, TO_DATE('18.11.2000 05:45', 'dd.mm.yyyy hh24:mi') moment, 16.8 measurement
FROM dual
UNION
SELECT 9380 id, TO_DATE('18.11.2000 06:15', 'dd.mm.yyyy hh24:mi') moment, 16.8 measurement
FROM dual
SELECT id, moment, measurement, diff
FROM (SELECT id, moment, measurement,
moment - TO_DATE(TO_CHAR(moment, 'dd.mm.yyyy ') || '06:00', 'dd.mm.yyyy hh24:mi') diff
FROM t1
ORDER BY abs(diff) asc, SIGN(diff) desc;
C. -
How to omit records based on a status code
I am trying to select records based on a status 'N' or 'A' by a certain date as long as by the chosen date there is not another status given to the same course like 'D' or 'X'.
Here are some sample data...
CREATE TABLE #TEST
(ID VARCHAR(7),
COURSE VARCHAR(10),
CURRENT_STATUS VARCHAR(1),
STATUS_DATE DATETIME)
INSERT INTO #TEST
(ID, COURSE, CURRENT_STATUS, STATUS_DATE)
VALUES
('0020541','AAAA-1000H', 'X', '2013-08-27 00:00:00.000'),
('0020541','AAAA-1000H', 'N', '2013-08-26 00:00:00.000'),
('0020541','BBBB-2000H', 'D', '2013-11-04 00:00:00.000'),
('0020541','BBBB-2000H', 'N', '2013-08-27 00:00:00.000'),
('0020541','CCCC-3000H', 'D', '2013-09-11 00:00:00.000'),
('0020541','CCCC-3000H', 'N', '2013-08-26 00:00:00.000'),
('0020541','DDDD-4000H', 'A', '2013-08-25 00:00:00.000'),
('0020541','EEEE-5000H', 'N', '2013-08-26 00:00:00.000')
SELECT * FROM #TEST
DROP TABLE #TEST
If the query is to determine how many records with a status of N or A up to and incl August 26, 2013, I should see 4 records... AAAA-1000H, CCCC-3000H, DDDD-4000H, EEEE-5000H
If the query is to determine how many records with a status of N or A up to and incl October 1, 2013, I should see 3 records... BBBB-2000H, DDDD-4000H, EEEE-5000H
...and so on.
Any help is appreciated.I am trying to select records based on a status 'N' or 'A' by a certain date as long as by the chosen date there is not another status given to the same course like 'D' or 'X'.
Here are some sample data...
CREATE TABLE #TEST
(ID VARCHAR(7),
COURSE VARCHAR(10),
CURRENT_STATUS VARCHAR(1),
STATUS_DATE DATETIME)
INSERT INTO #TEST
(ID, COURSE, CURRENT_STATUS, STATUS_DATE)
VALUES
('0020541','AAAA-1000H', 'X', '2013-08-27 00:00:00.000'),
('0020541','AAAA-1000H', 'N', '2013-08-26 00:00:00.000'),
('0020541','BBBB-2000H', 'D', '2013-11-04 00:00:00.000'),
('0020541','BBBB-2000H', 'N', '2013-08-27 00:00:00.000'),
('0020541','CCCC-3000H', 'D', '2013-09-11 00:00:00.000'),
('0020541','CCCC-3000H', 'N', '2013-08-26 00:00:00.000'),
('0020541','DDDD-4000H', 'A', '2013-08-25 00:00:00.000'),
('0020541','EEEE-5000H', 'N', '2013-08-26 00:00:00.000')
SELECT * FROM #TEST
DROP TABLE #TEST
If the query is to determine how many records with a status of N or A up to and incl August 26, 2013, I should see 4 records... AAAA-1000H, CCCC-3000H, DDDD-4000H, EEEE-5000H
If the query is to determine how many records with a status of N or A up to and incl October 1, 2013, I should see 3 records... BBBB-2000H, DDDD-4000H, EEEE-5000H
...and so on.
Any help is appreciated.
I guess I didn't understand your problem. Not sure if this might help you.
SELECT * FROM #TEST
where CURRENT_STATUS IN ('N','A') AND STATUS_DATE<='2013-08-26'
AND STATUS_DATE NOT IN(Select STATUS_DATE from #test
where CURRENT_STATUS IN ('D','X'))
SELECT * FROM #TEST
where CURRENT_STATUS IN ('N','A')
AND STATUS_DATE<='2013-10-01'
AND STATUS_DATE NOT IN(Select STATUS_DATE from #test
where CURRENT_STATUS IN ('D','X'))
- please mark correct answers -
How to select records in ALV using FM
Hi guys,
How to select records in ALV using FM. Not the OO method. Thx in advance!
KunhI
by using REUSE_ALV_FIELDCATALOUG_MERGE. Iys fill field catalouge table as per internal table description. Then use REUSE_ALV_GRID_DISPLAY for display ALV REPORT.
**Please reward suitable points***
With Regards
Navin Khedikar -
Select records based on monthly anniversary date
Hi,
I have a table with a date_added field and I want to select records based on the monthly anniversary date of this field.
eg. ID, Date_added
1, 10-DEC-2012
2, 11-NOV-2012
3, 10-MAR-2012
4, 28-FEB-2012
5, 30-DEC-2012
So For the 10th of Jan 2013, I would want to return records 1 and 3 only
I started looking at the extract function, but this soon falls down for records at the end of the month. For example, on the 28th Feb, I would also want to include records where the date_added day is the 29th, 30th or 31st. So, in the table above I would want to return records 4 and 5, but extract would only return 4.
Is there a simple function to do this month anniversary query - am I missing something very obvious? Or, do I need to write a query to explicitly cope with dates at the end of the month? So far I haven't found a sensible simple solution!
I'm using 11g
thanksI didn't look into leap year, but this should give you a starting point:
select *
from t
where 1 = case last_day(to_date(:target_date,'mmddyyyy'))
when to_date(:target_date,'mmddyyyy')
then case
when to_char(date_added,'dd') >= to_char(to_date(:target_date,'mmddyyyy'),'dd')
then 1
end
else case
when to_char(date_added,'dd') = to_char(to_date(:target_date,'mmddyyyy'),'dd')
then 1
end
end
/For example, target date is 1/10/2013:
SQL> variable target_date varchar2(8)
SQL> exec :target_date := '01102013';
PL/SQL procedure successfully completed.
SQL> with t as (
2 select 1 id,to_date('10-DEC-2012','dd-mon-yyyy') date_added from dual union all
3 select 2,to_date('11-NOV-2012','dd-mon-yyyy') from dual union all
4 select 3,to_date('10-MAR-2012','dd-mon-yyyy') from dual union all
5 select 4,to_date('28-FEB-2012','dd-mon-yyyy') from dual union all
6 select 5,to_date('30-DEC-2012','dd-mon-yyyy') from dual
7 )
8 select *
9 from t
10 where 1 = case last_day(to_date(:target_date,'mmddyyyy'))
11 when to_date(:target_date,'mmddyyyy')
12 then case
13 when to_char(date_added,'dd') >= to_char(to_date(:target_date,'mmddyyyy'),'dd')
14 then 1
15 end
16 else case
17 when to_char(date_added,'dd') = to_char(to_date(:target_date,'mmddyyyy'),'dd')
18 then 1
19 end
20 end
21 /
ID DATE_ADDE
1 10-DEC-12
3 10-MAR-12
SQL> And target date is 2/28/2013:
SQL> exec :target_date := '02282013';
PL/SQL procedure successfully completed.
SQL> with t as (
2 select 1 id,to_date('10-DEC-2012','dd-mon-yyyy') date_added from dual union all
3 select 2,to_date('11-NOV-2012','dd-mon-yyyy') from dual union all
4 select 3,to_date('10-MAR-2012','dd-mon-yyyy') from dual union all
5 select 4,to_date('28-FEB-2012','dd-mon-yyyy') from dual union all
6 select 5,to_date('30-DEC-2012','dd-mon-yyyy') from dual
7 )
8 select *
9 from t
10 where 1 = case last_day(to_date(:target_date,'mmddyyyy'))
11 when to_date(:target_date,'mmddyyyy')
12 then case
13 when to_char(date_added,'dd') >= to_char(to_date(:target_date,'mmddyyyy'),'dd')
14 then 1
15 end
16 else case
17 when to_char(date_added,'dd') = to_char(to_date(:target_date,'mmddyyyy'),'dd')
18 then 1
19 end
20 end
21 /
ID DATE_ADDE
4 28-FEB-12
5 30-DEC-12
SQL> SY. -
Crystal Report Formula to select Record of only MAX Value
hi Everyone,
i need a simple crystal report formula to select one department whose recived quantity Maximum.
for example:
itemcode dscription departmen op recived issue
1 a ab 2 2 2
1 a bb 0 2 2
1 a bc 4 8 2
1 a cc 2 2 2
i group by item the item show just once but i want a formula to show one department who's recived quantity is maximum.i suppress the detail section.and just show the group footer/
itemcode dscription departmen op recived issue
1 a bc 8 14 8Thanks
Re: Crystal Report Formula to select Record of only MAX Value
Abhilash Kumar -
How to Select Members Based on Attributes using Excel VBA
Hi there,Does anyone know how to select members based on attributes using Excel VBA?I don't seem to be able to find macros or functions to do that.Panda
you can use the Range or the Cells objects to get a reference to your range values
in this sample the data is on A1 to B5
Dim lineList(4)
lineList(0) = Array(Cells(1, 1).Value, Cells(1, 2).Value)
lineList(1) = Array(Cells(2, 1).Value, Cells(2, 2).Value)
lineList(2) = Array(Cells(3, 1).Value, Cells(3, 2).Value)
lineList(3) = Array(Cells(4, 1).Value, Cells(4, 2).Value)
lineList(4) = Array(Cells(5, 1).Value, Cells(5, 2).Value) -
How can I get these two results in two different columns?
SELECT COUNT(attr_id)
FROM pv_attribute
WHERE port_id = 322;
SELECT COUNT(attr_id)
FROM pv_attribute
WHERE port_id = 323;
How can I get these two results in two different columns?
ThanksSo Jens made a tiny mistake with the column name... shame you couldn't be bothered to research Pivoting so you could have corrected the mistake (possibly it was deliberate *{:-) ) and learnt something.
with pv_attribute as (select 16123 attr_id, 322 port_id from dual union all
select 16123 attr_id, 322 port_id from dual union all
select 19223 attr_id, 322 port_id from dual union all
select 11193 attr_id, 323 port_id from dual union all
select 13163 attr_id, 323 port_id from dual)
-- end of test data setup
select sum(decode(port_id, 322, 1, 0)) cnt_322,
sum(decode(port_id, 323, 1, 0)) cnt_323
from pv_attribute
where port_id in (322, 323) -
How to save settings in the front panel to different files and retrieve it later?
How to save the front-panel controllers' settings to different files and retrieve it later?
What I've archived is the "Save Settings" function, but when trying to "Reload Settings", I can only retrieve the saved data to front-panel indicators, but not the controllers!
Can anybody give me some advises?
Thanks in advanced!
Charles LuHi
Just write the retrieved data to a local variable (but make it writable first) or property node of the controls.
Hope this helps.
Thomas
Using LV8.0
Don't be afraid to rate a good answer... -
How to separate joint Apple ID's account using different mails and passwords
How to separate joint Apple ID's account using different mails and passwords
Hello ROOOS,
When you speak of a joint Apple ID account it seems you mean a shared Apple ID.
Frequently asked questions about Apple ID - Apple Support
Can I share my Apple ID with someone else?
You shouldn't share your Apple ID account information with other people. Each person should have their own Apple ID.
To eliminate the share one will keep it and the others will create their own Apple IDs.
Apple - My Apple ID
Best regards,
Nubz -
Selecting records based on a max value of a field?
I want to do a select on a table and only get those records that
have the maximum cum order lead time within a supplier_id group.
I want to do this within SQL, not a procedure.
create table bamb.ta_test (
FO_ID NUMBER(10),
CC_ID NUMBER(10),
SUPPLIER_ID NUMBER(10),
CUM_ORDER_LEAD_TIME NUMBER(10),
ORDER_LEAD_TIME NUMBER(10));
insert into bamb.ta_test values(886,550,6,256,13);
insert into bamb.ta_test values(886,550,6,410,10);
insert into bamb.ta_test values(886,550,12,190,10);
insert into bamb.ta_test values(886,550,12,168,48);
insert into bamb.ta_test values(886,550,27,91,22);
insert into bamb.ta_test values(886,550,27,179,17);
FO_ID CC_ID SUPPLIER_ID CUM_ORDER_LEAD_TIME ORDER_LEAD_TIME
886 550 6 256 13
886 550 6 410 10
886 550 12 190 10
886 550 12 168 48
886 550 27 91 22
886 550 27 179 17
6 rows selected.
what I want to end up with is:
FO_ID CC_ID SUPPLIER_ID CUM_ORDER_LEAD_TIME ORDER_LEAD_TIME
886 550 6 410 10
886 550 12 190 10
886 550 27 179 17
How would I do this? [email protected]Hi Timothy!
Try a nested query as:
Greetings Peter
select ta_test.FO_ID, ta_test.CC_ID,
ta_test.SUPPLIER_ID, ta_test.CUM_ORDER_LEAD_TIME,
ta_test.ORDER_LEAD_TIME
from
( select SUPPLIER_ID, max(CUM_ORDER_LEAD_TIME) as max_colt
from ta_test
group by SUPPLIER_ID
) subquery_01,
ta_test
where subquery_01.SUPPLIER_ID = ta_test.SUPPLIER_ID
and
subquery_01.max_colt = ta_test.CUM_ORDER_LEAD_TIME
SQLWKS> select ta_test.FO_ID, ta_test.CC_ID,
2> ta_test.SUPPLIER_ID, ta_test.CUM_ORDER_LEAD_TIME,
3> ta_test.ORDER_LEAD_TIME
4>
5> from
6> ( select SUPPLIER_ID, max(CUM_ORDER_LEAD_TIME) as
max_colt
7> from ta_test
8> group by SUPPLIER_ID
9>
10> ) subquery_01,
11> ta_test
12>
13> where subquery_01.SUPPLIER_ID = ta_test.SUPPLIER_ID
14> and
15> subquery_01.max_colt = ta_test.CUM_ORDER_LEAD_TIME
16>
FO_ID CC_ID SUPPLIER_I CUM_ORDER_ ORDER_LEAD
886 550 6 410 10
886 550 12 190 10
886 550 27 179 17 -
How to select records inserted in a table in last 30 mins
I need to select the records inserted or generated in the basic scott.emp table assuming date created is a new column which inserts the time at the time of creation
select * from emp where (date_Created) between (sysdate -30/1440) and sysdate;Does the above code works or should i need to look something new
Please advise
Regards,sri wrote:
I need to select the records inserted or generated in the basic scott.emp table assuming date created is a new column which inserts the time at the time of creation
select * from emp where (date_Created) between (sysdate -30/1440) and sysdate;Does the above code works or should i need to look something new
Please advise
Regards,It would be better to say:
select * from emp where date_created >= sysdate-30/(24*60); -
Selecting records based on user formula
Post Author: Josh@RTA
CA Forum: Formula
I'm writing reports for a company that stores all of their dates as 8 digit numerical fields rather than a date or datetime datatype. I want to convert this field to a date type, then compare it to a parameter that stores user input as a date type.
However, due to the way that Crystal does it's passes over the data, I can't use a selection formula based off of another formula. So I'm wondering , has anyone ever used a selection formula that references another formula and how have you been able to do it? Maybe use group selection instead of record selection? Just not sure.
I'm including the formula I'm using to convert the date, as well as the selection formula so you get an Idea of what I'm doing.
//This converts the numeric 'date' field to a dateshared stringvar DateString := totext({wotrans.ROP_TRAN_DATE}, 0, '');shared datevar ConvertedDate :=If {wotrans.ROP_TRAN_DATE} < 19590101 then Date (1959, 01, 01) else Date ( Val (DateString [1 to 4]), Val (DateString [5 to 6]), Val (DateString [7 to 8]) );
//This is the select statement I'm using to compare the above formula to my parameter using record selection{@ConvertedTransDate} = {?TransDateRange}Post Author: SKodidine
CA Forum: Formula
Replace your formula with this and then equate it to your parameter value in your selection criteria and see if it will work.
If {wotrans.ROP_TRAN_DATE} <= 19590101 then Date (1959, 01, 01)
else
date(
tonumber(totext({wotrans.ROP_TRAN_DATE},0,'','')[1 to 4]),
tonumber(totext({wotrans.ROP_TRAN_DATE},0,'','')[5 to 6]),
tonumber(totext({wotrans.ROP_TRAN_DATE},0,'','')[7 to 8]));
The process might be faster if you convert or change the data type of your parameter to numeric and then compare to the numeric date. -
Select records based on criteria and update those records once read
hi,
I am very new to bpel and DB adapters.
I have a requirement where in I need to query two tables to fetch some records and update these selected records with a new value for field to indicate that bpel has processed these records.
Once I select these I needs the output to be mapped to the output variable.
I am able to select the records based on criteria , but how will i lock these records so that these records do not get processed again. This should be a very simple usecase just that I am not aware.
Thanks,
RobinOnce you have finished reading the records fire an update query , update some field in the table so that it does not get picked up next time.
if you are using polling for picking up the records, then use logical delete scenario, refer....http://docs.oracle.com/cd/E15523_01/integration.1111/e10231/adptr_db.htm#BABEEBIH
Maybe you are looking for
-
I need page no's and top of page header to every page
Hi Experts, i need page no's and top of page header to every page to this report. i took print outs it came only page number 1 to all the pages, after took prints it seems header to all the pages but not displaying in output screen. TYPE-POOLS: SLIS.
-
Ipod Touch 3rd Gen won't sync with windows
I've tried all the different troubleshooting through Apple but can't get my Ipod Touch 3rd Gen to show up on windows much less sync with Itunes. I just went through the process of fixing my Iphone so I know that Itunes is properly installed and the
-
Lightroom: upgrade from trial to full version, how to enter license key?
I installed Lightroom from Creative Cloud (CC) trial on Windows 7 Pro, 64-bit. The version is LR 5.6. I do not want CC, so I purchased the full version of LR on DVD. I was going to reinstall LR from the DVD, but see that it is only version 5.2. I hav
-
Lack of 64-bit version of Reader
Who is to blame for Adobe lack of interest in providing a decent update to the now mainstream 64-bit environments for one of their mainstream products on mainstream Linux distros ? It's a shame Adobe ! I refuse to install dozens of 32-bit libs just t
-
Can't put music on my Iphone after iTunes update and reinstalling my Windows 7 Ultimate
If anyone can help with a suggestion what to do: - i reinstalled my PC to Windows 7 Ultimate and installed latest iTunes version. now I cannot drag and drop any music to my Iphone 4s in iTunes or make playlists to do the same. Does anyone have the sa