473,703 Members | 2,340 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

SQL/Query to pick every "x"th value

Hi guys,

I have a user who wants to have a report that shows only the 10th
value recorded by his equipment. (i.e. His equipment records
temperature every 30 seconds, but he only wants to view the data at
every 5 minutes.)

I proposed using the "mod" function in a WHERE statement on the
seconds "datepart" to test if each datapoint is an interval of 5
minutes from the starting number.

Our concern is that there will be drift or reset in the datalogging
time stamps, and the MOD method will break because the seconds won't
be exact.

So, I know you can step through a recordset at an interval. And I
know you can do interval grouping on reports. But, I don't know how
to write the SQL to simply kick that out. Every search I do for "SQL
skip" or "SQL interval" gives me lots of basic questions about
skipping single records or finding dates "since" an event.

Is there an accepted way to set a query/SQL statement that simply
picks every 5th value? (Could I do a recordset stepper with a 5 on my
gotorecord interval and write each value to a temporary table? I
think so, but -yeck- that seems like a hack.)

TIA,

Jon

Apr 23 '07 #1
4 4102
jonceramic wrote:
Hi guys,

I have a user who wants to have a report that shows only the 10th
value recorded by his equipment. (i.e. His equipment records
temperature every 30 seconds, but he only wants to view the data at
every 5 minutes.)

I proposed using the "mod" function in a WHERE statement on the
seconds "datepart" to test if each datapoint is an interval of 5
minutes from the starting number.

Our concern is that there will be drift or reset in the datalogging
time stamps, and the MOD method will break because the seconds won't
be exact.

So, I know you can step through a recordset at an interval. And I
know you can do interval grouping on reports. But, I don't know how
to write the SQL to simply kick that out. Every search I do for "SQL
skip" or "SQL interval" gives me lots of basic questions about
skipping single records or finding dates "since" an event.

Is there an accepted way to set a query/SQL statement that simply
picks every 5th value? (Could I do a recordset stepper with a 5 on my
gotorecord interval and write each value to a temporary table? I
think so, but -yeck- that seems like a hack.)

TIA,

Jon
There is a VBA + SQL solution here:

http://www.mvps.org/access/queries/qry0006.htm

--
Smartin
Apr 23 '07 #2
On Apr 23, 5:20 pm, Smartin <smartin...@yah oo.comwrote:
jonceramic wrote:
Hi guys,
I have a user who wants to have a report that shows only the 10th
value recorded by his equipment. (i.e. His equipment records
temperature every 30 seconds, but he only wants to view the data at
every 5 minutes.)
I proposed using the "mod" function in a WHERE statement on the
seconds "datepart" to test if each datapoint is an interval of 5
minutes from the starting number.
Our concern is that there will be drift or reset in the datalogging
time stamps, and the MOD method will break because the seconds won't
be exact.
So, I know you can step through a recordset at an interval. And I
know you can do interval grouping on reports. But, I don't know how
to write the SQL to simply kick that out. Every search I do for "SQL
skip" or "SQL interval" gives me lots of basic questions about
skipping single records or finding dates "since" an event.
Is there an accepted way to set a query/SQL statement that simply
picks every 5th value? (Could I do a recordset stepper with a 5 on my
gotorecord interval and write each value to a temporary table? I
think so, but -yeck- that seems like a hack.)
TIA,
Jon

There is a VBA + SQL solution here:

http://www.mvps.org/access/queries/qry0006.htm

--
Smartin- Hide quoted text -

- Show quoted text -
Thanks Smartin. The solution is okay and confirmed I should use the
MOD method I eventually used. The problem was that it relies on
adding a sequential number to your data. Not always something you can
do. In our case, we were pulling dated process values from an ODBC
link, so we couldn't.

However, the values were time stamped, and when we reviewed the data,
it turned out that no date-time stamp was more than +/-5seconds off of
the exact minute.

1:00:00
1:00:59
1:02:04
1:03:02
etc.

So, MOD worked just fine with a few modification. It's in another
guy's database, so I don't have the code, but the solution I think was
to add a column to the query with the equation.

