Solved

Show the detail data of what I have grouped in SQL2005

Posted on 2014-09-18
4
136 Views
Last Modified: 2014-09-18
I am selecting a number of rows from SQL database within a Crystal report using an SQL command
I have a product field - 20 long  

I want to group my report by the first 4 characters of the Product - I do this by
CAST (rtrim(LTRIM(substring(product_ID,1,4))) AS char) as item_grouping,

This then reports all records that have the same first 4 characters.

In a seperate field I would like to see all the products that I have selected by there full product ID


example

File of data is like the following
ProductID, QTy
B3AA001            10
B3AA001            12
B3AA001            4
B3AA002            6
B3AA003            5
B3AB001            3
B3AB002            5
B3AB003            7
B3AB004            6



Report should look like
B3AA      37    (B3AA001, B3AA002 , B3AA003)

B3AB      21        (B3AB001, B3AB002 , B3AB003, B3AB004)
0
Comment
Question by:MECR123
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 49

Expert Comment

by:PortletPaul
ID: 40330057
you want this in TSQL (SQL Server) or to be done in Crystal?

if TSQL please provide the existing query.
0
 
LVL 8

Accepted Solution

by:
Ganapathi earned 500 total points
ID: 40330073
Try this.
Select SUBSTRING(ProductID,1,4) as Product,Count(1) as TotalQuantity
STUFF((SELECT ',' + ProductID FROM mytable WHERE (
ProductID=Result.ProductID) FOR XML PATH ('')),1,1,'') AS list
From mytable AS ProductIDAll
GROUP BY SUBSTRING(ProductID,1,4)

Open in new window

0
 

Author Comment

by:MECR123
ID: 40330260
hi Ganapathi

thanks

I think this will do the job
the only bit I can't understand is ProductID=Result.ProductID
in the sub Select for each record read I only want to select records that meet my substring criteria
0
 

Author Closing Comment

by:MECR123
ID: 40330301
excellent answer
0

Featured Post

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've encountered valid database schemas that do not have a primary key.  For example, I use LogParser from Microsoft to push IIS logs into a SQL database table for processing and analysis.  However, occasionally due to user error or a scheduled task…
INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

729 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question