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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:03:15+00:00 2026-05-25T20:03:15+00:00

I have some sql which is grouping data to summarize it. Its basically a

  • 0

I have some sql which is grouping data to summarize it.
Its basically a list of locations, items and quantities.

SELECT ItemCode, SUM(Qty) AS Qty
FROM Putaway 
GROUP BY ItemCode

Which is working as expected, giving a summary of the results.

ItemCode - Qty   
AAAA - 1
BBBB - 2
CCCC - 3

But I have been asked to give a list of locations for each of the items as well.

So basically I want to Generate the results to be like:

ItemCode - Qty - Locations
AAAA     -  1  - A1
BBBB     -  2  - B1, B2
CCCC     -  3  - B5, B6, B7

If I do the obvious – and just Add [Location] to the SELECT and Group By
Then its giving me 2 rows for BBBB, 3 for CCCC etc. (Exactly as it should be, but not how I want it)

So, since there are multiple rows for each item in the table – how can I summarize it and combine the rows [Location]’s field into 1 field.

I am hoping for a simple SQL solution to avoid me having to write some sort of webpage to do it.

I hope this makes sense.

  • 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-25T20:03:16+00:00Added an answer on May 25, 2026 at 8:03 pm

    Provided that you’re using SQL Server 2005 or newer, you can use FOR XML to do this bit for you:

    create table Putaway (
        ItemCode    varchar(4),
        Qty         int,
        Location    varchar(3)
    )
    
    insert into Putaway
    values ('AAAA', 1, 'A1'),
        ('BBBB', 1, 'B1'),
        ('BBBB', 1, 'B2'),
        ('CCCC', 1, 'B5'),
        ('CCCC', 1, 'B6'),
        ('CCCC', 1, 'B7')
    
    SELECT ItemCode, SUM(Qty) AS Qty,
        (   SELECT x.Location + ', '
            FROM Putaway x
            WHERE p.ItemCode = x.ItemCode
            ORDER BY x.Location
            FOR XML PATH ('')
        ) as Locations
    FROM Putaway p
    GROUP BY ItemCode
    

    Unfortunately, this results in an extra comma at the end, but you can easily trim that out in your GUI or by making it a sub-select.

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

Sidebar

Related Questions

I have a Microsoft SQL server DB that imports some data which needs a
I have some sql scripts which use INSERT INTO secondtable (field, field1) SELECT field,
Hai Techies, I have some stored procedure which was written in SQL server.Now i
I have an SQL question which may be basic to some but is confusing
I I have a linq-to-sql database in which I want to log some of
I have a development vm which is running sql server as well as some
I have a PL/SQL function (running on Oracle 10g) in which I update some
I have some set of SQL queries which is in a file(i.e query.sql), and
We have C# entity classes which map to some of our SQL Server database
I have a datareader source firing some sql which takes longer than 30 seconds

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.