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

Simple SQL Distinct...!?

@sh
This may be a really simple question, but I always have problems with
Distinct queries.

In this instance, I have a table of Delivery addresses, some will be exactly
the same EXCEPT for the DeliveryID field, but eitherway I need the
DeliveryID so that I can pass it onto the next form.

So, I need to run a query that will find only addresses that are unique
whilst retaining the respective DID for the rows...

SELECT DISTINCT DeliveryID, Address, Organisation, Organisation, Town,
County, PostCode FROM SOL_Delivery WHERE CustomerID = 15)

How do I adjust the following to only 'Distinct' the fields other than the
DeliveryID?

Thanks!
Feb 27 '06 #1
8 1659
For the non-unique addresses, you will have a choice
of DeliveryIDs, which one do you want?

If you want just one of them, you can do this

SELECT MAX(DeliveryID), Address, Organisation, Organisation, Town,
County, PostCode
FROM SOL_Delivery
WHERE CustomerID = 15
GROUP BY Address, Organisation, Organisation, Town,
County, PostCode

Feb 27 '06 #2
@sh wrote:
This may be a really simple question, but I always have problems with
Distinct queries.

In this instance, I have a table of Delivery addresses, some will be exactly
the same EXCEPT for the DeliveryID field, but eitherway I need the
DeliveryID so that I can pass it onto the next form.

So, I need to run a query that will find only addresses that are unique
whilst retaining the respective DID for the rows...

SELECT DISTINCT DeliveryID, Address, Organisation, Organisation, Town,
County, PostCode FROM SOL_Delivery WHERE CustomerID = 15)

How do I adjust the following to only 'Distinct' the fields other than the
DeliveryID?

Thanks!


Given that there may be more than one ID per address, which ID do you
want to see in the result? To get the minimum ID value for example:

SELECT MIN(deliveryid), address, organisation, town, county, postcode
FROM sol_delivery
WHERE customerid = 15
GROUP BY address, organisation, town, county, postcode ;

Why not get rid of the duplicates and then add a unique constraint so
that you prevent them in future?

For future reference remember that SELECT DISTINCT always applies the
distinct operator across ALL columns in the result. That's why GROUP BY
is what you actually require here.

--
David Portas, SQL Server MVP

Whenever possible please post enough code to reproduce your problem.
Including CREATE TABLE and INSERT statements usually helps.
State what version of SQL Server you are using and specify the content
of any error messages.

SQL Server Books Online:
http://msdn2.microsoft.com/library/m...S,SQL.90).aspx
--

Feb 27 '06 #3
@sh
Thanks to you both for your replies, I did actually try the Group By but
received the 'Aggregate function' error message that I couldn't quite
interpret into English....!!!

Many thanks, I'll give those a go - in the meantime I actually used this in
the end...

SELECT DeliveryID, Address, Organisation, Organisation, Town, County,
PostCode FROM SOL_Delivery WHERE DeliveryID IN (SELECT DISTINCT
REPLACE(Postcode,' ','') FROM sol_delivery WHERE CID = CID (ASP Variable)
ORDER BY Address DESC

....but I'll replace with your variants if you think the above is less
efficient?

Cheers, Ash
Feb 27 '06 #4
Could you please post sample data and expected results?

--
Tom

----------------------------------------------------
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
SQL Server MVP
Columnist, SQL Server Professional
Toronto, ON Canada
www.pinpub.com
..
"@sh" <sp**@spam.com> wrote in message
news:dt**********@nwrdmz01.dmz.ncs.ea.ibs-infra.bt.com...
This may be a really simple question, but I always have problems with
Distinct queries.

In this instance, I have a table of Delivery addresses, some will be exactly
the same EXCEPT for the DeliveryID field, but eitherway I need the
DeliveryID so that I can pass it onto the next form.

So, I need to run a query that will find only addresses that are unique
whilst retaining the respective DID for the rows...

SELECT DISTINCT DeliveryID, Address, Organisation, Organisation, Town,
County, PostCode FROM SOL_Delivery WHERE CustomerID = 15)

How do I adjust the following to only 'Distinct' the fields other than the
DeliveryID?

Thanks!
Feb 27 '06 #5
@sh
UPDATE...

I'll definitely be using your suggestions seen as mine didn't work, hehe -
seemed like a good idea at the time, not sure what I thinking looking back
at it now...

