SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


how to avoid duplictae records of column in a table in a select query in this proc?


how to avoid duplictae records of column in a table in a select query in this proc?

Author
Message
Sivaganesh Tamilvendhan
Sivaganesh Tamilvendhan
Old Hand
Old Hand (320 reputation)Old Hand (320 reputation)Old Hand (320 reputation)Old Hand (320 reputation)Old Hand (320 reputation)Old Hand (320 reputation)Old Hand (320 reputation)Old Hand (320 reputation)

Group: General Forum Members
Points: 320 Visits: 1399
here iam having two table @companytable and @boardtable in the @boardtable table will have foreign key of companyid from @companytable and i just want to avoid duplicate record of company name repeated


DECLARE @companytable table
(
companyid int,
companyname varchar(200),
countrycode varchar(3),
city varchar(30),
isactive int
)


insert into @companytable
select 1,'s1','US','California',1 union all
select 2,'s2','in','Chennai',1 union all
select 3,'s1','in','Chennai',1 union all
select 4,'s6','in','bangolre',1 union all
select 5,'s5','uk','london',1 union all
select 6,'s1','uk','london',1 union all
select 7,'s5','us','newyork',1


DECLARE @boardtable table
(
boardid int identity(1,1),
companyid int ,
createddate datetime,
isactive int
)

insert into @boardtable
select 1,'2012-11-01 12:34:49.860',1 union all
select 2,'2012-11-02 12:34:49.860',1 union all
select 3,'2012-11-04 12:34:49.860',1 union all
select 4,'2012-11-06 12:34:49.858',1 union all
select 5,'2012-11-09 11:34:49.859',1 union all
select 6,'2012-11-09 11:34:49.860',1 union all
select 7,'2012-11-09 12:34:49.861',1


here just trying a select proc by joining this two table and select top 4 oder by the created date desc

select top 4 r.boardid,r.companyid,c.companyname,c.countrycode,c.city
from
@companytable c
join
@boardtable r
on c.companyid=r.companyid
order by createddate desc


but it provinding out put like this

boardid companyid companyname countrycode city
7 7 s5 us newyork
5 5 s5 uk london
6 6 s1 uk london
4 4 s6 in bangolre






but iam trying for the out putlike this

boardid companyid companyname countrycode city
7 7 s5 us newyork
6 6 s1 uk london
4 4 s6 in bangolre
2 2 s2 in Chennai



vinu512
vinu512
SSCommitted
SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)

Group: General Forum Members
Points: 1697 Visits: 1625
You can do it as follows:


--Query for your requirement

Select boardid, companyid, companyname, countrycode, city From
(
Select b.boardid, b.companyid, a.companyname, a.countrycode, a.city ,ROW_NUMBER() Over (Partition By a.CompanyName Order by b.createddate DESC) As rn
From @companytable as a
JOIN @boardtable as b On a.companyid = b.companyid
) As p
Where rn = 1
Order by boardid DESC



The query gets you the data that you want. But, you did not mention the logic which was behind your requirement. The query may need some change if your logic is different from what i used.

From next time please post the logic behind the requirement as well so that it becomes easier to deduce a solution.

Vinu Vijayan

For better and faster solutions please check..."How to post data/code on a forum to get the best help" - Jeff Moden ;-)
Sivaganesh Tamilvendhan
Sivaganesh Tamilvendhan
Old Hand
Old Hand (320 reputation)Old Hand (320 reputation)Old Hand (320 reputation)Old Hand (320 reputation)Old Hand (320 reputation)Old Hand (320 reputation)Old Hand (320 reputation)Old Hand (320 reputation)

Group: General Forum Members
Points: 320 Visits: 1399
hi
vinu512

thanks for the solution which u just posted for me
aaron.reese
aaron.reese
Ten Centuries
Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)

Group: General Forum Members
Points: 1047 Visits: 902
As company S1 has offices in UK,US and IN. how is SQL supposed to know that you want the US office instead of one of the others.

Unless you specify an ORDER BY clause, you cannot guarantee that the data will always be returned in the same sequence so the results of the query may change even if the data does not.
vinu512
vinu512
SSCommitted
SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)

Group: General Forum Members
Points: 1697 Visits: 1625
sivajii (11/15/2012)
hi
vinu512

thanks for the solution which u just posted for me


You're Welcome. Glad it was halpfull for you.

Vinu Vijayan

For better and faster solutions please check..."How to post data/code on a forum to get the best help" - Jeff Moden ;-)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search