Min and Max of sequential number
Hello
I have a table called pins (lot number(4),
sequence number ( 8)
In the table I have data:
lot sequence
386 100
386 101
386 102
386 800
386 801
387 1
387 2
I want to know by lot the low and high values for sequential number, the result I am looking for is
lot min(sequnce), max(sequence)
386 100 102
386 800 801
387 1 2
Oracle 10g rel2
Thanks
Hi,
You want something like this:
WITH got_grp AS
SELECT lot
, seq_num -- SEQUENCE is not a good column name
, seq_num - ROW_NUMBER () OVER ( PARTITION BY lot
ORDER BY seq_num
) AS grp
FROM pins
-- WHERE ... -- any filtering goes here
SELECT lot
, MIN (seq_num) AS min_seq_num
, MAX (seq_num) AS max_seq_num
FROM got_grp
GROUP BY lot
, grp
ORDER BY lot
, min_seq_num
;As I understand the probem, you want to GROUP BY, but lot doesn't define the groups by itself: each contiguous set of seq_nums in a lot is a separate group. In each of those groups, the difference between seq_num and a counter reflecting the order of seq_num generated in the query (using ROW_NUMBER) will be constant.
This assumes that seq_num is an integer, and that (lot, seq_num) is unique.
If not, the query above will need a little adjustment.
If you'd like to post INSERT statements for your sample data, then I could test this.
Similar Messages
-
Calculating min and max value from number array
I have array which is having return type Number.i want to calculate min and max value of this array..try to help asap..
Thanks
dhanuhi there,
public class MinMax {
int arr[]= { 27, 3, 42, 18, 243, -43, 6, -8 };
int min, max;
public MinMax() {
// it's very bad way to do it.
// min=99999;
// max=-99999;
// this must be as following
min = max = arr[0];
search();
System.out.println("The minimum value is: "+min);
System.out.println("The maximum value is: "+max);
private void search() {
// and here we can search from <1> index instead of <0>
for (int j=1; j<arr.length; j++) {
if (arr[j]<min) {
min=arr[j];
else if (arr[j]>max) {
max=arr[j];
public static void main(String args[]) {
new MinMax();
there is a bug in the supplied code. Your homework is
to figure out what it is without posting another
message for help with your homework.well, i think it's a tragedy :( -
Min and MAx Value in a SELECT Statement
Hi,
I have a scenario where I am Selecting the values BETWEEN MIN and MAX values:
SELECT * FROM ABC WHERE CODE BETWEEN MIN(CODE) AND MAX(CODE)
ITS GETTING Error as:ORA-00934: group function is not allowed here
Any help will be needful for me.select substr(no,1,3)||to_char(substr(no,4,1)+1) "first missing number"
from
with t as
(select 'ABC1' no from dual
union select 'ABC2' from dual
union select 'ABC3' from dual
union select 'ABC5' from dual
union select 'ABC6' from dual
union select 'ABC8' from dual
select no, lead(no,1,0) over (order by no) next_no from t
where substr(next_no,4,1) - substr(no,4,1) > 1
and rownum = 1; -
Difference of value of a dimension based on min and max
Database: Oracle 10g
BO-BOXIr3
Let me explain the exact problem again.
As per the below code, I have the data in this format in my table:
Code:
Date Site ID KWH
1/2/2009 00:00 IN-1 22
1/2/2009 01:00 IN-1 28
1/3/2009 03:00 IN-2 25
1/3/2009 04:00 IN-2 46
1/4/2009 00:00 IN-3 28
1/4/2009 10:00 IN-3 34
1/5/2009 08:00 IN-4 31
1/5/2009 09:00 IN-4 55
1/5/2009 11:00 IN-4 77
1/6/2009 00:00 IN-5 34
Now want to build a report with following columns:
Site Count KWH
IN-1 2 6 (ex.-28-22)
IN-2 2 21
IN-3 2 6
IN-4 3 46 (ex.-77-31)
IN-5 2 34
SITE- distinct site name.
COUNT-count is number of repetitions of site id between min and max date.
KWH -(Delta between the min and max date)
To get the above result I have created 3 report from different queries since not able to get these al in a single report viz Count, Max Value and Min value. Well I have all these 3 reports or table on a single page.
Count-this report will give the count between the dates
Max Value-this report will give me the values of kwh for max dates for each site id
Min Value-this report will give me the values of kwh for min dates for each site id
Now want to create a single report based on these 3 reports which contains the column
Site|Count|KWH
IS IT POSSIBLE?
Or
Is it possible to build such report in a single one with all the required column which I mentioned?
The variables which I created to get the max & min dates,
Mx_dt= =Max([Query 2].[Hourly]) In ([Query 2].[SITE_ID])
Mn_dt= =Min([Query 3 (12)].[Hourly]) In ([Query 3 (12)].[SITE_ID])
For filtering on report used following variables:
if_st_mn=If([mn_dt])=[Hourly] Then "ok" Else "no"
if_st_mx =If([mx_dt])=[Hourly] Then "ok" Else "no"
will filter on "ok" to get the max and min date values.
rest of the variable in the snap are not usable.Yes, you can do it in one report.
I created a sample report from efashion:
Year | Lines | Sales Revenue
2001 | Accessories | $250
2003 | Accessories | $550
2001 | City Skirts | $1050
2003 | City Skirts | $1150...........
Create 2 variables 1) Count and 2) Difference:
1) Count as formula - =Count([Lines]) In Report
2) Difference as formula - =Sum([Sales revenue]) Where (Max([Year]) In Report = [Year]) - Sum([Sales revenue]) Where (Min([Year]) In Report = [Year])
You can replace the formula with your report variables. Then just drag Site ID, Count and Difference variables to your report.
Thanks
Jai -
MIN and MAX datetimes ti find range
I am using Oracle 11g version
create table re(Name char(20),Datetime char(45),val1 number);
insert into re values('abc','10/29/2012 13:00','1.5')
insert into re values('abc','10/29/2012 13:05','1.5')
insert into re values('abc','10/29/2012 13:10','1.5')
insert into re values('abc','10/29/2012 13:15','1.5')
insert into re values('abc','10/29/2012 13:20','0.00')
insert into re values('abc','10/29/2012 13:25','0.00')
insert into re values('abc','10/29/2012 13:30','0.00')
insert into re values('abc','10/29/2012 13:35','0.00')
insert into re values('abc','10/29/2012 13:40','2.1')
insert into re values('abc','10/29/2012 13:45','2.3')
insert into re values('abc','10/29/2012 13:50','2.1')
insert into re values('abc','10/29/2012 13:55','2.1')
insert into re values('abc','10/29/2012 14:00','2.2')
O/P:
In this way data is stored in database.Needed output is, I want the datetime column data range with min and max values where val1>0 only.
Expected result while we consider the above data is::
Name mintime maxtime
abc 10/19/2012 13:00 10/19/2012 13:15
abc 10/29/2012 13:40 10/29/2012 14:00
For this I tried something like this,
select name, min(to_date(Datetime ,'mm/dd/yyyy hh24:mi')) start, max(to_date(Datetime ,'mm/dd/yyyy hh24:mi')) end from (
select name, Datetime ,to_date(Datetime ,'mm/dd/yyyy hh24:mi') - rank() over (partition by loc_name order by t1 asc) Val_col from re where val1 > 0
) group by lname, Val_col
but I am getting the output like this for above query.
name start end
abc 10/29/2012 13:00 10/29/2012 13:00
abc 10/29/2012 13:05 10/29/2012 13:05
so.on.
Edited by: 913672 on Apr 3, 2013 3:07 AM913672 wrote:
I am using Oracle 11g version
create table re(Name char(20),Datetime char(45),val1 number);
insert into re values('abc','10/29/2012 13:00','1.5')
insert into re values('abc','10/29/2012 13:05','1.5')
insert into re values('abc','10/29/2012 13:10','1.5')
insert into re values('abc','10/29/2012 13:15','1.5')
insert into re values('abc','10/29/2012 13:20','0.00')
insert into re values('abc','10/29/2012 13:25','0.00')
insert into re values('abc','10/29/2012 13:30','0.00')
insert into re values('abc','10/29/2012 13:35','0.00')
insert into re values('abc','10/29/2012 13:40','2.1')
insert into re values('abc','10/29/2012 13:45','2.3')
insert into re values('abc','10/29/2012 13:50','2.1')
insert into re values('abc','10/29/2012 13:55','2.1')
insert into re values('abc','10/29/2012 14:00','2.2')
O/P:
In this way data is stored in database.Needed output is, I want the datetime column data range with min and max values where val1>0 only.
Expected result while we consider the above data is::
Name mintime maxtime
abc 10/19/2012 13:00 10/19/2012 13:15
abc 10/29/2012 13:40 10/29/2012 14:00
For this I tried something like this,
select name, min(to_date(Datetime ,'mm/dd/yyyy hh24:mi')) start, max(to_date(Datetime ,'mm/dd/yyyy hh24:mi')) end from (
select name, Datetime ,to_date(Datetime ,'mm/dd/yyyy hh24:mi') - rank() over (partition by loc_name order by t1 asc) Val_col from re where val1 > 0
) group by lname, Val_col
but I am getting the output like this for above query.
name start end
abc 10/29/2012 13:00 10/29/2012 13:00
abc 10/29/2012 13:05 10/29/2012 13:05
so.on.Firstly and most importantly do NOT store dates as char columns: that's what the DATE type is for.
Secondly, i'm not sure how you get your output from the input, particularly as your SQL contains 'partition by loc_name' which
doesn't even exist in your example table.
Why has the same name got two rows with those min/max times. Are you partitioning by val1? -
Setting up Min and Max on Forms
I am trying to set up a swim lesson sign up form and want to set a minimum and maximum number of kids that can sign up for any given class. Is this possible in Adobe Forms?
We do not support a min and max number of response. You would have to close the enrollment form manually once the max is reached. This is a feature you could vote for on our feature request page.
-
I have a datetime axis stuck on 1970 for both min and max..
I have a datetime axis stuck on 1970 for both min and max..
The lineseries dataprovider has an array collection with objects
with each object having a dateAdded2 attribute of format mm/dd/yyyy
and the dates are in this year 2008. the objects also have an
attribute called amount which is a number.
eg
object.dateAdded2 = "07/14/2008"
object.amount =5
Anyone with any idea what is going wrong? Similar type of
data works fine in my other charts.Pull the CPU cover and look for leaks from the LCS.
If the gray mat is wet, the LCS has leaked and the machine will soon be dead.
PowerMac G5 Coolant Leaks/Repairs
PowerMac G5 Coolant Leak Repair/Overhaul (w/photos) -
Find min and max from a bunch of numbers
hi,
i have a bunch of numbers, and i need to find the min and max.
so i have a arraylist which contains objects....whats the best way to find the min number and max number of all these objects?
thanksYou're close, but you can do them all at the same time without temp variables..
double maximumX = 0;
double minimumX = 0;
double maximumY = 0;
double minimumY = 0;
for (int i = 0; i < myobjects.size(); i++) {
mything d2d = (mything) myobjects.get(i);
maximumX = Math.max(d2d.getBounds().getMaxX(), maximumX);
minimumX= Math.min(d2d.getBounds().getMinX(), minimumX);
maximumY = Math.max(d2d.getBounds().getMaxY(), maximumY);
minimumY = Math.min(d2d.getBounds().getMinY(), minimumY);
}Since you are dealing with rectangles, you could add them all to an Area and get the bounds of the Area, but that's probably slower. -
Query about min and max and middle row of a table
suppose i have table emp and field
v_date date;
which has data in time stamp
time
10:20
10:25
10:30
10:32
10:33
10:35
10:36
10:38
I need only min time and max time and two record between min and maxI need only min time and max time and two record between min and max Like this?
SQL> create table t (id number);
Table created.
SQL>
SQL> insert into t values (1020);
1 row created.
SQL> insert into t values (1025);
1 row created.
SQL> insert into t values (1030);
1 row created.
SQL> insert into t values (1032);
1 row created.
SQL> insert into t values (1033);
1 row created.
SQL> insert into t values (1035);
1 row created.
SQL> insert into t values (1036);
1 row created.
SQL> insert into t values (1038);
1 row created.
SQL>
SQL> commit;
Commit complete.
SQL>
SQL> select * from t;
ID
1020
1025
1030
1032
1033
1035
1036
1038
8 rows selected.
SQL>
SQL>
SQL> select decode(rownum, 1, min_val, 4, max_val, next_val) your_data from (
2 select first_value (id) over (partition by 'a' order by 'a') min_val,
3 last_value (id) over (partition by 'a' order by 'a') max_val,
4 id,
5 lead(id) over (partition by 'a' order by id) next_val
6 from t
7 order by id
8 )
9 where min_val <> next_val and max_val <> next_val
10 and rownum <= 4;
YOUR_DATA
1020
1030
1032
1038
SQL> -
Find more than one min and max in 2D array contain 0 rows
Hi
I have a 2D array and I would like to find the max and min elements between series of 0s. As you can see in the picture If I remove all 0s from the array and use the max and min function then I will have just one min and one max but I need to find min and max after every 0 rows so you can see from the picture ( just as an example) I will have 3 min and 3 max numbers. Would you please help me with this code. Do you now any algorithm that can find min and max between 0s?
I have also attached my code to remove 0s and then search for max and min numbers but as I mentioned I need min and max for every part
Many thanks
Attachments:
2.jpg 82 KB
3.jpg 27 KBThanks altenbach
I have attached the vi to this post. I would really apperciate if you help me with this example. The min values should be 100, 1500 and 4000 and the max values should be 1200,2600,5400 so as u mentioned the output should be this 2D array
100,1200
1500,2600
4000,5400
Attachments:
new.vi 6 KB -
I shutdown Zone Alarm, and deleted the contents of the file, which contained the "exe" file, then reinstalled Firefox version 5.0. I still get the XUL Runner error message about incompatibility of Version 5.0 with min and max 5.0.1. What next?
The updater wasn't able to update all the files and some were left as older versions.<br />
You need to do a clean reinstall.
Download a fresh Firefox copy and save the file to the desktop.
* Firefox 5.0.x: http://www.mozilla.com/en-US/firefox/all.html
* Uninstall your current Firefox version.
* Do not remove personal data when you uninstall the current version.
Remove the Firefox program folder before installing that newly downloaded copy of the Firefox installer.
* It is important to delete the Firefox program folder to remove all the files and make sure that there are no problems with files that were leftover after uninstalling.
Your bookmarks and other profile data are stored elsewhere in the Firefox Profile Folder and won't be affected by a reinstall, but make sure that you do not select to remove personal data if you uninstall Firefox.
* http://kb.mozillazine.org/Profile_folder_-_Firefox
* http://kb.mozillazine.org/Profile_backup -
Performance Issue using min() and max() in one SQL statement
I have a simple query that selects min() and max() from one column in a table in one sql statment.
The table has about 9 Million rows and the selected column has a non unique index. The query takes 10 secs. When i select min() and max() in separate statements, each takes only 10 msecs:
This statement takes 10 secs:
select min(date_key) , max(date_key)
from CAPS_KPIC_BG_Fact_0_A
where date_key != TO_DATE(('1900-1-1' ),( 'YYYY-MM-DD'))
This statement takes 10 msecs:
select min(date_key)
from MYTABLE
where date_key != TO_DATE(('1900-1-1' ),( 'YYYY-MM-DD'))
union all
select max(date_key) from MYTABLE
Because the first statement is part of an autmatic generated SQL of an application, i can't change it and i have to optimize the data model. How can i speed up the first statement?I've ran similar query on a table that has 10 milliion rows, with an index on the date column
This is what I have found:
SQL> set timing on
1 SELECT MIN(ID_DATE) MIN_DATE, MAX(ID_DATE) MAX_DATE
2 FROM MY_DATE
3* WHERE ID_DATE != TO_DATE(('1900-1-1' ),( 'YYYY-MM-DD'))
SQL> /
MIN_DATE MAX_DATE
03-APR-76 06-JAN-02
real: 43383
SQL> SELECT MIN(ID_DATE) MIN_DATE FROM MY_DATE
2 WHERE ID_DATE != TO_DATE(('1900-1-1' ),( 'YYYY-MM-DD'))
3 UNION ALL
4 SELECT MAX(ID_DATE) MAX_DATE FROM MY_DATE
5 WHERE ID_DATE != TO_DATE(('1900-1-1' ),( 'YYYY-MM-DD'))
6 /
MIN_DATE
03-APR-76
06-JAN-02
real: 20
SQL> SELECT MIN_DATE, MAX_DATE FROM
2 (SELECT MAX(ID_DATE) MAX_DATE FROM MY_DATE
3 WHERE ID_DATE != TO_DATE(('1900-1-1' ),( 'YYYY-MM-DD')) ) A,
4 (SELECT MIN(ID_DATE) MIN_DATE FROM MY_DATE
5 WHERE ID_DATE != TO_DATE(('1900-1-1' ),( 'YYYY-MM-DD')) ) B
6 /
MIN_DATE MAX_DATE
03-APR-76 06-JAN-02
real: 10
SQL> My conculsion, there is nothing you can do to the tables that will improve that particular statement.
Why can't you modify the application? -
In my database design and implementation module I'm at the implementation stage. I've set up all of my tables with PK and FK constraints and I'm wondering if you can enforce min and max column constraints. One of the rules of my DB is for my staff table there has to be a minimum of 10 and a maximum of 50, how would I enforce this rule as a constraint on the staff table or isn't this possible?
We have only been learning database design and SQL fundamentals so I don't think I'd be allowed to use triggers or procedures.
Many thanks
Mikemharper wrote:
thanks guys these commands were accepted
alter table staff
add constraint chk_staff_max check (staff_id <= 50);
alter table staff
add constraint chk_staff_min check (staff_id >= 10);
Thing is my staff_id's are in the format 'S110001' will these constraints only check for integers 10 - 50? maybe I need to use a count function on the column or something?
thanks again for your help!
MikeCheck constraint can also be used to check a list of values.
example:
ALTER TABLE <table_name>
ADD CONSTRAINT <constraint_name>
CHECK (<column_name> LIKE <condition>);
ALTER TABLE <table_name>
ADD CONSTRAINT <constraint_name>
CHECK (<column_name> NOT LIKE <condition>);
ALTER TABLE <table_name>
ADD CONSTRAINT <constraint_name>
CHECK (<column_name>
IN (<comma delimited list of values>);
ALTER TABLE <table_name>
ADD CONSTRAINT <constraint_name>
CHECK (<column_name>
NOT IN (<comma delimited list of values>);
ALTER TABLE <table_name>
ADD CONSTRAINT <constraint_name>
CHECK (<column_name> BETWEEN <lower_value>
AND <higher_value>);
ALTER TABLE <table_name>
ADD CONSTRAINT <constraint_name>
CHECK (<column_name>
NOT BETWEEN <lower_value> AND <higher_value>);
ALTER TABLE <table_name>
ADD CONSTRAINT <constraint_name>
CHECK (<column_name>) > (<condition>); -
Formula for MIN and MAX per range required in BW
Good day
Please assist with the following? I need to create a formula (CKF) in BW Query which will give me the same result as the following formula in Excel.
What I have is one Key Figure and want to calculate a Score on that key figure.
Score =Key figure/(MAX(range of ie. column B1 to B50 which contains data for the same KF)-MIN(range of ie. column B50 which contains data for the same KF).
In excel it's easy, ie" Score =TD completed/(MAX(B28:B130)-MIN(B28:B130)
If more information is required to assist, please let me know?
Always appreciated.
CorneliusThank U so much for responding so fast.
I did the MIN and MAX formula, but it is only taking the MIN or MAX from the single cell of the applicable key figure and not from a 'range' of cells. This is a further example of what I want to do:
Cell: B1 = 10
B2 = 12
B3 = 15
B4 = 0
I want to display the MIN as '0' and the MAX as '15' and then do the calculation per row.
We are in 3.2x. I want to stay away from structures as I have not done that before.
Any other assistance will be highly appreciated.
Cornelius -
To find out the min and max memory been used by each parameter under SGA_MA
Hi,
Can any please tell me how to find out the min and max memory been used by each parameter under SGA_MAX and SGA_TARGET ? below is the db CRMS65T. If any such script is there please provide me
SQL> select name from v$database;
NAME
CRMS65T
SQL> show parameter sga
NAME TYPE VALUE
lock_sga boolean FALSE
pre_page_sga boolean FALSE
sga_max_size big integer 1000M
sga_target big integer 1000MThanks in advanceCan any please tell me how to find out the min and max memory been used by each parameter under SGA_MAX and SGA_TARGET ? below is the db CRMS65T. If any such script is there please provide meI guess your question is, each memory components of SGA? if so
SQL> select * from v$sgainfo;
NAME BYTES RES
Fixed SGA Size 2088504 No
Redo Buffers 18882560 No
Buffer Cache Size 616562688 Yes
Shared Pool Size 301989888 Yes
Large Pool Size 4194304 Yes
Java Pool Size 4194304 Yes
Streams Pool Size 0 Yes
Granule Size 4194304 No
Maximum SGA Size 947912704 No
Startup overhead in Shared Pool 125829120 No
Free SGA Memory Available 0Also check
SQL> select COMPONENT,CURRENT_SIZE,MIN_SIZE,MAX_SIZE,USER_SPECIFIED_SIZE from v$sga_dynamic_components;
shared pool 301989888 301989888 0 209715200
large pool 4194304 4194304 0 4194304
java pool 4194304 4194304 0 4194304
streams pool 0 0 0 0
DEFAULT buffer cache 616562688 616562688 0 603979776
KEEP buffer cache 0 0 0 0
RECYCLE buffer cache 0 0 0 0
DEFAULT 2K buffer cache 0 0 0 0
DEFAULT 4K buffer cache 0 0 0 0
DEFAULT 8K buffer cache 0 0 0 0
DEFAULT 16K buffer cache 0 0 0 0
DEFAULT 32K buffer cache 0 0 0 0
ASM Buffer Cache 0 0 0 603979776
13 rows selected.
SQL>Edited by: CKPT on Sep 19, 2011 8:55 AM
Maybe you are looking for
-
Reverting to iPhoto '09?
Anyone know the smoothest way to go back to '09? TimeMachine back-up? There are just too many bugs and too many missing features in '11. It really is next to useless. I hope these are short term problems & that Apple will bring it back to being a use
-
How to configure time synchronization for two NTP servers
We have IOSXR 4.2.1 on routers CRS3 and ASR9K with all recomended SMUs; we need to configure the time synchronization for two NTP servers with the configuration below, but the routers became unstable; synchronize with one NTP servers for some time, t
-
T430 Won't Boot - ThinkPad startup screen just cycles
Please help... Have been using my T430 as usual this morning, all running fine...then...battery died while using it, so went to get the charger, now have plugged in and powered back on, and it will only get to the "ThinkPad" loading screen. It won't
-
Uni project methods & negative numbers arrghhh
Hi there i have written a program for uni it creates a random number & gives the user 3 guesses at getting the number before giving them a message saying they have lost and revealing the actual number. So far so good . I managed to get this working b
-
My Digital Copy won't transfer to my iPad
I bought A Bug's Life on Blu-ray that included a digital copy. I "purchased" the digital copy in iTunes using the included code. The movie works and plays fine in iTunes, but it won't transfer over to my iPad at all. I have the latest iTunes and i