Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7660075
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:24:59+00:00 2026-05-31T13:24:59+00:00

A short description: I have a table with data that is updated over a

  • 0

A short description: I have a table with data that is updated over a certain time period. Now the problem is, that – depending on the nature of the sensor which sends the data – in this time period there could be either 50 data sets or 50.000. As I want to visualize this data (using ASP.NET / c#), for a first preview I would like to SELECT just 1000 values from the table.

I already have an approach doing this: I count the rows in the time period of interest, with a simple “where” clause to specify the sensor-id, save it as a variable in SQL, and then divide the count() by 1000. I’ve tried it in MS Access, where it works just fine:

set @divider = select count(*) from table where [...]

SELECT (Int([RowNumber]/@divider)), First(Value)
FROM myTable
GROUP BY (Int([RowNumber]/@divider));

The trick in Access was, that I simply have a data field (“RowNumber”), which is my PK/ID, and goes from 0 up. I tried to accomplish that in SQL Server using the ROW_NUMBER() method, which works more or less. I’ve got the right syntax for the method, but I can not use the GROUP BY statement

Windowed functions can only appear in the SELECT or ORDER BY
clauses.

meaning ROW_NUMBER() can’t be in the GROUP BY statement.

Now I’m kinda stuck. I’ve tried to save the ROW_NUMBER value into a char or a separate column, and GROUP BY it later on, but I couldn’t get it done. And somehow I start to think, that my strategy might have its weaknesses …? :/

To clarify once more: I don’t need to SELECT TOP 1000 from my table, because this would just mean that I select the first 1000 values (depending on the sorting). I need to SELECT every x-th value, while I can compute the x (and I could even round it to an INT, if that would help to get it done). I hope I was able to describe the problem understandable …

This is my first post here on StackOverflow, I hope I didn’t forget anything essential or important, if you need any further information (table structure, my queries so far, …) please don’t hesitate to ask. Any help or hint is highly appreciated – thanks in advance! 🙂


Update: SOLUTION! Big thanks to https://stackoverflow.com/users/52598/lieven!!!

Here is how I did it in the end:

I declare 2 variables – I count my rows and SET it into the first var. Then I use ROUND() on the just assigned variable, and divide it by 1000 (because in the end I want ABOUT 1000 values!). I split this operation into 2 variables, because if I used the value from the COUNT function as basis for my ROUND operation, there were some mistakes.

declare @myvar decimal(10,2) 
declare @myvar2 decimal(10,2)

set @myvar = (select COUNT(*)
from value_table
where channelid=135 and myDate >= '2011-01-14 22:00:00.000' and myDate <= '2011-02-14 22:00:00.000'
)

set @myvar2 = ROUND(@myvar/1000, 0)

Now I have the rounded value, which I want to be my step-size (take every x-th value -> this is our “x” ;)) stored in @myvar2. Next I will subselect the data of the desired timespan and channel, and add the ROW_NUMBER() as column “rn”, and finally add a WHERE-clause to the outer SELECT, where I divide the ROW_NUMBER through @myvar2 – when the modulus is 0, the row will be SELECTed.

select * from
(
select (ROW_NUMBER() over (order by id desc)) as rn, myValue, myDate
from value_table
where channel_id=135 and myDate >= '2011-01-14 22:00:00.000' and myDate<= '2011-02-14 22:00:00.000'
) d
WHERE rn % @myvar2 = 0

Works like a charm – once again all my thanks to https://stackoverflow.com/users/52598/lieven, see the comment below for the original posting!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T13:25:00+00:00Added an answer on May 31, 2026 at 1:25 pm

    In essence, all you need to do to select the x-th value is retain all rows where the modulus of the rownumber divided by x is 0.

    WHERE rn % @x_thValues = 0
    

    Now to be able to use your ROW_NUMBER‘s result, you’ll need to wrap the entire statement into in a subselect

    SELECT  *
    FROM    (
                SELECT  *
                        , rn = ROW_NUMBER() OVER (ORDER BY Value)
                FROM    DummyData
            ) d
    WHERE   rn % @x_thValues = 0                    
    

    Combined with a variable to what x-th values you need, you might use something like this testscript

    DECLARE @x_thValues INTEGER = 2
    
    ;WITH DummyData AS (SELECT * FROM (VALUES (1), (2), (3), (4)) v (Value))
    SELECT  *
    FROM    (
                SELECT  *
                        , rn = ROW_NUMBER() OVER (ORDER BY Value)
                FROM    DummyData
            ) d
    WHERE   rn % @x_thValues = 0                    
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table with data such as this: <table> <tr onclick=window.location='download.php?id=1'> <td>Program 1<br/>Short
MYSQL Database: I have a table of data that I need to put into
I have a table with films. There is a field with short description. Using
SHORT DESCRIPTION OF PROBLEM: I want to set the text of a searchbar without
I have fetched some records from core data, and presented it in a table
I have a few models that contain static data, and I'm trying to cache
I have table like that : <table id=table_id class=tablesorter> <thead> <tr class=trheader> <th colspan=2>111</th>
I have an XML file that contains price value.I want to sort my table
I have a DataGrid that is loaded from an XML data store, all created
I have a database that have two tables. The sql query fetching data correctly.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.