473,804 Members | 3,018 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

SQL unique combinations?

Hi all, I'm not sure how to get around this, I was hoping someone
could provide me with some help. Let's take this simple data set of 2
columns in a table:

Tom - id1
Bob - id2
Jane - id1
John - id8
Fred - id2
John - id8
Bob - id1

Using SQL, I'm trying to return the ones that are *not* unique. I
don't really know if Tom is actually id1 or if Jane is. Since they
both share an ID, I want to return them (as the errors!). Since Bob
has 2 different IDs and ID2 can also belong to Fred, I want to return
all of those. (etc). The only one not coming back should be
John-id8, because he's the one with the unique ID (even if he's listed
twice).

These names and IDs are two different columns in the same table, so no
inter-table relationships involved here!

Can anyone possibly offer assistance? Much thanks!

T-McK
Nov 13 '05 #1
3 8027
"Thomas McK" <ed********@yah oo.com> wrote in message
news:85******** *************** **@posting.goog le.com...
Hi all, I'm not sure how to get around this, I was hoping someone
could provide me with some help. Let's take this simple data set of 2
columns in a table:

Tom - id1
Bob - id2
Jane - id1
John - id8
Fred - id2
John - id8
Bob - id1

Using SQL, I'm trying to return the ones that are *not* unique. I
don't really know if Tom is actually id1 or if Jane is. Since they
both share an ID, I want to return them (as the errors!). Since Bob
has 2 different IDs and ID2 can also belong to Fred, I want to return
all of those. (etc). The only one not coming back should be
John-id8, because he's the one with the unique ID (even if he's listed
twice).

These names and IDs are two different columns in the same table, so no
inter-table relationships involved here!

Can anyone possibly offer assistance? Much thanks!

T-McK

We don't have your table and field names, but you should be able to adjust
the following accordingly:

SELECT ConID, ConName FROM tblContacts WHERE ConID IN
(SELECT X.ConID FROM
(SELECT ConID FROM tblContacts GROUP BY ConID, ConName) AS X
GROUP BY X.ConID HAVING Count(*)>1)
ORDER BY ConID, ConName

Nov 13 '05 #2
On 28 Sep 2004 12:39:01 -0700, ed********@yaho o.com (Thomas McK)
wrote:
Hi all, I'm not sure how to get around this, I was hoping someone
could provide me with some help. Let's take this simple data set of 2
columns in a table:

Tom - id1
Bob - id2
Jane - id1
John - id8
Fred - id2
John - id8
Bob - id1

Using SQL, I'm trying to return the ones that are *not* unique. I
don't really know if Tom is actually id1 or if Jane is. Since they
both share an ID, I want to return them (as the errors!). Since Bob
has 2 different IDs and ID2 can also belong to Fred, I want to return
all of those. (etc). The only one not coming back should be
John-id8, because he's the one with the unique ID (even if he's listed
twice).

These names and IDs are two different columns in the same table, so no
inter-table relationships involved here!

Can anyone possibly offer assistance? Much thanks!

T-McK


Hi
Here is a brute-force method, I am sure someone has a simple and or
elegant one!

query2 = SELECT DISTINCT Table2.name, Table2.id FROM Table2;

singleids =
SELECT id FROM Query2 GROUP BY id HAVING Count(Query2.na me)=1;

singlenames =
SELECT name FROM Query2 GROUP BY name HAVING Count(Query2.id )=1;

unique =
SELECT DISTINCT Table2.*
FROM (Table2 INNER JOIN singlenames ON Table2.name = singlenames.nam e)
INNER JOIN singleids ON Table2.id = singleids.id;

David
Nov 13 '05 #3
That was very, very helpful! Both of you presented different ways to
do it, and certainly opened my eyes to new solutions. Thanks!!


d.************* **@blueyonder.c o.uk (David Schofield) wrote in message news:<4159e768. 439640759@local host>...
On 28 Sep 2004 12:39:01 -0700, ed********@yaho o.com (Thomas McK)
wrote:
Hi all, I'm not sure how to get around this, I was hoping someone
could provide me with some help. Let's take this simple data set of 2
columns in a table:

