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

SQL Command help for Unique records.

What is the syntax for an sql command to get the following.

I want to pull in a handful of fields from a table but one of the
fields needs to be unique.

For example, if I have a customer table and I want to get each unique
customer number but I also want to get the customer's name, address,
email address, etc. What is the syntax?

I am trying to use DISTINCTROW but to no avail. If I use DISTINCT,
how do I pull in the other fields?

TIA.

Feb 16 '07 #1
10 2771
On Feb 16, 12:50 pm, mscu...@gmail.com wrote:
What is the syntax for an sql command to get the following.

I want to pull in a handful of fields from a table but one of the
fields needs to be unique.

For example, if I have a customer table and I want to get each unique
customer number but I also want to get the customer's name, address,
email address, etc. What is the syntax?

I am trying to use DISTINCTROW but to no avail. If I use DISTINCT,
how do I pull in the other fields?

TIA.
This is a little confusing, perhaps due to the tables not being
normalized better.
Why would a customer be in a 'customer table' more than once? Futher,
why would there be different information attached to the same cutomer
number? A customers table really should contain information about a
customer only once.
Feb 16 '07 #2
On Feb 16, 12:29 pm, "storrboy" <storr...@sympatico.cawrote:
On Feb 16, 12:50 pm, mscu...@gmail.com wrote:
What is the syntax for an sql command to get the following.
I want to pull in a handful of fields from a table but one of the
fields needs to be unique.
For example, if I have a customer table and I want to get each unique
customer number but I also want to get the customer's name, address,
email address, etc. What is the syntax?
I am trying to use DISTINCTROW but to no avail. If I use DISTINCT,
how do I pull in the other fields?
TIA.

This is a little confusing, perhaps due to the tables not being
normalized better.
Why would a customer be in a 'customer table' more than once? Futher,
why would there be different information attached to the same cutomer
number? A customers table really should contain information about a
customer only once.
Trust me, the db I have to work with here was not created by me and
the relationships are set up very poorly.

Feb 19 '07 #3
On Feb 19, 9:20 am, mscu...@gmail.com wrote:
On Feb 16, 12:29 pm, "storrboy" <storr...@sympatico.cawrote:
On Feb 16, 12:50 pm, mscu...@gmail.com wrote:
What is the syntax for an sql command to get the following.
I want to pull in a handful of fields from a table but one of the
fields needs to be unique.
For example, if I have a customer table and I want to get each unique
customer number but I also want to get the customer's name, address,
email address, etc. What is the syntax?
I am trying to use DISTINCTROW but to no avail. If I use DISTINCT,
how do I pull in the other fields?
TIA.
This is a little confusing, perhaps due to the tables not being
normalized better.
Why would a customer be in a 'customer table' more than once? Futher,
why would there be different information attached to the same cutomer
number? A customers table really should contain information about a
customer only once.

Trust me, the db I have to work with here was not created by me and
the relationships are set up very poorly.

I don't have a table like your to try things on, but if there is
different information attached to the same customer number then I
think the best you can do without normalizing is to use groupings.
This should merge all like records but duplicate the ones where one or
more fields are different.

Feb 21 '07 #4
Simple workaround

Query 1 SELECT DISTINCT (CUSTOMER_NUMBER)

then join that query to the main table and I think you will be able to
get what you want!
storrboy wrote:
On Feb 19, 9:20 am, mscu...@gmail.com wrote:
On Feb 16, 12:29 pm, "storrboy" <storr...@sympatico.cawrote:
On Feb 16, 12:50 pm, mscu...@gmail.com wrote:
What is the syntax for an sql command to get the following.
I want to pull in a handful of fields from a table but one of the
fields needs to be unique.
For example, if I have a customer table and I want to get each unique
customer number but I also want to get the customer's name, address,
email address, etc. What is the syntax?
I am trying to use DISTINCTROW but to no avail. If I use DISTINCT,
how do I pull in the other fields?
TIA.
This is a little confusing, perhaps due to the tables not being
normalized better.
Why would a customer be in a 'customer table' more than once? Futher,
why would there be different information attached to the same cutomer
number? A customers table really should contain information about a
customer only once.
Trust me, the db I have to work with here was not created by me and
the relationships are set up very poorly.


