473,594 Members | 2,770 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

staggered query?

hi! im new to SQL, and i need to find a solution
to this problem:

i have a table with two columns, the first column
is of type timestamp.

the table contains hundreds of thousands of records.
i need to get all the entries/records at every 10 seconds
interval. example, given a table:

hh/mm/ss | data
---------------
00:00:00 1
00:00:01 2
00:00:02 3
00:00:03 4
00:00:04 5
00:00:05 6
00:00:06 7
00:00:07 8
...
...

my query should return:
00:00:10
00:00:20
00:00:30
(etc)

is this possible? if yes, how do i do it?

thanks!

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.484 / Virus Database: 282 - Release Date: 5/27/2003

---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

Nov 23 '05 #1
4 1272
On Wed, Apr 21, 2004 at 10:53:16AM +0800, Vincent Ladlad wrote:

the table contains hundreds of thousands of records.
i need to get all the entries/records at every 10 seconds
interval. example, given a table:

hh/mm/ss | data
---------------
00:00:00 1
00:00:01 2
00:00:02 3
00:00:03 4
00:00:04 5
00:00:05 6
00:00:06 7
00:00:07 8
..
..

my query should return:
00:00:10
00:00:20
00:00:30
(etc)


If I understood your problem, the only solution i found was write a
simple plpgsql function (read at the end of the mail).

Don't know if it is the best solution but it works !

Ciao,
Federico.

------------------------------------------------------------------------

Usage :

select * from timetable ('23/06/1974 18:15', '23/06/1974 20:30', '00:10');
timetable
---------------------
1974-06-23 18:15:00
1974-06-23 18:25:00
1974-06-23 18:35:00
1974-06-23 18:45:00
1974-06-23 18:55:00
....
CREATE OR REPLACE FUNCTION timetable(times tamp, timestamp, interval) RETURNS SETOF timestamp
AS '
DECLARE
inizio alias for $1;
fine alias for $2 ;
inter alias for $3;

tt timestamp;
BEGIN
tt := inizio;

while tt <= fine loop
return next tt;
tt:=tt+inter;
end loop;
RETURN ;
END;
'
LANGUAGE plpgsql IMMUTABLE;

---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

Nov 23 '05 #2
On Wed, Apr 21, 2004 at 10:53:16AM +0800, Vincent Ladlad wrote:

the table contains hundreds of thousands of records.
i need to get all the entries/records at every 10 seconds
interval. example, given a table:

hh/mm/ss | data
---------------
00:00:00 1
00:00:01 2
00:00:02 3
00:00:03 4
00:00:04 5
00:00:05 6
00:00:06 7
00:00:07 8
..
..

my query should return:
00:00:10
00:00:20
00:00:30
(etc)


If I understood your problem, the only solution i found was write a
simple plpgsql function (read at the end of the mail).

Don't know if it is the best solution but it works !

Ciao,
Federico.

------------------------------------------------------------------------

Usage :

select * from timetable ('23/06/1974 18:15', '23/06/1974 20:30', '00:10');
timetable
---------------------
1974-06-23 18:15:00
1974-06-23 18:25:00
1974-06-23 18:35:00
1974-06-23 18:45:00
1974-06-23 18:55:00
....
CREATE OR REPLACE FUNCTION timetable(times tamp, timestamp, interval) RETURNS SETOF timestamp
AS '
DECLARE
inizio alias for $1;
fine alias for $2 ;
inter alias for $3;

tt timestamp;
BEGIN
tt := inizio;

while tt <= fine loop
return next tt;
tt:=tt+inter;
end loop;
RETURN ;
END;
'
LANGUAGE plpgsql IMMUTABLE;

---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

Nov 23 '05 #3
Federico Pedemonte <fe****@email.i t> writes:
On Wed, Apr 21, 2004 at 10:53:16AM +0800, Vincent Ladlad wrote:

the table contains hundreds of thousands of records.
i need to get all the entries/records at every 10 seconds
interval. example, given a table:

hh/mm/ss | data
---------------
00:00:00 1
00:00:01 2
00:00:02 3
00:00:03 4
00:00:04 5
00:00:05 6
00:00:06 7
00:00:07 8
..
..

my query should return:
00:00:10
00:00:20
00:00:30
(etc)


If I understood your problem, the only solution i found was write a
simple plpgsql function (read at the end of the mail).


There are plenty of solutions for this using standard SQL or non-standard but
still plain SQL queries.

Do you have exactly one sample for every second? And do you want precisely the
first second of the ten second interval? If so then all you really need are
every row where the seconds are divisible by 10.

select * from table where hhmmss::abstime ::integer % 10 = 0;

(there are probably more standard ways of testing if the seconds are divisible
by 10, but this is the first way that came to mind)

If you don't always have a sample for every second and just want the first
sample from each ten second interval you could do something like:

