Solved

SQL - How to get Count, Group by based on Like Message

Posted on 2016-07-31
7
45 Views
Last Modified: 2016-08-28
Below query will execute a Table of records with Error, Error Types and its count.

I have to group by further based on Like messages in the  ErrorType Column.

How should i do that.

create table #t 
(
    BillId int,
    StepName varchar(100),
    StepExec varchar(100),
    StepExecResult varchar(100),
    Created_date datetime
)

insert into #t 
values
    (1, 'Initiated', 'Taken Place','Pass', getdate()-10),
    (1, 'POS', 'Deadlock Error','Error', getdate()-9),
    (1, 'POS', 'Processed','Pass', getdate()-9),
    (1, 'Merchandise', 'Taken Place','Pass', getdate()-8),
    (1, 'verification', 'Webservice call error','Error', getdate()-7),
    (1, 'verification', 'Webservice call error','Error', getdate()-6),
    (1, 'verification', 'Webservice call','Pass', getdate()-5),
    (1, 'verification', 'Webservice Response','Error', getdate()-5),
    (1, 'verification', 'Webservice Response','Pass', getdate()-5),
    (1, 'verification', 'Timeout Error','Error', getdate()-5),
    (1, 'verification', 'Timeout Error','Error', getdate()-5),
    (1, 'verification', 'Timeout Error','Error', getdate()-5),
    (1, 'verification', '','Pass', getdate()-5),
    (1, 'Payment', 'calculationError','Error', getdate()-4),
    (1, 'Payment', 'calculationProcessed','Pass', getdate()-3),
    (1, 'Completed', 'Archived','Pass', getdate()-1),
    (2, 'Initiated', 'Taken Place','Pass', getdate()-10),
    (2, 'POS', 'Deadlock Error','Error', getdate()-9),
    (2, 'POS', 'Processed','Pass', getdate()-9),
    (2, 'Merchandise', 'Taken Place','Pass', getdate()-8),
    (2, 'verification', 'Webservice call error on 07/23/2016','Error', getdate()-7),
    (2, 'verification', 'Webservice call error on 07/24/2016','Error', getdate()-6),
    (2, 'verification', 'Webservice call','Pass', getdate()-5),
    (2, 'verification', 'Webservice Response','Error', getdate()-5),
    (2, 'verification', 'Webservice Response','Pass', getdate()-5),
    (2, 'verification', '','Pass', getdate()-5),
    (2, 'Payment', 'calculationProcessed','Pass', getdate()-3),
    (2, 'Completed', 'Archived','Pass', getdate()-1)

select 
    stepname, count(*) as ErrorCount 
from 
    #t
where 
    StepExecResult = 'Error'
group by 
    stepname


	SELECT  StepName ,
        SUM(COUNT(DISTINCT BillId)) OVER (PARTITION BY StepName) TotalStepError,
        StepExec ,
        COUNT(DISTINCT BillId) ErrorTypeCount
FROM    #t
WHERE   StepExecResult = 'Error'
GROUP BY StepName,
        StepExec


StepName	TotalStepError	StepExec			ErrorTypeCount
----------------------------------------------------------------
Payment			1			calculationError		1
POS				2			Deadlock Error			2
verification	6			Timeout Error			1
verification	6			Webservice call error	3
verification	6			Webservice Response		2

Open in new window

0
Comment
Question by:chokka
  • 3
  • 2
  • 2
7 Comments
 
LVL 48

Expert Comment

by:PortletPaul
ID: 41736788
"conditional aggregates" use a case expression INSIDE the aggregation function

COUNT( case when ErrorType like 'webservice%' then ErrorType end )

note: count only increments for a non-null value so don't count and provided a non-null else condition
e.g. alternatives are:

COUNT( case when ErrorType like 'webservice%' then 1 else NULL end )

SUM( case when ErrorType like 'webservice%' then 1 else  0 end )

Personally I prefer to use COUNT() instead of SUM() for this.
0
 

Author Comment

by:chokka
ID: 41736797
Thanks Paul. But Error Type like .. will not be always Webservice. In my production data, I have around 100 different types of error. Almost 70% of errors have date time on it. So when i run the count, group by query .. it distinct the error based on date time also.
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 41736803
I did not expect one example to cover all your needs.

What is "the expected result" from you sample data?

If you don't want rows by date/time use max(datetimecolumn)   or min() depending on what you want.

Or perhaps
 group by cast(datetimecol as date)
This would give rows by date only without time of day
0
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 

Author Comment

by:chokka
ID: 41736887
I think , It has to be some thing like this. I have created a Temp table with Like Type messages.



create table #t
(Stepname varchar(100),
TotalStepError int,
StepExec nvarchar(1000),
ErrorTypeCount int
)

insert into #t values
('Payment',1,'calculationError',1),
('POS',	2,	'Deadlock Error on 07/21/2016',	1),
('POS',	2,	'Deadlock Error on 07/22/2016',	1),
('verification',	6,	'Timeout Error',	1),
('verification',	6,	'Webservice call error',	1),
('verification',	6,	'Webservice call error on 07/23/2016',	1),
('verification',	6,	'Webservice call error on 07/24/2016',	1),
('verification',	6,	'Webservice Response',	2)


select * from #t



-- This is based on Like Type 

Create Table #ErrorLikeType
(
	TypeLike varchar(1000)
)

Insert into #ErrorLikeType values
('Webservice call error'),
('Timeout error')

select * from #ErrorLikeType

--Expected Result  - I have to write some kind of relation between my Temp Table and Like type table.

Stepname		TotalStepError	StepExec						ErrorTypeCount
---------------------------------------------------------------------------------
Payment				1			calculationError					1
POS					2			Deadlock Error						2
verification		6			Timeout Error						1
verification		6			Webservice call error				3
verification		6			Webservice Response					2

Open in new window

0
 
LVL 24

Expert Comment

by:chaau
ID: 41736910
Why did you change the structure of your #t table?
I have originally made this query, but now it does not work with your new #t structure. Anyway, here is the query:
	SELECT  StepName ,
        SUM(COUNT(DISTINCT BillId)) OVER (PARTITION BY StepName) TotalStepError,
        e.TypeLike ,
        COUNT(DISTINCT BillId) ErrorTypeCount
FROM    #t t left join #ErrorLikeType e on t.StepExec like e.TypeLike + '%'
WHERE   StepExecResult = 'Error'
GROUP BY StepName,
        e.TypeLike

Open in new window

0
 

Author Comment

by:chokka
ID: 41736912
Thank you Chaau. I haven't changed the original structure. I just added a new table as ErrorTypeLike.

I thought, that will simplify the issue.
0
 
LVL 24

Accepted Solution

by:
chaau earned 500 total points
ID: 41736915
No, you have:
Original table:
create table #t 
(
    BillId int,
    StepName varchar(100),
    StepExec varchar(100),
    StepExecResult varchar(100),
    Created_date datetime
)

Open in new window


New table:
create table #t
(Stepname varchar(100),
TotalStepError int,
StepExec nvarchar(1000),
ErrorTypeCount int
)

Open in new window

But anyway, you'll figure out how to adjust the query
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
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, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

828 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