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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T20:10:57+00:00 2026-06-18T20:10:57+00:00

I have 2 tables UserSession and Sale . Table User has 4 columns, namely

  • 0

I have 2 tables UserSession and Sale.

Table User has 4 columns, namely UserID, UserSessionID, SessionOpenDate and SessionCloseDate.

Table Sale has 2 columns, which are price, cost, userID and CompletedDate.

When a user logs in, a new row is created in the User table, where the user’s login timestamp will be saved in the SessionOpenDate and a new UserSessionID will be assigned to the session. When the user logs off, the log off timestamp will be be saved in SessionCloseDate.

When the user is still logged in, the user can make some sale and the sale information is saved in the Sale table. The timestamp when the sale is finalized in saved in CompletedDate column.

For some reason, I need to get the all sales done in a certain UserSessionID where the CompletedDate must be within the SessionOpenDate and SessionCloseDate. However, if the user has not logged off yet, which means that the value in SessionCloseDate is null, the CompletedDate should be between SessionOpenDate and now.

Here’s my query:

SELECT SUM(s.cost) AS Cost, SUM(s.price) AS Price
FROM Sale AS s 
  INNER JOIN UserSession AS u 
  ON s.userID = u.userID
WHERE 
  (s.CompletedDate >=
    ( SELECT SessionOpenDate
      FROM UserSession
      WHERE (UserSessionID = u.UserSessionID)
)
  ) 
  AND 
  (s.CompletedDate < 
    ( 
      IF EXISTS
       (    
         SELECT SessionCloseDate AS closeTime
         FROM UserSession AS UserSessionTemp
         WHERE (UserSessionID = u.UserSessionID)
       ) 
       BEGIN
         SET closeTime = SELECT CURRENT_TIMESTAMP
       END
    )
  ) 
  AND u.UserSessionID IN (1) 

However, Sql Server says Incorrect syntax near the keyword 'IF'. and Incorrect syntax near ')'.

Can anyone tell me what went wrong with my IF block?

  • 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-18T20:10:58+00:00Added an answer on June 18, 2026 at 8:10 pm

    You can’t use an IF block inside a SELECT statement. Also, I don’t know what you’re really trying to accomplish with SET, since closeTime is not a variable/parameter.

    You can use IIF in SQL Server 2012 (syntactical sugar for CASE WHEN <condition> THEN <true_value> ELSE <false_value> END – use this syntax for earlier versions):

    IIF(EXISTS
       (    
         SELECT SessionCloseDate AS closeTime
         FROM UserSession AS UserSessionTemp
         WHERE (UserSessionID = u.UserSessionID)
       ), (
         SELECT SessionCloseDate AS closeTime
         FROM UserSession AS UserSessionTemp
         WHERE (UserSessionID = u.UserSessionID)
       ),  
         CURRENT_TIMESTAMP
    )
    

    Honestly, without getting too complicated, here’s what I would do instead:

    SELECT SUM(s.cost) AS Cost, SUM(s.price) AS Price
    FROM userSession AS u
    INNER JOIN Sale AS s ON u.userID = s.userID 
    WHERE u.UserSessionID = @UserSessionId
    AND s.CompletedDate >= u.SessionOpenDate 
    AND (u.SessionCloseDate IS NULL OR s.CompletedDate < u.SessionCloseDate)
    

    Or,

    SELECT SUM(s.cost) AS Cost, SUM(s.price) AS Price
    FROM userSession AS u
    INNER JOIN Sale AS s ON u.userID = s.userID 
    WHERE u.UserSessionID = @UserSessionId
    AND s.CompletedDate BETWEEN u.SessionOpenDate 
                        AND COALESCE(u.SessionCloseDate, '12/31/9999 23:59:59.9999')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have tables item and store (it's a store management system). item table has
I have tables in a MySQL database like this... CREATE TABLE `someUserTable` ( userId
I have tables like this: tblUsers int UserID string UserName tblUsersInRoles int UserID int
I have tables linked by FK, I query on the first table using entity
I have a table with several million rows. Each row represents a user session
I have a website already running made with CakePHP, which has its own login
I have tables like this in SQL Server Users UserId (Unique) Name Age Friends
have tables like this Table checklist idCardno name permAddress datetime Table persons name idcardno
I have tables foo and bar: create table foo(a int, b varchar(10), primary key
I have tables & data like this: venues table contains : id +----+---------+ |

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.