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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

query help 1

Status
Not open for further replies.

MartDawg

Programmer
Jan 14, 2004
33
US
I have a table that looks like this...

SomeTable
userID(int)
somedate(datetime)
somethingelse(varchar)

There is no unique field currently. I want to make the first two columns together the primary key(userID, somedate).

When I do this I get an error for primary contraint.

I relize that there must me duplicate records with same userID and somedate.

How can I query this table to pull those dups out so I can change them to make my necessary change?

For some reason I'm having a brainfart on this. This is what I've tried...

Select TS.userID, TS.somedate from SomeTable as TS where (select * from SomeTable as TD where TS.userID = TD.userID and TS.somedate = TD.somedate)

This seems to bring back all records though.
 
Try this:

Select userID,somedate from Yourtablename group by userID having Count(userID)>1

-VJ
 
I get an error about having somedate not in the group by clause.
 
Try this:

Select userID from Yourtablename group by userID having Count(userID)>1


this will give all the userID's which occur more than once in your database


-VJ
 
I want a little bit more than that though. I want the userid's that occur more than once, only if they have the same datetime?
 
Try this:

Select t1.userID from Yourtablename t1
Inner join yourtablename t2
on
t2.userid=t1.userid
Where t2.userid IN (SELECT t2.userID FROM yourtablename WHERE t2.somedate=t1.somedate group by t2.userID having Count(userID)>1 )


-VJ
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top