ColdFusion Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Try It Free
Go Back   Dev Shed ForumsProgramming Languages - MoreColdFusion Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
  #1  
Old July 26th, 2004, 09:43 PM
markdavies1975 markdavies1975 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Location: Perth, Western Australia
Posts: 52 markdavies1975 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 14 m 52 sec
Reputation Power: 5
select distinct but return all records

hi there,

I have a database that has about 6 columns, such as firstName, familyName etc and also a column called ticketNumber. the unique id is called id. I want to select records using DISTINCT for the ticketNumber, but return all records including id. using DISTINCT on all records returns everything still because as id is always unique, every row is unique. i tried:

SELECT * FROM competition_entries WHERE ticketNumber IN (SELECT DISTINCT ticketNumber FROM competition_entries)

but that still select everything because it returns mutliple rows for each distinct ticketNumber is selects from the subquery!

any help would be great, thanks

mark.

Reply With Quote
  #2  
Old July 27th, 2004, 08:52 AM
bfolger71 bfolger71 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Location: Boston, MA
Posts: 47 bfolger71 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 34 sec
Reputation Power: 5
Hi Mark -

Sorry, but I'm having trouble understanding your goal.

You mention that you want to return all records, but then also mention that you want to only select DISTINCT on the ticketNumber field. These two cases are mutually exclusive, and cannot exist at the same time, as far as I know. This is because SELECT DISTINCT doesn't care what row the value is from, it's just returning common values as one result record. When you add the ID field to a SELECT DISTINCT, it (as you noted) makes each resulting record unique, so you'll get all records.

As far as I know, there's no way to do what you're explaining. Perhaps if you could wrap some context around what you're trying to do, one of the resident SQL experts here might be able to offer a better solution? Also, this is more of a SQL question than a CF one, so you might try posting on that board as well.

Hope that helps.

Brad

Reply With Quote
  #3  
Old July 27th, 2004, 11:12 AM
jordonbedwell jordonbedwell is offline
Ultra Geek!
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Posts: 294 jordonbedwell User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 52 m 19 sec
Reputation Power: 5
Wink

Ok if im correct all you are wanting to do is select records from a database that have a specific ticket number right?

You dont use the DISTINCT function for that, you would use <cfqueryparam>.

If this is comming from a form, you would do somthing like :

SELECT * FROM competition_entries WHERE ticketNumber LIKE (<cfqueryparam value="#form.value#">)

You could even drop the <cfquery> and just use standard sql,

SELECT * FROM competition_entries WHERE ticketnumber LIKE '#form.value#'

If this is comming from an array or list your going to have to use <cfqueryparam> and set it to list and use a dilimter of ,

You wont use IN because you are not selecting records in, you want to select records LIKE people often get them mixed up because they are quite similer.


PROGRAMMING TO PRINCE - - Party like its 1999
-------------------------------------------------------------
CFML PROGRAMMER (WWW.COLDFUSIONZONE.COM)

Reply With Quote
  #4  
Old July 27th, 2004, 06:05 PM
markdavies1975 markdavies1975 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Location: Perth, Western Australia
Posts: 52 markdavies1975 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 14 m 52 sec
Reputation Power: 5
Hi guys,

The thing is that i don't know what the ticket number is before I do my select. Basically there's a system where for each entry made by a customer of certain ticket types you get 4 competition chances, some 6 chances and others just the one, so there are 6 entries of almost identical data for some people and just one for others. I'm trying to get just the unique ticket numbers, but along with all the other data that goes with it. For example if i have these rows:

id firstName familyName ticketNumber
33 Joe Bloggs 1234
34 Joe Bloggs 1234
35 Bob Smith 9999
37 Some Guy 8888
38 Some Guy 8888
39 Some Guy 8888


i need to get 3 records returned, but with their id numbers, so i can sort them into a semblance of chronological order, but if i use distinct then the id numbers mean that every row is returned because SQL sees the id/ticketNumber combo as always being unique.

Cheers,

mark.

Reply With Quote
  #5  
Old July 27th, 2004, 08:10 PM
kiteless kiteless is offline
Moderator
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jun 2002
Location: Raleigh, NC
Posts: 3,626 kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 4 Days 10 h 6 m 34 sec
Reputation Power: 53
Quote:
Originally Posted by markdavies1975
SELECT * FROM competition_entries WHERE ticketNumber IN (SELECT DISTINCT ticketNumber FROM competition_entries)

but that still select everything...