I don't have a table like your to try things on, but if there is
different information attached to the same customer number then I
think the best you can do without normalizing is to use groupings.
This should merge all like records but duplicate the ones where one or
more fields are different.
Feb 21 '07 #5
On Feb 21, 3:43 am, "purpleflash" <k...@bgs.ac.ukwrote:
Simple workaround

Query 1 SELECT DISTINCT (CUSTOMER_NUMBER)

then join that query to the main table and I think you will be able to
get what you want!

storrboy wrote:
On Feb 19, 9:20 am, mscu...@gmail.com wrote:
On Feb 16, 12:29 pm, "storrboy" <storr...@sympatico.cawrote:
On Feb 16, 12:50 pm, mscu...@gmail.com wrote:
What is the syntax for an sql command to get the following.
I want to pull in a handful of fields from a table but one of the
fields needs to be unique.
For example, if I have a customer table and I want to get each unique
customer number but I also want to get the customer's name, address,
email address, etc. What is the syntax?
I am trying to use DISTINCTROW but to no avail. If I use DISTINCT,
how do I pull in the other fields?
TIA.
This is a little confusing, perhaps due to the tables not being
normalized better.
Why would a customer be in a 'customer table' more than once? Futher,
why would there be different information attached to the same cutomer
number? A customers table really should contain information about a
customer only once.
Trust me, the db I have to work with here was not created by me and
the relationships are set up very poorly.
I don't have a table like your to try things on, but if there is
different information attached to the same customer number then I
think the best you can do without normalizing is to use groupings.
This should merge all like records but duplicate the ones where one or
more fields are different.- Hide quoted text -

- Show quoted text -
Does this command pull in all the fields of the table though?

Feb 21 '07 #6
On Feb 21, 12:13 pm, mscu...@gmail.com wrote:
On Feb 21, 3:43 am, "purpleflash" <k...@bgs.ac.ukwrote:


Simple workaround
Query 1 SELECT DISTINCT (CUSTOMER_NUMBER)
then join that query to the main table and I think you will be able to
get what you want!
storrboy wrote:
On Feb 19, 9:20 am, mscu...@gmail.com wrote:
On Feb 16, 12:29 pm, "storrboy" <storr...@sympatico.cawrote:
On Feb 16, 12:50 pm, mscu...@gmail.com wrote:
What is the syntax for an sql command to get the following.
I want to pull in a handful of fields from a table but one of the
fields needs to be unique.
For example, if I have a customer table and I want to get each unique
customer number but I also want to get the customer's name, address,
email address, etc. What is the syntax?
I am trying to use DISTINCTROW but to no avail. If I use DISTINCT,
how do I pull in the other fields?
TIA.
This is a little confusing, perhaps due to the tables not being
normalized better.
Why would a customer be in a 'customer table' more than once? Futher,
why would there be different information attached to the same cutomer
number? A customers table really should contain information about a
customer only once.
Trust me, the db I have to work with here was not created by me and
the relationships are set up very poorly.
I don't have a table like your to try things on, but if there is
different information attached to the same customer number then I
think the best you can do without normalizing is to use groupings.
This should merge all like records but duplicate the ones where one or
more fields are different.- Hide quoted text -
- Show quoted text -

Does this command pull in all the fields of the table though?- Hide quoted text -

- Show quoted text -
To elaborate further, I am using the following command:

SELECT DISTINCTROW [EA_K]
FROM Customers;

I want to display all the fields in this query when the query runs and
use this to make a comparison against another table. When I run this,
it only displays the EA_K field. How do I get it to display the other
12 fields in the Customers table?

Feb 21 '07 #7
On Feb 21, 11:20 am, mscu...@gmail.com wrote:
To elaborate further, I am using the following command:

SELECT DISTINCTROW [EA_K]
FROM Customers;

I want to display all the fields in this query when the query runs and
use this to make a comparison against another table. When I run this,
it only displays the EA_K field. How do I get it to display the other
12 fields in the Customers table?
Just drag and drop the astrix field into the design grid, that will
show all fields in that table.

