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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T05:15:48+00:00 2026-06-15T05:15:48+00:00

I have the following tables in a SQL Server 2000 database: Master MasterID |

  • 0

I have the following tables in a SQL Server 2000 database:

Master

MasterID | Details   | [other fields]
=====================================
PK (int) | Free text | ...

LogTable

LogID    | MasterID | UserID    | LogDate    | LogText
==========================================================
PK (int) | FK (int) | VarChar(2)| Date stamp | Free text

There may be many Log entries for each master record.

I have a query which extracts the most recent three associated Log entries for each Master row as shown below. Note that appropriate conversion and formatting is performed to achieve the LogData concatenation (omitted for clarity):

SELECT 
    M.MasterID, M.Details, L.LogDate + L.UserID + L.LogText AS LogData 
FROM
    MasterTable M 
INNER JOIN 
    LogTable L ON M.MasterID = L.MasterID 
    AND L.LogID IN (SELECT TOP 3 LogID FROM LogTable 
                    WHERE MasterID = M. MasterID ORDER BY LogDate DESC)

This produces output like this:

MasterID | Details | LogData
========================================================
1        | First   | 05/11/2012 AB Called Client  
2        | Second  | 08/11/2012 CD Client Visit  
2        | Second  | 07/11/2012 CD Called Client  
2        | Second  | 05/11/2012 AB Called Client  

What I need to achieve is showing the data from the second table as columns in the output, all reported against each single master record, thus avoiding repeated data. Like so:

MasterID | Details | LogData1                    | LogData2                    | LogData3
===========================================================================================================
1        | First   | 05/11/2012 AB Called Client | (null)                      | (null)  
2        | Second  | 08/11/2012 CD Client Visit  | 07/11/2012 CD Called Client | 05/11/2012 AB Called Client  

Note that in the real world requirement, this solution will be part of flattening 5 tables with the output consisting of approx 20,000 rows and 90 columns of data.

Thanks in advance.

  • 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-15T05:15:49+00:00Added an answer on June 15, 2026 at 5:15 am

    I’m going to post this, just to show it can be done, but HIGHLY SUGGEST, not do it through SQL. Should be done through the UI that’s displaying to be more dynamic on your columns. Even then, I would design this differently.

    -- create master table
    DECLARE @MasterTable TABLE (
        [MasterID] [int] IDENTITY (1, 1) NOT NULL ,
        [Details] [varchar] (50) ,
        [AdditionalField_1] [varchar] (50) ,
        [AdditionalField_n] [varchar] (50) 
    )
    -- create log table
    DECLARE @LogTable TABLE (
        [LogID] [int] IDENTITY (1, 1) NOT NULL ,
        [MasterID] [int] NULL ,
        [UserID] [varchar] (2) ,
        [LogDate] [datetime] NULL ,
        [LogText] [varchar] (50) 
    ) 
    -- insert into master table
    INSERT INTO @MasterTable  (Details)
            VALUES ('First')
    INSERT INTO @MasterTable  (Details)
            VALUES ('Second') 
    -- insert into log table
    INSERT INTO @LogTable  (MasterID, UserID, LogDate, LogText)
            VALUES (1, 'AB', '05/11/2012', 'Called Client')   
    INSERT INTO @LogTable  (MasterID, UserID, LogDate, LogText)
            VALUES (2, 'AB', '05/11/2012', 'Called Client')   
    INSERT INTO @LogTable  (MasterID, UserID, LogDate, LogText)
            VALUES (2, 'CD', '07/11/2012', 'Called Client')   
    INSERT INTO @LogTable  (MasterID, UserID, LogDate, LogText)
            VALUES (2, 'CD', '08/11/2012', 'Client Visit') 
    -- create table to display data
    DECLARE @MyTemp TABLE (MasterID INT, Details VARCHAR(50), LogData1 VARCHAR(50), LogData2 VARCHAR(50), LogData3 VARCHAR(50))
    INSERT INTO @MyTemp SELECT MasterID, Details, NULL, NULL, NULL FROM @MasterTable
    -- create vars
    DECLARE @ID INT, @NewID INT, @MasterID INT, @NewValue VARCHAR(100)
    SET @ID = 0
    -- loop through data
    WHILE @ID >-1
    BEGIN
        -- clear vars
        SELECT @NewID = NULL, @MasterID = NULL, @NewValue = NULL
        -- get first record
        SELECT TOP 1    
            @NewValue = CONVERT(VARCHAR(10), LogDate, 103)+ ' ' + UserID + ': ' + LogText
        ,   @MasterID=MasterID
        ,   @NewID=LogID 
        FROM @LogTable WHERE LogID>@ID
        -- if no data, exit loop
        IF @NewID IS NULL
            BREAK
        -- update record based on valuds in fields
        UPDATE m 
            SET @ID = @NewID
            ,   LogData1 = (CASE WHEN m.LogData1 IS NULL THEN @NewValue ELSE m.LogData1 END)
            ,   LogData2 = (CASE WHEN m.LogData1 IS NOT NULL THEN 
                                (CASE WHEN m.LogData2 IS NULL THEN @NewValue ELSE m.LogData2 END)
                            ELSE m.LogData2 END)
            ,   LogData3 = (CASE WHEN m.LogData1 IS NOT NULL THEN 
                                (CASE WHEN m.LogData2 IS NOT NULL THEN 
                                    (CASE WHEN m.LogData3 IS NULL THEN @NewValue ELSE m.LogData3 END)
                                 ELSE m.LogData3 END)
                            ELSE m.LogData3 END)
        FROM @MyTemp m
        WHERE m.MasterID=@MasterID
    END
    --display all data
    SELECT * FROM @MyTemp 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following XML generated from various tables in my SQL SERVER database
I have a SQL Server 2000 database with 7 tables. I want to copy
I have the following 2 tables defined in a SQL Server database: CREATE TABLE
I have the following table in SQL Server 2000: TABLE_NAME | COLUMN_NAME | TYPE_NAME
I have the following tables in SQL Server 2005 ReceiptPoint: ID (PK), Name GasIndexLocation:
i have the following tables in sql server: photoalbumsTable: album_ID album_caption albumtagmap id album_id
I have a following scenario in my SQL Server 2005 database. zipcodes table has
Programs used: SQL Server 2000, Excel 2003 We have a table in our database
I have the following data in a SQL Server 2000 table: Dates ----------------------- 2012-05-04
I have a table in SQL Server 2000 with data similar to the following:

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.