473,396 Members | 1,866 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,396 software developers and data experts.

SQL - SELECT DISTINCT

Okay so this is baking my noodle. I want to select all the
attritbutes/fields from a table but then to excluded any row in which
a single attributes data has been duplicated.

I.E. Here's my table:-

ID Ref Name DATE
1 AAA Joe 1/2
2 BBB Ken 1/2
3 AAA Len 6/3

"SELECT DISTINCT * FROM tblWhatever ORDER BY Date;"

So I've read up and this statement doesn't work because all the
attributes in the record have to be the same in order for that record
to be omited from the results but they must be a way to do what I'm
after?

I'd appreciate the help,

Kelvin
Nov 12 '05 #1
9 10817
Maybe you're out with a Totals query; you can group by Ref (in your
sample) and choose e.g. First for other fields. Sum(date) doesn't really
make sense does it?

Kelvin wrote:
Okay so this is baking my noodle. I want to select all the
attritbutes/fields from a table but then to excluded any row in which
a single attributes data has been duplicated.

I.E. Here's my table:-

ID Ref Name DATE
1 AAA Joe 1/2
2 BBB Ken 1/2
3 AAA Len 6/3

"SELECT DISTINCT * FROM tblWhatever ORDER BY Date;"

So I've read up and this statement doesn't work because all the
attributes in the record have to be the same in order for that record
to be omited from the results but they must be a way to do what I'm
after?

I'd appreciate the help,

Kelvin


--
Bas Cost Budde

Nov 12 '05 #2

What a melon scratcher!

The way I would tackle that problem would be to do a query for each of the fields, using the "distinct" parameter.
qryREF_field = "select distinct REF from tblWhatever"
qryNAME_Field = "select distinct NAME from tblWhatever"
etc...

Then would create another query using the table, and each of the separate queries for the fields.
Join the appropriate fields from the queries to the fields on the main table. The default join is what you want.
- So long as those joins are in place.

Then add in all of the fields from tblwhatever, and hopefully should work!? (no time to try it at the moment I'm sorry, but let me know how it goes and if it doesn't work will have a closer look tomorrow for you!)

Regards,

Fraser.

ke**************@dsl.pipex.com (Kelvin) wrote:
Okay so this is baking my noodle. I want to select all the
attritbutes/fields from a table but then to excluded any row in which
a single attributes data has been duplicated.

I.E. Here's my table:-

ID Ref Name DATE
1 AAA Joe 1/2
2 BBB Ken 1/2
3 AAA Len 6/3

"SELECT DISTINCT * FROM tblWhatever ORDER BY Date;"

So I've read up and this statement doesn't work because all the
attributes in the record have to be the same in order for that record
to be omited from the results but they must be a way to do what I'm
after?

I'd appreciate the help,

Kelvin


Nov 12 '05 #3
You're right Sum(date) isn't really any good to me but I understand your
train of thought. What it is I'm after doing is this. I have a
database of empolyees, everytime one of these employees leaves I appded
that employees details to a leavers table with additional information
such as the date they left and and comments pertaining to that person.
Then the original record is then removed from the current employees
table.

E.G.

tblCurrentEmps -----------> tblLeavers
xxOrigRemovedxx xxDate&CommentsAddedxx

Each employee has a unique ID which when they leave is freed up and can
get re-allocated to a new starter. So if I want to view historical
performance statistics based around the unique ID's of the employees I
have a problem as the ID's could have been re-allocated and a incorrect
name against the performance stats is displayed.

Hence if I can get a list out of my tblLeavers table of all the ID's
after a speific date ignoring any repeated ID's after the first has been
found then I can recompile an accurate list of employees at that point
in time.

Okay so I know this sounds a little mad but I believe the theory to be
sound. What do you think?

Kelvin

P.S. Thanks for your help by the way.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 12 '05 #4
Try something like this:

select tblwhatever.*
from tblwhatever
where (((tblwhatever) In (select [Ref] FROM [tblwhatever] as tmp group by
[Ref] having Count(*)<2 )))
order by tblwhatever.Ref;

That would work for eliminating results where 1 field contains duplicate
info. If you wanted to include this criteria for every field then include
more sub-queries.
e.g.:

select tblwhatever.*
from tblwhatever
where (((tblwhatever.Ref) in (select [Ref] from [tblwhatever] as tmp group
by [Ref] having Count(*)<2 )) and ((tblwhatever.[Name]) in (select [name]
from [tblwhatever] as tmp group by [name] having count(*)<2 )))
order by tblwhatever.ref;
Mark
"Kelvin" <ke**************@dsl.pipex.com> wrote in message
news:17************************@posting.google.com ...
Okay so this is baking my noodle. I want to select all the
attritbutes/fields from a table but then to excluded any row in which
a single attributes data has been duplicated.

