Solved

how to group items with a like clause  in MSSQL SP

Posted on 2016-10-10
3
55 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:
Scott Pletcher earned 500 total points
ID: 41837172
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 28

Expert Comment

by:Pawan Kumar
ID: 41837177
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
ID: 41837186
well done...
thanks for the quick response...   good technique to remember.
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

740 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