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 a SELECT statement like this: SELECT T1.COD, T1.NAME, (SELECT MAX(T2.DATA) FROM dbo.TAB2
I have a select statement where I want to take 100 characters from a
I have a SELECT statement (see below) that gets a few items from the
I have created a procedure and used the below statement inside that . select
I have a select statement running which pulls data into $row. $result = mysql_query(Select
I have this ugly SELECT statement : Select 'Urbain' as Véhicule, Count( DISTINCT Plaintes.IDEvenements)
We have this statement: (SELECT res_bev.bev_id, property_value.name AS priority FROM res_bev, bev_property, property_value WHERE
I have a SELECT statement similar to the one below which returns several counts
How to get a correct result for my select statement below SELECT * FROM
I have below SQL statment INSERT INTO my_counts (type,code,count) SELECT type,id ,1 from my_data

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.