I.E. Here's my table:-

ID Ref Name DATE
1 AAA Joe 1/2
2 BBB Ken 1/2
3 AAA Len 6/3

"SELECT DISTINCT * FROM tblWhatever ORDER BY Date;"

So I've read up and this statement doesn't work because all the
attributes in the record have to be the same in order for that record
to be omited from the results but they must be a way to do what I'm
after?

I'd appreciate the help,

Kelvin

Nov 12 '05 #5
Maybe you should not use the employee ID as primary key (I guess you
don't already)

Take a two-step approach. Create a query that retrieves the oldest
employee IDs with their true PK, presumably some autonumber (I must
review SQL books on how exactly, maybe someone else here can show how.
Grouping on empID alone will not do the trick; first(date) will show the
first date, but how to retrieve the PK?)
On this query, create another that retrieves all other information about
the history.

Kelvin Middleton wrote:
Each employee has a unique ID which when they leave is freed up and can
get re-allocated to a new starter. So if I want to view historical
performance statistics based around the unique ID's of the employees I
have a problem as the ID's could have been re-allocated and a incorrect
name against the performance stats is displayed.

Hence if I can get a list out of my tblLeavers table of all the ID's
after a speific date ignoring any repeated ID's after the first has been
found then I can recompile an accurate list of employees at that point
in time.

Okay so I know this sounds a little mad but I believe the theory to be
sound. What do you think?

Kelvin

P.S. Thanks for your help by the way.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!


--
Bas Cost Budde

Nov 12 '05 #6
<fr***@clear.net.nz> wrote in message news:<40********@clear.net.nz>...
What a melon scratcher!

The way I would tackle that problem would be to do a query for each of the fields, using the "distinct" parameter.
qryREF_field = "select distinct REF from tblWhatever"
qryNAME_Field = "select distinct NAME from tblWhatever"
etc...

Then would create another query using the table, and each of the separate queries for the fields.
Join the appropriate fields from the queries to the fields on the main table. The default join is what you want.
- So long as those joins are in place.

Then add in all of the fields from tblwhatever, and hopefully should work!? (no time to try it at the moment I'm sorry, but let me know how it goes and if it doesn't work will have a closer look tomorrow for you!)

Regards,

Fraser.

ke**************@dsl.pipex.com (Kelvin) wrote:
Okay so this is baking my noodle. I want to select all the
attritbutes/fields from a table but then to excluded any row in which
a single attributes data has been duplicated.

I.E. Here's my table:-

ID Ref Name DATE
1 AAA Joe 1/2
2 BBB Ken 1/2
3 AAA Len 6/3

"SELECT DISTINCT * FROM tblWhatever ORDER BY Date;"

So I've read up and this statement doesn't work because all the
attributes in the record have to be the same in order for that record
to be omited from the results but they must be a way to do what I'm
after?

I'd appreciate the help,

Kelvin


Its a good idea but how would I maintain the relations ships between
ref and name if I'm querying them individually.

In my example above a "SELECT DISTINCT Ref FROM tblWhatever" would
return:-

Ref
AAA
BBB

And a "SELECT DISTINCT Name FROM tblWhatever" would return:-

Name
Joe
Ken
Len

So they don't match up, and don't forget I need to query them by order
of 'date'...just to make it even harder :-)

I've had an idea of creating the SQL to join back onto the same table
in a subquery and compare the REF field like that but although I've
heard of this before I've never actually joined a table back onto
itself before. Could this be a possibility?

I appreciate all this help folks, cheers.

Kelvin
Nov 12 '05 #7
Hi,

Take all of this with a grain of salt; but I wouldn't remove employees
from the original table at all. I would merely have a field indicating
that they were or were not a current employee (in my apps, it is
usually just a "deleted?" field) and have a related table of leavers
that has the termination information. That way you never have to deal
with duplicate or incorrect keys, etc.

JimA
Kelvin Middleton <ke**************@dsl.pipex.com> wrote in message news:<40***********************@news.frii.net>...
You're right Sum(date) isn't really any good to me but I understand your
train of thought. What it is I'm after doing is this. I have a
database of empolyees, everytime one of these employees leaves I appded
that employees details to a leavers table with additional information
such as the date they left and and comments pertaining to that person.
Then the original record is then removed from the current employees
table.

E.G.

tblCurrentEmps -----------> tblLeavers
xxOrigRemovedxx xxDate&CommentsAddedxx

Each employee has a unique ID which when they leave is freed up and can
get re-allocated to a new starter. So if I want to view historical
performance statistics based around the unique ID's of the employees I
have a problem as the ID's could have been re-allocated and a incorrect
name against the performance stats is displayed.

