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

  • Home
  • SEARCH
  • 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 9150549
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:37:33+00:00 2026-06-17T11:37:33+00:00

I feel so close… I think my problem is how I’m using the MOD

  • 0

I feel so close… I think my problem is how I’m using the MOD function combined with the ROW_NUMBER() function, but I don’t understand what I’m doing wrong.

I’m using the ROW_NUMBER() function because I need a way to select every “nth” row. I’ve read the other pages about this (I used them to create my SQL)…but I’m getting an error from SQL Server. I need the inner table join (2 instances of the table Tick_OneMin, H1 and H2) to get prices of different securities at the same time snapshot.

If I comment out the line with the MOD function…the SQL executes fine…but if I put it in….SQL Server throws the error message:

An expression of non-boolean type specified in a context where a condition is expected, near ‘MOD’.

Here is my attempt at the SQL–

SELECT 
   ROW_NUMBER() OVER (ORDER BY H1.CombDateTime ASC) AS RowID,
   H1.CombDateTime, 
   H1.Close_PX as 'TYA_Close', 
   H2.Close_PX 'ESA_Close'
FROM 
   Tick_OneMin as H1, Tick_OneMin as H2
WHERE 
   H1.Ticker = 'TYA'
   AND H2.Ticker = 'ESA'
   AND H1.CombDateTime >= '12/28/2012 10:00 AM'
   AND H1.CombDateTime <= '12/28/2012 10:30 AM'
   AND H1.CombDateTime = H2.CombDateTime
   AND RowID MOD 4 = 0     
   -- this "RowID MOD 4 = 0" is throwing an error in SQL Server
ORDER BY 
   H1.CombDateTime ASC

My table looks like the following (1 table with 3 columns)

Table Tick_OneMin

Ticker - CombDateTime - Close_PX
------------------------------------
ES     - 1/3/2012 10:00 AM    - 1470
ZN     - 1/3/2012 10:00 AM   - 132.5
ES     - 1/3/2012 10:01 AM   - 1475
ZN     - 1/3/2012 10:01 AM   - 133

and I want to create the following output

Date  -   ZN.Price -  ES.Price
====     ========   ========
1/3/2012 -  132.5  - 1470
1/3/2012 -  133    - 1475

Any ideas why SQL SErver is throwing the error?

  • 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-17T11:37:34+00:00Added an answer on June 17, 2026 at 11:37 am

    You can’t reference an alias defined in the SELECT clause in the WHERE clause, since WHERE is parsed first. One workaround is to use a subquery or CTE:

    WITH x AS
    (
     SELECT ROW_NUMBER() OVER (ORDER BY H1.CombDateTime ASC) AS RowID,
      ... rest of query
    )
    SELECT CombDateTime, TYA_Close, ESA_Close --, RowID
    FROM x
    WHERE RowID % 4 = 0
    ORDER BY CombDateTime;
    

    Note also what Martin and Marc have pointed out – SQL Server uses % not the MOD operator you’re bringing in from VB or elsewhere.

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

Sidebar

Related Questions

Foreword: Although I don't think it's exactly a duplicate, feel free to close if
I feel like I am getting close using the debugger but am still not
Feel free to close and/or redirect if this has been asked, but here's my
I feel like I'm fairly close to this, but it's not quite right. I'm
I feel as though this fairly simple and probably a common problem, but i
I feel completely out of my depth but I feel so close.. I'm trying
I feel like im extremely close on this one but the query keeps on
I have a problem that I need some help on but I feel I'm
So I feel like im pretty close, but I also have a feeling I
I feel my question is close to this one, but I want a more

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.