Feb 21 '07 #8
On Feb 21, 12:20 pm, mscu...@gmail.com wrote:
On Feb 21, 12:13 pm, mscu...@gmail.com wrote:
On Feb 21, 3:43 am, "purpleflash" <k...@bgs.ac.ukwrote:
Simple workaround
Query 1 SELECT DISTINCT (CUSTOMER_NUMBER)
then join that query to the main table and I think you will be able to
get what you want!
storrboy wrote:
On Feb 19, 9:20 am, mscu...@gmail.com wrote:
On Feb 16, 12:29 pm, "storrboy" <storr...@sympatico.cawrote:
On Feb 16, 12:50 pm, mscu...@gmail.com wrote:
What is the syntax for an sql command to get the following.
I want to pull in a handful of fields from a table but one of the
fields needs to be unique.
For example, if I have a customer table and I want to get each unique
customer number but I also want to get the customer's name, address,
email address, etc. What is the syntax?
I am trying to use DISTINCTROW but to no avail. If I use DISTINCT,
how do I pull in the other fields?
TIA.
This is a little confusing, perhaps due to the tables not being
normalized better.
Why would a customer be in a 'customer table' more than once? Futher,
why would there be different information attached to the same cutomer
number? A customers table really should contain information about a
customer only once.
Trust me, the db I have to work with here was not created by me and
the relationships are set up very poorly.
I don't have a table like your to try things on, but if there is
different information attached to the same customer number then I
think the best you can do without normalizing is to use groupings.
This should merge all like records but duplicate the ones where one or
more fields are different.- Hide quoted text -
- Show quoted text -
Does this command pull in all the fields of the table though?- Hide quoted text -
- Show quoted text -

To elaborate further, I am using the following command:

SELECT DISTINCTROW [EA_K]
FROM Customers;

I want to display all the fields in this query when the query runs and
use this to make a comparison against another table. When I run this,
it only displays the EA_K field. How do I get it to display the other
12 fields in the Customers table?
>I am trying to use DISTINCTROW but to no avail. If I use DISTINCT, how
do I pull in the other fields?
The DISTINCT and DISTINCTROW clauses do not affect the actual columns
that are returned by a query. That is determined by the column names
listed before the FROM clause, e.g.

SELECT [Table1].[Column1], [Table1].[Column2] FROM [Table1]

or

SELECT [Table1].* FROM [Table1]

However, DISTINCT and DISTINCTROW affect the *rows* that are returned,
and it is important to note the difference. DISTINCT removes duplicate
rows by comparing just the selected columns, whereas DISTINCTROW
removes duplicate rows by comparing *all* columns in the source table
(or set of joined tables) regardless of which columns are to be
returned. So if you had a table of Cities and Provinces called
[CityProv], like this

[City] [Province]
Calgary AB
Edmonton AB
Vancouver BC
Victoria BC
Toronto ON
Montreal QC

You would see the following:

SELECT DISTINCT [CityProv].[Province] FROM [CityProv];

Province
--------
AB
BC
ON
QC

SELECT DISTINCTROW [CityProv].[Province] FROM [CityProv];

Province
--------
AB
AB
BC
BC
ON
QC
Feb 21 '07 #9
On Feb 21, 4:43 pm, "Gord" <g...@kingston.netwrote:
On Feb 21, 12:20 pm, mscu...@gmail.com wrote:


