Solved

how to group items with a like clause  in MSSQL SP

Posted on 2016-10-10
3
26 Views
Last Modified: 2016-10-10
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
0
Comment
Question by:CASorter
3 Comments
 
LVL 69

Accepted Solution

by:
ScottPletcher earned 500 total points
Comment Utility
SELECT        COUNT(*) AS Expr1, evtDate, CASE WHEN Products.ProdExtId LIKE '%LOGO%' THEN 'LOGO' ELSE Products.ProdExtId END AS 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, CASE WHEN Products.ProdExtId LIKE '%LOGO%' THEN 'LOGO' ELSE Products.ProdExtId END
ORDER BY evtDate, CASE WHEN Products.ProdExtId LIKE '%LOGO%' THEN 'LOGO' ELSE Products.ProdExtId END
0
 
LVL 16

Expert Comment

by:Pawan Kumar Khowal
Comment Utility
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

0
 

Author Closing Comment

by:CASorter
Comment Utility
well done...
thanks for the quick response...   good technique to remember.
0

Featured Post

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Join & Write a Comment

Suggested Solutions

I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

762 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

11 Experts available now in Live!

Get 1:1 Help Now