SQL Server auto Id for a View result





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















We are using below view to generate identity column values using the ROW_NUMBER() function, but how would I change this view to return incremental numbers in identity column, for example if I run view today it will return 100 records and the identity column value is from 1 to 100, if I run it tomorrow, it should return 101 to whatever the records it has in that day.greatly appreciate your help.



CREATE VIEW [dbo].[Data_Old] 
AS
SELECT
ROW_NUMBER() OVER (ORDER BY event_time) AS id,
event_time, loc, c, d, e, f, Expr1
FROM
(SELECT
FORMAT(h.ACTIVITYDT, 'yyyy MMM dd hh:mm:ss') AS event_time,
ISNULL(h.LOCATION, N'17.51.12.24') loc,
' LEEF:1.0|Manage|WorkSite|9.5|' + CAST(NEWID() AS varchar(50)) c,
'|devTime=' + FORMAT(h.ACTIVITYDT, 'MMM dd hh:mm:ss') d,
'|usrName=' + ISNULL(h.USER, N'{null}') + '|fullName=' + ISNULL(u.FULLNAME, N'{null}') e,
'|activity=' + ISNULL(h.ACTIVITYDT, N'{null}') + '|library=COLLECTIONS' + '|Num=' + CAST(h.NUM AS varchar) + '|version=' + CAST(h.VERSION AS varchar) f,
'|pagesPrinted=' + CAST(h.PAGES_PRINTED AS varchar) + '|FTI=' + ISNULL(CAST(d.CBOOL2 AS varchar), N'False')
+ '|keyData=case:' + ISNULL(d.C5ALIAS, N'{null}') + ';serialNo:' + ISNULL(d.C16ALIAS, N'{null}')
+ ';documentNo:' + ISNULL(CAST(d.CDBL1 AS varchar), N'') AS Expr1
FROM
dbo.HISTORY AS h
LEFT OUTER JOIN
dbo.MASTERS AS d WITH (nolock) ON d.NUM = h.NUM AND d.VERSION = h.VERSION
LEFT OUTER JOIN
dbo.USERS AS u WITH (nolock) ON h.USER = u.USERID
WHERE
(h.ACTIVITYDT >= DATEADD(YEAR, DATEDIFF(YEAR, 0, GETDATE()), 0)
AND h.ACTIVITYDT < DATEADD(YEAR, DATEDIFF(YEAR, 0, GETDATE()) + 1, 0))
AND (h.ACTIVITYDT >= DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0)
AND h.ACTIVITYDT < DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) + 1, 0))
AND (h.ACTIVITYDT >= DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), 0)
AND h.ACTIVITYDT < DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()) + 1, 0))

UNION ALL

SELECT
FORMAT(ACTIONDT, 'yyyy MMM dd hh:mm:ss') AS event_time,
' 127.55.82.124' b,
' LEEF:1.0|Manage|WorkSite|9.5|' + CONVERT(VARCHAR, SID) + CONVERT(VARCHAR, DATEDIFF(SECOND, ACTIONDT, '2017-01-01')) c,
'|devTime=' + FORMAT(ACTIONDT, 'MMM dd hh:mm:ss') d,
'|usrName=' + ISNULL(USERID, '{null}') e,
'|activity=' + CASE ACTIONSID
WHEN 1 THEN 'Successful Login'
WHEN 2 THEN 'Unsuccessful Login'
WHEN 3 THEN 'Impersonated Login'
WHEN 4 THEN 'Impersonated Logoff'
END f,
'|library=COLLECTIONS' AS LEEF
FROM
dbo.HISTORY
WHERE
(ACTIONDT >= DATEADD(YEAR, DATEDIFF(YEAR, 0, GETDATE()), 0)
AND ACTIONDT < DATEADD(YEAR, DATEDIFF(YEAR, 0, GETDATE()) + 1, 0))
AND (ACTIONDT >= DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0)
AND ACTIONDT < DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) + 1, 0))
AND (ACTIONDT >= DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), 0)
AND ACTIONDT < DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()) + 1, 0))
) AS MyResult









share|improve this question

























  • row_number is going to give you a number for every row. So if you have 101 rows, the ID's will be 1-101. How your force only 101 or how ever many rows to be returned is up to you.

    – scsimon
    Jan 3 at 20:19













  • 1-101 are single day records, next day If I have another 100 records I want the identity number should start from 101 to 200

    – Sunny
    Jan 3 at 20:21











  • Well since view's aren't materialized, you'd have to log the output of the execution of the previous day and then add it to your row_number so that you can keep a running total. Not really sure what the end game is here though.

    – scsimon
    Jan 3 at 20:23




















