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 2000 Forums
 Transact-SQL (2000)
 Match and count with a given record

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2004-05-05 : 14:56:10
Ajay writes "Hello (*)
I have a table named UserMast.
There are 10 fields in the table. (one of them is the primary key).

All the fields are inserted through a select-option from an asp.net page.

One of the user say x login and wants to find matches to his own record.

The resultant select query must fetch all the records from the database that matches user x's inputs (at least one) and also a count for each record on how many inputs matched.

A stored procedure will be much helpful here.


Thanks
Ajay"

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2004-05-05 : 15:11:39
Could you provide the table DDL and some sample data and expected result set?

Tara
Go to Top of Page

askajay
Starting Member

2 Posts

Posted - 2004-05-06 : 00:55:36
Hello Tara and others
I provide here with some information as you asked for.
The table UserMast contains fields like :
ID, FName, LName, Age(Years), Sex, Race and few other fields.

Records:
ID Fname Lname Age Sex Income
1 ram shah 25 M 20000
2 rahim shukla 35 M 10000
3 viral desai 23 M 20000
4 tara jaiswal 26 F 25000
5 manu shah 22 M 10000
6 viral shah 26 M 20000
7 ram shah 23 M 20000
8 sumi shah 25 F 20000
9 imli patel 27 - 25000

User "ram" (with id = 1) login and want to search that matches his profile.

This must be the output for the search:
ID Fname Lname Age Sex Income Match
2 rahim shukla 35 M 10000 1
3 viral desai 23 M 20000 2
4 tara jaiswal 25 F 25000 1
5 manu shah 22 M 10000 2
6 viral shah 26 M 20000 3
7 ram shah 23 M 20000 4
8 sumi shah 25 F 20000 3

The record with id = 9 is not included coz not a single parameter matched. The last column "Match" is the result of how many parameters matched with "ram's" profile.
As I mentioned a stored procedure would be better for this. If theres any other possible way please let me know.

Ajay
Go to Top of Page

xpandre
Posting Yak Master

212 Posts

Posted - 2004-05-06 : 08:11:04
You can try this...

create table t6
(id int,
fname varchar(40),
lname varchar(40),
age int,
sex char(1),
income int
)


insert into t6 values (1,'ram','shah',25,'M',20000)
insert into t6 values (2,'rahim','shukla',35,'M',10000)
insert into t6 values (3,'viral','desai',23,'M',20000)
insert into t6 values (4,'tara','jaiswal',26,'F',25000)
insert into t6 values (5,'manu','shah',22,'M',10000)
insert into t6 values (6,'viral','shah',26,'M',20000)
insert into t6 values (7,'ram','shah',23,'M',20000)
insert into t6 values (8,'sumi','shah',25,'F',20000)
insert into t6 values (9,'imli','patel',27,'M',25000)
insert into t6 values (10,'imli','patel',27,'F',25000)



select id,fname,lname,sex,age,income,
case
when fname=(select fname from t6 where id=1) then 1 else 0 end +
case
when lname=(select lname from t6 where id=1) then 1 else 0 end +
case
when age=(select age from t6 where id=1) then 1 else 0 end +
case
when sex=(select sex from t6 where id=1) then 1 else 0 end +
case
when income=(select income from t6 where id=1) then 1 else 0 end Match

from t6
where ( fname like (select fname from t6 where id=1) or
lname like (select lname from t6 where id=1) or
age=(select age from t6 where id=1) or
sex=(select sex from t6 where id=1) or
income=(select income from t6 where id=1)
)
and id!=1
order by id

Go to Top of Page

askajay
Starting Member

2 Posts

Posted - 2004-05-07 : 23:50:32
Hello xpandre and tduggan,
Thanks for showing interest to my post and replying to it.
xpandre, the query given by you has done my work and has helped me a lot. I wish good luck to all of you and the sqlteam.

Thanks again...
Ajay Shukla
Go to Top of Page
   

- Advertisement -