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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T18:13:28+00:00 2026-05-11T18:13:28+00:00

I have some tables with data: Category CategoryID CategoryName 1 Home 2 Contact 3

  • 0

I have some tables with data:

Category

CategoryID     CategoryName

1              Home
2              Contact
3              About

Position

PositionID     PositionName

1              Main menu
2              Left menu
3              Right menu

…(new row can be added later)

CategoryPosition

CPID   CID    PID    COrder

1      1      1      1 
2      1      2      2
3      1      3      3
4      2      1      4
5      2      3      5

How can I make a table like this:

CID    CName     MainMenu   LeftMenu   RightMenu

1      Home      1          2          3
2      Contact   4          0          5
3      About     0          0          0

And if a new Category or Position row is added later, the query should reflect the change automatically, e.g:

CID    CName     MainMenu   LeftMenu   RightMenu   BottomMenu

1      Home      1          2          3           0
2      Contact   4          0          5           0
3      About     0          0          0           0
4      News      0          0          0           0
  • 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-11T18:13:28+00:00Added an answer on May 11, 2026 at 6:13 pm

    The following dynamic query seems to work:

    declare @columnlist nvarchar(4000)
    select @columnlist = IsNull(@columnlist + ', ', '') + '[' + PositionName + ']'
    from #Position
    
    declare @query nvarchar(4000)
    select @query = '
        select *
        from (
            select CategoryId, CategoryName, PositionName, 
                    IsNull(COrder,0) as COrder
            from #Position p
            cross join #Category c
            left join #CategoryPosition cp 
                    on cp.pid = p.PositionId 
                    and cp.cid = c.CategoryId
        ) pv
        PIVOT (max(COrder) FOR PositionName in (' + @columnlist + ')) as Y
        ORDER BY CategoryId, CategoryName
    '
    
    exec sp_executesql @query
    

    Some clarification:

    • The @columnlist contains the dymamic field list, built from the Positions table
    • The cross join creates a list of all categories and all positions
    • The left join seeks the corresponding COrder
    • max() selects the highest COrder per category+position, if there is more than one
    • PIVOT() turns the various PositionNames into separate columns

    P.S. My table names begin with #, because I created them as temporary tables. Remove the # to refer to a permanent table.

    P.S.2. If anyone wants to try his hands at this, here is a script to create the tables in this question:

    set nocount on 
    
    if object_id('tempdb..#Category') is not null drop table #Category
    create table #Category (
        CategoryId int identity,
        CategoryName varchar(50)
    )
    
    insert into #Category (CategoryName) values ('Home')
    insert into #Category (CategoryName) values ('Contact')
    insert into #Category (CategoryName) values ('About')
    --insert into #Category (CategoryName) values ('News')
    
    if object_id('tempdb..#Position') is not null drop table #Position
    create table #Position (
        PositionID int identity,
        PositionName varchar(50)
    )
    
    insert into #Position (PositionName) values ('Main menu')
    insert into #Position (PositionName) values ('Left menu')
    insert into #Position (PositionName) values ('Right menu')
    --insert into #Position (PositionName) values ('Bottom menu')
    
    if object_id('tempdb..#CategoryPosition') is not null 
        drop table #CategoryPosition
    create table #CategoryPosition (
        CPID int identity,
        CID int,
        PID int,
        COrder int
    )
    
    insert into #CategoryPosition (CID, PID, COrder) values (1,1,1)
    insert into #CategoryPosition (CID, PID, COrder) values (1,2,2)
    insert into #CategoryPosition (CID, PID, COrder) values (1,3,3)
    insert into #CategoryPosition (CID, PID, COrder) values (2,1,4)
    insert into #CategoryPosition (CID, PID, COrder) values (2,3,5)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some PHP code that generates dynamic tables of data on the fly.
I have a number of tables containing some basic (business related) mapping data. What's
I have a data-upload function that load some data into several tables and processes
We have a set of tables and views that merely store some config data
I have a Table Category, 1) Id 2) CategoryName 3) CategoryMaster with data as:
Ok so I have some data in a product table that contains a category
I have two tables of data categories and category relations. This is a setup
I have these tables cat (holds main data for categories) id_cat, original name cat_lang
I have some data in a table that looks roughly like the following: table
I have some data in an SQL table with an XML datatype column where

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.