I don't see how you could expect to get anything else. The subquery says "select all unique ticket numbers", and then the outer query says "select records from the table where the ticket number is present in this list: select all unique ticket numbers". How could you not expect get back every record?
__________________
Ask if you have a question, but also help answer questions that you have knowledge of! Thanks, Brian.
How to Post a Question in the Forums

Reply With Quote
  #6  
Old July 28th, 2004, 12:02 AM
markdavies1975 markdavies1975 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Location: Perth, Western Australia
Posts: 52 markdavies1975 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 14 m 52 sec
Reputation Power: 5
Quote:
Originally Posted by kiteless
I don't see how you could expect to get anything else. The subquery says "select all unique ticket numbers", and then the outer query says "select records from the table where the ticket number is present in this list: select all unique ticket numbers". How could you not expect get back every record?



yes, i know. i'd like to get back only the first record for each ticket number, obviously this SQL isn't going to do it, so i was hoping someone could help me out by showing me some SQL that did...

Reply With Quote
  #7  
Old July 28th, 2004, 08:32 AM
kiteless kiteless is offline
Moderator
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jun 2002
Location: Raleigh, NC
Posts: 3,626 kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 4 Days 10 h 6 m 34 sec
Reputation Power: 53
Why not just say:

select distinct ticket_no, name
from competition_entries

which would give each unique combination of ticket number and name?

Reply With Quote
  #8  
Old July 28th, 2004, 06:09 PM
markdavies1975 markdavies1975 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Location: Perth, Western Australia
Posts: 52 markdavies1975 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 14 m 52 sec
Reputation Power: 5
Quote:
Originally Posted by kiteless
Why not just say:

select distinct ticket_no, name
from competition_entries

which would give each unique combination of ticket number and name?


I'm hoping to get the Id field as well - but if i say:

select distinct id, ticket_no, name
from competition_entries

i get everything because the id is unique.

Reply With Quote
  #9  
Old July 28th, 2004, 08:13 PM
kiteless kiteless is offline
Moderator
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jun 2002
Location: Raleigh, NC
Posts: 3,626 kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 4 Days 10 h 6 m 34 sec
Reputation Power: 53
This just seems strange to me, but let me be sure I understand. You want all unique names and ticket numbers, and then just ONE of the id's? I don't see what good it would do for you to have just a random choice of id for that name and ticket number...

Reply With Quote
  #10  
Old July 28th, 2004, 08:36 PM
markdavies1975 markdavies1975 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Location: Perth, Western Australia
Posts: 52 markdavies1975 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 14 m 52 sec
Reputation Power: 5
Quote:
Originally Posted by kiteless
This just seems strange to me, but let me be sure I understand. You want all unique names and ticket numbers, and then just ONE of the id's? I don't see what good it would do for you to have just a random choice of id for that name and ticket number...



I need to get the ids because there was not timestamp added to the database and thats the only way i can get it into some kind of order - a fudge i know, but thats the only way i can do it... i thought that id be able to use:

SELECT DISTINCT ticket_no, name FROM competition_entries ORDER BY id

but that throws an error.

Reply With Quote
  #11  
Old July 29th, 2004, 08:56 AM
kiteless kiteless is offline
Moderator
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jun 2002
Location: Raleigh, NC
Posts: 3,626 kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 4 Days 10 h 6 m 34 sec
Reputation Power: 53
Could you not just order it by name?

Maybe it would help if you could post up a small "grid" of example data in the database, maybe for 2 or 3 names, and then show what you want to get in the results?

Reply With Quote
  #12  
Old August 11th, 2004, 02:58 PM
nindoo nindoo is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2004
Posts: 2 nindoo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Quote:
Originally Posted by markdavies1975
hi there,

I have a database that has about 6 columns, such as firstName, familyName etc and also a column called ticketNumber. the unique id is called id. I want to select records using DISTINCT for the ticketNumber, but return all records including id. using DISTINCT on all records returns everything still because as id is always unique, every row is unique. i tried:

SELECT * FROM competition_entries WHERE ticketNumber IN (SELECT DISTINCT ticketNumber FROM competition_entries)

but that still select everything because it returns mutliple rows for each distinct ticketNumber is selects from the subquery!

any help would be great, thanks

mark.


SELECT h1, MAX(h2), MAX(h3)
FROM Table1
GROUP BY h1

so h1 would be your ticket_number and h2 and so on would the rest of your attributes and Table1 would be competition_entries

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreColdFusion Development > select distinct but return all records


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |