Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Distinct wont work

Author  Topic 

Johnhamman
Starting Member

37 Posts

Posted - 2002-09-27 : 17:44:26
Hi all, Does anyone know why this works:
Select Distinct cID  from #temptable

I get this;
cID
21
22
23
24
But when I do this:
Select Distinct cID,Name from #temptable

I get this
cID Name
21 test1
22 test2
22 test3
23 test4
24 test5

I doesn't select just one of '22' cID? Does anyone have any Idea why this is happening?

-John

robvolk
Most Valuable Yak

15732 Posts

Posted - 2002-09-27 : 17:52:59
If you include two columns, DISTINCT will give you every distinct combination of those two columns. What you see is normal behavior.

Go to Top of Page

Johnhamman
Starting Member

37 Posts

Posted - 2002-09-27 : 17:59:31
crumb. I want to select multiple colums but only one from a single distinct random cID.
For example
cId Name
21 test1
22 test3
23 test4
24 test5


Any Idea on how to do that?


Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2002-09-27 : 18:05:11
SELECT CID, Max(Name) FROM #temptable GROUP BY CID

Go to Top of Page

Johnhamman
Starting Member

37 Posts

Posted - 2002-09-27 : 19:10:12
Great! thanks. Maybe I should go look at the sql Documentation! any ideas on how to make it more random?

Go to Top of Page

Johnhamman
Starting Member

37 Posts

Posted - 2002-09-27 : 19:15:12
This is my code:
Set @groupID = 15

--Declare @groupID int = 14
Declare @group int



Select @group = isnull(ParentID,0) from V020U18NUC_AGC.Groups
where ParentID = @groupID

If (@group = 0) -- there is no SubCats
Begin
Select * from V020U18NUC_AGC.Products
where cID = @groupID
End
Else -- there is subcats
Begin
--create TempTable

CREATE TABLE #TempTable
(
pID int IDENTITY PRIMARY KEY,
cID int,
Name varchar(50),
short varchar (300),
long varchar(4000),
man varchar(50),
Made datetime,
price money,
Weight int,
qty int,
Thumb varchar(30),
Pic varchar(30)
)
SET IDENTITY_INSERT #temptable ON

INSERT into #temptable
(
pId,
cID,
name,
Short,Long,Man,Made,Price,Weight,qty,thumb,pic
)
SELECT Products.pID, Products.cID, Products.Name, Products.Short, Products.Long, Products.Man, Products.Made, Products.Price, Products.Weight, Products.Qty, Products.Thumb, Products.Pic
FROM V020U18NUC_AGC.Groups INNER JOIN V020U18NUC_AGC.Products ON Groups.GroupID = Products.cID
WHERE (((Groups.parentID)=@groupID))order by newid()

--get all subcats
-- select random product from each subcat
--fill temptable


end

Select cID, Max(Name) from #temptable GROUP BY CID
Drop Table #TempTable


Go to Top of Page
   

- Advertisement -