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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T21:03:37+00:00 2026-06-10T21:03:37+00:00

I have a query that returns items like this Item — Code — Thing

  • 0

I have a query that returns items like this

Item    --  Code   --  Thing
------------------------------
Item A  --  Code A --  Thing 1
Item A  --  Code A --  Thing 2
Item A  --  Code A --  Thing 3
Item A  --  Code A --  Thing 4
Item B  --  Code B --  Thing x
Item B  --  Code B --  Thing y
Item C  --  Code C --  Thing z
Item C  --  Code C --  Thing a
Item C  --  Code C --  Thing b
Item C  --  Code C --  Thing c

And I want to turn this into something like this

Item    --  Code   --  Thing 1 -- Thing 2 -- Thing 3 -- Thing 4 -- Thing 5
---------------------------------------------------------------------------
Item A  --  Code A --  Thing 1 -- Thing 2 -- Thing 3 -- Thing 4 -- NULL
Item B  --  Code B --  Thing x -- Thing y -- NULL    -- NULL    -- NULL
Item C  --  Code C --  Thing a -- Thing b -- Thing c -- Thing d -- NULL

Where any item over 5 can be ignored.


Update:

By adding
“ROW_NUMBER() over (Partition by Table.Id order by Table2.Id)” In my query I now get:

Item    --  Code   --  Thing  -- Index
---------------------------------------
Item A  --  Code A --  Thing 1 -- 1
Item A  --  Code A --  Thing 2 -- 2
Item A  --  Code A --  Thing 3 -- 3
Item A  --  Code A --  Thing 4 -- 4
Item B  --  Code B --  Thing x -- 1
Item B  --  Code B --  Thing y -- 2
Item C  --  Code C --  Thing z -- 1
Item C  --  Code C --  Thing a -- 2
Item C  --  Code C --  Thing b -- 3
Item C  --  Code C --  Thing c -- 4

Which allows me to use the Pivot function and change the data accordingly.
Still working on that so any help is very appreciated.

  • 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-10T21:03:38+00:00Added an answer on June 10, 2026 at 9:03 pm

    (First try that didn’t work)
    One clean way to code this is:

        SELECT
            Item,
            Code,
            Thing1 = Case When Thing = 'Thing 1' Then 'Thing 1' Else Null End,
            Thing2 = Case When Thing = 'Thing 2' Then 'Thing 2' Else Null End,
            Thing3 = Case When Thing = 'Thing 3' Then 'Thing 3' Else Null End,
            Thing4 = Case When Thing = 'Thing 4' Then 'Thing 4' Else Null End,
            Thing5 = Case When Thing = 'Thing 5' Then 'Thing 5' Else Null End
       FROM [Items]
       WHERE Thing BETWEEN 'Thing 1' AND 'Thing 5' -- preselection should improve performance
       GROUP BY 
            Item,
            Code,
            Thing1 = Case When Thing = 'Thing 1' Then 'Thing 1' Else Null End,
            Thing2 = Case When Thing = 'Thing 2' Then 'Thing 2' Else Null End,
            Thing3 = Case When Thing = 'Thing 3' Then 'Thing 3' Else Null End,
            Thing4 = Case When Thing = 'Thing 4' Then 'Thing 4' Else Null End,
            Thing5 = Case When Thing = 'Thing 5' Then 'Thing 5' Else Null End
    

    Works because you have a known, limited number of columns to pivot.
    The PIVOT function is another way but I am not familiar with this one.

    (Second try) This works!

        DECLARE @Items TABLE
        (
            Item    char(1),
            Code    char(1),
            Thing   char(1)
        )
        INSERT INTO @Items
        SELECT 'A', 'A', '1'
        UNION
        SELECT 'A', 'A', '2'
        UNION
        SELECT 'A', 'A', '3'
        UNION
        SELECT 'A', 'A', '4'
        UNION
        SELECT 'B', 'B', 'x'
        UNION
        SELECT 'B', 'B', 'y'
        UNION
        SELECT 'C', 'C', 'f'
        UNION
        SELECT 'C', 'C', 'g'
        UNION
        SELECT 'C', 'C', 'h'
        UNION 
        SELECT 'C', 'C', 'j'
    
        SELECT
            Items.Item,
            Items.Code,
            Thing1 = Max(Case When OrderedItems.ThingPlace = 1 Then OrderedItems.Thing Else Null End),
            Thing2 = Max(Case When OrderedItems.ThingPlace = 2 Then OrderedItems.Thing Else Null End),
            Thing3 = Max(Case When OrderedItems.ThingPlace = 3 Then OrderedItems.Thing Else Null End),
            Thing4 = Max(Case When OrderedItems.ThingPlace = 4 Then OrderedItems.Thing Else Null End),
            Thing5 = Max(Case When OrderedItems.ThingPlace = 5 Then OrderedItems.Thing Else Null End)
        FROM @Items Items
            LEFT OUTER JOIN 
            (
                SELECT
                    Code, Thing, 
                    ThingPlace = Row_Number() OVER (PARTITION BY Code ORDER BY Thing)
                FROM @Items
                GROUP BY Code, Thing
            ) OrderedItems
                ON OrderedItems.Code = Items.Code
        WHERE OrderedItems.ThingPlace Is Null OR OrderedItems.ThingPlace <= 5
        GROUP BY
            Items.Item,
            Items.Code
    

    Result:

    A A 1 2 3 4 NULL

    B B x y NULL NULL NULL

    C C f g h j NULL

    The trick was to build an ordered list of things first. I used only Code for the lookup as this seems in this case with the data given to be the key (Item didn’t really matter). You might have to extend the join connection.
    Using the ordered list I then exclude all Thing(s) that are above the sixth place in order. This only works if Thing(s) is actually a natural order – otherwise it’s back to square one to find out first what Thing(s) is.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a query that returns a list of items. What I would like
I have this query that returns the results by ordering it by focus.name ASC.
i have a query that returns rows that i want, e.g. QuestionID QuestionTitle UpVotes
I have a query that returns one row. However, I want to invert the
I have a query that returns a lot of data into a CSV file.
I have a query that returns data with group category and some details like
I have a query that returns list of results. I want a single random
I have a fairly complicated linq query that returns an item and a string[]
I have a model that looks like this: class Item(models.Model): ... publish_date = models.DateTimeField(default=datetime.datetime.now)
I have a query that returns me around 6 million rows, which is too

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.