| Author |
Topic |
|
dhani
Posting Yak Master
132 Posts |
Posted - 2009-09-10 : 15:29:11
|
| how to check, one record in one table occurs how many number of times in another table ?????Hello All,i have below two tables in my database, TempStudentTableStID,Name,Fee100,Mark,2500101,Alan,1500102,Stev,1000103,King,3000104,Kara,4000105,Limb,2500106,Trac,1500107,Kilda,3000OrigStudentTableStID,Name,Fee100,Mark,2500101,Alan,1500101,Alan,1500102,Stev,1000103,King,3000103,King,3000105,Limb,2500101,Alan,1500106,Trac,1500101,Alan,1500101,Alan,1500103,King,3000100,Mark,2500first table data needs to be check on OrigStudentTable data, to find out each tempStudentTable record has how many number of records it has in OrigStudentTableit needs to be match whole record with both tableany ideas,Please Kindly Help MeThanks in advanceBest RegardsDhani |
|
|
dhani
Posting Yak Master
132 Posts |
Posted - 2009-09-10 : 15:42:17
|
| Please any ideasThanks in advance!!!the result will beStID,Name,Fee,Occurs100,Mark,2500,2101,Alan,1500,5102,Stev,1000,1103,King,3000,3104,Kara,4000,0105,Limb,2500,0106,Trac,1500,1107,Kilda,3000,0PleaseThanks in advancemohini |
 |
|
|
cat_jesus
Aged Yak Warrior
547 Posts |
Posted - 2009-09-10 : 15:56:41
|
| select B.StID,b.Name,b.Fee, count(*)from TempStudentTable Ajoin OrigStudentTable B on A.StID = B.StID and A.Name = b.Name and A.Fee = b.Feegroup by B.StID,b.Name,b.FeeAn infinite universe is the ultimate cartesian product. |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2009-09-10 : 15:56:53
|
[code]declare @TempStudentTable table (StID int, [Name] varchar(255), Fee int)declare @OrigStudentTable table (StID int, [Name] varchar(255), Fee int)insert @TempStudentTableselect 100,'Mark',2500 union allselect 101,'Alan',1500 union allselect 102,'Stev',1000 union allselect 103,'King',3000 union allselect 104,'Kara',4000 union allselect 105,'Limb',2500 union allselect 106,'Trac',1500 union allselect 107,'Kilda',3000insert @OrigStudentTableselect 100,'Mark',2500 union allselect 101,'Alan',1500 union allselect 101,'Alan',1500 union allselect 102,'Stev',1000 union allselect 103,'King',3000 union allselect 103,'King',3000 union allselect 105,'Limb',2500 union allselect 101,'Alan',1500 union allselect 106,'Trac',1500 union allselect 101,'Alan',1500 union allselect 101,'Alan',1500 union allselect 103,'King',3000 union allselect 100,'Mark',2500select o.StID,o.[Name],o.Fee,count(*)from @OrigStudentTable owhere exists (select * from @TempStudentTable t where t.StId=o.StId and t.[Name] = o.[Name] and t.Fee = o.Fee)group by o.StID,o.[Name],o.Fee[/code] No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
dhani
Posting Yak Master
132 Posts |
Posted - 2009-09-10 : 16:17:56
|
| Hi Webfred,I Great ful to you Thank you very much jesus.,,,,,,,Best Regardsmohini |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2009-09-10 : 16:46:56
|
welcome  No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
cat_jesus
Aged Yak Warrior
547 Posts |
Posted - 2009-09-10 : 17:43:15
|
| Webfred's solution is more efficient with the test data. Generally, I stick with joins unless there is an overriding need to do something "fancy". An infinite universe is the ultimate cartesian product. |
 |
|
|
|
|
|