I am trying to run an SQL query which would fetch me all people who are
1. Only employees,
2. Employees and a sales person and
3. Only sales persons.
I am working on the Oracle E-Business Suite. So far, my query returns only those people who are employees only and those people who are employees and also a sales person. Here is what I've managed so far:
select distinct PAF.LAST_NAME,
PAF.START_DATE "HIRE_DATE",
PAF.EMPLOYEE_NUMBER,
PPT.SYSTEM_PERSON_TYPE "PERSON_TYPE",
JRS.SALES_CREDIT_TYPE_ID,
JRS.SALESREP_NUMBER
from PER_ALL_PEOPLE_F PAF,
PER_PERSON_TYPES PPT,
PER_PERSON_TYPE_USAGES_F PPTU,
JTF_RS_DEFRESOURCES_VL JRDV,
JTF_RS_SALESREPS JRS
where PAF.PERSON_ID = PPTU.PERSON_ID
and PPTU.PERSON_TYPE_ID = PPT.PERSON_TYPE_ID
and PPT.SYSTEM_PERSON_TYPE in ('EMP','OTHER')
and JRDV.category in ('EMPLOYEE','OTHER')
and (JRS.SALESREP_NUMBER(+) = PAF.EMPLOYEE_NUMBER)
and sysdate between PAF.EFFECTIVE_START_DATE and PAF.EFFECTIVE_END_DATE;
This is what I want to achieve
I have to include those people who are ONLY salespersons. Basically, there should be some rows which have no Employee_Number but only SALESREP_NUMBER. What am I doing wrong?
Pretty sure your issue lies here:
AND PAF.EMPLOYEE_NUMBER = JRS.SALESREP_NUMBER
Like the comment above says, you don't give much info. But, an educated guess would be that the equivalence indicated above would make that person an employee AND a salesperson. Maybe something more like:
AND (
PAF.EMPLOYEE_NUMBER = JRS.SALESREP_NUMBER
OR
( PAF.EMPLOYEE_NUMBER AND JRS.SALESREP_NUMBER IS NULL)
OR
( PAF.EMPLOYEE_NUMBER IS NULL AND JRS.SALESREP_NUMBER)
)
Or maybe just delete that clause?
Related
Ok, hopefully I can explain this accurately. I work in SQL Server, and I am trying to get one row from a table that will show multiple rows for the same person for various reasons.
There is a column called college_attend which will show either New or Cont for each student.
My issue: my initial query narrows down the rows I'm pulling by Academic Year, which consists of two semesters: Fall of one year, and Spring of the following to create an academic year. This is why there are two rows returned for some students.
Basically, I need to generate an accurate count of those that are "New" and those that are "Cont", but I don't want both records for the same student counted. They will have two records because they will have one for spring and one for fall (usually). So if a student is "New" in fall, they will have a "Cont" record for spring. I want the query to show ONLY the "New" record if they have both a "New' and "Cont" record, and count it (which I will do in Report Builder). The other students will basically have two records that are "Cont": one for fall, and one "Cont" for spring, and so those would be considered the continuing ones or "Cont".
Here is the basic query I have so far:
SELECT DISTINCT
people.people_id,
people.last_name,
people.first_name,
academic.college_attend AS NewORCont,
academic.academic_year,
academic.academic_term,
FROM
academic
INNER JOIN
people ON people.people_id = academic.people_id
INNER JOIN
academiccalendar acc ON acc.academic_year = academic.academic_year
AND acc.academic_term = academic.academic_term
AND acc.true_academic_year = #Academic_year
I'm not sure if this can be done with a CASE statement? I thought of a GROUP BY, but then SQL Server will want me to add all of my columns to the GROUP BY clause, and that ends up negating the purpose of the grouping in the first place.
Just a sample of what I work with for each student:
People ID
Last
First
NeworCont
12345
Soanso
Guy
New
12345
Soanso
Guy
Cont
32345
Person
Nancy
Cont
32345
Person
Nancy
Cont
55555
Smith
John
New
55555
Smith
John
Cont
---------
------
-------
----------
Hopefully this sheds some light on the duplicate record issue I mentioned.
Without sample data its awkward to visualize the problem, and without the expected results specified it's also unclear what you want as the outcome. Perhaps this will assist, it will limit the results to only those who have both 'New' and 'Cont' in a single "true academic year" but the count seems redundant as this (I imagine) will always be 2 (being 1 New term and 1 Cont term)
SELECT
people.people_id
, people.last_name
, people.first_name
, acc.true_academic_year
, count(*) AS count_of
FROM academic
INNER JOIN people ON people.people_id = academic.people_id
INNER JOIN academiccalendar acc ON acc.academic_year = academic.academic_year
AND acc.academic_term = academic.academic_term
AND acc.true_academic_year = #Academic_year
GROUP BY
people.people_id
, people.last_name
, people.first_name
, acc.true_academic_year
HAVING MAX(academic.college_attend) = 'New'
AND MIN(academic.college_attend) = 'Cont'
I seem to be having problem getting a certain query to work. I know I'm so close. Here's a copy of my er diagram
I think I am so close to achieving what I want to do with this code, only I get invalid identifier when trying to run it. I think its because the practice is being changed somehow after joining, as I am only getting invalid identifier on row 5?
SELECT staffid, staff_firstname, staff_surname, practice.practice_name, practice.practice_city
from staff
join practice on staff.practiceid = practice.practiceid
MINUS
SELECT staffid, staff_firstname, staff_surname, practice.practice_name, practice.practice_city
from staff
where role = 'GP';
Basically I'm trying to use the minus construct to find practices which do not employ a GP and include some information such as the CITY and practice_address.
I can use the minus construct to find out how many staff do not have the role of GP like so:
SELECT staffid, staff_firstname, staff_surname
from staff
MINUS
SELECT staffid, staff_firstname, staff_surname
from staff
where role = 'GP';
where I get the results:
STAFFID STAFF_FIRS STAFF_SURN
__________ __________ __________
8 NYSSA THORNTON
9 MONA BRADSHAW
10 GLORIA PENA
I'm struggling to use the join with the minus construct to get information about the GP's practice address and city etc.
Any help would be greatly appreciated!
The second select, after the minus, is referring to columns from the practice table - but it doesn't join to it:
SELECT staffid, staff_firstname, staff_surname,
practice.practice_name, practice.practice_city
from staff
join practice on staff.practiceid = practice.practiceid
MINUS
SELECT staffid, staff_firstname, staff_surname,
practice.practice_name, practice.practice_city
from staff
join practice on staff.practiceid = practice.practiceid
where role = 'GP';
That isn't going to give you what you want though, it will just remove the rows for staff that are GPs, not all trace of practices that have any GPs - non-GP staff at all practices will still be shown.
if you don't want the remaining staff details you only need to include the columns from the practice table in the select lists, and the minus would then give you what you want (and Gordon Linoff has shown two alternatives to minus in that case). If you do want the remaining staff details then you can use a not-exists clause rather than a minus - something like:
select s.staffid, s.staff_firstname, s.staff_surname,
p.practice_name, p.practice_city
from staff s
join practice p on s.practiceid = p.practiceid
where not exists (
select 1
from staff s2
where s2.practice_id = p.practice_id
and s2.role = 'GP
);
This is similar to Gordon's second query but has an extra join to staff for the details. Again, if you don't want those, use Gordon's simpler query.
You could also use an aggregate check, or could probably do something with an analytic function if you've learned abput those, to save having to hit the tables twice.
Your original query only operates on the level of "staff", not "practice". I would be inclined to solve this using aggregation:
select p.practice_name, p.practice_city
from staff s join
practice p
on s.practiceid = p.practiceid
group by p.practice_name, p.practice_city
having sum(case when s.role = 'GP' then 1 else 0 end) = 0;
Or, even better:
select p.*
from practice p
where not exists (select 1
from staff s
where s.practiceid = p.practiceid and s.role = 'GP'
);
I think this is the simplest and most direct interpretation of your question.
I REALLY need help.
I have a list of faculty members that can either be in assigned to a department or just affiliated with the department.
I am trying to query but order the results so that any faculty that appear in the specified department appear in the list first, while anyone affiliated with the specified department are at the bottom of the queried results.
I want the affiliated faculty to be at the bottom of the query results!
Here is my example:
I am querying Philosophy, 20 faculty members are displayed, alphabetically but in no particular order and the results include ANYONE who has Philosophy in Dept 1 or Dept 2 of their record AND anyone who is simply Affiliated with the department. Some are in the actual department (Dept 1 or Dept 2 fields) but others are just affiliated with the department (Affiliation 1 - 5 fields) all interdispersed.
Now I want the query results to be ordered to show: faculty who have DEPT 1 or DEPT 2 = Philosophy, and any faculty that have Affiliations = Philosophy to be at the bottom of the list by themselves.
How do I do this? I've tried Order By and Having, I've tried Group By... Nothing seems to work the way I want it to.. I don't think I am using the write code.
Please, please help me. I've been stuck on this for hours.
Please help - here's my query code:
SELECT listOfAllFaculty.[ID], listOfAllFaculty.[lastName], listOfAllFaculty.[firstName], listOfAllFaculty.[middleInitialMiddleName], listOfAllFaculty.[position], listOfAllFaculty.[emeritusPosition], listOfAllFaculty.[department1], listOfAllFaculty.[department2], listOfAllFaculty.[school1], listOfAllFaculty.[school2], listOfAllFaculty.[affiliation1], listOfAllFaculty.[affiliation2], listOfAllFaculty.[affiliation3], listOfAllFaculty.[affiliation4], listOfAllFaculty.[affiliation5], listOfAllFaculty.[degree], listOfAllFaculty.[specialty], listOfAllFaculty.[awards], listOfAllFaculty.[other]
FROM listOfAllFaculty
WHERE (((listOfAllFaculty.department1)=[Type Department1])) Or (((listOfAllFaculty.department2)=[Type Department2])) Or (((listOfAllFaculty.affiliation1)=[Type Affiliation1])) Or (((listOfAllFaculty.affiliation2)=[Type Affiliation2])) Or (((listOfAllFaculty.affiliation3)=[Type Affiliation3])) Or (((listOfAllFaculty.affiliation4)=[Type Affiliation4])) Or (((listOfAllFaculty.affiliation5)=[Type Affiliation5]))
Perhaps this is what you're looking for using iif in your order by clause:
SELECT listOfAllFaculty.[ID], listOfAllFaculty.[lastName], ...
FROM listOfAllFaculty
WHERE (((listOfAllFaculty.department1)=[Type Department1])) Or
(((listOfAllFaculty.department2)=[Type Department2])) Or
(((listOfAllFaculty.affiliation1)=[Type Affiliation1])) Or
(((listOfAllFaculty.affiliation2)=[Type Affiliation2])) Or
(((listOfAllFaculty.affiliation3)=[Type Affiliation3])) Or
(((listOfAllFaculty.affiliation4)=[Type Affiliation4])) Or
(((listOfAllFaculty.affiliation5)=[Type Affiliation5]))
ORDER BY
IIF(listOfAllFaculty.department1=[Type Department1],0,
IIF(listOfAllFaculty.department2=[Type Department2],0,1))
SELECT Company.CompanyName
,Student.Status
,Student.Level
,Student.PlacementYear
,Company.CompanyCode
,Company.HREmail
,Company.Telephone
,Company.HRContact
,PlacedStudents.DateAdded
FROM Student
RIGHT JOIN (Company INNER JOIN PlacedStudents
ON Company.CompanyCode = PlacedStudents.CompanyCode)
ON Student.StudentNo = PlacedStudents.StudentNo
WHERE (((Student.PlacementYear)=" & Year & "))
AND((Student.Status)<>'Still Seeking YOPE')
ORDER BY Company.CompanyName
I have this SQL Query which pulls HR Contacts from Companies where students are currently placed. However, there are multiple students at one company so when I run the query there are duplicates. I'm fairly new to SQL, I tried DISTINCT, however it didn't seem to do anything, the duplicates remained.
How can I remove duplicates in the CompanyCode field so that the Company only appears once when the query is run.
Below is an image of what happens when I run query. Hopefully this makes sense?
Any help would be appreciated.
This query should give you companies that have placed students:
SELECT Company.CompanyName
,Company.CompanyCode
,Company.HREmail
,Company.Telephone
,Company.HRContact
FROM Company
WHERE EXISTS (SELECT * FROM PlacedStudents INNER JOIN
Student ON Student.StudentNo = PlacedStudents.StudentNo
WHERE Company.CompanyCode = PlacedStudents.CompanyCode
AND Student.PlacementYear =" & Year & "
AND Student.Status <>'Still Seeking YOPE')
ORDER BY Company.CompanyName;
Your question is asking for HR Contacts from Companies where students are placed. I assume this means if you have 1, 2 or 1,000,000 students at a single company, you only want to see the company listed once?
Your current query is returning information from STUDENT and PLACEDSTUDENTS which is going to result in output like
COMPANY_A STUDENT01 .........
COMPANY_A STUDENT02 .........
COMPANY_A STUDENT03 .........
and so on.
If so, and taking a best guess (since I can't know what's in STUDENT or PLACEDSTUDENTS tables), try not including anything related to STUDENT in the SELECT.
SELECT DISTINCT Company.CompanyName, Company.CompanyCode, Company.HREmail,
Company.Telephone, Company.HRContact FROM
I'll be happy to help more if you can provide more information about the structure of the tables and some examples of data, AND what you actually want from the query.
I’m going to calculate a ratio between two entities but are having some trouble with the query.
The principal is the same to, say a forum, where you say:
A user gets points for every new thread. Then, calculate the ratio of points for the number of threads.
Example:
User A has 300 points. User A has started 6 thread. The point ratio is: 50:6
My schemas look as following:
student(studentid, name, class, major)
course(courseid, coursename, department)
courseoffering(courseid, semester, year, instructor)
faculty(name, office, salary)
gradereport(studentid, courseid, semester, year, grade)
The relations is a following:
Faculity(name) = courseoffering(instructor)
Student(studentid) = gradereport (studentid)
Courseoffering(courseid) = course(courseid)
Gradereport(courseid) = courseoffering(courseid)
I have this query to select the faculty names there is teaching one or more students:
SELECT COUNT(faculty.name) FROM faculty, courseoffering, gradereport, student WHERE faculty.name = courseoffering.instructor AND courseoffering.courseid = gradereport.courseid AND gradereport.studentid = student.studentid
My problem is to find the ratio between the faculty members salary in regarding to the number of students they are teaching.
Say, a teacher get 10.000 in salary and teaches 5 students, then his ratio should be 1:5.
I hope that someone has an answer to my problem and understand what I'm having trouble with.
Thanks
Mestika
Some further explanation and examples on my problem and request:
Employee 1: Salary = 10.000 | # of courses he teaches: 3 | # of students (totaly) following thoes 3 courses: 15.
Then, Employee 1 earns 666,7 pr. each student. (i believe this is the ratio)
Employee 2: Salary = 30.000 | # of courses he teaches: 1 | # of students (totaly) following thoes 3 courses: 6.
Then, Employee 2 earns 5000 pr. each student.
You are completely right that my own ratio examples don’t make sense so I will try to explain further.
What I am seeking to do is, to find out how much salary each faculty member has depending on the number of students they are teaching. I imagine that it is a simple question about dividing a faculty members salary by the number of students following a course that the member is teaching.
I get an error when I am running your query, my MySQL has a problem with the convert part (it seems) but otherwise you query is correct it seems.
I haven’t tried the convert statement before, but is it (and why) necessary to convert them? If I for each faculty member that has the correct conditions, find the number of students that are attending the course. Then take that faculty members salary and divide it by the found numbers of student?
when I look at your first example it says that 300 points for 6 threads works out to 50:6 rato. Don't you mean in your later example that 10000 salary for 5 students works out to 2000:5 ratio? not 1:5 ratio?
anyway if my understanding of your example is correct then this should be a good solution
select f.name, f.salary, count(s.studentid) as noofstudents, convert(f.salary / count(s.studentid),varchar(50)) + ':' + convert(count(s.studentid),varchar(10)) as ratio
from faculty f
inner join courseoffering co on f.name = co.instructor
inner join gradereport gr on co.courseid = gr.courseid
inner join student s on gr.studentid = s.studentid
where co.semester = #semester
and co.year = #year
group by f.name, f.salary
perhaps you could expand on your question a bit if this isn't what you're looking for.