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

Select Distinct Keyword Problems...

Let's say i have a database with the following structure and data

tablename: customers

customerID| customername | PictureID|

1 | MyCustomer | 1.jpg |
1 | MyCustomer | 1_1.jpg |
1 | MyCustomer | 1_3.jpg |
2 | MyCustomer2 | 2.jpg |
3 | MyCustomer3 | 3.jpg |
3 | MyCustomer3 | 3_2.jpg |
4 | MyCustomer4 | 4_2.jpg |
4 | MyCustomer4 | 4_1.jpg |

Is it possible to pull back only one entry per customer? I don't care
which Picture ID it uses. I would perfer if the query would return the
topmost PictureID for a customer, but i don't really care.

desired output

customerID| customername | PictureID|

1 | MyCustomer | 1.jpg |
2 | MyCustomer2 | 2.jpg |
3 | MyCustomer3 | 3.jpg |
4 | MyCustomer4 | 4_2.jpg |
I have tried using the DISTINCT keyword, but it does not really help
me. my original thought was to use...

"Select Distinct CustomerID, Customername from Customers" but then i
don't have access to the PictureID? can i use a sub query?

Aug 26 '05 #1
7 1510
wirelessguy,

I don't know what you mean by "topmost", but you could
pull the first in alphabetical order:

select customerID, customername, min(PictureID)
from T
group by customerID, customername

This assumes that there is a 1-1 relationship
between customerID and customername.

In fact, you should put a unique constraint on
(customerID, customername) and store that information
separately. If you need to keep all these picture file
names, store them in a separate table, with customerID
as a foreign key.

If you have more columns, or if customername can vary
per customerID, use this:

select customerID, customername, PictureID
from T
where PictureID = (
select min(PictureID)
from T as Tcopy
where Tcopy.customerID = T.customerID
)

Steve Kass
Drew University
wirelessguy wrote:
Let's say i have a database with the following structure and data

tablename: customers

customerID| customername | PictureID|

1 | MyCustomer | 1.jpg |
1 | MyCustomer | 1_1.jpg |
1 | MyCustomer | 1_3.jpg |
2 | MyCustomer2 | 2.jpg |
3 | MyCustomer3 | 3.jpg |
3 | MyCustomer3 | 3_2.jpg |
4 | MyCustomer4 | 4_2.jpg |
4 | MyCustomer4 | 4_1.jpg |

Is it possible to pull back only one entry per customer? I don't care
which Picture ID it uses. I would perfer if the query would return the
topmost PictureID for a customer, but i don't really care.

desired output

customerID| customername | PictureID|

1 | MyCustomer | 1.jpg |
2 | MyCustomer2 | 2.jpg |
3 | MyCustomer3 | 3.jpg |
4 | MyCustomer4 | 4_2.jpg |
I have tried using the DISTINCT keyword, but it does not really help
me. my original thought was to use...

"Select Distinct CustomerID, Customername from Customers" but then i
don't have access to the PictureID? can i use a sub query?

Aug 26 '05 #2
thanks for the response. the "top most" entry refers to the first entry
for each customer in the table.

based on the informaiton provided, i would have to use the first
suggestion. However, does the min() funtction work with non numeric
values? my picture ID's are text.

Aug 26 '05 #3
wirelessguy (la****@hotmail.com) writes:
based on the informaiton provided, i would have to use the first
suggestion. However, does the min() funtction work with non numeric
values? my picture ID's are text.


Yes, MIN() works with varchar values. (I assume you don't mean the
data type text, because that would be a funny thing to use for a file
name.)
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp

Aug 26 '05 #4
There is no concept in SQL Server for top entry or first entry. Only
explict ordering based on the data. For first entry you wound need an
additional field describing the order in which the entries were made.

"wirelessguy" <la****@hotmail.com> wrote in message
news:11*********************@g47g2000cwa.googlegro ups.com...
thanks for the response. the "top most" entry refers to the first entry
for each customer in the table.

based on the informaiton provided, i would have to use the first
suggestion. However, does the min() funtction work with non numeric
values? my picture ID's are text.

Aug 26 '05 #5
thanks for the responses. I looks like i will have to store an
additional piece of information in order to retreive the info that i
need.

thanks for your responses.

Aug 26 '05 #6
d

Aug 28 '05 #7
I found a solution in an old forum. here is a simplified version of
what i end up using....

SELECT Customername, CustomerID, min(PictureID) as PictureID FROM
coupons
GROUP BY CustomerID, Customername

i just realized that this is very similir to what Steve reported
earlier. I'm not sure why that didn't work before for me. but it works
now.

thanks for all of you help!

Aug 28 '05 #8

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

Similar topics

4
by: 001 | last post by:
Hello, The select statement needs only 1 second to complete the query. But the update statement spends 30 minutes. Why? SELECT STATEMENT: declare @IDate smalldatetime select @IDate=col001...
3
by: Tcs | last post by:
My backend is DB2 on our AS/400. While I do HAVE DB2 PE for my PC, I haven't loaded it yet. I'm still using MS Access. And no, I don't believe this is an Access question. (But who knows? I...
1
by: Station Media | last post by:
Hi, here my problem, i use stored procedure in ACCESS database(latest version), and i would like to use this procedure but it doesnt work, do you know why ? Procedure: PARAMETERS MyField Text...
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...
2
by: Jim H | last post by:
I am storing incoming data in memory using a DataTable. After I'm done retrieving the data I need to get the distinct rows. I tried using DataTable.Select but that doesn't work. If I have...
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,...
2
by: Genalube | last post by:
I am running a query that includes the inclusion of memo fields, my query pulls from four different tables with one to many relationships between them. The relationships are such that when I run my...
3
by: chrisg | last post by:
Hi all. I'm really stuck with getting the right output from a XML query. Given this structure: create table GENCMP.SCRIPTS ( SCRIPT_ID CHAR(10) not null, PAGE_NO ...
2
omerbutt
by: omerbutt | last post by:
hi there i have to select 7 columns from table1, 1 column from table2, and 1 column from tables3 and show the result but i am not getting it right the main thing that i am trying to achieve is that i...
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...

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.