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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T06:27:32+00:00 2026-06-17T06:27:32+00:00

I’m trying to set row numbers to a table’s output. Table looks like AccountNum

  • 0

I’m trying to set row numbers to a table’s output.

Table looks like

AccountNum  DataAction  ActionType  ActionStartCounter
123         11/01/2013  HELLO       1
123         12/01/2013  NONO        NULL
123         16/01/2013  YESYES      NULL
123         1/02/2013   HELLO       2
123         4/02/2013   YESYES      NULL
456         10/01/2013  HELLO       1
456         13/01/2013  NONO        NULL
456         14/01/1900  WHYWHY      NULL
456         15/01/2013  YESYES      NULL
456         20/03/2013  HELLO       2
456         31/03/2013  YESYES      NULL

What this is trying to do is

1) Every time there is Hello for an account it sequences the table based on DateAction under the column ActionStartCounter

2) the NULLs signify these actions are not HELLO and are part of Previous Numbered row

i.e. for AccountNum 123 NONO on 12/01/2013 is linked to HELLO on 11/01/2013.

3) YESYES is the last action for any Account for any start by HELLO.

I want the output to be

AccountNum  DataAction  ActionType  ActionStartCounter    ActionCounter
123         11/01/2013  HELLO       1                     11
123         12/01/2013  NONO        NULL                  12
123         16/01/2013  YESYES      NULL                  13
123         1/02/2013   HELLO       2                     21
123         4/02/2013   YESYES      NULL                  22
456         10/01/2013  HELLO       1                     11
456         13/01/2013  NONO        NULL                  12
456         14/01/1900  WHYWHY      NULL                  13
456         15/01/2013  YESYES      NULL                  14
456         20/03/2013  HELLO       2                     21
456         31/03/2013  YESYES      NULL                  22

Where new field ActionCounter will essentially be concatenation of

ActionStartCounter and rownumber within the accountNum and ActionStartCounter

meaning

the second piece in the concatenation is about the row_number within the start of the HELLO, as soon as new HELLO comes in the counter resets.

ActionStartCounter also is based on case when ActionType ='Hello' then row_number() over(partition by AccountNum order by DateAction)

If you guys reckon that should be changed we can get that done. If you think the AccountNum or Date can be part of the new column to make it unique we can do that. There is no limitation on number of fields we need to transform to get to last column.

Thanks for your help.

PS: Platform SQL Server 2005

Here’s DDL

Create Table ActionDetails
(
AccountNum Int,
DataAction datetime,
ActionType Varchar(25),
ActionStart int 
)

Insert into ActionDetails
Select 123,CONVERT(datetime,'20130111' ,112),'HELLO',1 UNION 
Select 123,CONVERT(datetime,'20130112' ,112),'NONO',NULL UNION 
Select 123,CONVERT(datetime,'20130116' ,112),'YESYES',NULL UNION 
Select 123,CONVERT(datetime,'20130201' ,112),'HELLO',2 UNION 
Select 123,CONVERT(datetime,'20130204' ,112),'YESYES',NULL UNION 
Select 456,CONVERT(datetime,'20130110' ,112),'HELLO',1 UNION 
Select 456,CONVERT(datetime,'20130113' ,112),'NONO',NULL UNION 
Select 456,CONVERT(datetime,'20130114' ,112),'WHYWHY',NULL UNION 
Select 456,CONVERT(datetime,'20130115' ,112),'YESYES',NULL UNION 
Select 456,CONVERT(datetime,'20130320' ,112),'HELLO',2 UNION 
Select 456,CONVERT(datetime,'20130331' ,112),'YESYES',NULL 
  • 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-17T06:27:33+00:00Added an answer on June 17, 2026 at 6:27 am

    (456 doesn’t start with HELLO – not sure how to handle).
    Had to rewrite your dates, might have made an error there. The mechanics should be sound though.

    ;WITH MyTable (AccountNum,  DataAction,  ActionType,  ActionStartCounter) AS
    (
    SELECT 123,         CAST('01/11/2013' AS DATETIME),  'HELLO',       1       UNION ALL
    SELECT 123,         '01/12/2013',  'NONO',        NULL  UNION ALL
    SELECT 123,         '01/16/2013',  'YESYES',      NULL  UNION ALL
    SELECT 123,         '2/1/2013',   'HELLO',       2      UNION ALL
    SELECT 123,         '2/4/2013',   'YESYES',      NULL   UNION ALL
    SELECT 456,         '1/10/2013',  'HELLO',       1      UNION ALL
    SELECT 456,         '1/13/2013',  'NONO',        NULL   UNION ALL
    SELECT 456,         '1/14/1900',  'WHYWHY',      NULL   UNION ALL
    SELECT 456,         '01/15/2013',  'YESYES',      NULL  UNION ALL
    SELECT 456,         '03/20/2013',  'HELLO',       2     UNION ALL
    SELECT 456,         '3/31/2013',  'YESYES',      NULL   
    )
    ,CTE AS
    (
        SELECT   * 
                ,SeqGroupAcc    = ROW_NUMBER() OVER (PARTITION BY AccountNum ORDER BY DataAction)
        FROM MyTable
    )
    ,CTE2 AS
    (
        SELECT   *
                ,MyCounting = 1
        FROM CTE
        WHERE SeqGroupAcc = 1
        UNION ALL
        SELECT   T2.AccountNum
                ,T2.DataAction
                ,T2.ActionType
                ,CASE WHEN T2.ActionStartCounter IS NULL THEN T1.ActionStartCounter ELSE  T2.ActionStartCounter END
                ,T2.SeqGroupAcc
                ,CASE WHEN T2.ActionType = 'HELLO' THEN 1 ELSE T1.MyCounting + 1 END
        FROM CTE2   T1
        JOIN CTE    T2 ON T1.SeqGroupAcc = T2.SeqGroupAcc - 1 AND T1.AccountNum = T2.AccountNum
    )                                                       
    SELECT   AccountNum
            ,DataAction
            ,ActionType
            ,ActionStartCounter
            ,ActionCounter  = (ActionStartCounter) * 10 + MyCounting
    --OR: ,ActionCounter    = CAST(ActionStartCounter AS VARCHAR(5)) + CAST(MyCounting AS VARCHAR(5))
    FROM CTE2
    WHERE AccountNum = 123
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to render a haml file in a javascript response like so:
I would like to run a str_replace or preg_replace which looks for certain words
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this

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.