Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

how to avoid duplictae records of column in a table in a select query in this proc? Expand / Collapse
Author
Message
Posted Friday, November 9, 2012 12:58 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Monday, October 13, 2014 1:11 AM
Points: 212, Visits: 1,385
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


Post #1382872
Posted Friday, November 9, 2012 1:52 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Tuesday, October 28, 2014 6:04 AM
Points: 1,127, Visits: 1,599
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
Post #1382879
Posted Thursday, November 15, 2012 9:48 PM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Monday, October 13, 2014 1:11 AM
Points: 212, Visits: 1,385
hi
vinu512

thanks for the solution which u just posted for me
Post #1385454
Posted Friday, November 16, 2012 3:47 AM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Thursday, December 18, 2014 12:16 AM
Points: 461, Visits: 693
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.

Post #1385585
Posted Monday, November 19, 2012 4:44 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Tuesday, October 28, 2014 6:04 AM
Points: 1,127, Visits: 1,599
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
Post #1386302
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse