Link to home
Start Free TrialLog in
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
ASKER CERTIFIED SOLUTION
Avatar of Scott Pletcher
Scott Pletcher
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

ASKER

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