Tom - id1
Bob - id2
Jane - id1
John - id8
Fred - id2
John - id8
Bob - id1

Using SQL, I'm trying to return the ones that are *not* unique. I
don't really know if Tom is actually id1 or if Jane is. Since they
both share an ID, I want to return them (as the errors!). Since Bob
has 2 different IDs and ID2 can also belong to Fred, I want to return
all of those. (etc). The only one not coming back should be
John-id8, because he's the one with the unique ID (even if he's listed
twice).

These names and IDs are two different columns in the same table, so no
inter-table relationships involved here!

Can anyone possibly offer assistance? Much thanks!

T-McK


Hi
Here is a brute-force method, I am sure someone has a simple and or
elegant one!

query2 = SELECT DISTINCT Table2.name, Table2.id FROM Table2;

singleids =
SELECT id FROM Query2 GROUP BY id HAVING Count(Query2.na me)=1;

singlenames =
SELECT name FROM Query2 GROUP BY name HAVING Count(Query2.id )=1;

unique =
SELECT DISTINCT Table2.*
FROM (Table2 INNER JOIN singlenames ON Table2.name = singlenames.nam e)
INNER JOIN singleids ON Table2.id = singleids.id;

David

Nov 13 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2703
by: Ulf Heyder | last post by:
Hello everyone, I want to add a unique constraint to a XSD I created. After I modified validation (XMLSpy, Castor-0.9.5 marshaller) of my example (see below) against the XSD (also see below) doesn't lead to any errors/warnings. I tried several different combinations for the <xs:selector> and <xs:field>.
3
2511
by: Nel | last post by:
From your (group) opinion, when sending a unique URL to a user, what steps are a must in making sure the link can't be hacked. i.e. Bad link www.example.com?id=10&action=reset_password would be better as www.example.com?id=505B6EF41388913908D9B65B35DEAAEE&action=reset_password
2
3112
by: john | last post by:
I have a table with 5 fields. In a query I would like to make a count for every unique combination of field 2,3, and 4. I fiddled with the count and dcount option, but can't get it to work for more than one field. Next question is if it's possible in such a query to show only those unique combinations that have a count higher than 5. Thanks, john
9
18144
by: Robert Mago | last post by:
Is there a way to create a 10 characthers or less, alph-numeric string which is unique. I can't use the guid since its longer then 10 characthers. Also i cannot use a random number, since being random does not mean that its unique.
2
4453
by: srusskinyon | last post by:
I need some help getting unique records from our database! I work for a small non-profit homeless shelter. We keep track of guest information as well as what services we have offered for statistical purposes. I've been using Here's the situation: I have two main tables:
0
1149
by: DesperateStdnt | last post by:
Basically, the program needs to take a data set of X (i.e. 80) students and assign them them into Y (i.e. 10) groups. Then assign Y (i.e. 10) number of faculty members, one to each group. But, students can’t be in a group with anyone they have been in a group with before or have a faculty member they have had before. Currently, there are three sets of groups that already exist. So, the program would need to be able to consult that data and...
11
7182
by: breal | last post by:
I have three lists... for instance a = ; b = ; c = ; I want to take those and end up with all of the combinations they create like the following lists
9
5416
by: Dawid Zolkiewicz | last post by:
Hello I'm new here, so at the beginning I'd like to say hello for everybody. First I'll describe my problem. There is db2 database :) with about 0.5 mln people. Every person has three features. These features have allowable values and also are stored in db. I have to prepare query to find all possible combinations ok there features and for every combination find one person having them.
8
3138
by: zyzolus | last post by:
Hello, I create 2 unique indexes on 2 fields: Field1 and Field2. What is the way generate custom error message independent of each field?ex.: "Duplicate value in Field1" or "Duplicate value in Field2". If "OnError" event is used (error code 3022 in this case) the same error
0
10578
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10321
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9152
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7620
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6853
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4300
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3820
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2991
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.