Hence if I can get a list out of my tblLeavers table of all the ID's
after a speific date ignoring any repeated ID's after the first has been
found then I can recompile an accurate list of employees at that point
in time.

Okay so I know this sounds a little mad but I believe the theory to be
sound. What do you think?

Kelvin

P.S. Thanks for your help by the way.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 12 '05 #8
Cheers Jim but I can't leave them in the same table you see each
employee has a unique ID which is also what they use to access several
of our IT systems. As the system is quite old there is only a finite
number of these unique ID's and as such we re-allocate old ones from
leavers to new starters. So as I said these ID's are unique and thus
they are the Primary Key for each record so I have to remove the leavers
into another table when they go.

Thanks as well to Bas and Mark gunna try your ideas out soon, i'll let
ya know how I've got on.

Kelvin

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 12 '05 #9

Not trying to achieve much... ...Perhaps a better method might have been to keep unique ID's? For example, at my last job every employee had an employee number that was unique to them. It was used for payroll, etc. If you have something like that it might be a good idea to capture and use because it is truly unique.

However, that's not the case, and not the answer to your question, so here goes...

two queries.
First query gets the data from tblLeavers.
The second query should be an "unmatched records" query (You'll find there is a wizard for this in access). This query should filter out all of the records that are in the tblLeavers AND tblCurrentEmp tables.

You will then need to combine these queries into one... ...to do this you will need to ensure both of your two new queries have the same fields, and in the same order. Once this is the case, go into the SQL view of a new query and type along the lines of:

"SELECT *
FROM qrytblLeavers

UNION

SELECT *
FROM qrytblCurrentEmp_Unmatched;"

Then try that. That will take all of the records from tblLeavers, and only those records from tblCurrentEmp that don't have a matching ID in tblLeavers.

Hope that helps,

Fraser.

Kelvin Middleton <ke**************@dsl.pipex.com> wrote:
You're right Sum(date) isn't really any good to me but I understand your
train of thought. What it is I'm after doing is this. I have a
database of empolyees, everytime one of these employees leaves I appded
that employees details to a leavers table with additional information
such as the date they left and and comments pertaining to that person.
Then the original record is then removed from the current employees
table.

E.G.

tblCurrentEmps -----------> tblLeavers
xxOrigRemovedxx xxDate&CommentsAddedxx

Each employee has a unique ID which when they leave is freed up and can
get re-allocated to a new starter. So if I want to view historical
performance statistics based around the unique ID's of the employees I
have a problem as the ID's could have been re-allocated and a incorrect
name against the performance stats is displayed.

Hence if I can get a list out of my tblLeavers table of all the ID's
after a speific date ignoring any repeated ID's after the first has been
found then I can recompile an accurate list of employees at that point
in time.

Okay so I know this sounds a little mad but I believe the theory to be
sound. What do you think?

Kelvin

P.S. Thanks for your help by the way.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!


Nov 12 '05 #10

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

Similar topics

5
by: Martin Feuersteiner | last post by:
Dear Group I'm having trouble with the clause below. I would like to select only records with a distinct TransactionDate but somehow it still lists duplicates. I need to select the...
3
by: blue | last post by:
I'm trying to order a varchar column first numerically, and second alphanumerically using the following SQL: SELECT distinct doc_number FROM doc_line WHERE product_id = 'WD' AND doc_type = 'O'...
2
by: mfyahya | last post by:
I have two tables, both containing an 'authors' column. Is there a way to get a unique list of authors from the two tables? I tried SELECT DISTINCT `authors` from `table1`, `table2`; but I got an...
3
by: Tcs | last post by:
My backend is DB2 on our AS/400. While I do HAVE DB2 PE for my PC, I haven't loaded it yet. I'm still using MS Access. And no, I don't believe this is an Access question. (But who knows? I...
6
by: John M | last post by:
Hi, The line below is used to feed a combobox. (It is from a database which is used to log pupil behaviour!) The 'incidents' table contains a list of students who have been involved in incidents....
18
by: mathilda | last post by:
My boss has been adamant that SELECT DISTINCT is a faster query than SELECT all other factors being equal. I disagree. We are linking an Access front end to a SQL Server back end and normally are...
3
by: orekinbck | last post by:
Hi There Our test database has duplicate data: COMPANYID COMPANYNAME 1 Grupple Group 2 Grupple Group 5 Grupple Group 3 Yada Inc 4 Yada...
6
by: Bob Stearns | last post by:
I am getting unwanted duplicate rows in my result set, so I added the DISTINCT keyword to my outermost SELECT. My working query then returned the following message: DB2 SQL error: SQLCODE: -214,...
5
by: Daniel Wetzler | last post by:
Dear MSSQL experts, I use MSSQL 2000 and encountered a strange problem wqhile I tried to use a select into statement . If I perform the command command below I get only one dataset which has...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
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
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.