Author |
Topic |
scottichrosaviakosmos
Yak Posting Veteran
66 Posts |
Posted - 2010-09-21 : 03:43:54
|
name age empcpde mobnoA 1 1 111A 1 1 112A 2 1 111B 2 1 111B 1 1 121B 1 1 111C 1 1 112D 1 1 112D 2 2 111this is the table above .i want to achive 2 task: 1) result set where name ,age and empcode are same for more then once 2) where all the columns r same more then once these is a dumy table but in actual table this situation is posible .. hope u ll get the scenario.scoo |
|
pk_bohra
Master Smack Fu Yak Hacker
1182 Posts |
Posted - 2010-09-21 : 04:03:57
|
Is this you are looking for ?Select name,age,empcode,count(*) as NoOfRepeatationfrom yourtablegroup by name,age,empcodehaving count(*) >1Select name,age,empcode,mobno from yourtablegroup by name,age,empcode,mobnohaving count(*) >1Regards,BohraI am here to learn from Masters and help new bees in learning. |
 |
|
scottichrosaviakosmos
Yak Posting Veteran
66 Posts |
Posted - 2010-09-21 : 05:31:23
|
yes this is ok but i want those data which is same Eg: name and age and emp three same ... this three combine to make a unique data , and i want those data which is duplicate to combination of this combine three fields.Eg:name1 10 111name1 10 111scoo |
 |
|
kunal.mehta
Yak Posting Veteran
83 Posts |
Posted - 2010-09-21 : 06:47:31
|
DECLARE @T TABLE (name varchar(10),age int,empcode int,mobno int)INSERT @t VALUES('a',1,1,111)INSERT @t VALUES('a',1,1,112)INSERT @t VALUES('a',2,1,111)INSERT @t VALUES('b',2,1,111)INSERT @t VALUES('b',1,1,121)INSERT @t VALUES('b',1,1,111)INSERT @t VALUES('c',1,1,112)INSERT @t VALUES('d',1,1,112)INSERT @t VALUES('d',2,2,111)select * from @tselect name,age,empcode, COUNT(*) [Count] from @tgroup by name,age,empcodehaving count(*)>1select name,age,empcode,mobno, COUNT(*) [Count] from @tgroup by name,age,empcode,mobnohaving count(*)>1 |
 |
|
vaibhavktiwari83
Aged Yak Warrior
843 Posts |
Posted - 2010-09-21 : 07:39:46
|
Try this - I hope that I understood the requirement correctly. DECLARE @T TABLE (name varchar(10),age int,empcode int,mobno int)INSERT @t VALUES('a',1,1,111)INSERT @t VALUES('a',1,1,112)INSERT @t VALUES('a',2,1,111)INSERT @t VALUES('b',2,1,111)INSERT @t VALUES('b',1,1,121)INSERT @t VALUES('b',1,1,111)INSERT @t VALUES('c',1,1,112)INSERT @t VALUES('d',1,1,112)INSERT @t VALUES('d',2,2,111)select * from @t;WITH CTE AS ( SELECT Row_Number() OVER ( Partition BY Name, Age, EmpCode ORDER BY (SELECT Name) ) RowNo, Name, Age, empCode, mobNo FROM @t )SELECT A.Name, A.Age, A.EmpCode, A.MobNo FROM CTE AINNER JOIN (SELECT Name, Age, EmpCode, SUM(RowNo) SumRow FROM CTE GROUP BY Name, Age, EmpCode Having Sum(RowNo) > 1) B ON A.Name = B.Name AND A.Age = B.Age AND A.EmpCode = B.EmpCode Vaibhav TTo walk FAST walk ALONE To walk FAR walk TOGETHER |
 |
|
Sachin.Nand
2937 Posts |
Posted - 2010-09-21 : 07:59:23
|
Maybe thisDECLARE @T TABLE( name VARCHAR(10), age INT, empcode INT, mobno INT ) INSERT @t VALUES('a',1,1,111) INSERT @t VALUES('a',1,1,111) INSERT @t VALUES('a',1,1,111) INSERT @t VALUES('a',1,1,112) INSERT @t VALUES('a',1,1,112) INSERT @t VALUES('a',2,1,111) INSERT @t VALUES('b',2,1,111) INSERT @t VALUES('b',1,1,121) INSERT @t VALUES('b',1,1,111) INSERT @t VALUES('c',1,1,112) INSERT @t VALUES('d',1,1,112) INSERT @t VALUES('d',2,2,111) SELECT * FROM @t SELECT name, age, empcode, mobno, Count(rowid) [noof duplicates] FROM (SELECT *, Dense_rank() OVER(ORDER BY name, age, empcode, mobno) rowid FROM @T) t GROUP BY name, age, empcode, mobno, rowid HAVING Count(CASE WHEN rowid = 1 THEN 1 ELSE 0 END) >= 2 PBUH |
 |
|
|
|
|