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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T21:40:37+00:00 2026-05-17T21:40:37+00:00

Please bear with me on this, this is quite difficult to explain and I

  • 0

Please bear with me on this, this is quite difficult to explain and I am not sure if I am using the correct terminology.

I need to do a rather complicated select. It is effectively a select statement which conditionally decides what field to use to filter the select. If a certain date field is not null, I need to check that the value in that field is within a certain range. Otherwise if that date field is null I need to check another field, int field, on the same table is within a certain range:

Pseudocode:

If [Date] is not null
    Get sum (table.value) for rows Date >= dateValue and Date < dateValue
Else
    Get sum (table.value) for rows Int >= intValue and Int < intValue

My current attempt:

SELECT CASE WHEN a.Date IS NOT NULL THEN 
(SUM(CASE WHEN (a.Date >= cal.Date) THEN ABS(a.Value) ELSE 0 END)) 
ELSE 
(SUM(CASE WHEN (b.Days >= 0) THEN ABS(a.Value) ELSE 0 END) 
END AS 'A'

Any ideas? Ask if you need more information.
Thanks in advance.

  • 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-17T21:40:38+00:00Added an answer on May 17, 2026 at 9:40 pm

    One way is to have 2 separate queries UNIONed together, but only one clause will produce a value. This avoids having OR statements.

    SELECT SUM(Value)
    FROM MyTable
    WHERE MyTable.Date >= dateValue and Table.Date < dateValue  --NULL Date = false always
    UNION ALL
    SELECT SUM(Value)
    FROM MyTable
    WHERE MyTable.Int >= intValue and Table.Int < intValue
        AND --ignore this clause if date is NOT NULL
        MyTable.Date IS NULL
    

    Edit, with OR:

    SELECT SUM(Value)
    FROM MyTable
    WHERE
         (
          MyTable.Date IS NOT NULL AND 
          MyTable.Date >= dateValue and Table.Date < dateValue
          )
          OR --actually mutually exclusive
         (
          MyTable.Date IS NULL AND 
          MyTable.Int >= intValue and Table.Int < intValue 
          )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm not quite sure of the terminology here so please bear with me.... Let's
This might be a bit difficult to explain in writing, so please bear with
I am not totally sure I am naming this right, but please bear with
Firstly this has turned out to be quite a long post so please bear
This will be quite a long way to ask my question, so please bear
This is all hypothetical, so please bear with me. Say I'm writing a tool
This is a long text. Please bear with me. Boiled down, the question is:
I am new to visual studio/asp.net so please bear with me. Using vs 2005
I'm quite new to Ruby on Rails so please bear with me :) I'm
I'm certain this has been asked before-in other ways-please bear with me (for a

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.