0















We are using below view to generate identity column values using the ROW_NUMBER() function, but how would I change this view to return incremental numbers in identity column, for example if I run view today it will return 100 records and the identity column value is from 1 to 100, if I run it tomorrow, it should return 101 to whatever the records it has in that day.greatly appreciate your help.



CREATE VIEW [dbo].[Data_Old] 
AS
SELECT
ROW_NUMBER() OVER (ORDER BY event_time) AS id,
event_time, loc, c, d, e, f, Expr1
FROM
(SELECT
FORMAT(h.ACTIVITYDT, 'yyyy MMM dd hh:mm:ss') AS event_time,
ISNULL(h.LOCATION, N'17.51.12.24') loc,
' LEEF:1.0|Manage|WorkSite|9.5|' + CAST(NEWID() AS varchar(50)) c,
'|devTime=' + FORMAT(h.ACTIVITYDT, 'MMM dd hh:mm:ss') d,
'|usrName=' + ISNULL(h.USER, N'{null}') + '|fullName=' + ISNULL(u.FULLNAME, N'{null}') e,
'|activity=' + ISNULL(h.ACTIVITYDT, N'{null}') + '|library=COLLECTIONS' + '|Num=' + CAST(h.NUM AS varchar) + '|version=' + CAST(h.VERSION AS varchar) f,
'|pagesPrinted=' + CAST(h.PAGES_PRINTED AS varchar) + '|FTI=' + ISNULL(CAST(d.CBOOL2 AS varchar), N'False')
+ '|keyData=case:' + ISNULL(d.C5ALIAS, N'{null}') + ';serialNo:' + ISNULL(d.C16ALIAS, N'{null}')
+ ';documentNo:' + ISNULL(CAST(d.CDBL1 AS varchar), N'') AS Expr1
FROM
dbo.HISTORY AS h
LEFT OUTER JOIN
dbo.MASTERS AS d WITH (nolock) ON d.NUM = h.NUM AND d.VERSION = h.VERSION
LEFT OUTER JOIN
dbo.USERS AS u WITH (nolock) ON h.USER = u.USERID
WHERE
(h.ACTIVITYDT >= DATEADD(YEAR, DATEDIFF(YEAR, 0, GETDATE()), 0)
AND h.ACTIVITYDT < DATEADD(YEAR, DATEDIFF(YEAR, 0, GETDATE()) + 1, 0))
AND (h.ACTIVITYDT >= DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0)
AND h.ACTIVITYDT < DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) + 1, 0))
AND (h.ACTIVITYDT >= DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), 0)
AND h.ACTIVITYDT < DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()) + 1, 0))

UNION ALL

SELECT
FORMAT(ACTIONDT, 'yyyy MMM dd hh:mm:ss') AS event_time,
' 127.55.82.124' b,
' LEEF:1.0|Manage|WorkSite|9.5|' + CONVERT(VARCHAR, SID) + CONVERT(VARCHAR, DATEDIFF(SECOND, ACTIONDT, '2017-01-01')) c,
'|devTime=' + FORMAT(ACTIONDT, 'MMM dd hh:mm:ss') d,
'|usrName=' + ISNULL(USERID, '{null}') e,
'|activity=' + CASE ACTIONSID
WHEN 1 THEN 'Successful Login'
WHEN 2 THEN 'Unsuccessful Login'
WHEN 3 THEN 'Impersonated Login'
WHEN 4 THEN 'Impersonated Logoff'
END f,
'|library=COLLECTIONS' AS LEEF
FROM
dbo.HISTORY
WHERE
(ACTIONDT >= DATEADD(YEAR, DATEDIFF(YEAR, 0, GETDATE()), 0)
AND ACTIONDT < DATEADD(YEAR, DATEDIFF(YEAR, 0, GETDATE()) + 1, 0))
AND (ACTIONDT >= DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0)
AND ACTIONDT < DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) + 1, 0))
AND (ACTIONDT >= DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), 0)
AND ACTIONDT < DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()) + 1, 0))
) AS MyResult









