Home > Archive > Microsoft SQL Server forum > February 2006 > Simple SQL Distinct...!?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Simple SQL Distinct...!?
@sh

2006-02-28, 8:29 pm

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!


markc600@hotmail.com

2006-02-28, 8:29 pm

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

David Portas

2006-02-28, 8:29 pm

@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/ms130214(en-US,SQL.90).aspx
--

@sh

2006-02-28, 8:29 pm

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


Tom Moreau

2006-02-28, 8:29 pm

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!


@sh

2006-02-28, 8:29 pm

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
>



@sh

2006-02-28, 8:29 pm

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
>



markc600@hotmail.com

2006-02-28, 8:29 pm


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.

@sh

2006-02-28, 8:29 pm

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.
>



Sponsored Links





Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive | Programming forum archive

Copyright 2008 droptable.com