Solved

how to group items with a like clause  in MSSQL SP

Posted on 2016-10-10
3
56 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
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.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

734 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