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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:39:35+00:00 2026-06-17T20:39:35+00:00

Say I have the following schema: — Create the dbo.Transaction table CREATE TABLE [dbo].[Transaction]

  • 0

Say I have the following schema:

-- Create the dbo.Transaction table
CREATE TABLE [dbo].[Transaction] (
    [TransactionId] INT NOT NULL IDENTITY,
    [AccountId] INT NOT NULL,
    [TransactionDate] DateTime2(7) NOT NULL,
    [Amount] decimal(9,3) NOT NULL
    CONSTRAINT [PK_Transaction] PRIMARY KEY ([TransactionId])
);

And the following query:

Select
    AccountId,
    TransactionDate,
    Amount,
    AverageAmount       = Avg(Amount)   Over (Partition By AccountId Order By TransactionDate ROWS BETWEEN 2 PRECEDING AND CURRENT ROW),
    TransactionCount    = Count(Amount) Over (Partition By AccountId Order By TransactionDate ROWS 2 PRECEDING),
    MinimumAmount       = Min(Amount)   Over (Partition By AccountId Order By TransactionDate ROWS 2 PRECEDING),
    MaximumAmount       = Max(Amount)   Over (Partition By AccountId Order By TransactionDate ROWS 2 PRECEDING),
    SumAmount           = Sum(Amount)   Over (Partition By AccountId Order By TransactionDate ROWS 2 PRECEDING)
From dbo.[Transaction]
Order By AccountId, TransactionDate

How would I perform this query if it were contained in a UDF or stored proc, and the sliding interval (2 in this example) wasn’t known until runtime, as passed as a parameter to the UDF / stored proc? It seems SQL 2012 doesn’t permit the use of a variable here.

  • 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-06-17T20:39:36+00:00Added an answer on June 17, 2026 at 8:39 pm

    As you mentioned, SQL Server only supports integer literals for PRECEDING and FOLLOWING in OVER clauses.

    There are two options available to you: dynamic sql and re-writing the query to not use PRECEDING

    Dynamic sql is the simpliest, but I’d be careful about putting it in a UDF.

    set @sql = N'Select AccountId, ... ROWS ' 
              + cast(@sz as varchar(10)) + N' PRECEDING) ...'
    exec sp_executesql @sql
    

    However window functions are just fancy syntax. You can re-write the query without them:

    DECLARE @sz INT
    SET @sz = 2
    
    ;
    WITH    q AS ( SELECT   AccountId ,
                            TransactionDate ,
                            Amount ,
                            ROW_NUMBER() OVER ( PARTITION BY AccountId 
                                                ORDER BY TransactionDate ) rw
                   FROM     [Transaction]
                 )
        SELECT  accountID ,
                TransactionDate ,
                Amount ,
                ( SELECT    AVG(q1.Amount) FROM  q q1
                  WHERE     q1.accountid = q.accountid
                            AND q1.rw BETWEEN q.rw - @sz AND q.rw
                ) AverageAmount,
                ( SELECT    COUNT(q1.Amount) FROM  q q1
                  WHERE     q1.accountid = q.accountid
                            AND q1.rw BETWEEN q.rw - @sz AND q.rw
                ) TransactionAmount
                -- etc.
                FROM q
                ORDER BY AccountID, TransactionDate
    

    Here is another way to re-write the query as well:

    DECLARE @sz INT
    SET @sz = 2;
    WITH    q AS ( SELECT   AccountId ,
                            TransactionDate ,
                            Amount ,
                            ROW_NUMBER() OVER ( PARTITION BY AccountId 
                                                ORDER BY TransactionDate ) rw
                   FROM     [Transaction]
                 )
        SELECT  q.accountID ,
                q.TransactionDate ,
                q.Amount ,
                AVG(q1.Amount) AverageAmount ,
                COUNT(q1.Amount) TransactionAmount ,
                MAX(q1.Amount) MaxAmount ,
                MIN(q1.Amount) MinAmount
                    -- etc.
        FROM    q
                INNER JOIN q q1 ON q1.accountid = q.accountid
                                   AND q1.rw BETWEEN q.rw - @sz AND q.rw
        GROUP BY q.accountid ,
                q.transactiondate ,
                q.amount
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have a table with the following schema: CREATE TABLE tasks (taskID TEXT,
I have a table(say TableA) with the following schema A(int) B(int) D (varchar) C(date)
Let's say I have some the following table schema: year|val1|val2 I want to get
Supposing I have a table with the following schema: int pitchId, int pitcherId, int
Say you have the following in a schema: <xsd:complexType name=shape/> <xsd:complexType name=circle> <xsd:complexContent> <xsd:extension
Let's say I have following table like this <!DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0
Say I have the following data IEnumerable<IEnumerable<int>> items = new IEnumerable<int>[] { new int[]
Let's say we have the following schema: root/ application/ -public/ index.php css/ img/ javascript/
Let say I'm doing a basic transaction system where I have the following objects.
For example, let us say we have the following schema (listed in http://www.w3.org/TR/xmlschema-0/#NS )

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.