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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:16:00+00:00 2026-05-26T08:16:00+00:00

I have two master tables CompanyMaster , ActivityMaster for a child table CompanyActivities ActivityMaster

  • 0

I have two master tables CompanyMaster, ActivityMaster for a child table CompanyActivities

ActivityMaster

ACTIVITYID   ACTIVITYNAME
A1           testActivity
A2           someActivity
A3           otheractivity
A4           someotheractivity
A5           anyotheractivity

CompanyMaster

COMPANYID  COMPANYNAME
C1         testcompany
C2         ACompany
C3         MyCompany
C4         SomeCompany
C5         ZCompany
C6         Company123
C7         ComapnyABC

CompanyActivities – The COMPANYID in CompanyActivities is having a primarykey-foreighkey relation ship with COMPANYID in CompanyMaster (primary key table) and ACTIVITYID is having a primarykey-foreighkey relation ship with ACTIVITYID in ActivityMaster(primary key table)

COMPANYID   ACTIVITYID
C1          A1
C1          A3
C3          A1
C3          A2
C4          A5
C5          A1
C6          A3
C7          A3

I want to do write a query to get the following output where all the rows in ACTIVITYID column of the ActivityMaster table will be converted to columns

Output

Companies  A1   A2  A3  A4  A5
C1         Y    N   Y   N   N
C2         N    N   N   N   N
C3         Y    Y   N   N   N
C4         N    N   N   N   Y
C5         Y    N   N   N   N
C6         N    N   Y   N   N
C7         N    N   Y   N   N

The output table displays all the companies as rows in the first column and all the activities are shown as columns that start after the first column, if there is row that contains both ACTIVITYID and COMPANYID it will set to Y in output otherwise it would be set to N

eg- COMPANYID C1 is having an activity ACTIVITYID A1 in CompanyActivities table so the first row in the second column that comes just below A1 and in the right to C1 is set Y, whereas C1 and A2 are not having a row, so the third column in the first row is set to N

I am using C#.net and 4 for loops to achieve the output now which is talking a heavy toll on the performance of the application, So i would like to do this using a query, I have searched for pivot queries, but all the examples i found knows the column names before-hand, which i don’t i only get the names of the column names by querying the ActivityMaster.

  • 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-26T08:16:01+00:00Added an answer on May 26, 2026 at 8:16 am
    create table #CompanyMaster (COMPANYID int, COMPANYNAME varchar(30))
    create table #ActivityMaster (ACTIVITYID int, ACTIVITYNAME varchar(30))
    create table #CompanyActivities (COMPANYID int, ACTIVITYID int)
    
    insert into #CompanyMaster
        SELECT 1, 'Company A'
        union all
        SELECT 2, 'Company B'
    
    insert into #ActivityMaster
        SELECT 101, 'Activity X'
        union all
        SELECT 102, 'Activity Y'    
        union all
        SELECT 103, 'Activity Z'    
    
    insert into #CompanyActivities      
        select 1, 102
        union all
        select 2, 101
    
    -- build activities column names
    --case [Activity X] when 0 then ''N'' else ''Y'' end as [Activity X],
    --case [Activity Y] when 0 then ''N'' else ''Y'' end as [Activity Y],
    --case [Activity Z] when 0 then ''N'' else ''Y'' end as [Activity Z]
    declare @activities nvarchar(max)
    set @activities
       = (
        select 'case [' + ACTIVITYNAME + '] when 0 then ''N'' else ''Y'' end as [' + ACTIVITYNAME + '],' + char(10)
        from #ActivityMaster
        for xml path('')
       )
    set @activities = substring(@activities, 0, len(@activities)-1)
    
    declare @activities_for nvarchar(max)
    -- build activities column names in for
    --[Activity X], [Activity Y], [Activity Z]
    set @activities_for
       = (
        select '[' + ACTIVITYNAME + '],' + char(10)
        from #ActivityMaster
        for xml path('')
       )
    set @activities_for = substring(@activities_for, 0, len(@activities_for)-1)
    
    
    declare @sql nvarchar(MAX) = N'
    select COMPANYNAME,
        <activities>
    From
        (select c.COMPANYNAME, a.ACTIVITYNAME,
            (case 
                when ca.ACTIVITYID is not null and ca.COMPANYID is not null then 1
                else 0
            end) as STATUS
        from #CompanyMaster c
        cross join #ActivityMaster a
        left join #CompanyActivities ca on ca.COMPANYID = c.COMPANYID and a.ACTIVITYID = ca.ACTIVITYID)  p
    pivot
        (
            sum(STATUS) for ACTIVITYNAME IN (<activities_for>)
        ) as pvt
    '
    
    
    set @sql = replace(@sql, '<activities>', @activities)
    set @sql = replace(@sql, '<activities_for>', @activities_for)
    
    print @sql
    exec sp_executesql @sql
    
    drop table #CompanyMaster
    drop table #ActivityMaster
    drop table #CompanyActivities
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've two master tables Org & Item and then there's an OrgItem table.I have
I have two tables nested (master view) for representing my data. The parent table
I have two tables: P and PC (master/detail joined by the column Id) Table
so I have two tables. They are pictured below. I have a master table
I have two tables Master Detail In the Master & Detail table I get
I have two tables Master Detail In the Master & Detail table, i have
I have two tables as shown below The master Table ID keyword_tags ----------- ---------------------------------------------
I have two large tables -Master table A: 1.4million rows -Detail table B: 9
I have two InnoDB tables: CREATE TABLE master( id INTEGER UNSIGNED NOT NULL auto_increment
I have two tables: Master Table Assets Table -AssetNo- -AssetNo- AssetNo is the PK,

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.