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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T21:31:06+00:00 2026-05-14T21:31:06+00:00

I have a table (actually a view, but simplified my example to a table)

  • 0

I have a table (actually a view, but simplified my example to a table) which gives me some data like this

 CompanyName website
 Google         google.com
 Google         google.net
 Google         google.org
 Google         google.in
 Google         google.de
 Microsoft       Microsoft.com
 Microsoft       live.com
 Microsoft       bing.com
 Microsoft       hotmail.com

I am looking to convert it to get a result like this

CompanyName website1      website2   website3   website 4   website5  website6 
----------- ------------- ---------- ---------- ----------- --------- --------
Google      google.com    google.net google.org google.in   google.de NULL
Microsoft   Microsoft.com live.com   bing.com   hotmail.com NULL      NULL

I have looked into pivot but looks like the record(row values) cannot be dynamic (i.e can only be certain predefined values).

Also, if there are more than 6 websites, I want to limit it to the first 6

Dynamic pivot makes sense, but I would have to incorporate it into my view ?? Is there a simpler solution for this ?

Here are the SQL scripts

CREATE TABLE [dbo].[Company](

 [CompanyName] [varchar](50) NULL,
 [website] [varchar](50) NULL
) ON [PRIMARY]
GO

insert into company values ('Google','google.com')
insert into company values ('Google','google.net')
insert into company values ('Google','google.org')
insert into company values ('Google','google.in')
insert into company values ('Google','google.de')
insert into company values ('Microsoft','Microsoft.com')
insert into company values ('Microsoft','live.com')
insert into company values ('Microsoft','bing.com')
insert into company values ('Microsoft','hotmail.com')

Edit: I am removing the ID as it was created for simplification. I think I should not have it.Sorry about it

  • 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-14T21:31:07+00:00Added an answer on May 14, 2026 at 9:31 pm

    try this:

    DECLARE @Company table (
        [id] [int] NULL,
        [CompanyName] [varchar](15) NULL,
        [website] [varchar](15) NULL
    ) 
    
    insert into @company values (1,'Google','google.com')
    insert into @company values (2,'Google','google.net')
    insert into @company values (3,'Google','google.org')
    insert into @company values (4,'Google','google.in')
    insert into @company values (5,'Google','google.de')
    insert into @company values (6,'Microsoft','Microsoft.com')
    insert into @company values (7,'Microsoft','live.com')
    insert into @company values (8,'Microsoft','bing.com')
    insert into @company values (9,'Microsoft','hotmail.com')
    
    ;WITH CompanyGrouped AS
    (   SELECT
            *,ROW_NUMBER() OVER(PARTITION BY CompanyName ORDER BY CompanyName, id) AS ColumnNumber
            FROM @company
    )
    SELECT DISTINCT
        t.CompanyName
            ,t1.website AS website1,t2.website AS website2,t3.website AS website3, t4.website AS website4, t5.website AS website5, t6.website AS website6
        FROM @Company                      t
            LEFT OUTER JOIN CompanyGrouped t1 ON t.CompanyName=t1.CompanyName AND t1.ColumnNumber=1
            LEFT OUTER JOIN CompanyGrouped t2 ON t.CompanyName=t2.CompanyName AND t2.ColumnNumber=2
            LEFT OUTER JOIN CompanyGrouped t3 ON t.CompanyName=t3.CompanyName AND t3.ColumnNumber=3
            LEFT OUTER JOIN CompanyGrouped t4 ON t.CompanyName=t4.CompanyName AND t4.ColumnNumber=4
            LEFT OUTER JOIN CompanyGrouped t5 ON t.CompanyName=t5.CompanyName AND t5.ColumnNumber=5
            LEFT OUTER JOIN CompanyGrouped t6 ON t.CompanyName=t6.CompanyName AND t6.ColumnNumber=6
    

    Output:

    CompanyName website1      website2   website3   website4    website5  website6
    ----------- ------------- ---------- ---------- ----------- --------- --------
    Google      google.com    google.net google.org google.in   google.de NULL
    Microsoft   Microsoft.com live.com   bing.com   hotmail.com NULL      NULL
    
    (2 row(s) affected)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a view (actually, it's a table valued function, but the observed behavior
I have a view like this: CREATE VIEW MyView AS SELECT Column FROM Table
Greetings, fellow coders! I have this table that contains categories and subcategories (actually I
I have table in which I am inserting rows for employee but next time
I have table and this table contain result column with some entries. I just
I'm curious to know if this is actually possible... In this simplified example, I
I have a table view which I want to bring up in edit mode
I have this code in my table view controller (and delegate): - (void)tableView:(UITableView *)tableView
I have a table view which has section indexes enabled. All the indexes are
Hi guys I have a table view but I don't want it to fill

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.