Drop Table

Support Forum for database administrators and web based access to important newsgroups related to databases
Register on Database Support Forum Edit your profileCalendarFind other Database Support forum membersFrequently Asked QuestionsSearch this forum -> 
For Database admins: Free Database-related Magazines Now Free shipping to Texas


Post New Thread










Thread
Author

Simple SQL Distinct...!?
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!



Report this thread to moderator Post Follow-up to this message
Old Post
@sh
03-01-06 01:29 AM


Re: Simple SQL Distinct...!?
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


Report this thread to moderator Post Follow-up to this message
Old Post
markc600@hotmail.com
03-01-06 01:29 AM


Re: Simple SQL Distinct...!?
@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 exact
ly
> 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/ms130214(en-US,SQL.90).aspx
--


Report this thread to moderator Post Follow-up to this message
Old Post
David Portas
03-01-06 01:29 AM


Re: Simple SQL Distinct...!?
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



Report this thread to moderator Post Follow-up to this message
Old Post
@sh
03-01-06 01:29 AM


Re: Simple SQL Distinct...!?
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" <spam@spam.com> wrote in message
 news:dtv2c2$mhe$1@nw
rdmz01.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!



Report this thread to moderator Post Follow-up to this message
Old Post
Tom Moreau
03-01-06 01:29 AM


Re: Simple SQL Distinct...!?
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" <spam@spam.com> wrote in message
 news:dtv3r4$at1$1@nw
rdmz02.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
>



Report this thread to moderator Post Follow-up to this message
Old Post
@sh
03-01-06 01:29 AM


Re: Simple SQL Distinct...!?
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


<markc600@hotmail.com> wrote in message
news:1141051443.398579.176470@v46g2000cwv.googlegroups.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
>



Report this thread to moderator Post Follow-up to this message
Old Post
@sh
03-01-06 01:29 AM


Re: Simple SQL Distinct...!?
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.


Report this thread to moderator Post Follow-up to this message
Old Post
markc600@hotmail.com
03-01-06 01:29 AM


Re: Simple SQL Distinct...!?
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


<markc600@hotmail.com> wrote in message
news:1141055631.655033.148880@v46g2000cwv.googlegroups.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.
>



Report this thread to moderator Post Follow-up to this message
Old Post
@sh
03-01-06 01:29 AM


Sponsored Links





Last Thread Next Thread
Post New Thread

Microsoft SQL Server forum archive

Show a Printable Version Email This Page to Someone! Receive updates to this thread
Microsoft SQL Server
Access database support
PostgreSQL Replication
SQL Server ODBC
FoxPro Support
PostgreSQL pgAdmin
SQL Server Clustering
MySQL ODBC
Web Applications with dBASE
SQL Server CE
MySQL++
Sybase Database Support
MS SQL Full Text Search
PostgreSQL Administration
SQL Anywhere support
DB2 UDB Database
Paradox Database Support
Filemaker Database
Berkley DB
SQL 2000/2000i database
ASE Database
Forum Jump:
All times are GMT. The time now is 08:21 AM.

 
Mobile devices forum | Database support forum archive




Copyrights DropTable.com Database Support Forum 2004 - 2006