select distinct on (hhmmss::abstim e::integer / 10) hhmmss order by hhmmss::abstime ::integer / 10;

but i expect that would be slower since it would have to do a big sort.

--
greg
---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddres sHere" to ma*******@postg resql.org)

Nov 23 '05 #4
Federico Pedemonte <fe****@email.i t> writes:
On Wed, Apr 21, 2004 at 10:53:16AM +0800, Vincent Ladlad wrote:

the table contains hundreds of thousands of records.
i need to get all the entries/records at every 10 seconds
interval. example, given a table:

hh/mm/ss | data
---------------
00:00:00 1
00:00:01 2
00:00:02 3
00:00:03 4
00:00:04 5
00:00:05 6
00:00:06 7
00:00:07 8
..
..

my query should return:
00:00:10
00:00:20
00:00:30
(etc)


If I understood your problem, the only solution i found was write a
simple plpgsql function (read at the end of the mail).


There are plenty of solutions for this using standard SQL or non-standard but
still plain SQL queries.

Do you have exactly one sample for every second? And do you want precisely the
first second of the ten second interval? If so then all you really need are
every row where the seconds are divisible by 10.

select * from table where hhmmss::abstime ::integer % 10 = 0;

(there are probably more standard ways of testing if the seconds are divisible
by 10, but this is the first way that came to mind)

If you don't always have a sample for every second and just want the first
sample from each ten second interval you could do something like:

select distinct on (hhmmss::abstim e::integer / 10) hhmmss order by hhmmss::abstime ::integer / 10;

but i expect that would be slower since it would have to do a big sort.

--
greg
---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddres sHere" to ma*******@postg resql.org)

Nov 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
3423
by: jaysonsch | last post by:
Hello! I am having some problems with a database query that I am trying to do. I am trying to develop a way to search a database for an entry and then edit the existing values. Upon submit, the new values are updated in all corresponding tables (the function of the pages in question). However, on the page that does the DB update, I also want to do some checks on the data before performing the update. Now, the problem that I am...
29
2477
by: shank | last post by:
1) I'm getting this error: Syntax error (missing operator) in query expression on the below statement. Can I get some advice. 2) I searched ASPFAQ and came up blank. Where can find the "rules" for when and how to use single quotes and double quotes in ASP? thanks! ---------------------- SQL = SQL & "WHERE '" & REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE("GenKTitles.
9
3120
by: netpurpose | last post by:
I need to extract data from this table to find the lowest prices of each product as of today. The product will be listed/grouped by the name only, discarding the product code - I use SUBSTRING(ProductName, 1, CHARINDEX('(', ProductName)-2). I can get this result, but I had to use several views (totally inefficient). I think this can be done in one efficient/fast query, but I can't think of one. In the case that one query is not...
5
5306
by: Stephen Miller | last post by:
Hi, I am trying to add a staggered running total and average to a query returning quarterly CPI data. I need to add 4 quarterly data points together to calculate a moving 12-month sum (YrCPI), and then to complicate things, calculate a moving average of the 12-month figure (AvgYrCPI). Given the sample data:
3
5378
by: Harvey | last post by:
Hi, I try to write an asp query form that lets client search any text-string and display all pages in my web server that contain the text. I have IIS 6.0 on a server 2003. The MSDN site says there is a sample file called Ixtrasp.asp, but I could not find it in my system although I installed indexing service. I followed the steps in MSDN site to create a basic .asp query form (too long to post it here), but it always displays: No...
4
2138
by: Diamondback | last post by:
I have two tables, WIDGETS and VERSIONS. The WIDGETS table has descriptive information about the widgets while the VERSIONS table contains IDs relating to different iterations of those widgets over the years. The idea is that from any widget in the database you can look forward and backward to see different versions of the widget through the years. Here are the tables: WIDGETS widget_id name
14
3871
by: Dave Thomas | last post by:
If I have a table set up like this: Name | VARCHAR Email | VARCHAR Age | TINYINT | NULL (Default: NULL) And I want the user to enter his or her name, email, and age - but AGE is optional. My insert would look something like:
2
18690
by: Dom | last post by:
I need to run a query based on a query in Access. The second query has a number of conditions which all work well, but there is one more contition I need to set to make it run properly. the first query returns a number of data items, consisting, among other fields, of a Company_ID and a Rating_Date. For most companies, the latest value of Rating_Date is equal to a value in a seperate table (tblVariables) which logs the last time a...
0
760
by: Vincent Ladlad | last post by:
hi! im new to SQL, and i need to find a solution to this problem: i have a table with two columns, the first column is of type timestamp. the table contains hundreds of thousands of records. i need to get all the entries/records at every 10 seconds interval. example, given a table:
0
7936
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7874
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8366
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7997
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6646
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5738
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
3853
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
1469
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1203
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.