Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 condense duplicate data

Author  Topic 

squirrelsuccess
Starting Member

4 Posts

Posted - 2012-09-05 : 10:38:30
Hi All

How can I possibly condense some of this duplicate data?

05:54 2012-09-05 2012-09-05 05:54:25 testus 10.1.1.1 4624
06:02 2012-09-05 2012-09-05 06:02:25 testus 10.1.1.2 4624
07:26 2012-09-05 2012-09-05 07:26:26 testus 10.1.1.1 4624
07:38 2012-09-05 2012-09-05 07:38:26 testus 10.1.1.2 4624
08:35 2012-09-05 2012-09-05 08:35:00 testus 10.1.1.1 4624
08:35 2012-09-05 2012-09-05 08:35:02 testus 10.1.1.1 4624
08:35 2012-09-05 2012-09-05 08:35:22 testus 10.1.1.1 4624
08:35 2012-09-05 2012-09-05 08:35:23 testus 10.1.1.1 4624
08:35 2012-09-05 2012-09-05 08:35:27 testus 10.1.1.1 4624
08:35 2012-09-05 2012-09-05 08:35:28 testus 10.1.1.1 4624

I have the following SQL query:
quote:

SELECT
distinct
TimeGenerated,
EXTRACT_TOKEN(Strings,5,'|') AS User,
EXTRACT_TOKEN(Strings,18,'|') AS ClientAddress,
EventID
FROM \\MYSERVER\%SOURCE%
WHERE EventID='4624' AND TimeGenerated > '2012-09-04 23:59:00' and user not like '%$%' and user not like '%ANONYMOUS%' and user = 'testus'
--ORDER BY TimeGenerated ASC


I hope you can help me with this, because I am at the end of my tether.

Thanks
Jamie

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-09-05 : 10:46:07
One way is to use the row_number function. In the query below, what you need to change are indicated in RED. The partition by clause should have the columns that you want to consider as distinct. The order by clause should specify how to order it so that you will get the first one in that order by clause.
;WITH cte AS
(
SELECT *,
ROW_NUMBER() OVER (PARTITION BY col1,col2,col3 ORDER BY timegeneraged DESC) AS RN
FROM
YourTable
)
SELECT * FROM cte WHERE RN = 1;
Go to Top of Page

squirrelsuccess
Starting Member

4 Posts

Posted - 2012-09-05 : 11:31:32
quote:
Originally posted by sunitabeck

One way is to use the row_number function. In the query below, what you need to change are indicated in RED. The partition by clause should have the columns that you want to consider as distinct. The order by clause should specify how to order it so that you will get the first one in that order by clause.
;WITH cte AS
(
SELECT *,
ROW_NUMBER() OVER (PARTITION BY col1,col2,col3 ORDER BY timegeneraged DESC) AS RN
FROM
YourTable
)
SELECT * FROM cte WHERE RN = 1;




Brilliant. I will give this a go in the morning and provide you with some feedback.

Thank you so much.
Jamie
Go to Top of Page

squirrelsuccess
Starting Member

4 Posts

Posted - 2012-09-12 : 07:57:09
Thanks for the solution. It didn't work for me but thats because the function isn't available on SQL 2000. We are moving the DB to SQL 2008 soon anyway so I know it will work there. I did try the syntax with some sample data on a SQL 2005 server which we had lying around and the syntax worked fine.

Thanks for your assistance.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-09-12 : 11:00:44
quote:
Originally posted by squirrelsuccess

Thanks for the solution. It didn't work for me but thats because the function isn't available on SQL 2000. We are moving the DB to SQL 2008 soon anyway so I know it will work there. I did try the syntax with some sample data on a SQL 2005 server which we had lying around and the syntax worked fine.

Thanks for your assistance.


if you want to do this in sql 2000 use like


SELECT t.*
FROM table t
INNER JOIN (SELECT col1,col2,col3,MAX(datefield) AS latest
FROM table
GROUP BY col1,col2,col3
)t1
ON t.col1 = t1.col1
AND t.col2 = t1.col2
AND t.col3 = t1.col3
AND t.datefield = t1.latest


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -