Solved

Show the detail data of what I have grouped in SQL2005

Posted on 2014-09-18
4
133 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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

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…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

803 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