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

Select distinct one some fields, but return all feilds

I was curious...

Is there a way to select distinct on a combination of some fields and
the for each record returned also get the other fields of an
arbitrarily chosen record matching the fields in the distinct record.

For example, if I have a select distinct on say three fields:
SELECT DISTINCT Code1, Code2, Code3
but the table also has other fields, maybe Foo1 and Foo2, and I want
Foo1 and Foo2 to also be displayed. Since there may be multiple
records that match a particular Code1, Code2, Code3, then I just want
one of those to be arbitrarily chosen.

Jul 23 '05 #1
3 3713
What's the key of this table? It would be useful to know. Assuming it is
key_col, you could do this:

SELECT code1, code2, code3, foo1, foo2
FROM YourTable AS T
WHERE key_col =
(SELECT MIN(key_col)
FROM YourTable
WHERE code1 = T.code1
AND code2 = T.code2
AND code3 = T.code3)

This also assumes that code1, code2 and code3 are non-nullable columns.
Again, it does help if you post a better spec of your problem. The best way
is to include DDL (CREATE TABLE statement including keys and constraints),
sample data (INSERT statements) and show your required end results.
Otherwise answers are just guesswork.

--
David Portas
SQL Server MVP
--
Jul 23 '05 #2
Very helpful. I didn't think I should post scripts because the tables
have alot of fields, and the queries were kind of long(and at least to
me seemed complicated). I thought it would overcomplicate things. So
I thought a simplified example would be better. I guess I should make
a database just for creating simple tables in for experimentation so
that I can post scripts from them.

Jul 23 '05 #3
If possible, simplify your DDL to just the essential columns (including the
keys). Yes, it's a good idea to keep a scratch database on your laptop or
desktop for this sort of thing. See also:
http://www.aspfaq.com/etiquette.asp?id=5006

Glad I helped.

--
David Portas
SQL Server MVP
--
Jul 23 '05 #4

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

Similar topics

5
by: Reestit Mutton | last post by:
Hi, I'm currently learning the ropes with CSS, PHP, MYSQL, Javascript etc... by redesigning my website as a database driven site. Okay, so I'm skilled at perl, data manipulation and data...
9
by: Kelvin | last post by:
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...
8
by: skinnybloke | last post by:
Hi - I have a problem with a memo field being truncated to about 255 characters when running a Access 2002 query. This only seems to happen if I use SELECT DISTINCT. It works ok using SELECT by...
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...
22
by: MP | last post by:
vb6,ado,mdb,win2k i pass the sql string to the .Execute method on the open connection to Table_Name(const) db table fwiw (the connection opened via class wrapper:) msConnString = "Data Source="...
6
by: pooh80133 | last post by:
Hi! I am pasting my SQL code at the end of this message. I am trying to use SELECT DISTINCT in a query, but I am a beginner for using Access. Right now I have duplicate ID's (Indiv ID) in my...
5
by: justanothernewbie804 | last post by:
hi all. I have a dataset and would like to do something like "select distinct field1, field2 from fields" against the dataset. I do not have the option of creating a new database table and...
9
by: P3Eddie | last post by:
Hello all! I don't know if this can even be done, but I'm sure you will either help or suggest another avenue to accomplish the same. My problem may be a simple find duplicates / do something...
8
by: penfold33 | last post by:
Hi I need to generate a CSV of names and address from a table and I would like only one result (it doesn't matter which) per distinct email address. If possible, they also need to be ordered by...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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?
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...

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.