MOD_TIME: datepart("n",[time_stamp]+#00:20:00#) MOD 5

The +20 seconds wiped out any error due to the time not being exactly
on "00" seconds. (Note: we could have used 29 or 30 seconds if the
error was greater.)

Then, we put "0" as the the sort criteria under MOD_TIME.

In the interest of Usenet posterity, there was something in our code
that made the MOD_TIME above choke when it ran the SQL with the "0" in
the sort. I'm still not sure what. But, I'm pretty sure we solved it
by making it read:

MOD_TIME: cdate(datepart( "n",[time_stamp]+#00:20:00#) MOD 5)

Note: I had thought I'd need to take [time_stamp] - [first_time] and
then calculate total minutes since start data. I was concerned how
I'd get the [first_time] (probably dlookup), but we realized this was
unnecessary as we were tweaking the MOD statement. Since we wanted 0,
5, 10, and since an hour was divisible by 5 already, each new hour
simply set the value back to "0", and we were back to the same cycle
of MOD's being 0, 1, 2, 3, 4, 0, 1, 2, 3, 4....

Jon
Apr 27 '07 #3
jonceramic wrote:
On Apr 23, 5:20 pm, Smartin <smartin...@yah oo.comwrote:
>jonceramic wrote:
>>Hi guys,
I have a user who wants to have a report that shows only the 10th
value recorded by his equipment. (i.e. His equipment records
temperature every 30 seconds, but he only wants to view the data at
every 5 minutes.)
I proposed using the "mod" function in a WHERE statement on the
seconds "datepart" to test if each datapoint is an interval of 5
minutes from the starting number.
Our concern is that there will be drift or reset in the datalogging
time stamps, and the MOD method will break because the seconds won't
be exact.
So, I know you can step through a recordset at an interval. And I
know you can do interval grouping on reports. But, I don't know how
to write the SQL to simply kick that out. Every search I do for "SQL
skip" or "SQL interval" gives me lots of basic questions about
skipping single records or finding dates "since" an event.
Is there an accepted way to set a query/SQL statement that simply
picks every 5th value? (Could I do a recordset stepper with a 5 on my
gotorecord interval and write each value to a temporary table? I
think so, but -yeck- that seems like a hack.)
TIA,
Jon
There is a VBA + SQL solution here:

http://www.mvps.org/access/queries/qry0006.htm

--
Smartin- Hide quoted text -

- Show quoted text -

Thanks Smartin. The solution is okay and confirmed I should use the
MOD method I eventually used. The problem was that it relies on
adding a sequential number to your data. Not always something you can
do. In our case, we were pulling dated process values from an ODBC
link, so we couldn't.
[snip]

Hmm, I shall have to try that over ODBC. The method does not, AFAICT,
add anything to the data, neither in the table(s), nor in the result
recordset. It simply increments a static variable (in Access) every time
a row is retrieved, and limits the result set to those rows that pass
the MOD test.

Glad you found something that worked.

--
Smartin
Apr 27 '07 #4
On Apr 27, 5:07 pm, Smartin <smartin...@yah oo.comwrote:
jonceramic wrote:
On Apr 23, 5:20 pm, Smartin <smartin...@yah oo.comwrote:
jonceramic wrote:
Hi guys,
I have a user who wants to have a report that shows only the 10th
value recorded by his equipment. (i.e. His equipment records
temperature every 30 seconds, but he only wants to view the data at
every 5 minutes.)
I proposed using the "mod" function in a WHERE statement on the
seconds "datepart" to test if each datapoint is an interval of 5
minutes from the starting number.
Our concern is that there will be drift or reset in the datalogging
time stamps, and the MOD method will break because the seconds won't
be exact.
So, I know you can step through a recordset at an interval. And I
know you can do interval grouping on reports. But, I don't know how
to write the SQL to simply kick that out. Every search I do for "SQL
skip" or "SQL interval" gives me lots of basic questions about
skipping single records or finding dates "since" an event.
Is there an accepted way to set a query/SQL statement that simply
picks every 5th value? (Could I do a recordset stepper with a 5 on my
gotorecord interval and write each value to a temporary table? I
think so, but -yeck- that seems like a hack.)
TIA,
Jon
There is a VBA + SQL solution here:
>http://www.mvps.org/access/queries/qry0006.htm
--
Smartin- Hide quoted text -
- Show quoted text -
Thanks Smartin. The solution is okay and confirmed I should use the
MOD method I eventually used. The problem was that it relies on
adding a sequential number to your data. Not always something you can
do. In our case, we were pulling dated process values from an ODBC
link, so we couldn't.

[snip]

Hmm, I shall have to try that over ODBC. The method does not, AFAICT,
add anything to the data, neither in the table(s), nor in the result
recordset. It simply increments a static variable (in Access) every time
a row is retrieved, and limits the result set to those rows that pass
the MOD test.

Glad you found something that worked.

--
Smartin

Ah, I see what you're saying about the query now. (I originally
interpreted the "i" code as being similar to an autonumber as people
added the data.)

Either way, we were able to deal with the date code, so we didn't need
to use that function to generate the values. But, we did use the
exact same MOD and 0 concept in your link.

Thanks again!

Jon

Apr 28 '07 #5

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

Similar topics

1
3001
by: mike | last post by:
Ok, I have a page that queries a database and retrieves values to create a custom report in ms excel format on the fly so that it can be downloaded. I can use the following sub to generate the report just fine. It works correctly for every field in the database and creates the report as desired except for one field and one field only. The column type for this field is the text data type in MySQL. I chose this type because, the field...
3
1779
by: Robin Tucker | last post by:
Hi, Can anyone tell me how to select the "most recent" date values from a grouped query? Consider the following: CREATE TABLE . ( NOT NULL , NOT NULL , NOT NULL ) ON This is a simplified adjacency list. What I want to do is find the highest
2
2716
by: Ellen Manning | last post by:
Using A2K. In my table I have the field "Grant" which can have a value or be null. I have a query that counts the number of records and has a Where clause on the Grant field. The query won't work when Grant is null. What's the proper syntax for getting it to work when Grant is null? Here's my Where criteria: !!
3
1924
by: MLH | last post by:
I have a query, qryAppend30DayOld260ies that attempts to append records to tblCorrespondence. When run, it can result in any of the following: appending no records, appending 1 record or appending many records. Two of the target fields in tblCorrespondence receiving values in the append operation are and . For any given VehicleJobID value, I want only ONE record in correspondence table to have an value of "01". This query blindly appends...
4
3246
by: Zorpiedoman | last post by:
This evaluates to true, why? chr(254) = "th" -- --Zorpie
4
5606
by: Giggle Girl | last post by:
I have inherited a stylesheet at work, with the designer no longer working here. In an external stylesheet it says: thead th, thead th.locked { position:relative; }
1
7082
by: mark | last post by:
Forgive me if this seems like a stupid question but I need help... I'm trying to do a simple online form that emails me the results from a few fields. Here is the code: <form action="http://cm1web1/WebSurveyComponents/script/ processform.asp" method="post">
3
3138
by: geebanga88 | last post by:
Hi i am using oracle sql developer and am making a sub query for a question. The question states: "Find Name of patients who have been treated by Dr Brian or who have had an Extended Consultation. The following is the relational schema for the database: Patient (PatientID, FamilyName, GivenName) Doctor (ProviderNo, Name) Item (ItemNo, Description, Fee) Account (AccountNo, ProviderNo, PatientID, Date) AccountLine (AccountNo,...
1
8676
by: alex21 | last post by:
Ok i am trying to use a Linq query to access a dictionary. public static Dictionary<string, Client> Clients = new Dictionary<string, Client>();Using this Linq query: IEnumerable<Staff> loginquery = from staff in Database.Staff where staff.Value.Passcode == txt_password.Text select staff;but i get the error Error 2 Cannot implicitly convert type...
0
8758
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
8669
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
9251
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9121
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
9017
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
8962
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4432
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...
2
2450
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2069
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.