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

  • Home
  • SEARCH
  • 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 8742095
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:26:21+00:00 2026-06-13T11:26:21+00:00

Here is the sample code for what I am trying to do and below

  • 0

Here is the sample code for what I am trying to do and below is the result:

   CREATE TABLE dbo.#TempDoc_DocContRoles (DocID int null, FullName varchar(500), DocContRole 
     varchar (100), NumRole int null)

   INSERT INTO #TempDoc_DocContRoles(DocID, FullName, DocContRole) 

      SELECT 
          d.DocID, c.FirstName + ' ' + c.LastName as FullName, ldcro.DocContRole 
      FROM 
          Document as d 
      JOIN 
          dbo.Split( ',','30,31') AS l ON d.DocID = cast(l.[Value] AS int)
      JOIN 
          Doc_Contact AS dc ON d.DocID = dc.DocID 
      JOIN 
          Contact AS c ON dc.P_Number = c.P_Number 
      LEFT JOIN 
          lkpDocContactRole AS ldcro ON ldcro.DocContRoleID = dc.DocContRoleID 
      JOIN 
          dbo.Split( ',','1,2,7') AS r ON ldcro.DocContRoleID = cast(r.[Value] AS int)


   CREATE TABLE dbo.#MaxNumRoles (DocID int null, DocContRole varchar(100), NumRole int null)

   INSERT INTO  dbo.#MaxNumRoles (DocID,DocContRole,NumRole)
      SELECT 
          DocID, DocContRole, COUNT(*) 
      FROM 
          dbo.#TempDoc_DocContRoles 
      GROUP BY 
          DocID, DocContRole 
      HAVING 
          Count(*) > 0

   UPDATE td 
   SET td.NumRole = mr.NumRole
   FROM dbo.#TempDoc_DocContRoles as td
   INNER JOIN dbo.#MaxNumRoles as mr ON td.DocContRole = mr.docContRole 

   SELECT * FROM   dbo.#TempDoc_DocContRoles

   DROP TABLE dbo.#TempDoc_DocContRoles   
   DROP TABLE dbo.#MaxNumRoles 

Result:

DocID   FullName    DocContRole NumRole
30      Smith    Author         3
30      Daln     Staff          2
30      Dolby    Author         3
31      Tammy    Author         3
30      Barny    Author         3
30      Sanny    Res Coor       1
30      Johny    Staff Rev      2

I would like to actually get:

DocID   FullName    DocContRole NumRole
30      Smith    Author         1
30      Daln     Staff          1
30      Dolby    Author         2
31      Tammy    Author         1
30      Barny    Author         3
30      Sanny    Res Coor       1
30      Johny    Staff Rev      2

It should increment the number in NumRole per docContRole and docID (ex Author 1, Author 2 etc). Currently it gives the total number of authors per DocID.

My ultimate goal is to get something like

       DocID    Author_1  Author_2 Author_3 Staff_1 Staff_2 ResCoor_1
30              Smith      Dolby    Barny    Daln    Johny    Sanny 
31              Tammy   
  • 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-13T11:26:22+00:00Added an answer on June 13, 2026 at 11:26 am

    You can try this

    select
        td.DocID, td.FullName, td.DocContRole,
        row_number() over (partition by td.DocID, td.DocContRole order by td.FullName) as NumRole
    from dbo.#TempDoc_DocContRoles as td
    

    So dynamic SQL will be smth like that

    SQL FIDDLE EXAMPLE

    create table #t2
    (
        DocID int, FullName nvarchar(max), 
        NumRole nvarchar(max)
    )
    
    declare @pivot_columns nvarchar(max), @stmt nvarchar(max)
    
    insert into #t2
    select
        td.DocID, td.FullName,
        td.DocContRole + 
        cast(
            row_number() over 
            (partition by td.DocID, td.DocContRole order by td.FullName)
        as nvarchar(max)) as NumRole
    from t as td
    
    select
        @pivot_columns = 
        isnull(@pivot_columns + ', ', '') + 
        '[' +  NumRole + ']'
    from (select distinct NumRole from #t2) as T
    
    select @stmt = '
    select *
    from #t2 as t
    pivot
    (
    min(FullName)
    for NumRole in (' + @pivot_columns + ')
    ) as PT'
    
    exec sp_executesql
        @stmt = @stmt
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's the sample code: public static void col (int n) { if (n %
Here's the sample code: class TestAO { int[] x; public TestAO () { this.x
Here are some sample starting values for variables in the code below. sd <-
I'm using simple jcarousel here is ther code: http://jsfiddle.net/w6fLA/ I'm trying to show div
Here is the sample code that I ran on Visual Studio 2010: #include <iostream>
Here's an sample code, where only the string object is released. NSString *nameOfFile =
Here is the sample code, which produces interesting output: > gg<-data.frame(x=c(a,b),y=as.integer(c(1000,100000))) > gg x
Here is a sample code I'm working on: <%@ Page Language=C# AutoEventWireup=true CodeFile=Default.aspx.cs Inherits=_Default
Here's a sample code: #include <stack> #include <cstddef> template <std::size_t N, template <class> class
I tried to compile Ercia Sadun's sample code here , but this error came

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.