473,406 Members | 2,843 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Simple SQL Help

I think it's simple, but I can't get it to work.

In English its: find records in TableA where the field [Field1] has
more than one unique value in Field2

sample records in TableA

Field1 Field2
2241 12345
2241 12345
2242 12345
2242 99856

desired return (2 records)
2242 12345
2242 99856

thank you for your help

Paul

Mar 7 '07 #1
3 1391
Here is one way to accomplish this:

SELECT Field1, Field2
FROM TableA
WHERE Field1 IN
(SELECT Field1
FROM (SELECT DISTINCT Field1, Field2
FROM TableA) AS A
GROUP BY Field1
HAVING Count(*) 1)

HTH,

Plamen Ratchev
http://www.SQLStudio.com
Mar 7 '07 #2
I wish i could get my brain to think like that. Worked perfectly,
thanks for your help.

Mar 7 '07 #3
>find records [sic] in TableA where field1 [sic] has more than one unique value in field2 <<

Your firtst problem is conceptual; rows are not records; fields are
not columns; tables are not files. Next, think about the phrase "more
than one unique value" versus "more than one non-unique value". SQL
would prefer the phrase "without redundant dupicates" or something.

The vague narrative you posted is not a table at all -- it has no
key! That is one of the MANY differences between rows and records.
In short, if you did things right this would not be a problem at all.

CREATE TABLE Foobar
(field1 INTEGER NOT NULL PRIMARY KEY,
field2 INTEGER NOT NULL);

or maybe you meant:

CREATE TABLE Foobar
(field1 INTEGER NOT NULL,
field2 INTEGER NOT NULL,
PRIMARY KEY (field1, field2));

Who knows from that vague narrative? This is why you post DDL even for
the simple, short things. But using a guess at your original non-
table:

SELECT T1.field1, T1.field2
FROM NonTable AS T1
WHERE T1.field1
IN (SELECT T2.field1
FROM NonTable AS T2
GROUP BY T2.field1
HAVING MIN(T2.field2 <MAX(T2.field2);

Look at this versus Plamen's solution. Once you can think in sets,
instead records and fields, there is no need to use horrible nested
subqueries for such problems. Plamen also has a SELECT DISTINCT with
a GROUP BY that is expensive and redundant.
Mar 10 '07 #4

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

Similar topics

3
by: Patchwork | last post by:
Hi Everyone, Please take a look at the following (simple and fun) program: //////////////////////////////////////////////////////////////////////////// ///////////// // Monster Munch, example...
0
by: unixman | last post by:
As usual, it is 2:00am, and I'm pulling my hair out, finally resorting to posting in the newsgroups for help. :) Simple problem, in theory. Given table "map": CREATE TABLE map ( entry_id...
17
by: savesdeday | last post by:
In my beginnning computer science class we were asked to translate a simple interest problem. We are expected to write an algorithm that gets values for the starting account balance B, annual...
6
by: Scott Niu | last post by:
Hi, I have this following simple c++ program, it will produce memory leak ( see what I did below ). My observation also showed that: There will be a mem leak when all the 3 conditions are true:...
6
by: francisco lopez | last post by:
ok , first of all sorry if my english is not so good, I do my best. here is my problem: I don´t know much javascript so I wrote a very simple one to validate a form I have on my webpage. ...
0
by: Mick Hardy | last post by:
Hi, Has anyone seen this weird behaviour or have any suggestions or can anyone reproduce it? The history: I converted a large third party DB from 97 to XP and it uses the...
2
by: Don Wash | last post by:
Hi All! I've been searching everywhere for a simple sample of producing a bar graph using CrystalReport by specifying SQL Query, and I've found none of it! I find so many complex samples with so...
2
by: Evan | last post by:
Hey, I posted this yesterday, but no one had any ideas? C'mon now, I know this isn't that hard, i'm just a little new to javascript, and I can't quite figure this out. I searched and searched to...
2
by: Michael7 | last post by:
Hi everyone, I'm new to CSS of course, and have been trying to learn it. However, when I try to pull off something as simple as positioning of text . . . nothing works in my index page. So in...
1
by: astrogirl77 | last post by:
I'm new to C++ and am hoping to find help with coding a simple C program, am wanting to obtain code and functioning exe's. I code in an old version of Visual Basic 4.0, I have a simple app that...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...

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.