Home All Groups Group Topic Archive Search About

Select all Rows on distinct only on one column

Author
8 Jun 2006 1:42 AM
jason.teen
Hi,

I've been racking my brain for days on how to select all the details on
a distinct field of only on one column.

For Eg.

TableOne
------------------------------
ProdID  | Description
------------------------------
1         | Toy
1         | Toys
1         |  Xmas Toys
2         | Lollies
2         | Candy


TableTwo
--------------------------------
ProdID   |   Price
--------------------------------
1           |   $25
2           |   $90


Result
----------------------------------------------------
ProdID   | Description      |     Price
----------------------------------------------------
1           |   Toys             | $25
2           |   Lollies           | $90



Above is ultimately what I want the join to become

1) "Group" all the items from Table 1, if they have the same product ID
(even if the description is not the exact same) consider them the same
item

2) Join that distinct list of products from Table 1 with the pricing
from Table 2 and display the final output.


If anyone has an idea, please post....

Thanks

Author
8 Jun 2006 1:47 AM
Aaron Bertrand [SQL Server MVP]
> 1           |   Toys             | $25

How did you come up with this result? Why "Toys" and not "Toy" or "Xmas
Toys"?
Author
8 Jun 2006 1:55 AM
jason.teen
Well actaully you are right. I dont actually care which description is
there, becuase I will be able to interpret them after I get ther result

In that example I just put the first one listed there thats all


Aaron Bertrand [SQL Server MVP] wrote:
Show quote
> > 1           |   Toys             | $25
>
> How did you come up with this result? Why "Toys" and not "Toy" or "Xmas
> Toys"?
Author
8 Jun 2006 2:09 AM
Stu
If you don't care, then do something simple like:

SELECT a.ProdID, a.ProdDescription, b.Price
FROM (SELECT ProdID, MIN(ProdDescription) as ProdDescription
            FROM Table1
            GROUP BY ProdID) a JOIN Table2 b ON a.ProdID = b.ProdID
ORDER BY a.ProdID

HTH,
Stuj

ason.t***@gmail.com wrote:
Show quote
> Well actaully you are right. I dont actually care which description is
> there, becuase I will be able to interpret them after I get ther result
>
> In that example I just put the first one listed there thats all
>
>
> Aaron Bertrand [SQL Server MVP] wrote:
> > > 1           |   Toys             | $25
> >
> > How did you come up with this result? Why "Toys" and not "Toy" or "Xmas
> > Toys"?
Author
8 Jun 2006 2:47 AM
Sylvain Lafontaine
Another way would be to redesign the schema to add another table for the
alternate descriptions.  Might seem something that will be more complicated
than your present schema but in fact, this will give much more simpler
queries.

--
Sylvain Lafontaine, ing.
MVP - Technologies Virtual-PC
E-mail: http://cerbermail.com/?QugbLEWINF


Show quote
"Stu" <stuart.ainswo***@gmail.com> wrote in message
news:1149732590.909173.205850@u72g2000cwu.googlegroups.com...
>
> If you don't care, then do something simple like:
>
> SELECT a.ProdID, a.ProdDescription, b.Price
> FROM (SELECT ProdID, MIN(ProdDescription) as ProdDescription
>            FROM Table1
>            GROUP BY ProdID) a JOIN Table2 b ON a.ProdID = b.ProdID
> ORDER BY a.ProdID
>
> HTH,
> Stuj
>
> ason.t***@gmail.com wrote:
>> Well actaully you are right. I dont actually care which description is
>> there, becuase I will be able to interpret them after I get ther result
>>
>> In that example I just put the first one listed there thats all
>>
>>
>> Aaron Bertrand [SQL Server MVP] wrote:
>> > > 1           |   Toys             | $25
>> >
>> > How did you come up with this result? Why "Toys" and not "Toy" or "Xmas
>> > Toys"?
>

AddThis Social Bookmark Button