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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:33:07+00:00 2026-05-26T15:33:07+00:00

I do not quite understand why those two different codesamples return a different value.

  • 0

I do not quite understand why those two different codesamples return a different value.

somehow incorrect but working syntax, returns false results, e.g it returns 0 when the comparison is done over two equal values:

(SELECT CASE 
    WHEN 
       SUM(V.IsCompatible) OVER 
          (PARTITION BY ComputerName, UserID) = ApplicationCount 
    THEN 1 ELSE 0 END
) AS CompatibleUser

The one below returns the correct values, ie. 1 when there are two equal values compared.

(CASE 
    WHEN 
       SUM(V.IsCompatible) OVER 
         (PARTITION BY ComputerName, UserID) = ApplicationCount 
    THEN 1 ELSE 0 END
) AS CompatibleUser

or even simpler:

(SELECT CASE 
    WHEN 
       X = Y 
    THEN 1 ELSE 0 END
) AS Result

X = 22 AND Y = 22 => Result = 0

(CASE 
    WHEN 
       X = Y 
    THEN 1 ELSE 0 END
) AS Result

X = 22 AND Y = 22 => Result = 1

I understand applying the correct syntax is important, and I am aware of the SELECT CASE syntax in T-SQL, but I do not understand how the first code sample is evaluated and delivering an unexpected result.

update: full query in it’s context

select userapplication.username,
   computerdetails.computername,
   sum(userapplication.iscompatible) 
       over (partition by computerdetails.computername, 
                          userapplication.userid) as compatiblecount,
   userapplication.applicationcount,
   ( case
       when sum(userapplication.iscompatible) 
           over (partition by 
           computerdetails.computername, 
           userapplication.userid) <> userapplication.applicationcount 
       then 0
       else 1
     end 
   ) as usercomputeriscompatible
from   computerdetails
   right outer join usercomputer
     on computerdetails.computerid = usercomputer.computerid
   right outer join userapplication
     on usercomputer.gebruikerid = userapplication.userid 

so userComputerIsCompatible is the result in question 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-05-26T15:33:08+00:00Added an answer on May 26, 2026 at 3:33 pm

    I think the reason for this behavior is the next one: the expressions like (SELECT ...) are considered to be sub-queries even they don’t have FROM clause. Is assume the source of data for these (false) “sub-queries” is only the current row. So, (SELECT expression) is interpreted as (SELECT expression FROM current_row) and (SELECT SUM(iscompatible)OVER(...)) is executed as (SELECT SUM(iscompatible)OVER(current_row)).

    Argument: analyzing execution plan for (SELECT SUM(IsWeb) OVER(PARTITION BY OrderDate) [FROM current_row]) expression
    enter image description here

    I see a Constant Scan (Scan an internal table of constants) operator instead of Clustered Index Scan before Segment and Stream Aggregate ([Expr1007] = Scalar Operator(SUM(@OrderHeader.[IsWeb] as [h].[IsWeb]))) operators. This internal table (Constant Scan) is constructed from current row.

    Example (tested with SQL2005SP3 and SQL2008):

    DECLARE @OrderHeader TABLE
    (
         OrderHeaderID  INT IDENTITY PRIMARY KEY
        ,OrderDate      DATETIME NOT NULL
        ,IsWeb          TINYINT NOT NULL --or BIT
    );
    INSERT  @OrderHeader 
    SELECT  '20110101', 0
    UNION ALL 
    SELECT  '20110101', 1
    UNION ALL
    SELECT  '20110101', 1
    UNION ALL 
    SELECT  '20110102', 1
    UNION ALL
    SELECT  '20110103', 0
    UNION ALL
    SELECT  '20110103', 0;
    
    SELECT  *
            ,SUM(IsWeb) OVER(PARTITION BY OrderDate) SumExpression_1
    FROM    @OrderHeader h
    ORDER BY h.OrderDate;
    
    SELECT  *
            ,(SELECT SUM(IsWeb) OVER(PARTITION BY OrderDate)) SumWithSubquery_2
    FROM    @OrderHeader h
    ORDER BY h.OrderDate;
    

    Results:

    OrderHeaderID OrderDate               IsWeb SumExpression_1
    ------------- ----------------------- ----- ---------------
    1             2011-01-01 00:00:00.000 0     2
    2             2011-01-01 00:00:00.000 1     2
    3             2011-01-01 00:00:00.000 1     2
    4             2011-01-02 00:00:00.000 1     1
    5             2011-01-03 00:00:00.000 0     0
    6             2011-01-03 00:00:00.000 0     0
    
    OrderHeaderID OrderDate               IsWeb SumWithSubquery_2
    ------------- ----------------------- ----- -----------------
    1             2011-01-01 00:00:00.000 0     0
    2             2011-01-01 00:00:00.000 1     1
    3             2011-01-01 00:00:00.000 1     1
    4             2011-01-02 00:00:00.000 1     1
    5             2011-01-03 00:00:00.000 0     0
    6             2011-01-03 00:00:00.000 0     0
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My previous question got closed as those people did not quite understand my question.
I do not quite understand the difference between a C# reference and a pointer.
To be honest I'm not quite sure if I understand the task myself :)
Does [_\s^] mean underscore and whitespace but not (quote) in Reg I understand that
It's not quite a programming question, but I hope its related closely enough. Do
I'm not sure I quite understand the extent to which undefined behavior can jeopardize
I still don't quite understand what a closure is so I posted these two
I do not quite understand the benefit of multiple independent virtual address, which point
Not quite sure how to troubleshoot this. I'm maintaining an ASP site, its mostly
Not quite an Attribute, not quite a Method. Stereotypes? <<get>> <<set>> ? I'm retro-modelling

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.