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 2005 Forums
 .NET Inside SQL Server (2005)
 SQL DISTINCT Problem

Author  Topic 

just.net
Starting Member

24 Posts

Posted - 2009-09-11 : 12:26:05
Hello,

How can I remove duplicate rows in my query?
But the rows are not exactly the same, there are 2 different fields:
One is Datetime(same date different time) and the another is int(different number), the other are exactly the same.
the part of the query:
SELECT ID(different), RsvTime(different), RsvNumber(the same),
price(the same), ammount(the same), spCodeTT(the same)

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2009-09-11 : 13:32:17
Do you care which ID and which RsvTime you get back?

You could group by the other columns and take the min or max of ID and RsvTime if you don't mind them possibly being from different rows. or you could use cross apply to get the value from the same row:

SELECT max(ID) as ID
,max(RsvTime) as RsvTime
,RsvNumber
,price
,ammount
,spCodeTT
from <yourTable>
group by RsvNumber
,price
,ammount
,spCodeTT


SELECT max(ca.ID) as ID
,max(ca.RsvTime) as RsvTime
,t.RsvNumber
,t.price
,t.ammount
,t.spCodeTT
from <yourTable> t
cross apply (
select top 1 ID, RsvTime
from <yourTable>
where RsvNumber = t.rsvNumber
and price = t.price
and amount = t.amount
and spCodeTT = t.spCodeTT
order by ID desc
) ca
group by t.RsvNumber
,t.price
,t.ammount
,t.spCodeTT


Be One with the Optimizer
TG
Go to Top of Page

just.net
Starting Member

24 Posts

Posted - 2009-09-11 : 18:18:55
I figure it out right after i wrote the message(except the cross part). but now i will learn the new thing(cross apply)

Thank tou
Go to Top of Page

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2009-09-11 : 18:54:40
quote:
Originally posted by just.net

I figure it out right after i wrote the message(except the cross part). but now i will learn the new thing(cross apply)

Thank tou



Good for you!

Be One with the Optimizer
TG
Go to Top of Page
   

- Advertisement -