Wrox Programmer Forums
|
SQL Server ASP Discussions about ASP programming with Microsoft's SQL Server. For more ASP forums, see the ASP forum category.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server ASP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 2nd, 2010, 09:27 AM
Authorized User
 
Join Date: Dec 2009
Posts: 85
Thanks: 16
Thanked 0 Times in 0 Posts
Default using DISTINCT

Hi there

I have a number of identical values in the rows of one of my tables in my database

How do I only pull one of those out using DISTINCT?

This is my SQL but it doesn't seem to be working - there's no error but it doesn't just pull out 1 record for each card, it returns the whole lot still.

SELECT DISTINCT tbl_WebserviceLog.cardNumber, tbl_Club.gym, tbl_Account.clubid, tbl_Account.Name, tbl_Account.Surname, tbl_WebserviceLog.LogID, tbl_WebserviceLog.AccountID, tbl_WebserviceLog.Dated, tbl_WebserviceLog.credit, tbl_WebserviceLog.Type, tbl_WebserviceLog.returnVal FROM tbl_WebserviceLog, tbl_Account, tbl_Club WHERE tbl_WebserviceLog.AccountID = '521' AND tbl_WebserviceLog.AccountID = tbl_Account.AccountID AND tbl_Club.gymid = tbl_Account.clubid GROUP BY tbl_WebserviceLog.cardNumber,tbl_Club.gym, tbl_Account.clubid, tbl_Account.Name, tbl_Account.Surname, tbl_WebserviceLog.LogID, tbl_WebserviceLog.AccountID, tbl_WebserviceLog.Dated, tbl_WebserviceLog.credit, tbl_WebserviceLog.Type, tbl_WebserviceLog.returnVal ORDER BY tbl_WebserviceLog.Dated

Any help greatly appreciated.

thanks

Adam
 
Old July 2nd, 2010, 07:48 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

SELECT DISTINCT will remove all ENTIRELY DUPLICATED rows. So if even ONE FIELD in your list of fields has even ONE CHARACTER difference between rows, then all rows will be returned.

Generally speaking, this means that if your SELECT list includes ID fields that are primary keys in any table except the first one, you *will* get duplicates.

Anyway, you should be able to just look carefully at the data being returned and find the (likely minor) differences yourself. If any field shows different values in different records, then you can *NOT* include that field in SELECT DISTINCT.

*************

Comment: Often, what you want isn't really SELECT DISTINCT. Often what you really want is the first (or last or...) record in a group where you get to select the characteristics of the group. If there is only one field that differs amongst the entire group, then instead of using SELECT DISTINCT, you can use MIN() or MAX() and GROUP BY. Example:
Code:
SELECT U.name, U.email, MAX(P.postingDate) AS lastPostingDate
FROM users AS U, postings AS P
WHERE U.userid = P.userid
GROUP BY U.name, U.email
But in more complex situations, you might not even be able to do that.

I won't bore you with methods you won't need, but if you think you need somethiing more advanced/complex, ask again.
The Following User Says Thank You to Old Pedant For This Useful Post:
adamhw (July 5th, 2010)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Using Distinct Smeghead SQL Server 2005 9 December 17th, 2007 11:10 AM
Distinct Eddywardo SQL Server 2005 1 September 2nd, 2006 07:41 AM
Distinct data arnabghosh Access 2 September 21st, 2005 10:46 AM
Distinct SELECT DISTINCT question... EndEffect Classic ASP Databases 4 August 18th, 2005 08:53 AM
Distinct or Group By? sprion Access 9 January 13th, 2005 09:31 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.