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
 Query help required please

Author  Topic 

theroversreturn
Starting Member

12 Posts

Posted - 2011-11-01 : 06:33:51
Hi,
I have a table which has record_ids and versions. I also have a field in the table called user_id. For all versions of the same record_id the user_id should be the same. Unfortunatley there was a little issue whereby the user_id was changed in some records and I need to find these records. The set up would be like below:-

Table
====
Record_id Version User_id
234443 1 7
234443 2 7
234444 1 12
234445 1 23
234445 2 23
234445 3 100
In the above example record_id 234445 would be what I'd like my query to return.

Thanks in advance

nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2011-11-01 : 06:37:45
select Record_id, count(distinct user_id), max(user_id), min(user(id), count(*)
from tbl
group by Record_id
having max(user_id) <> min(user_id)

==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-11-01 : 06:38:36
how do you determine which should be correct userid? do you have a master table for this user_id values associated to each record_id? what if you've two records one each with 23 and 100 for 234445. which one will you return in that case?

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

Go to Top of Page

theroversreturn
Starting Member

12 Posts

Posted - 2011-11-01 : 06:42:02
Perfect that you very much
Go to Top of Page

nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2011-11-01 : 06:42:18
The op said the record id was wanted not the user_id - look at my query.

==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

theroversreturn
Starting Member

12 Posts

Posted - 2011-11-01 : 06:44:14
All I wanted returned was the record_id of those records which had a variance in user_id which was exactly as Nigels query returned.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-11-01 : 07:17:54
quote:
Originally posted by theroversreturn

All I wanted returned was the record_id of those records which had a variance in user_id which was exactly as Nigels query returned.


oh ok.. I understood

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

Go to Top of Page
   

- Advertisement -