Cheers, Ash
"@sh" <sp**@spam.com> wrote in message
news:dt**********@nwrdmz02.dmz.ncs.ea.ibs-infra.bt.com...
Thanks to you both for your replies, I did actually try the Group By but
received the 'Aggregate function' error message that I couldn't quite
interpret into English....!!!

Many thanks, I'll give those a go - in the meantime I actually used this
in the end...

SELECT DeliveryID, Address, Organisation, Organisation, Town, County,
PostCode FROM SOL_Delivery WHERE DeliveryID IN (SELECT DISTINCT
REPLACE(Postcode,' ','') FROM sol_delivery WHERE CID = CID (ASP Variable)
ORDER BY Address DESC

...but I'll replace with your variants if you think the above is less
efficient?

Cheers, Ash

Feb 27 '06 #6
@sh
Its basically an application where they've previously had lots of
opportunities to enter delivery address and so there's duplicates as they
enter a new one each time.

I'm adding a page that looks up all 'unique' Delivery Addresses (although
each has a unique DID, DeliveryID), and I want to just show the latest
unique addresses that have the highest DID

So instead of getting this...

, ### Waterleaze, Maidenbrook, Taunton, Somerset, Somerset, TA7 8PS
Test, Ston Easton, Bath, Bath, BA3 4DF
, ### Waterleaze, Maidenbrook, Taunton, Somerset, Somerset, TA7 8PS
, ### Waterleaze, Maidenbrook, Taunton, Somerset, Somerset, TA7 8PS
Test, Ston Easton, Bath, Bath, BA3 4DF
, ### Waterleaze, Maidenbrook, Taunton, Somerset, Somerset, TA7 8PS
You would get this...

, 120 Waterleaze, Maidenbrook, Taunton, Somerset, Somerset, TA7 8PS
Test, Ston Easton, Bath, Bath, BA8 4DF
The DID becomes a hidden value applicable to the radio button for that
address.

Does that make sense? The trouble with the two other suggestions is that the
DID column value doesn't actually become a usable field in the recordset
that I create in ASP.

Cheers, Ash
<ma******@hotmail.com> wrote in message
news:11**********************@v46g2000cwv.googlegr oups.com...
For the non-unique addresses, you will have a choice
of DeliveryIDs, which one do you want?

If you want just one of them, you can do this

SELECT MAX(DeliveryID), Address, Organisation, Organisation, Town,
County, PostCode
FROM SOL_Delivery
WHERE CustomerID = 15
GROUP BY Address, Organisation, Organisation, Town,
County, PostCode

Feb 27 '06 #7

As David has already mentioned, you would be
better off fixing this to prevent duplicates
appearing in the first place. Picking the
latest address using the highest DeliveryID
will probably work (assuming this is an identity),
but you are filling your table with redundant
data - not good in the long term.

Feb 27 '06 #8
@sh
I totally agree and by offering them the latest address that matches the
address to which they're sending, a new one wouldn't be inserted, I'd pass
that DID (DeliveryID) on for use with order.

Is there a way to get the DID into the query too?

Cheers, Ash
<ma******@hotmail.com> wrote in message
news:11**********************@v46g2000cwv.googlegr oups.com...

As David has already mentioned, you would be
better off fixing this to prevent duplicates
appearing in the first place. Picking the
latest address using the highest DeliveryID
will probably work (assuming this is an identity),
but you are filling your table with redundant
data - not good in the long term.

Feb 27 '06 #9

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

Similar topics

3
by: ben | last post by:
This is a PHP / MySQL kind of question. I am making a script which simply pulls information from a database and displays it on screen. BUT there will be entries where fields could be the same, and...
2
by: Chris DiTommaso | last post by:
I'm trying to query the pubs database for last title for each author. The query returns each title for each author. How can I operate on max of title? Here's what I'm trying: select distinct...
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...
7
by: Riley DeWiley | last post by:
I am continually amazed by SQL's ability to humble me .... I have a toy query into a toy database that looks just like this: SELECT . FROM f, fw, w WHERE f.id = fw.fid and fw.wid = w.id and...
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...
1
by: nfrodsham | last post by:
In Microsoft's help literature, it states: "You can filter out non-unique rows by using the DISTINCT option of an aggregate function" I am trying to do this in Access 2003 with the COUNT...
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,...
6
by: issac | last post by:
Hi folks Im trying to do a simple query involving the distinct keyword and an access 2000 db, but have been frittering with it for amost and hour and a half and I cant make it work. This is...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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:
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: 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...
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
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...

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.