Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Weird Distinct Output

0 views
Skip to first unread message

Michael Lugasy

unread,
Jan 16, 2002, 6:26:05 PM1/16/02
to
I want to select distinct Product_name from Product table,
and to order it using Product_promo (int) and the length of Product_name
BIGGER int on Product_promo AND shorter name means better product!

For some reason, the following query:

SELECT DISTINCT
Product_name,Product_title,Product_description,Product_promo
FROM Products WHERE Product_category=#Val(url.Category_id)#
ORDER by Product_promo DESC, len(Product_name)

doesn't give DISTINCT Product_name!

1. What am I doing wrong?
2. Is there a way also to limit the DISTINCT query to 10 records?

Michael.

oj

unread,
Jan 17, 2002, 3:24:01 AM1/17/02
to
michael,

1. distinct is applied to the entire row.
2. if you're on sql7/2k, you can use top...

SELECT top 10 DISTINCT


Product_name,Product_title,Product_description,Product_promo
FROM Products WHERE Product_category=#Val(url.Category_id)#
ORDER by Product_promo DESC, len(Product_name)

if you're on sql6x, then use rowcount

set rowcount 10


SELECT DISTINCT
Product_name,Product_title,Product_description,Product_promo
FROM Products WHERE Product_category=#Val(url.Category_id)#
ORDER by Product_promo DESC, len(Product_name)

set rowcount 0

--
-oj
http://rac4sql.home.attbi.com


"Michael Lugasy" <mic...@netvision.net.il> wrote in message
news:7aeb9057.02011...@posting.google.com...

Michael Lugasy

unread,
Jan 17, 2002, 5:45:06 AM1/17/02
to
I got incorrect syntax using your query, anyhow - I fixed it
to the following:

SELECT DISTINCT top 10
Product_name,Product_title,Product_description,Product_promo,len(Product
_name) AS Product_length
FROM Product WHERE category_id=80
ORDER by Product_promo DESC, Product_length

Still - I can't get DISTINCT Product_name.
I want rows which are distinct ONLY in Product_name
and not in the "whole row", but still - to have
all other fields (Product_title/description/promo...) selected. is it
possible?

Michael.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Dieter Buecherl

unread,
Jan 17, 2002, 6:38:30 AM1/17/02
to
It's not possible,
which row should the server select for you in the following example:

Product_name, Product_title, ....

Name A Title 1
Name A Title 2

The server cannot make the selection for you without the
required info, so maybe you want

select product_name, max(product_title), max(....
from product
...

or use min, avg, ...

or

select product_name, product_title, ...
from product
where product_title = (select max(product_title) from product)
and ...

if you want the product_name only once, then you must say which
row; it's simply the concept of relational database

HTH

Dieter

"Michael Lugasy" <mic...@netvision.net.il> schrieb im Newsbeitrag
news:3c46ab32$0$183$7586...@news.frii.com...

Michael Lugasy

unread,
Jan 17, 2002, 6:53:08 AM1/17/02
to
I need to select several rows that match the WHERE clause (category_id =
X), there are plenty - I only need the top 10 records, sorted by my
relevency.

You're saying that's ain't possible?

John Narofsky

unread,
Jan 17, 2002, 11:24:48 AM1/17/02
to
Question 2. Set ROWCOUNT 10
Question 1.
If you want the top distinct product names, use the query you have
below and remove the other columns from the list that you are
selecting. If you are getting non-distinct product names, its because
you have multiple rows for each column. You could pull some magic
with the other columns to arbitrarily get the first entry for each
product name(ask if you really want to know...its ugly) but I
recommend that you figure out a way to get to each row uniquely.

JohnNarofsky


mic...@netvision.net.il (Michael Lugasy) wrote in message news:<7aeb9057.02011...@posting.google.com>...

John Narofsky

unread,
Jan 17, 2002, 3:12:47 PM1/17/02
to
I guess my previous message didn't get through. Oh well.
Point 1. If you want more than just the product name coming back, you
run the risk of having more than just one row per product name coming
back. Distinct works on the entire row, bud.

Point 2. set rowcount is nice. Can't use top with the distinct
keyword.

JohnNarofsky

Michael Lugasy <mic...@netvision.net.il> wrote in message news:<3c46bb24$0$190$7586...@news.frii.com>...

Chad Gould

unread,
Jan 17, 2002, 3:35:55 PM1/17/02
to
Michael Lugasy <mic...@netvision.net.il> wrote in message news:<3c46bb24$0$190$7586...@news.frii.com>...
> I need to select several rows that match the WHERE clause (category_id =
> X), there are plenty - I only need the top 10 records, sorted by my
> relevency.
> You're saying that's ain't possible?
> Michael.

It should be possible using subqueries. Something like this:

SELECT DISTINCT
Product_name,Product_title,Product_description,Product_promo
FROM Products

WHERE Product_name In
(SELECT DISTINCT TOP 10 Product_name FROM Products WHERE Product_category=#Val
(url.Category_id)# ORDER by Product_promo DESC, len(Product_name))

0 new messages