share|improve this question

























  • row_number is going to give you a number for every row. So if you have 101 rows, the ID's will be 1-101. How your force only 101 or how ever many rows to be returned is up to you.

    – scsimon
    Jan 3 at 20:19













  • 1-101 are single day records, next day If I have another 100 records I want the identity number should start from 101 to 200

    – Sunny
    Jan 3 at 20:21











  • Well since view's aren't materialized, you'd have to log the output of the execution of the previous day and then add it to your row_number so that you can keep a running total. Not really sure what the end game is here though.

    – scsimon
    Jan 3 at 20:23
















0












0








0








We are using below view to generate identity column values using the ROW_NUMBER() function, but how would I change this view to return incremental numbers in identity column, for example if I run view today it will return 100 records and the identity column value is from 1 to 100, if I run it tomorrow, it should return 101 to whatever the records it has in that day.greatly appreciate your help.



CREATE VIEW [dbo].[Data_Old] 
AS
SELECT
ROW_NUMBER() OVER (ORDER BY event_time) AS id,
event_time, loc, c, d, e, f, Expr1
FROM
(SELECT
FORMAT(h.ACTIVITYDT, 'yyyy MMM dd hh:mm:ss') AS event_time,
ISNULL(h.LOCATION, N'17.51.12.24') loc,
' LEEF:1.0|Manage|WorkSite|9.5|' + CAST(NEWID() AS varchar(50)) c,
'|devTime=' + FORMAT(h.ACTIVITYDT, 'MMM dd hh:mm:ss') d,
'|usrName=' + ISNULL(h.USER, N'{null}') + '|fullName=' + ISNULL(u.FULLNAME, N'{null}') e,
'|activity=' + ISNULL(h.ACTIVITYDT, N'{null}') + '|library=COLLECTIONS' + '|Num=' + CAST(h.NUM AS varchar) + '|version=' + CAST(h.VERSION AS varchar) f,
'|pagesPrinted=' + CAST(h.PAGES_PRINTED AS varchar) + '|FTI=' + ISNULL(CAST(d.CBOOL2 AS varchar), N'False')
+ '|keyData=case:' + ISNULL(d.C5ALIAS, N'{null}') + ';serialNo:' + ISNULL(d.C16ALIAS, N'{null}')
+ ';documentNo:' + ISNULL(CAST(d.CDBL1 AS varchar), N'') AS Expr1
FROM
dbo.HISTORY AS h
LEFT OUTER JOIN
dbo.MASTERS AS d WITH (nolock) ON d.NUM = h.NUM AND d.VERSION = h.VERSION
LEFT OUTER JOIN
dbo.USERS AS u WITH (nolock) ON h.USER = u.USERID
WHERE
(h.ACTIVITYDT >= DATEADD(YEAR, DATEDIFF(YEAR, 0, GETDATE()), 0)
AND h.ACTIVITYDT < DATEADD(YEAR, DATEDIFF(YEAR, 0, GETDATE()) + 1, 0))
AND (h.ACTIVITYDT >= DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0)
AND h.ACTIVITYDT < DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) + 1, 0))
AND (h.ACTIVITYDT >= DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), 0)
AND h.ACTIVITYDT < DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()) + 1, 0))

UNION ALL

SELECT
FORMAT(ACTIONDT, 'yyyy MMM dd hh:mm:ss') AS event_time,
' 127.55.82.124' b,
' LEEF:1.0|Manage|WorkSite|9.5|' + CONVERT(VARCHAR, SID) + CONVERT(VARCHAR, DATEDIFF(SECOND, ACTIONDT, '2017-01-01')) c,
'|devTime=' + FORMAT(ACTIONDT, 'MMM dd hh:mm:ss') d,
'|usrName=' + ISNULL(USERID, '{null}') e,
'|activity=' + CASE ACTIONSID
WHEN 1 THEN 'Successful Login'
WHEN 2 THEN 'Unsuccessful Login'
WHEN 3 THEN 'Impersonated Login'
WHEN 4 THEN 'Impersonated Logoff'
END f,
'|library=COLLECTIONS' AS LEEF
FROM
dbo.HISTORY
WHERE
(ACTIONDT >= DATEADD(YEAR, DATEDIFF(YEAR, 0, GETDATE()), 0)
AND ACTIONDT < DATEADD(YEAR, DATEDIFF(YEAR, 0, GETDATE()) + 1, 0))
AND (ACTIONDT >= DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0)
AND ACTIONDT < DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) + 1, 0))
AND (ACTIONDT >= DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), 0)
AND ACTIONDT < DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()) + 1, 0))
) AS MyResult









