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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T21:01:22+00:00 2026-05-11T21:01:22+00:00

I have a need for a run once query (so performance isn’t that critical)

  • 0

I have a need for a “run once” query (so performance isn’t that critical) using tables similar to these (simplified versions of the actual):

CREATE TABLE #Items
(    ItemPK               int            not null  primary key
    ,ItemDescription      varchar(25)    not null
)

INSERT INTO #Items VALUES (1,'rock')
INSERT INTO #Items VALUES (2,'wood')
INSERT INTO #Items VALUES (3,'plastic')

CREATE TABLE #ItemTypes
(    ItemType             char(1)        not null   primary key
    ,ItemTypeDescription  varchar(25)    not null
)

INSERT INTO #ItemTypes VALUES ('A','Type A')
INSERT INTO #ItemTypes VALUES ('B','BBBBB')
INSERT INTO #ItemTypes VALUES ('C','Color')

CREATE TABLE #ItemInfo
(    InfoPK               int            not null   primary key   identity(1,1)
    ,ItemPK               int            not null   foreign key references #ListOfItems.ItemPK
    ,ItemType             char(1)        not null   foreign key references #ItemTypes.ItemType
    ,ItemValue            varchar(10)    not null
)

INSERT INTO #ItemInfo VALUES (1,'A','hard')
INSERT INTO #ItemInfo VALUES (1,'A','natural')
INSERT INTO #ItemInfo VALUES (1,'B','cold')
INSERT INTO #ItemInfo VALUES (1,'C','gray')
INSERT INTO #ItemInfo VALUES (2,'B','grain')
INSERT INTO #ItemInfo VALUES (2,'B','brown')
INSERT INTO #ItemInfo VALUES (3,'A','flexible')
INSERT INTO #ItemInfo VALUES (3,'A','colorful')
INSERT INTO #ItemInfo VALUES (3,'A','unnatural')
INSERT INTO #ItemInfo VALUES (3,'C','waterproof')

You’ll get warnings about FKs and temp tables; I just show them for clarity. You can remove the “#” to make permanent tables…

When I run this query:

SELECT
    i.ItemDescription
        ,t.ItemTypeDescription
        ,o.ItemValue
    FROM #Items                    i
        LEFT OUTER JOIN #ItemInfo  o ON i.ItemPK=o.ItemPk
        LEFT OUTER JOIN #ItemTypes t ON o.ItemType=t.ItemType

I get this output:

ItemDescription           ItemTypeDescription       ItemValue
------------------------- ------------------------- ----------
rock                      Type A                    hard
rock                      Type A                    natural
rock                      BBBBB                     cold
rock                      Color                     gray
wood                      BBBBB                     grain
wood                      BBBBB                     brown
plastic                   Type A                    flexible
plastic                   Type A                    colorful
plastic                   Type A                    unnatural
plastic                   Color                     waterproof

However, I need output like:

ItemDescription           CombinedDescription
------------------------- -----------------------------------------------
rock                      Type A: hard, natural; BBBBB: cold; Color: gray
wood                      BBBBB: grain, brown
plastic                   Type A: flexible, colorful, unnatural; Color: waterproof


(3 row(s) affected)

thanks…

  • 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-11T21:01:22+00:00Added an answer on May 11, 2026 at 9:01 pm

    Using XML trick, this should do it:

    SELECT      i.ItemDescription,
                (SELECT x.ItemTypeDescription + ': ' + x.vals + 
                    CASE WHEN (ROW_NUMBER() OVER (ORDER BY x.ItemType DESC) = 1) THEN '' ELSE '; ' END
                    FROM  ( SELECT  DISTINCT
                                    ii.ItemPK,
                                    t.ItemTypeDescription,
                                    ii.ItemType,
                            (SELECT         x.ItemValue + 
                                            CASE WHEN (ROW_NUMBER() OVER (ORDER BY x.ItemValue DESC) = 1) THEN '' ELSE ', ' END
                                FROM        #ItemInfo x
                                WHERE       x.ItemPK = ii.ItemPK
                                        AND x.ItemType = ii.ItemType
                                ORDER BY    x.ItemValue
                                FOR XML PATH('')
                            ) AS VALS
                    FROM    #ItemInfo ii
                    JOIN    #ItemTypes t
                        ON  ii.ItemType = t.ItemType
                    ) x
                    WHERE       x.ItemPK = i.ItemPK
                    ORDER BY    x.ItemType
                    FOR XML PATH('')
                ) AS CombinedDescription
    
    FROM        #Items i
    
    • 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 I run once a day that takes certain tables
I have a JavaScript method that I need to run on one of my
I have a program that I need to run under *nix and windows. because
I have a list/queue of 200 commands that I need to run in a
I have two scripts that often need to be run with the same parameter:
I have an application that when it first starts i need it to run
I need to create an interval Timer that is set to run once a
I have a need to run a relatively large number of virtual machines on
I have a need to run different code when a user clicks or double-clicks
I have a few batch files I need to run frequently in developing a

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.