Solved

Show the detail data of what I have grouped in SQL2005

Posted on 2014-09-18
4
131 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
  • 2
4 Comments
 
LVL 48

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

This article will describe one method to parse a delimited string into a table of data.   Why would I do that you ask?  Let's say that you need to pass multiple parameters into a stored procedure to search for.  For our sake, we'll say that we wa…
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

758 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now