share|improve this question
















We are using below view to generate identity column values using the ROW_NUMBER() function, but how would I change this view to return incremental numbers in identity column, for example if I run view today it will return 100 records and the identity column value is from 1 to 100, if I run it tomorrow, it should return 101 to whatever the records it has in that day.greatly appreciate your help.



CREATE VIEW [dbo].[Data_Old] 
AS
SELECT
ROW_NUMBER() OVER (ORDER BY event_time) AS id,
event_time, loc, c, d, e, f, Expr1
FROM
(SELECT
FORMAT(h.ACTIVITYDT, 'yyyy MMM dd hh:mm:ss') AS event_time,
ISNULL(h.LOCATION, N'17.51.12.24') loc,
' LEEF:1.0|Manage|WorkSite|9.5|' + CAST(NEWID() AS varchar(50)) c,
'|devTime=' + FORMAT(h.ACTIVITYDT, 'MMM dd hh:mm:ss') d,
'|usrName=' + ISNULL(h.USER, N'{null}') + '|fullName=' + ISNULL(u.FULLNAME, N'{null}') e,
'|activity=' + ISNULL(h.ACTIVITYDT, N'{null}') + '|library=COLLECTIONS' + '|Num=' + CAST(h.NUM AS varchar) + '|version=' + CAST(h.VERSION AS varchar) f,
'|pagesPrinted=' + CAST(h.PAGES_PRINTED AS varchar) + '|FTI=' + ISNULL(CAST(d.CBOOL2 AS varchar), N'False')
+ '|keyData=case:' + ISNULL(d.C5ALIAS, N'{null}') + ';serialNo:' + ISNULL(d.C16ALIAS, N'{null}')
+ ';documentNo:' + ISNULL(CAST(d.CDBL1 AS varchar), N'') AS Expr1
FROM
dbo.HISTORY AS h
LEFT OUTER JOIN
dbo.MASTERS AS d WITH (nolock) ON d.NUM = h.NUM AND d.VERSION = h.VERSION
LEFT OUTER JOIN
dbo.USERS AS u WITH (nolock) ON h.USER = u.USERID
WHERE
(h.ACTIVITYDT >= DATEADD(YEAR, DATEDIFF(YEAR, 0, GETDATE()), 0)
AND h.ACTIVITYDT < DATEADD(YEAR, DATEDIFF(YEAR, 0, GETDATE()) + 1, 0))
AND (h.ACTIVITYDT >= DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0)
AND h.ACTIVITYDT < DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) + 1, 0))
AND (h.ACTIVITYDT >= DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), 0)
AND h.ACTIVITYDT < DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()) + 1, 0))

UNION ALL

SELECT
FORMAT(ACTIONDT, 'yyyy MMM dd hh:mm:ss') AS event_time,
' 127.55.82.124' b,
' LEEF:1.0|Manage|WorkSite|9.5|' + CONVERT(VARCHAR, SID) + CONVERT(VARCHAR, DATEDIFF(SECOND, ACTIONDT, '2017-01-01')) c,
'|devTime=' + FORMAT(ACTIONDT, 'MMM dd hh:mm:ss') d,
'|usrName=' + ISNULL(USERID, '{null}') e,
'|activity=' + CASE ACTIONSID
WHEN 1 THEN 'Successful Login'
WHEN 2 THEN 'Unsuccessful Login'
WHEN 3 THEN 'Impersonated Login'
WHEN 4 THEN 'Impersonated Logoff'
END f,
'|library=COLLECTIONS' AS LEEF
FROM
dbo.HISTORY
WHERE
(ACTIONDT >= DATEADD(YEAR, DATEDIFF(YEAR, 0, GETDATE()), 0)
AND ACTIONDT < DATEADD(YEAR, DATEDIFF(YEAR, 0, GETDATE()) + 1, 0))
AND (ACTIONDT >= DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0)
AND ACTIONDT < DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) + 1, 0))
AND (ACTIONDT >= DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), 0)
AND ACTIONDT < DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()) + 1, 0))
) AS MyResult






sql-server






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 4 at 16:46







Sunny

















asked Jan 3 at 20:14









SunnySunny

32