On Feb 21, 12:13 pm, mscu...@gmail.com wrote:
On Feb 21, 3:43 am, "purpleflash" <k...@bgs.ac.ukwrote:
Simple workaround
Query 1 SELECT DISTINCT (CUSTOMER_NUMBER)
then join that query to the main table and I think you will be able to
get what you want!
storrboy wrote:
On Feb 19, 9:20 am, mscu...@gmail.com wrote:
On Feb 16, 12:29 pm, "storrboy" <storr...@sympatico.cawrote:
On Feb 16, 12:50 pm, mscu...@gmail.com wrote:
What is the syntax for an sql command to get the following.
I want to pull in a handful of fields from a table but one of the
fields needs to be unique.
For example, if I have a customer table and I want to get each unique
customer number but I also want to get the customer's name, address,
email address, etc. What is the syntax?
I am trying to use DISTINCTROW but to no avail. If I use DISTINCT,
how do I pull in the other fields?
TIA.
This is a little confusing, perhaps due to the tables not being
normalized better.
Why would a customer be in a 'customer table' more than once? Futher,
why would there be different information attached to the same cutomer
number? A customers table really should contain information about a
customer only once.
Trust me, the db I have to work with here was not created by me and
the relationships are set up very poorly.
I don't have a table like your to try things on, but if there is
different information attached to the same customer number then I
think the best you can do without normalizing is to use groupings.
This should merge all like records but duplicate the ones where one or
more fields are different.- Hide quoted text -
- Show quoted text -
Does this command pull in all the fields of the table though?- Hide quoted text -
- Show quoted text -
To elaborate further, I am using the following command:
SELECT DISTINCTROW [EA_K]
FROM Customers;
I want to display all the fields in this query when the query runs and
use this to make a comparison against another table. When I run this,
it only displays the EA_K field. How do I get it to display the other
12 fields in the Customers table?
I am trying to use DISTINCTROW but to no avail. If I use DISTINCT, how
do I pull in the other fields?

The DISTINCT and DISTINCTROW clauses do not affect the actual columns
that are returned by a query. That is determined by the column names
listed before the FROM clause, e.g.

SELECT [Table1].[Column1], [Table1].[Column2] FROM [Table1]

or

SELECT [Table1].* FROM [Table1]

However, DISTINCT and DISTINCTROW affect the *rows* that are returned,
and it is important to note the difference. DISTINCT removes duplicate
rows by comparing just the selected columns, whereas DISTINCTROW
removes duplicate rows by comparing *all* columns in the source table
(or set of joined tables) regardless of which columns are to be
returned. So if you had a table of Cities and Provinces called
[CityProv], like this

[City] [Province]
Calgary AB
Edmonton AB
Vancouver BC
Victoria BC
Toronto ON
Montreal QC

You would see the following:

SELECT DISTINCT [CityProv].[Province] FROM [CityProv];

Province
--------
AB
BC
ON
QC

SELECT DISTINCTROW [CityProv].[Province] FROM [CityProv];

Province
--------
AB
AB
BC
BC
ON
QC- Hide quoted text -

- Show quoted text -
yes, that is what I am getting something similar.

How could I write the SQL command to get the following?

[City] [Province] (Original Table)
Calgary AB
Edmonton AB
Vancouver BC
Victoria BC
Toronto ON
Montreal QC

[City] [Province] (Results desired)
Calgary AB
Vancouver BC
Toronto ON
Montreal QC

I want to eliminate the duplicate Provinces (AB,BC) yet display the
City and the Province.


Feb 22 '07 #10
I want to eliminate the duplicate Provinces (AB,BC) yet display the
City and the Province.
Try something like this:

SELECT First(CityProv.City) AS City, CityProv.Province
FROM CityProv
GROUP BY CityProv.Province;

