Avatar of CASorter
CASorterFlag for United States of America

asked on 

how to group items with a like clause in MSSQL SP

i have this query result

2      08/29/2016      4X6NOGLOGO528100251
4      08/29/2016      4X8LOGO785329999
1      08/29/2016      5X10LOGO787879999
1      08/29/2016      5X7LOGO5119899LRG
2      08/29/2016      6X6LOGO78279ELK
2      08/29/2016      6X7LOGO50669CORDELL
50      08/29/2016      MG310B
2      08/29/2016      MG310BK
16      08/29/2016      MG310BM
41      08/29/2016      MG310RB


this is what i am trying to get

12      08/29/2016      LOGO
50      08/29/2016      MG310B
2      08/29/2016      MG310BK
16      08/29/2016      MG310BM
41      08/29/2016      MG310RB

here is the query that produces the top part


SELECT        COUNT(*) AS Expr1, evtDate, Products.ProdExtId
FROM            Events INNER JOIN
                         Items ON Events.EvtItemId = Items.ItmIntId INNER JOIN
                         EventTypes ON Events.EvtTypeId = EventTypes.EvtTypeIntId INNER JOIN
                         TouchPoints ON Events.TouchPointId = TouchPoints.TPtIntId AND EventTypes.EvtTypeIntId = TouchPoints.TPtEvtTypeId INNER JOIN
                         Products ON Items.ItmProdId = Products.ProdIntId
WHERE        (Events.EvtTime > @Param3) AND (Events.EvtTime < @Param4) AND (TouchPoints.TPtExtId <> 'plant' AND TouchPoints.TPtExtId <> 'storage')
GROUP BY evtDate, Products.ProdExtId
ORDER BY evtDate, Products.ProdExtId

Open in new window


i am looking for something that will group all the descriptions that are LIKE %LOGO% together in one line
Microsoft SQL Server

Avatar of undefined
Last Comment
CASorter
ASKER CERTIFIED SOLUTION
Avatar of Scott Pletcher
Scott Pletcher
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Pawan Kumar
Pawan Kumar
Flag of India image

Try this. Comma separated descriptions

DECLARE @Val AS VARCHAR(4) = 'LOGO'

SELECT * ,     STUFF 
                ((
                SELECT CONCAT(', ' + description)
                FROM yourtable a
                WHERE ( a.ProdExtId = t.ProdExtId  AND a.evtDate = t.evtDate)
					  AND CHARINDEX(@Val,description,0) > 0
                FOR XML PATH('')
                ) ,1,2,'') 
                AS Logos
FROM 
(
	SELECT        COUNT(*) AS Expr1, evtDate, Products.ProdExtId
	FROM            Events INNER JOIN
							 Items ON Events.EvtItemId = Items.ItmIntId INNER JOIN
							 EventTypes ON Events.EvtTypeId = EventTypes.EvtTypeIntId INNER JOIN
							 TouchPoints ON Events.TouchPointId = TouchPoints.TPtIntId AND EventTypes.EvtTypeIntId = TouchPoints.TPtEvtTypeId INNER JOIN
							 Products ON Items.ItmProdId = Products.ProdIntId
	WHERE        (Events.EvtTime > @Param3) AND (Events.EvtTime < @Param4) AND (TouchPoints.TPtExtId <> 'plant' AND TouchPoints.TPtExtId <> 'storage') 			 
	GROUP BY evtDate, Products.ProdExtId
)t
ORDER BY t.evtDate, tProdExtId

Open in new window

Avatar of CASorter
CASorter
Flag of United States of America image

ASKER

well done...
thanks for the quick response...   good technique to remember.
Microsoft SQL Server
Microsoft SQL Server

Microsoft SQL Server is a suite of relational database management system (RDBMS) products providing multi-user database access functionality.SQL Server is available in multiple versions, typically identified by release year, and versions are subdivided into editions to distinguish between product functionality. Component services include integration (SSIS), reporting (SSRS), analysis (SSAS), data quality, master data, T-SQL and performance tuning.

171K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo