bkreynolds48
asked on
Need to retrieve a range of records in one table
I need to retrieve a range of records in one table a min(timestamp) and a max(timestamp) range
SELECT a, b, count(*)
FROM (
SELECT t.ip a, t.signature b
FROM org t
WHERE t.timestamp (this needs to be the starting timestamp)
AND t.timestamp (this needs to be the ending timestamp)
)
GROUP BY a, b
I know you can't use min/max to get those so don't know how to code this.
Do you mean something like this?
WHERE t.timestamp between min(timestamp) and max(timestamp)
That means, you want all the records from the table. No need to include the WHERE clause.
WHERE t.timestamp between min(timestamp) and max(timestamp)
That means, you want all the records from the table. No need to include the WHERE clause.
ASKER
Yes I want to retrieve all records say between 1-FEB-10 and 31-MAR-10
still need a WHERE clause to exclude NULL values
unless timestamp is constrained to be not null
unless timestamp is constrained to be not null
>>> 1-FEB-10 and 31-MAR-10
how are those min and max? do you mean user inputs?
if so
SELECT t.ip a, t.signature b, count(*)
FROM org t
WHERE t.timestamp >= to_date('1-FEB-10','dd-MON -rr')
AND t.timestamp < to_date('31-MAR-10','dd-MO N-rr') + 1
GROUP BY ip, signature
how are those min and max? do you mean user inputs?
if so
SELECT t.ip a, t.signature b, count(*)
FROM org t
WHERE t.timestamp >= to_date('1-FEB-10','dd-MON
AND t.timestamp < to_date('31-MAR-10','dd-MO
GROUP BY ip, signature
this should be ok, no need another wrapping select
SELECT t.ip a, t.signature b, COUNT (1)
FROM org t
WHERE t.timestamp BETWEEN starting_timestamp AND ending_timestamp
GROUP BY ip, signature
SELECT t.ip a, t.signature b, COUNT (1)
FROM org t
WHERE t.timestamp BETWEEN starting_timestamp AND ending_timestamp
GROUP BY ip, signature
note the >= on the lower bound and < on the upper bound with + 1
this is because
31-MAR-2010 13:00:00 > 31-MAR-2010
since a date without a visible time is really a date rounded down to 00:00:00
so, to include all of March 31,
instead check to see if the data is less than April 1
this is because
31-MAR-2010 13:00:00 > 31-MAR-2010
since a date without a visible time is really a date rounded down to 00:00:00
so, to include all of March 31,
instead check to see if the data is less than April 1
>>> BETWEEN starting_timestamp AND ending_timestamp
This has the problem of dropping the end day as noted above
however, if your timestamp will always have all values truncated to midnight (00:00:00) then between will work
or as with the previous post, using <= but not adding 1
t.timestamp <= to_date('31-MAR-10','dd-MO N-rr')
This has the problem of dropping the end day as noted above
however, if your timestamp will always have all values truncated to midnight (00:00:00) then between will work
or as with the previous post, using <= but not adding 1
t.timestamp <= to_date('31-MAR-10','dd-MO
ASKER
sdstuber..........
how are those min and max? do you mean user inputs?
if so
SELECT t.ip a, t.signature b, count(*)
FROM org t
WHERE t.timestamp >= to_date('1-FEB-10','dd-MON
AND t.timestamp < to_date('31-MAR-10','dd-MO
GROUP BY ip, signature
These are not exactly user inputs - this script is a part of a perl script that I am trying to adjust to do this.
The perl script is meant to pull one weeks worth of data from this table and create a pdf report that get shoved into the database. I have not been able to get any query to work inside this perl script to pull out the needed data as I am not very familiar with perl.
"user" -real user, or "user" a script, either way.
they are inputs. they are not values derived from the data , right?
they are inputs. they are not values derived from the data , right?
ASKER
Correct - they are inputs
SELECT ip , signature , count(*)
FROM event_266
WHERE timestamp >= to_date('1-FEB-10','DD-MON -YY')
AND timestamp < to_date('31-MAR-10','DD-MO N-YY') + 1
GROUP BY ip, signature;
returns no data even though there is data to be retrieved.
SELECT ip , signature , count(*)
FROM event_266
WHERE timestamp >= to_date('1-FEB-10','DD-MON
AND timestamp < to_date('31-MAR-10','DD-MO
GROUP BY ip, signature;
returns no data even though there is data to be retrieved.
are you sure there is data?
what query did you use to verify?
also, is timestamp a "date" or "timestamp" type? or is it a string?
what query did you use to verify?
also, is timestamp a "date" or "timestamp" type? or is it a string?
just curious, did you intend to use 2010 data? or 2011?
also YY or (RR) invites reinventing the Y2K bug
I recommend using 4 digits years and YYYY format
also YY or (RR) invites reinventing the Y2K bug
I recommend using 4 digits years and YYYY format
ASKER
timestamp is a date field
I have verified that there is data from January 2010 through April 14 2010
Just changed the query to use the YYYY but still no data returned.
I have verified that there is data from January 2010 through April 14 2010
Just changed the query to use the YYYY but still no data returned.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
If I take out......... the second line I get back 2422 record............in sqlplus but none if I leave the second line in
SELECT ip , signature , count(*)
FROM event_266
WHERE timestamp >= to_date('1-FEB-2010','DD-M ON-YYYY')
GROUP BY ip, signature;
SELECT ip , signature , count(*)
FROM event_266
WHERE timestamp >= to_date('1-FEB-2010','DD-M
GROUP BY ip, signature;
what does this return?
SELECT to_char(timestamp,'yyyy-mm -dd hh24:mi:ss') ts
FROM event_266
WHERE timestamp >= to_date('1-FEB-2010','DD-M ON-YYYY')
and rownum < 20
SELECT to_char(timestamp,'yyyy-mm
FROM event_266
WHERE timestamp >= to_date('1-FEB-2010','DD-M
and rownum < 20
oops, try this instead, sorry
select * from (
SELECT to_char(timestamp,'yyyy-mm -dd hh24:mi:ss') ts
FROM event_266
WHERE timestamp >= to_date('1-FEB-2010','DD-M ON-YYYY')
order by ts)
and rownum < 20
select * from (
SELECT to_char(timestamp,'yyyy-mm
FROM event_266
WHERE timestamp >= to_date('1-FEB-2010','DD-M
order by ts)
and rownum < 20
ASKER
That returns 19 rows
what is the output?
ASKER
SELECT to_char(timestamp,'yyyy-mm -dd hh24:mi:ss') ts
FROM event_266
WHERE timestamp >= to_date('1-FEB-2010','DD-M ON-YYYY')
and rownum < 20 3 4 ;
TS
-------------------
2011-03-19 13:00:26
2011-03-19 13:39:58
2011-03-19 13:39:58
2011-03-19 13:40:01
2011-03-19 13:46:03
2011-03-19 13:58:11
2011-03-19 14:13:01
2011-03-19 14:13:05
2011-03-19 14:31:23
2011-03-19 15:11:10
2011-03-19 15:11:10
2011-03-19 15:13:24
2011-03-19 15:17:03
2011-03-19 16:10:06
2011-03-19 16:11:45
2011-03-19 17:02:57
2011-03-19 17:02:57
2011-03-19 18:30:54
2011-03-19 18:30:54
19 rows selected.
SELECT to_char(timestamp,'yyyy-mm -dd hh24:mi:ss') ts
FROM event_266
WHERE timestamp >= to_date('1-FEB-2010','DD-M ON-YYYY')
order by ts)
and rownum < 20 4 5 6 ;
and rownum < 20
*
ERROR at line 6:
ORA-00933: SQL command not properly ended
FROM event_266
WHERE timestamp >= to_date('1-FEB-2010','DD-M
and rownum < 20 3 4 ;
TS
-------------------
2011-03-19 13:00:26
2011-03-19 13:39:58
2011-03-19 13:39:58
2011-03-19 13:40:01
2011-03-19 13:46:03
2011-03-19 13:58:11
2011-03-19 14:13:01
2011-03-19 14:13:05
2011-03-19 14:31:23
2011-03-19 15:11:10
2011-03-19 15:11:10
2011-03-19 15:13:24
2011-03-19 15:17:03
2011-03-19 16:10:06
2011-03-19 16:11:45
2011-03-19 17:02:57
2011-03-19 17:02:57
2011-03-19 18:30:54
2011-03-19 18:30:54
19 rows selected.
SELECT to_char(timestamp,'yyyy-mm
FROM event_266
WHERE timestamp >= to_date('1-FEB-2010','DD-M
order by ts)
and rownum < 20 4 5 6 ;
and rownum < 20
*
ERROR at line 6:
ORA-00933: SQL command not properly ended
you left off part of the 2nd query
try it again, with the whole thing.
based on the results of the first query you have 2011 data, but your date range above is for 2010 as noted in http:#35507152
the 2nd query will determine absolutely if that's the problem or not
try it again, with the whole thing.
based on the results of the first query you have 2011 data, but your date range above is for 2010 as noted in http:#35507152
the 2nd query will determine absolutely if that's the problem or not
if so, that's not really a contraint. Every value will be between min and max.
instead, just say WHERE t.timestamp is not null
if that's not what you mean, please elaborate.
even better, provide sample data and expected results