all groups > sql server programming > july 2003 >
You're in the

sql server programming

group:

Query Required


Query Required Satish
7/16/2003 11:15:44 PM
sql server programming:
I have posted the same question, but since it did not
appear in the thread, I am reposting it.

Hello All,

Thanks for your previous query feedback, I wanted one more
help of such kind


SkillValue_ID EmployeeNo SkillType
------------- --------------- -----------
3 C_0014 3
6 C_0014 1
9 C_0014 2
1 QI_0509 1
2 QI_0509 2
7 QI_0509 1
10 QI_0509 2
12 QI_0509 3
13 QI_0509 3
2 QU_0087 2
3 QU_0087 3
8 QU_0087 1


I wanted a query which returns all EmployeeNo who satisfy
the SkillValue_ID condition like this

(1 or 10 or 6) AND (2 or 9) AND (3 or 8 or 13)



i.e. any of the first criteria AND any of the second
criteria AND any of the third criteria

So the output employeeno in this case should be

C_0014 and
QI_0509






Query Required Arun[Symbiosis]
7/16/2003 11:45:32 PM

You can use

select employee_id from table where (skillvalue_id in
(1,10,6)) and (skillvalue_id in(2,9))and (skillvalue_id in
(3,8,13))



[quoted text, click to view]
Re: Query Required Dean Savovic
7/17/2003 8:23:21 AM
Try reposting it with DDL and sample data!!!!!!!!!!!1

--
Dean Savovic
www.teched.hr


[quoted text, click to view]

Re: Query Required Joe Celko
7/17/2003 9:51:42 AM
You can translate Boolean expressions into pure SQL for elaborate
searches without using dynamic SQL. For example, given a table of job
candidates and their skills, you might write a query like this:

skill = Java AND (skill = Perl OR skill = PHP)

now you need to put it in the disjunctive canonical form. In English,
for the non-computer science majors, that mean a string of AND-ed groups
connected by OR operators like this:

(Java AND Perl) OR (Java AND PHP)

which we load into this table:

CREATE TABLE SkillQuery
(and_grp INTEGER NOT NULL,
skill CHAR(4) NOT NULL,
PRIMARY KEY (and_grp, skill));

INSERT INTO SkillQuery VALUES (1, 'Java');
INSERT INTO SkillQuery VALUES (1, 'Perl');
INSERT INTO SkillQuery VALUES (2, 'Java');
INSERT INTO SkillQuery VALUES (2, 'PHP');

Assume we have a table of job candidates:

CREATE TABLE Candidates
(candidate_name CHAR(10) NOT NULL,
skill CHAR(4) NOT NULL,
PRIMARY KEY (candidate_name, skill));

INSERT INTO Candidates VALUES ('John', 'Java'); --winner
INSERT INTO Candidates VALUES ('John', 'Perl');
INSERT INTO Candidates VALUES ('Mary', 'Java'); --winner
INSERT INTO Candidates VALUES ('Mary', 'PHP');
INSERT INTO Candidates VALUES ('Larry', 'Perl'); --winner
INSERT INTO Candidates VALUES ('Larry', 'PHP');
INSERT INTO Candidates VALUES ('Moe', 'Perl'); --winner
INSERT INTO Candidates VALUES ('Moe', 'PHP');
INSERT INTO Candidates VALUES ('Moe', 'Java');
INSERT INTO Candidates VALUES ('Celko', 'Java'); -- loser
INSERT INTO Candidates VALUES ('Celko', 'Algol');
INSERT INTO Candidates VALUES ('Smith', 'APL'); -- loser
INSERT INTO Candidates VALUES ('Smith', 'Algol');

The query is simple now:

SELECT DISTINCT C1.candidate_name
FROM Candidates AS C1, SkillQuery AS Q1
WHERE C1.skill = Q1.skill
GROUP BY Q1.and_grp, C1.candidate_name
HAVING COUNT(C1.skill)
= (SELECT COUNT(*) FROM SkillQuery AS Q2 WHERE Q1.and_grp =
Q2.and_grp);

You can retain the COUNT() information to rank candidates. For example
Moe meets both qualifications, while other candidates meet only one of
the two.

This query can be generalized for any number of columns. A UNION on
queries for each of the possible columns will be faster than putting it
all into a monzster query.

--CELKO--
===========================
Please post DDL, so that people do not have to guess what the keys,
constraints, Declarative Referential Integrity, datatypes, etc. in your
schema are.

*** Sent via Developersdex http://www.developersdex.com ***
Re: Query Required SriSamp
7/17/2003 11:59:56 AM
Here is a slight round-about solution.
SELECT DISTINCT EmployeeNo FROM testTable WHERE
EmployeeNo IN (SELECT EmployeeNo FROM testTable WHERE (SkillValue_ID = 1
OR SkillValue_ID = 10 OR SkillValue_ID = 6)) AND
EmployeeNo IN (SELECT EmployeeNo FROM testTable WHERE (SkillValue_ID = 2
OR SkillValue_ID = 9)) AND
EmployeeNo IN (SELECT EmployeeNo FROM testTable WHERE (SkillValue_ID = 3
OR SkillValue_ID = 8 OR SkillValue_ID = 13))
--
HTH,
SriSamp
Please reply to the whole group only!
http://www32.brinkster.com/srisamp

[quoted text, click to view]

Re: Query Required SriSamp
7/17/2003 12:35:31 PM
Your query will not work. Check it out in Query Analyzer. No rows will be
returned.
--
HTH,
SriSamp
Please reply to the whole group only!
http://www32.brinkster.com/srisamp

[quoted text, click to view]

AddThis Social Bookmark Button