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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T22:05:40+00:00 2026-05-14T22:05:40+00:00

If you have the select statement below where the PK is the primary key:

  • 0

If you have the select statement below where the PK is the primary key:

select  distinct dbo.DateAsInt( dateEntered) * 100 as PK, 
                 recordDescription as Data
from    MyTable

and the output is something like this (the first numbers are spaced for clarity):

      PK             Data
2010 01 01 00    New Years Day
2010 01 01 00    Make Resolutions
2010 01 01 00    Return Gifts
2010 02 14 00    Valentines day
2010 02 14 00    Buy flowers

and you want to output something like this:

      PK             Data
2010 01 01 01    New Years Day
2010 01 01 02    Make Resolutions
2010 01 01 03    Return Gifts
2010 02 14 01    Valentines day
2010 02 14 02    Buy flowers

Is it possible to make the “00” in the PK have an “identity” number effect within a single select? Otherwise, how could you increment the number by 1 for each found activity for that date?

I am already thinking as I type to try something like Sum(case when ?? then 1 end) with a group by.

Edit: (Answer provided by JohnFX below)

This was the final answer:

select    PK + row_number() over 
          (PARTITION BY eventid order by eventname) as PK,
          recordDescription
from      (select   distinct  -- Removes row counts of excluded rows)
                    dbo.DateAsInt( dateEntered) as PK,
                    recordDescription as Data
           from     MyTable) A
order by eventid, recordDescription
  • 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-14T22:05:40+00:00Added an answer on May 14, 2026 at 10:05 pm

    I think you are looking for ROW_NUMBER and the associated PARTITION clause.

    SELECT DISTINCT dbo.DateAsInt(DateEntered) * 100 as PK, 
                     ROW_NUMBER() OVER (PARTITION BY DateEntered ORDER BY recordDescription) as rowID,
                     recordDescription as Data
    FROM MyTable
    

    If DateEntered has duplicate values you probably also want to check out DENSE_RANK() and RANK() depending on your specific needs for what to do with the incrementor in those cases.

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

Sidebar

Related Questions

I have the following statement SELECT id, descr from mytable which returns 1, 'Test1'
I have the following SQL-statement: SELECT DISTINCT name FROM log WHERE NOT name =
i have sql statement like this SELECT DISTINCT results_sp_08.material_number FROM results_sp_08 INNER JOIN courses
I have a SQL statement that looks like: SELECT [Phone] FROM [Table] WHERE (
If i have a parameterized SQL statement like this: SELECT * FROM table WHERE
In SQL server 2008, I have select statement that spits below text output. 'text1d','text2','text3'
I have a select statement with calculated columns and I would like to use
Suppose I have a SELECT statement that returns some set of results. Is there
I am trying to find a way to have a SELECT statement return the
I have a relatively simple select statement in a VB6 program that I have

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.