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 6655259
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:29:41+00:00 2026-05-26T01:29:41+00:00

I am trying to use a CTE and CROSS JOIN the result set. I

  • 0

I am trying to use a CTE and CROSS JOIN the result set. I want to sum up the 4 rows leading up to the current row. The example online I found does not use a CTE, only a newly created table (http://sqlandme.com/2011/08/17/sql-server-denali-over-rows-range/). The syntax should work, but I get an error saying “Incorrect syntax near ‘ROWS'”.

An Example output would be this using the following statement:
SUM(y) OVER(ORDER BY x ROWS 4 PRECEDING) sum

X Y SUM


     1          7     0     No prev rows, so sum is 0
     2          1     7     Sum   = 7 
     3          2     8           = 1 + 7
     4          5     10          = 2 + 1 + 7
     5          7     15          = 5 + 2 + 1 + 7
     6         34     15          = 7 + 5 + 2 + 1
     7         32     48          = 34 + 7 + 5 + 2

Does anyone have any suggestion on what is incorrect with the query? Thanks in advance.

with quarterResults as (
      <subquery in here>
)

--COLUMN1: String
--COLUMN2: Date
--COLUMN3: Date
--COLUMN4: Double
select a.TIC, a.DATADATE, a.EFFDATE, SUM(b.valuei) OVER (ORDER BY a.TIC, a.DATADATE, a.EFFDATE ROWS 4 PRECEDING) AS [SUM]
from quarterResults a
cross join quarterResults b
where a.datadate > b.datadate
group by a.tic, a.datadate, a.EFFDATE, a.valuei
order by a.TIC, a.datadate
  • 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-26T01:29:42+00:00Added an answer on May 26, 2026 at 1:29 am

    The documentation you found for ROWS/RANGE is not for SQL Server 2008 – it’s for a future version of SQL Server.

    To accomplish your query in SQL 2008, one approach would be similar to:

    SELECT a.TIC, a.datadate, a.effdate, x.s
    FROM quarterResults a
        CROSS APPLY (   SELECT ISNULL(SUM(v), 0)
                        FROM (  SELECT TOP(4) b.valuei
                                FROM quarterResults b
                                WHERE b.datadate < a.datadate
                                ORDER BY b.datadate DESC ) x(v)
                    ) x(s)
    ORDER BY a.TIC, a.datadate
    

    Note that this is potentially an expensive query. The use of the OVER expression with ROWS would probably be more efficient but, again, it is not available in SQL Server 2008.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying use this example http://www.sajithmr.me/jrecorder/example2.html for recording audio and send it to
I was trying use a set of filter functions to run the appropriate routine,
I am trying use Date Picker and on Select i want to display the
I'm trying use the Sum method in a lambda expression for a comparison, but
I am trying to use CTE in sql server 2008 and consume it in
I'm trying use double type in openCL, but doesn't work anyway, i want use
I am trying use an iterator to go through a set and then do
I'm trying use a Visual Studio 2010 setup project to package a set of
I'm trying use self-signed certificate (c#): X509Certificate2 cert = new X509Certificate2( Server.MapPath(~/App_Data/myhost.pfx), pass); on
I'm trying use mod_rewrite to rewrite URLs from the following: http://www.site.com/one-two-file.php to http://www.site.com/one/two/file.php The

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.