Solved

Need to group the result

Posted on 2012-04-12
2
433 Views
Last Modified: 2012-06-27
There are 2 tables one contains present details and other once contains older details.I need to group into one common title.

Right Now i am getting result like

MSSQLSERVER.Application.pgTIPSR.17063General.Alert      2
MSSQLSERVER.Application.pgTIPSR.Error50000Severity16State1.Alert      2
MSSQLServer.Application.SQLServer.Themirroringconnectionhastimedout.Alert      3--
MSSQLServer.Application.SQLServer.Themirroringconnectionhastimedout.Alert      4-- need to be combined 3&4 to one as title are same
MSSQLSERVER.TMFSQL.MSSQLSERVER.Alert      15
SQLServerAgent.Application.SQLServer.SQLServerAgent208.Alert      5
StorageAgents.Hardware.StorageAgentsSYSTEM.Alert      1
StorageAgents.Hardware.StorageAgentsSYSTEMWARNING.Alert      1


My expected out is

MSSQLSERVER.Application.pgTIPSR.17063General.Alert      2
MSSQLSERVER.Application.pgTIPSR.Error50000Severity16State1.Alert      2
MSSQLServer.Application.SQLServer.Themirroringconnectionhastimedout.Alert      7 ------------------ It's should have 2 entries for same title.It's needs to grouped.
MSSQLSERVER.TMFSQL.MSSQLSERVER.Alert      15
SQLServerAgent.Application.SQLServer.SQLServerAgent208.Alert      5
StorageAgents.Hardware.StorageAgentsSYSTEM.Alert      1
StorageAgents.Hardware.StorageAgentsSYSTEMWARNING.Alert      1


Query:

SELECT SUBSTRING (SUBSTRING (ticketdesc, PATINDEX ('%Event[0-9]%', ticketdesc), CHARINDEX (';  Desc:', ticketdesc) - PATINDEX ('%Event[0-9]%', ticketdesc)),
      CHARINDEX('.', SUBSTRING (ticketdesc, PATINDEX ('%Event[0-9]%', ticketdesc), CHARINDEX (';  Desc:', ticketdesc) - PATINDEX ('%Event[0-9]%', ticketdesc))) + 1, 1000),
      COUNT (*)
FROM CT
WHERE pid =111 and  ticketdesc like '%sql%' and createddtim >='2012-03-11' and PATINDEX ('%Event[0-9]%', ticketdesc) > 0
And CHARINDEX (';  Desc:', ticketdesc) > 0
Group by SUBSTRING (SUBSTRING (ticketdesc, PATINDEX ('%Event[0-9]%', ticketdesc), CHARINDEX (';  Desc:', ticketdesc) - PATINDEX ('%Event[0-9]%', ticketdesc)),
      CHARINDEX('.', SUBSTRING (ticketdesc, PATINDEX ('%Event[0-9]%', ticketdesc), CHARINDEX (';  Desc:', ticketdesc) - PATINDEX ('%Event[0-9]%', ticketdesc))) + 1, 1000)
     
      union
      SELECT SUBSTRING (SUBSTRING (ticketdesc, PATINDEX ('%Event[0-9]%', ticketdesc), CHARINDEX (';  Desc:', ticketdesc) - PATINDEX ('%Event[0-9]%', ticketdesc)),
      CHARINDEX('.', SUBSTRING (ticketdesc, PATINDEX ('%Event[0-9]%', ticketdesc), CHARINDEX (';  Desc:', ticketdesc) - PATINDEX ('%Event[0-9]%', ticketdesc))) + 1, 1000),
      COUNT (*)
FROM T
WHERE pid =111 and  ticketdesc like '%sql%' and createddtim >='2012-03-11' and PATINDEX ('%Event[0-9]%', ticketdesc) > 0
And CHARINDEX (';  Desc:', ticketdesc) > 0
Group by SUBSTRING (SUBSTRING (ticketdesc, PATINDEX ('%Event[0-9]%', ticketdesc), CHARINDEX (';  Desc:', ticketdesc) - PATINDEX ('%Event[0-9]%', ticketdesc)),
      CHARINDEX('.', SUBSTRING (ticketdesc, PATINDEX ('%Event[0-9]%', ticketdesc), CHARINDEX (';  Desc:', ticketdesc) - PATINDEX ('%Event[0-9]%', ticketdesc))) + 1, 1000)
0
Comment
[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
2 Comments
 
LVL 37

Accepted Solution

by:
momi_sabag earned 500 total points
ID: 37840588
try

select group_name, sum(cnt) from (
SELECT SUBSTRING (SUBSTRING (ticketdesc, PATINDEX ('%Event[0-9]%', ticketdesc), CHARINDEX (';  Desc:', ticketdesc) - PATINDEX ('%Event[0-9]%', ticketdesc)),
      CHARINDEX('.', SUBSTRING (ticketdesc, PATINDEX ('%Event[0-9]%', ticketdesc), CHARINDEX (';  Desc:', ticketdesc) - PATINDEX ('%Event[0-9]%', ticketdesc))) + 1, 1000) group_name,
      COUNT (*) cnt
FROM CT
WHERE pid =111 and  ticketdesc like '%sql%' and createddtim >='2012-03-11' and PATINDEX ('%Event[0-9]%', ticketdesc) > 0
And CHARINDEX (';  Desc:', ticketdesc) > 0
Group by SUBSTRING (SUBSTRING (ticketdesc, PATINDEX ('%Event[0-9]%', ticketdesc), CHARINDEX (';  Desc:', ticketdesc) - PATINDEX ('%Event[0-9]%', ticketdesc)),
      CHARINDEX('.', SUBSTRING (ticketdesc, PATINDEX ('%Event[0-9]%', ticketdesc), CHARINDEX (';  Desc:', ticketdesc) - PATINDEX ('%Event[0-9]%', ticketdesc))) + 1, 1000)
     
      union
      SELECT SUBSTRING (SUBSTRING (ticketdesc, PATINDEX ('%Event[0-9]%', ticketdesc), CHARINDEX (';  Desc:', ticketdesc) - PATINDEX ('%Event[0-9]%', ticketdesc)),
      CHARINDEX('.', SUBSTRING (ticketdesc, PATINDEX ('%Event[0-9]%', ticketdesc), CHARINDEX (';  Desc:', ticketdesc) - PATINDEX ('%Event[0-9]%', ticketdesc))) + 1, 1000),
      COUNT (*)
FROM T
WHERE pid =111 and  ticketdesc like '%sql%' and createddtim >='2012-03-11' and PATINDEX ('%Event[0-9]%', ticketdesc) > 0
And CHARINDEX (';  Desc:', ticketdesc) > 0
Group by SUBSTRING (SUBSTRING (ticketdesc, PATINDEX ('%Event[0-9]%', ticketdesc), CHARINDEX (';  Desc:', ticketdesc) - PATINDEX ('%Event[0-9]%', ticketdesc)),
      CHARINDEX('.', SUBSTRING (ticketdesc, PATINDEX ('%Event[0-9]%', ticketdesc), CHARINDEX (';  Desc:', ticketdesc) - PATINDEX ('%Event[0-9]%', ticketdesc))) + 1, 1000)
) t
group by group_name
0
 
LVL 5

Author Closing Comment

by:VIVEKANANDHAN_PERIASAMY
ID: 37840703
Awesome
0

Featured Post

Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
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
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

696 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