32













  • row_number is going to give you a number for every row. So if you have 101 rows, the ID's will be 1-101. How your force only 101 or how ever many rows to be returned is up to you.

    – scsimon
    Jan 3 at 20:19













  • 1-101 are single day records, next day If I have another 100 records I want the identity number should start from 101 to 200

    – Sunny
    Jan 3 at 20:21











  • Well since view's aren't materialized, you'd have to log the output of the execution of the previous day and then add it to your row_number so that you can keep a running total. Not really sure what the end game is here though.

    – scsimon
    Jan 3 at 20:23





















  • row_number is going to give you a number for every row. So if you have 101 rows, the ID's will be 1-101. How your force only 101 or how ever many rows to be returned is up to you.

    – scsimon
    Jan 3 at 20:19













  • 1-101 are single day records, next day If I have another 100 records I want the identity number should start from 101 to 200

    – Sunny
    Jan 3 at 20:21











  • Well since view's aren't materialized, you'd have to log the output of the execution of the previous day and then add it to your row_number so that you can keep a running total. Not really sure what the end game is here though.

    – scsimon
    Jan 3 at 20:23



















row_number is going to give you a number for every row. So if you have 101 rows, the ID's will be 1-101. How your force only 101 or how ever many rows to be returned is up to you.

– scsimon
Jan 3 at 20:19







row_number is going to give you a number for every row. So if you have 101 rows, the ID's will be 1-101. How your force only 101 or how ever many rows to be returned is up to you.

– scsimon
Jan 3 at 20:19















1-101 are single day records, next day If I have another 100 records I want the identity number should start from 101 to 200

– Sunny
Jan 3 at 20:21





1-101 are single day records, next day If I have another 100 records I want the identity number should start from 101 to 200

– Sunny
Jan 3 at 20:21













Well since view's aren't materialized, you'd have to log the output of the execution of the previous day and then add it to your row_number so that you can keep a running total. Not really sure what the end game is here though.

– scsimon
Jan 3 at 20:23







Well since view's aren't materialized, you'd have to log the output of the execution of the previous day and then add it to your row_number so that you can keep a running total. Not really sure what the end game is here though.

– scsimon
Jan 3 at 20:23














1 Answer
1






active

oldest

votes


















1














Generate the ROW_NUMBER() over the entire table in a CTE without a WHERE clause.



Then to look at only today's data, select from the CTE and use a WHERE clause.



Your current code applies the WHERE clause first (in the derived table) and then uses ROW_NUMBER() over those filtered results, so the Row Number will always start at 1. You need to do the opposite.






share|improve this answer
























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54029176%2fsql-server-auto-id-for-a-view-result%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    Generate the ROW_NUMBER() over the entire table in a CTE without a WHERE clause.



    Then to look at only today's data, select from the CTE and use a WHERE clause.



    Your current code applies the WHERE clause first (in the derived table) and then uses ROW_NUMBER() over those filtered results, so the Row Number will always start at 1. You need to do the opposite.






    share|improve this answer




























      1














      Generate the ROW_NUMBER() over the entire table in a CTE without a WHERE clause.



      Then to look at only today's data, select from the CTE and use a WHERE clause.



      Your current code applies the WHERE clause first (in the derived table) and then uses ROW_NUMBER() over those filtered results, so the Row Number will always start at 1. You need to do the opposite.






      share|improve this answer


























        1












        1








        1







        Generate the ROW_NUMBER() over the entire table in a CTE without a WHERE clause.



        Then to look at only today's data, select from the CTE and use a WHERE clause.



        Your current code applies the WHERE clause first (in the derived table) and then uses ROW_NUMBER() over those filtered results, so the Row Number will always start at 1. You need to do the opposite.






        share|improve this answer













        Generate the ROW_NUMBER() over the entire table in a CTE without a WHERE clause.



        Then to look at only today's data, select from the CTE and use a WHERE clause.



        Your current code applies the WHERE clause first (in the derived table) and then uses ROW_NUMBER() over those filtered results, so the Row Number will always start at 1. You need to do the opposite.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 3 at 20:23









        Tab AllemanTab Alleman

        27.5k62443




        27.5k62443
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54029176%2fsql-server-auto-id-for-a-view-result%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Mossoró

            Can't read property showImagePicker of undefined in react native iOS

            Pushsharp Apns notification error: 'InvalidToken'