Advanced Ordering Or Grouping Of Query Results (having?), Access 2013 - sql

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))

Related

Having SQL Server choose and show one record over other

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'

Need help finding only Employees and Sales Persons in SQL

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?

SQL - Remove Duplicates in Single Field

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.

Creating Report, repetition in report output, Ms Access SQL

SELECT NMC.*, Exam.Final_Exam_Level
FROM
(SELECT Technicians.Technician_ID AS Technician_ID,
Technicians.First_Name AS First_Name,
Technicians.Surname AS Surname,
MAX(New_Models.Date_Issued) AS Last_Course_Date,
MAX(New_Models.Issue) AS Last_Issue,
MAX(New_Models.Model_ID) AS Last_Model_ID,
Technicians.Course_Level AS No_Training_Courses
FROM New_Models, New_Models_Allocation, Technicians
WHERE New_Models.Model_ID=New_models_Allocation.Model_ID
And Technicians.Technician_ID=New_Models_Allocation.Technician_ID
GROUP BY Technicians.Technician_ID, Technicians.Course_Level, First_Name, Surname
ORDER BY MAX(New_Models.Model_ID) DESC)
AS NMC
INNER JOIN (SELECT Technicians.Technician_ID, COUNT(*) AS Final_Exam_Level
FROM Technicians, Exams, Exam_Allocation
WHERE (Technicians.Technician_ID)=Exam_Allocation.Technician_ID
And ((Exams.Exam_ID)=Exam_Allocation.Exam_ID)
And (Exams.Date_Taken)<=#12/31/2010#
GROUP BY Technicians.Technician_ID, Technicians.Course_Level
ORDER BY Technicians.Technician_ID)
AS Exam ON Exam.Technician_ID=NMC.Technician_ID;
This query shows each technician, Last Exam, Last New_Model, Last course.
SELECT Technicians.Technician_ID, Jobs.Job_ID, Jobs.Date_Occured, Fix
FROM Technicians, Jobs, Tech_Allocation, Recovery
WHERE Technicians.Technician_ID=Tech_Allocation.Technician_ID
And Jobs.Job_ID=Tech_Allocation.Job_ID
And Jobs.Job_ID=Recovery.Job_ID
And Jobs.Date_Occured>=#1/1/2010#
And Jobs.Date_Occured<=#12/31/2010#
ORDER BY Fix;
This query shows the jobs each technician has done.
However, when creating a report in Ms Access, the jobs are repeated. Hence, instead of a technician having done 3 jobs, it shows 12 for example. Although when running the second query itself, results aren't repeated.
Any Help?
For obvious reasons, I don't usually read other people's SQL queries, but your example was very well formatted. Is this the problem?
INNER JOIN (SELECT Technicians.Technician_ID, COUNT(*) AS Final_Exam_Level
...
GROUP BY Technicians.Technician_ID, Technicians.Course_Level
These 2 lines are from the 2nd subquery of your first query. You have 1 index field (Technician_ID), but 2 grouping fields (Technician_ID and Course_Level). This would produce results like:
Technician_ID Final_Exam_Level
Bob 5
Bob 4
Nadine 5
I recommend either removing Course_Level from the Group By or adding it to the Select row.

Help with SQL query (Calculate a ratio between two entitiess)

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.