On Feb 22, 3:56 pm, mscu...@gmail.com wrote:
On Feb 21, 4:43 pm, "Gord" <g...@kingston.netwrote:
On Feb 21, 12:20 pm, mscu...@gmail.com wrote:
On Feb 21, 12:13 pm, mscu...@gmail.com wrote:
On Feb 21, 3:43 am, "purpleflash" <k...@bgs.ac.ukwrote:
Simple workaround
Query 1 SELECT DISTINCT (CUSTOMER_NUMBER)
then join that query to the main table and I think you will be able to
get what you want!
storrboy wrote:
On Feb 19, 9:20 am, mscu...@gmail.com wrote:
On Feb 16, 12:29 pm, "storrboy" <storr...@sympatico.cawrote:
On Feb 16, 12:50 pm, mscu...@gmail.com wrote:
What is the syntax for an sql command to get the following.
I want to pull in a handful of fields from a table but one of the
fields needs to be unique.
For example, if I have a customer table and I want to get each unique
customer number but I also want to get the customer's name, address,
email address, etc. What is the syntax?
I am trying to use DISTINCTROW but to no avail. If I use DISTINCT,
how do I pull in the other fields?
TIA.
This is a little confusing, perhaps due to the tables not being
normalized better.
Why would a customer be in a 'customer table' more than once? Futher,
why would there be different information attached to the same cutomer
number? A customers table really should contain information about a
customer only once.
Trust me, the db I have to work with here was not created by me and
the relationships are set up very poorly.
I don't have a table like your to try things on, but if there is
different information attached to the same customer number then I
think the best you can do without normalizing is to use groupings.
This should merge all like records but duplicate the ones where one or
more fields are different.- Hide quoted text -
- Show quoted text -
Does this command pull in all the fields of the table though?- Hide quoted text -
- Show quoted text -
To elaborate further, I am using the following command:
SELECT DISTINCTROW [EA_K]
FROM Customers;
I want to display all the fields in this query when the query runs and
use this to make a comparison against another table. When I run this,
it only displays the EA_K field. How do I get it to display the other
12 fields in the Customers table?
>I am trying to use DISTINCTROW but to no avail. If I use DISTINCT, how
>do I pull in the other fields?
The DISTINCT and DISTINCTROW clauses do not affect the actual columns
that are returned by a query. That is determined by the column names
listed before the FROM clause, e.g.
SELECT [Table1].[Column1], [Table1].[Column2] FROM [Table1]
or
SELECT [Table1].* FROM [Table1]
However, DISTINCT and DISTINCTROW affect the *rows* that are returned,
and it is important to note the difference. DISTINCT removes duplicate
rows by comparing just the selected columns, whereas DISTINCTROW
removes duplicate rows by comparing *all* columns in the source table
(or set of joined tables) regardless of which columns are to be
returned. So if you had a table of Cities and Provinces called
[CityProv], like this
[City] [Province]
Calgary AB
Edmonton AB
Vancouver BC
Victoria BC
Toronto ON
Montreal QC
You would see the following:
SELECT DISTINCT [CityProv].[Province] FROM [CityProv];
Province
--------
AB
BC
ON
QC
SELECT DISTINCTROW [CityProv].[Province] FROM [CityProv];
Province
--------
AB
AB
BC
BC
ON
QC- Hide quoted text -
- Show quoted text -

yes, that is what I am getting something similar.

How could I write the SQL command to get the following?

[City] [Province] (Original Table)
Calgary AB
Edmonton AB
Vancouver BC
Victoria BC
Toronto ON
Montreal QC

[City] [Province] (Results desired)
Calgary AB
Vancouver BC
Toronto ON
Montreal QC

I want to eliminate the duplicate Provinces (AB,BC) yet display the
City and the Province.

Feb 22 '07 #11

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

Similar topics

12
by: Jeff North | last post by:
I'm stumped and my brains are fried!!!! I have the following data +-------+------------------------------+------------+ | resID | FLEName | VersionNbr |...
3
by: Beat Scheidiger | last post by:
I do not quite understand this property. Everything is seems clear to me, when I read the corresponding help text. But in practice I have a question: Why are there 3 identical records in the...
5
by: Terri | last post by:
I have a form with a multi-select combo. I dynamically build a SELECT statement, open a report, and set the recordsource to my dynamic SELECT statement. I count the records returned in the report...
7
by: Nicolae Fieraru | last post by:
I have two tables, they contain: Table1: ID1, Name1, Address1, Purchase1 Table2: ID2, Name2, Address2, Purchase2 I need a query which creates Table3 with content from Table1 and Table2. The...
4
by: kdubble | last post by:
Hi I am trying to get the results of a query to show only unique student records (not duplicates). Is there a simple way to make the criteria field do this? I am not too familiar with SQL. ...
3
by: IsValidUN | last post by:
How do you select unique records? For example, if my data is like the following and I only want the repeated data (address information) to appear once for each theater_id. <xsl:for-each...
2
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...
2
Zwoker
by: Zwoker | last post by:
Greetings everyone, I have a problem that I hope has a simple solution. I am using MS Access 2003. I have a table that is a list of financial transactions. I am using a make table query over...
10
by: jmartmem | last post by:
Greetings, I have an ASP page with a 5x5 table embedded inside an Insert Record Form. This table contains several fields (mostly drop down list menus) and is used for corporate timekeeping (users...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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:
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...

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.