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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:47:14+00:00 2026-05-23T15:47:14+00:00

Table1 sub-id ref-id Name 1 1 Project 1 2 1 Project 2 3 2

  • 0

Table1

 sub-id  ref-id    Name
    1       1         Project 1
    2       1         Project 2  
    3       2         Project 3
    4       2         Project 4

Table2

 sub-id  ref-id    log_stamp      Recepient      log_type
    ----------------------------------------------------
    1     1       06/06/2011     person A       1
    1     1       06/14/2011     person B       2
    1     1       06/16/2011     person C       2
    1     1       06/17/2011     person D       3
    2     1       06/18/2011     person E       2
    2     1       06/19/2011     person F       2

    3     2       06/20/2011     person G       1
    4     2       06/23/2011     person H       3

Result

Name        ref-id    start_date    Recepient     latest_comment Recepient completion_date  Receipient
Project1    1         06/06/2011    person A      06/19/2011     person F      06/17/2011   person D
Project3    2         06/20/2011    person G      NULL           NULL          06/23/2011   person H


log_type of 1 stands for start_date
log_type of 2 stands for latest_comment
log_type of 3 stands for completion_date
The Name of the project is just the name of the top-most name in the same group of ref-id

have tried this for now

;with T as (select
    Table2.ref-id, 
        Table2.log_stamp,
        Table2 log.log_type
        when 1 then '1'
        when 2 then '2'    
        when 3 then '3'

    end as title

from 
    Submission sb inner join submission_log log on Table1.[sub-id] = Table2.[sub-id]
)

select * from T
pivot (
    max(log_stamp)
    for title IN ([1],[2],[3],[5],[6],[9],[11])
  • 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-23T15:47:15+00:00Added an answer on May 23, 2026 at 3:47 pm

    I was unable to do it as a pivot, I dont think it is possible as described

    DECLARE @table1 TABLE (sub_id INT, ref_id INT, name VARCHAR(50))
    INSERT @table1 VALUES (1, 1, 'Project 1')
    INSERT @table1 VALUES (2, 1, 'Project 2')   
    INSERT @table1 VALUES (3, 2, 'Project 3' )
    INSERT @table1 VALUES (4, 2, 'Project 4') 
    
    DECLARE @Table2 TABLE (sub_id INT, ref_id INT, log_stamp DATETIME, recepient VARCHAR(10), logtype INT)
    
    INSERT @table2 VALUES(1,1,'06/06/2011','person A',1) 
    INSERT @table2 VALUES(1,1,'06/14/2011','person B',2) 
    INSERT @table2 VALUES(1,1,'06/16/2011','person C',2)
    INSERT @table2 VALUES(1,1,'06/17/2011','person D',3)
    INSERT @table2 VALUES(2,1,'06/18/2011','person E',2)
    INSERT @table2 VALUES(2,1,'06/19/2011','person F',2)
    INSERT @table2 VALUES(3,2,'06/20/2011','person G',1)       
    INSERT @table2 VALUES(3,2,'06/23/2011','person H',3) 
    
    ;WITH a as ( 
    SELECT RN = ROW_NUMBER() OVER (PARTITION BY t1.sub_id, t1.ref_id, t1.name, t2.logtype ORDER BY log_stamp DESC), t1.sub_id, t1.ref_id, t1.name, t2.Recepient , t2.logtype ,log_stamp
    FROM  @table1 t1 JOIN @table2 t2 ON t1.ref_id = t2.ref_id AND 
    t1.sub_id = t2.sub_id),
    b as (SELECT * FROM a WHERE RN = 1)
    SELECT b1.name, b1.ref_id,b1.log_stamp start_date , b1.Recepient, b2.log_stamp latest_comment , b2.Recepient, b3.log_stamp completion_date , b3.Recepient
    FROM b b1
    LEFT JOIN b b2 ON b1.sub_id=b2.sub_id AND b1.ref_id = b2.ref_id AND b2.logtype = 2
    LEFT JOIN b b3 ON b1.sub_id=b3.sub_id AND b1.ref_id = b3.ref_id AND b3.logtype = 3
    WHERE b1.logtype = 1
    

    Result:

    name         ref_id      start_date              Recepient  latest_comment          Recepient  completion_date         Recepient
    ------------ ----------- ----------------------- ---------- ----------------------- ---------- ----------------------- ----------
    Project 1    1           2011-06-06 00:00:00.000 person A   2011-06-16 00:00:00.000 person C   2011-06-17 00:00:00.000 person D
    Project 3    2           2011-06-20 00:00:00.000 person G   NULL                    NULL       2011-06-23 00:00:00.000 person H
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have records in table1, if the records exist, it must copy into table2.
I am writing the following sub query for some project specific purpose: SELECT count(*)
I have a table in SQL-server with projectcodes and sub-project-codes in the same fields.
My question is how to sort a Linq query by a sub table: Table
I have 2 tables, Categories and Sub Categories. Is it prossible to present all
I have this table: fourn_category (id , sub) I am using this code to
Table1: Everything including the kitchen sink. Dates in the wrong format (year last so
Table1 Time 10:00:00 12:00:00 Absent 14:00:00 Holiday ..., Time column Datatype is varchar I
Table1 ID product 001 LG 001 Sony 001 LG 001 LG 001 Sony 001
Table1 ID | WorkTime ----------------- 001 | 10:50:00 001 | 00:00:00 002 | ....

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.