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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T21:22:03+00:00 2026-06-18T21:22:03+00:00

This is my code: USE [tempdb]; GO IF OBJECT_ID(N’dbo.t’) IS NOT NULL BEGIN DROP

  • 0

This is my code:

USE [tempdb];
GO

IF OBJECT_ID(N'dbo.t') IS NOT NULL
BEGIN
    DROP TABLE dbo.t
END
GO

CREATE TABLE dbo.t
(
    a NVARCHAR(8),
    b NVARCHAR(8)
);
GO

INSERT t VALUES ('a', 'b');
INSERT t VALUES ('a', 'b');
INSERT t VALUES ('a', 'b');
INSERT t VALUES ('c', 'd');
INSERT t VALUES ('c', 'd');
INSERT t VALUES ('c', 'd');
INSERT t VALUES ('c', 'd');
INSERT t VALUES ('e', NULL);
INSERT t VALUES (NULL, NULL);
INSERT t VALUES (NULL, NULL);
INSERT t VALUES (NULL, NULL);
INSERT t VALUES (NULL, NULL);
GO

SELECT  a, b,
    COUNT(*) OVER (ORDER BY a)
FROM    t;

On this page of BOL, Microsoft says that:

If PARTITION BY is not specified, the function treats all rows of the
query result set as a single group.

So based on my understanding, the last SELECT statement will give me the following result. Since all records are considered as in one single group, right?

a        b        
-------- -------- -----------
NULL     NULL     12
NULL     NULL     12
NULL     NULL     12
NULL     NULL     12
a        b        12
a        b        12
a        b        12
c        d        12
c        d        12
c        d        12
c        d        12
e        NULL     12

But the actual result is:

a        b        
-------- -------- -----------
NULL     NULL     4
NULL     NULL     4
NULL     NULL     4
NULL     NULL     4
a        b        7
a        b        7
a        b        7
c        d        11
c        d        11
c        d        11
c        d        11
e        NULL     12

Anyone can help to explain why? Thanks.

  • 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-18T21:22:04+00:00Added an answer on June 18, 2026 at 9:22 pm

    It gives a running total (this functionality was not implemented in SQL Server until version 2012.)

    The ORDER BY defines the window to be aggregated with UNBOUNDED PRECEDING and CURRENT ROW as the default when not specified. SQL Server defaults to the less well performing RANGE option rather than ROWS.

    They have different semantics in the case of ties in that the window for the RANGE version includes not just the current row (and preceding rows) but also any additional tied rows with the same value of a as the current row. This can be seen in the number of rows counted by each in the results below.

    SELECT  a, 
            b,
            COUNT(*) OVER (ORDER BY a 
                             ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS  [Rows],
            COUNT(*) OVER (ORDER BY a 
                             RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS [Range],
            COUNT(*) OVER() AS [Over()]
        FROM    t;
    

    Returns

    a        b        Rows        Range       Over()
    -------- -------- ----------- ----------- -----------
    NULL     NULL     1           4           12
    NULL     NULL     2           4           12
    NULL     NULL     3           4           12
    NULL     NULL     4           4           12
    a        b        5           7           12
    a        b        6           7           12
    a        b        7           7           12
    c        d        8           11          12
    c        d        9           11          12
    c        d        10          11          12
    c        d        11          11          12
    e        NULL     12          12          12
    

    To achieve the result that you were expecting to get omit both the PARTITION BY and ORDER BY and use an empty OVER() clause (also shown above).

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

Sidebar

Related Questions

Here's some sample code: if object_id('tempdb..#TempList') is not null drop table #TempList create table
This code: USE [db] GO /****** Object: UserDefinedFunction [dbo].[GetFieldPickerReports] Script Date: 01/11/2013 19:12:27 ******/
I currently have this code use for printing a table in mysql database ,
I know this code use to work, I must have accidentally changed something when
hello I have this code that use to sort a datatable. Dim sortingIndex As
How could I refactor this code to use only one Dir[ ] call? Dir[
Here is the code I use to define my array, this code is above
I make call from my app with this code (i use webview because after
Hi I use this code to save an xml file using (IsolatedStorageFile myIsolatedStorage =
If I use this code outside of a method or winmain: ostringstream vertexid; vertexid

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.