Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Distinct rows based on one column, yet get the other columns

Status
Not open for further replies.

POSAPAY

IS-IT--Management
Jul 27, 2001
192
HU
I'm trying to display from a log table records that have unique counter fields and date, and often the same IP address creates multiple entries, but I'd only want one record per IP address, and have all of it sorted by the date created.

here is what I have now, but it is still not listing unique IPs only:
Code:
SELECT * FROM (SELECT DISTINCT UserIP FROM dbo.tblReklamLog) AS C1 JOIN dbo.tblReklamLog AS C2 ON C1.UserIP = C2.UserIP WHERE ReklamKod = 'AUW1GMZU' ORDER BY DateCreated Desc

what am I doing wrong?
 

You're bascailly doing the same as this:

Code:
SELECT DISTINCT * 
FROM dbo.tblReklamLog
WHERE ReklamKod = 'AUW1GMZU' 
ORDER BY DateCreated Desc

Distinct IP addresses will still return multiple rows since each row contains unique column data. Can you provide sample data and an expected output?




Mark

"You guys pair up in groups of three, then line up in a circle."
- Bill Peterson, a Florida State football coach
 
how about
Code:
SELECT DISTINCT UserIP 
FROM dbo.tblReklamLog
WHERE ReklamKod = 'AUW1GMZU' 
ORDER BY DateCreated Desc

or

Code:
SELECT UserIP ,Min(counter fields ),Max(DateCreated )
FROM dbo.tblReklamLog
WHERE ReklamKod = 'AUW1GMZU' 
Group by UserIP 
ORDER BY DateCreated Desc
 
MarkSweetland - thanks for your quick reply.

Sample data would be:
Table: tblReklamLog
Fields: ID, ReklamKod, UserIP, UserDomain, LinkSource, DateCreated
Data:
1, AUW1GMZU, 200.200.200.200, dsl123.somedomain.com, 12/1/2011 5:30:09 AM

2, AUW1GMZU, 200.200.200.200, dsl123.somedomain.com, 12/1/2011 5:32:49 AM

3, AUW1GMZU, 100.100.100.100, cable22.somedomain.com, 1/1/2012 1:17:11 AM

4, AUW1GMZU, 100.100.100.100, cable22.somedomain.com, 1/1/2012 1:20:11 AM

5, AUW1GMZU, 100.100.100.100, cable22.somedomain.com, 1/1/2012 1:25:11 AM

6, AUW1GMZU, 100.100.100.111, 1.2.otherdomain.com, 1/1/2012 2:59:08 AM


I would expect the following data to be returned:
1, AUW1GMZU, 200.200.200.200, dsl123.somedomain.com, 12/1/2011 5:30:09 AM

3, AUW1GMZU, 100.100.100.100, cable22.somedomain.com, 1/1/2012 1:17:11 AM

6, AUW1GMZU, 100.100.100.111, 1.2.otherdomain.com, 1/1/2012 2:59:08 AM

Basically one record returned per UserIP.
I tried using Group By, but I'm not very knowledgable on that.

Thanks for any and all help!
 

Since the driver of the query is the 'first' ID of all the rows that may contain the User's IP, you could use something like this:
Code:
SELECT * 
from tblReklamLog a
	join (
		select UserIP, minId=MIN(id)
		from tblReklamLog b
		group by UserIP
	) as b 
	on a.id = b.minId




Mark

"You guys pair up in groups of three, then line up in a circle."
- Bill Peterson, a Florida State football coach
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top