SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Help with Distinct on 2 columns
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

dimepop
Starting Member

30 Posts

Posted - 08/10/2012 :  06:55:27  Show Profile  Reply with Quote
Hi, i have the following query which works fine:

SELECT TOP (10) UPPER(displayName) AS Devices, SUBSTRING(message, LEN('to') + 28, CHARINDEX(' failures ', message) - LEN('to') - 19) AS Pings
FROM eventlogevents
WHERE (type = 2)


But when i try to Add Distinct so it only shows distinct devices, the query Runs for a long time and usually times out.

Is there a way to select Distinct Devices in better way?
Thanks

NickC
Yak Posting Veteran

United Kingdom
68 Posts

Posted - 08/10/2012 :  07:00:53  Show Profile  Reply with Quote
Temp table? then try it ?

Select Distinct * from #temp table~?

Go to Top of Page

sunitabeck
Flowing Fount of Yak Knowledge

5152 Posts

Posted - 08/10/2012 :  07:05:17  Show Profile  Reply with Quote
Distinct would select multiple rows for a given displayname if the second column is different. In other words, distinctness test applies to every column in the select list.

You can try the following - not sure if that will be any more efficient though:
SELECT 
	 UPPER(displayName) AS Devices,
	 MAX(
	 	       SUBSTRING(
           MESSAGE,
           LEN('to') + 28,
           CHARINDEX(' failures ', MESSAGE) - LEN('to') - 19
       )) AS Pings
FROM   eventlogevents
WHERE  (TYPE = 2)
GROUP BY UPPER(displayName);
Go to Top of Page

dimepop
Starting Member

30 Posts

Posted - 08/10/2012 :  07:35:49  Show Profile  Reply with Quote
Hi, thanks for the help, unfortunately your query errors with the message:
"Msg 537, Level 16, State 6, Line 1
Invalid length parameter passed to the LEFT or SUBSTRING function"
Regards

quote:
Originally posted by sunitabeck

Distinct would select multiple rows for a given displayname if the second column is different. In other words, distinctness test applies to every column in the select list.

You can try the following - not sure if that will be any more efficient though:
SELECT 
	 UPPER(displayName) AS Devices,
	 MAX(
	 	       SUBSTRING(
           MESSAGE,
           LEN('to') + 28,
           CHARINDEX(' failures ', MESSAGE) - LEN('to') - 19
       )) AS Pings
FROM   eventlogevents
WHERE  (TYPE = 2)
GROUP BY UPPER(displayName);


Go to Top of Page

sunitabeck
Flowing Fount of Yak Knowledge

5152 Posts

Posted - 08/10/2012 :  07:58:37  Show Profile  Reply with Quote
quote:
Originally posted by dimepop

Hi, thanks for the help, unfortunately your query errors with the message:
"Msg 537, Level 16, State 6, Line 1
Invalid length parameter passed to the LEFT or SUBSTRING function"
Regards

quote:
Originally posted by sunitabeck

Distinct would select multiple rows for a given displayname if the second column is different. In other words, distinctness test applies to every column in the select list.

You can try the following - not sure if that will be any more efficient though:
SELECT 
	 UPPER(displayName) AS Devices,
	 MAX(
	 	       SUBSTRING(
           MESSAGE,
           LEN('to') + 28,
           CHARINDEX(' failures ', MESSAGE) - LEN('to') - 19
       )) AS Pings
FROM   eventlogevents
WHERE  (TYPE = 2)
GROUP BY UPPER(displayName);




This error is happening because of the CHARINDEX function in the second column. My guess is that there are rows where MESSAGE does not have the word ' failures ' and so one of the parameters to the SUBSTRING function ends up being negative:
SUBSTRING(message, LEN('to') + 28, CHARINDEX(' failures ', message) - LEN('to') - 19)
I did not quite follow what you are trying to do there - can you explain with some examples?

Edited by - sunitabeck on 08/10/2012 07:59:24
Go to Top of Page

dimepop
Starting Member

30 Posts

Posted - 08/10/2012 :  09:03:07  Show Profile  Reply with Quote

Hi, i think you are right and some rows don't have the word 'failures'
I have added more conditions to make sure it contains the word failure and seems fine.
I have to do more tests but your query seems to work anyway.
Thanks
quote:
Originally posted by sunitabeck

quote:
Originally posted by dimepop

Hi, thanks for the help, unfortunately your query errors with the message:
"Msg 537, Level 16, State 6, Line 1
Invalid length parameter passed to the LEFT or SUBSTRING function"
Regards

quote:
Originally posted by sunitabeck

Distinct would select multiple rows for a given displayname if the second column is different. In other words, distinctness test applies to every column in the select list.

You can try the following - not sure if that will be any more efficient though:
SELECT 
	 UPPER(displayName) AS Devices,
	 MAX(
	 	       SUBSTRING(
           MESSAGE,
           LEN('to') + 28,
           CHARINDEX(' failures ', MESSAGE) - LEN('to') - 19
       )) AS Pings
FROM   eventlogevents
WHERE  (TYPE = 2)
GROUP BY UPPER(displayName);




This error is happening because of the CHARINDEX function in the second column. My guess is that there are rows where MESSAGE does not have the word ' failures ' and so one of the parameters to the SUBSTRING function ends up being negative:
SUBSTRING(message, LEN('to') + 28, CHARINDEX(' failures ', message) - LEN('to') - 19)
I did not quite follow what you are trying to do there - can you explain with some examples?

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.08 seconds. Powered By: Snitz Forums 2000