Mdx aggregate field per person - mdx

I have a table sample, sample belongs to patient, the relation is patient has many samples. Here is the schema:
Sample id patient_id result date
1 1 positive 2015
2 2 positive 2015
3 2 negative 2016
Could I sum the positive result by the max date for each patient?
SQL equivalent code:
select
sum(case when sout.result_id=1 then 1 else 0 end)
from
samples sout ,
(
select max(sin.date) as maxdate,
sin.patient_id
from samples sin
where sin.date between date(${StartDateParam}) and date(${EndDateParam})
group by sin.patient_id
) tmp
where
sout.patient_id = tmp.patient_id and
sout.date = tmp.maxdate

sample query for patient:
currently admitted :Select p.patient_no, p.pat_name,p.date_admitted,r.room_extension,p.date_discharged FROM Patient p JOIN room r ON p.room_location = r.room_location where p.date_discharged IS NULL ORDER BY p.patient_no,p.pat_name,p.date_admitted,r.room_extension,p.date_discharged;
vacant rooms: SELECT r.room_location, r.room_accomadation, r.room_extension FROM room r where r.room_location NOT IN (Select p.room_location FROM patient.p where p.date_discharged IS NULL) ORDER BY r.room_location, r.room_accomadation, r.room_extension;
no charges yet : SELECT p.patient_no, p.pat_name, COALESCE (b.charge,0.00) charge FROM patient p LEFT JOIN billed b on p.patient_no = b.patient_no WHERE p.patient_no NOT IN (SELECT patient_no FROM billed) group by p.patient_no ORDER BY p.patient_no, p.pat_name,charge;
max salarised : SELECT phy_id,phy_name, salary FROM physician where salary in (SELECT MAX(salary) FROM physician) UNION
SELECT phy_id,phy_name, salary FROM physician where salary in (SELECT MIN(salary) FROM physician) ORDER BY phy_id,phy_name, salary;
various item consumed by: select p.pat_name, i.discription, count (i.item code) as item code from patient p join billed b on p.patient no = b. patient no join item i on b.item code = i.item code group by p.patient no, i.item code order by..
patient not receivede treatment: SELECT p.patient_no,p.pat_name FROM patient p where p.patient_no NOT IN (SELECT t.patient_no FROM treats t)
ORDER BY p.patient_no,p.pat_name;
2 high paid : Select phy_id, phy_name, date_of_joining, max(salary) as salary from physician group by salary having salary IN (Select salary from physician)
Order by phy_id, phy_name, date_of_joining, salary limit 2;
over 200: select patient_no, sum (charge), as total charge from billed group by patient no having total charges > 200 order by patient no, total charges

Related

How to correctly combine these two queries?

Employees table :
EmpID (Primary Key)
LastName
FirstName
Orders table :
OrderID (Primary Key)
CustID
EmpID
OrderDate
RequiredDate
ShippedDate
I need a query which returns EmpID, LastName, FirstName, total number of orders by employee, and total number of orders shipped late. An order is late if its ShippedDate is 5 days after RequiredDate. I got the late shipments by :
julianday(ShippedDate) - julianday(RequiredDate) >= 5
I thought to make two separate queries, one that takes a total count of the submissions and one for total count of late orders, then UNION them. I'm not getting the result I am looking for:
SELECT Employees.EmpId, Employees.LastName, Employees.FirstName, COUNT(*) as TotalCount, NULL
FROM Orders, Employees
WHERE Orders.EmpID = Employees.EmpID
GROUP BY LastName
UNION
SELECT Employees.EmpId, Employees.LastName, Employees.FirstName, NULL, COUNT(*) as LateCount
FROM Orders, Employees
WHERE Orders.EmpID = Employees.EmpID
AND julianday(ShippedDate) - julianday(RequiredDate) >= 5
GROUP BY LastName
I end up with offset null values on right side of the table :
TotalCount
NULL
17
NULL
NULL
25
etc.
What went wrong with my UNION? Why is the data offset and the column header wrong?
Is there a better approach?
"Is there a better approach?"
JOIN instead of UNION :
SELECT
Employees.EmpID,
Employees.LastName,
Employees.FirstName,
count(*) AS TotalCount,
sum(
julianday(Orders.ShippedDate) - julianday(Orders.RequiredDate) >= 5
) AS LateCount
FROM
Employees
JOIN Orders ON Orders.EmpID = Employees.EmpID
GROUP BY
Employees.EmpID
ORDER BY
TotalCount DESC,
LateCount DESC

Sql join with two tables max and count

i have two tables
employee :
employee_number employee_name salary divison_id
division :
division_id division_name
How to Show division id, division name, number of employees working in each division that has the highest number of employees.
I want to have an outcome like this :
division_id division_name total_employee
Z-100 | finance | 3
SELECT *
FROM (SELECT e.division_id,
d.division_name,
Count(1) total_employee
FROM employee e
JOIN division d
ON e.division_id = d.division_id
GROUP BY e.division_id
ORDER BY Count(1) DESC)a
LIMIT 1
If you want all the department with most employees you should consider that more than one department can have that. So I am revising my answer and instead of top I am using Rank() with CTE to get all the divisions with most number of employees.
with DivisionWithMostEmployee as
(
select Rank()over(order by count(*) desc) RowNumber, d.division_id, d.division_name, count(*) TotalEmployee
from division d, employee e where d.division_id=e.division_id
group by d.division_id,d.division_name
)
select Division_id,Division_name,TotalEmployee from DivisionWithMostEmployee where RowNumber=1

Name of Teacher with Highest Wage - recursive CTE

I am trying to get the max salary of each dept and display that teacher by first name as a separate column. So dept 1 may have 4 rows but one name showing for max salary. I'm Using SQL SERVER
With TeacherList AS(
Select Teachers.FirstName,Teachers.LastName,
Teachers.FacultyID,TeacherID, 1 AS LVL,PrincipalTeacherID AS ManagerID
FROM dbo.Teachers
WHERE PrincipalTeacherID IS NULL
UNION ALL
Select Teachers.FirstName,Teachers.LastName,
Teachers.FacultyID,Teachers.TeacherID, TeacherList.LVL +
1,Teachers.PrincipalTeacherID
FROM dbo.Teachers
INNER JOIN TeacherList ON Teachers.PrincipalTeacherID =
TeacherList.TeacherID
WHERE Teachers.PrincipalTeacherID IS NOT NULL)
SELECT * FROM TeacherList;
SAMPLE OUTPUT :
Teacher First Name | Teacher Last Name | Faculty| Highest Paid In Faculty
Eric Smith 1 Eric
Alex John 1 Eric
Jessica Sewel 1 Eric
Aaron Gaye 2 Aaron
Bob Turf 2 Aaron
I'm not sure from your description but this will return all teachers and the last row is the name of the teacher with the highest pay on the faculty.
select tr.FirstName,
tr.LastName,
tr.FacultyID,
th.FirstName
from Teachers tr
join (
select FacultyID, max(pay) highest_pay
from Teachers
group by FacultyID
) t on tr.FacultyID = t.FacultyID
join Teachers th on th.FacultyID = t.FacultyID and
th.pay = t.highest_pay
this will produce an unexpected result (duplicate rows) if there are more persons with the highest salary on the faculty. In such case you may use window functions as follows:
select tr.FirstName,
tr.LastName,
tr.FacultyID,
t.FirstName
from Teachers tr
join
(
select t.FirstName,
t.FacultyID
from
(
select t.*,
row_number() over (partition by FacultyID order by pay desc) rn
from Teachers t
) t
where t.rn = 1
) t on tr.FacultyID = t.FacultyID
This will display just one random teacher from faculty with highest salary.
dbfiddle demo
You can do this with a CROSS APPLY.
SELECT FirstName, LastName, FacultyID, HighestPaid
FROM Teachers t
CROSS APPLY (SELECT TOP 1 FirstName AS HighestPaid
FROM Teachers
WHERE FacultyID = t.FacultyID
ORDER BY Salary DESC) ca

SQL - Using Sum command multiplies the salary for the same person

I'm trying to increase the salary for those employees who treat at least 2 patients by 10%. My problem is that the salary first multiplies by 2 for every patient they treat and then multiplies by 10% at the end. For exampel if the employee earns 25.000 and treats 3 people the new salary becomes 82.500.
select distinct t.empNbr, e.Salary, sum(e.Salary*1.1) as NewSalary from Treats t
inner join Employee e
on e.empNbr=t.empNbr
WHERE t.empNbr IN
(
SELECT empNbr
FROM Treats
GROUP BY empNbr
HAVING COUNT(*) >= 2)
group by t.empNbr, e.Salary
CROSS APPLY should help:
SELECT e.empNbr,
e.Salary,
e.Salary*1.1 as NewSalary
FROM Employee e
CROSS APPLY (
SELECT empNbr
FROM Treats
WHERE e.empNbr = empNbr
GROUP BY empNbr
HAVING COUNT(*) > 1
) as t
The t part gets empNbr we need. Then we select empNbr and salary from Employee table and do math :)
One more way:
SELECT TOP 1 WITH TIES
e.empNbr,
e.Salary,
e.Salary*1.1 as NewSalary
FROM Employee e
INNER JOIN Treats t
ON e.empNbr = t.empNbr
ORDER BY
CASE WHEN COUNT(t.empNbr) OVER (PARTITION BY t.empNbr ORDER BY t.empNbr) > 1 THEN 1 ELSE 0 END DESC,
ROW_NUMBER() OVER (PARTITION BY t.empNbr ORDER BY t.empNbr)
This should be the right query. Let me know if this works
select empNbr, Salary, sum(Salary*1.1) as NewSalary
from employee
where empNbr in (select empNbr
from Treats
group by empNbr
having count(*) >=2) ----- Ordered as a code
Christopher, You can use below query to get the result,
SELECT t.empNbr,
e.Salary,
(e.Salary * e.count +(e.Salary/10) ) as NewSalary
from Treats t
INNER JOIN
(SELECT empNbr, COUNT(*) AS count Employee GROUP BY empNbr) e
ON e.empNbr=t.empNbr
AND e.count >=2
Explination
1. We can calculate result without having clause2.In the inner join, empNbr and the count of that employee in derived3.Using this count in the select query, the current salary is multiplied and 10% is added with the salary
Hope this is what you need. Any issues, feel free to ask

SQL Select the highest sale for the current month

I am trying to Select the name, email address that has the highest individual sale for the current month but for some reason my select statement won't return the right date or the highest amount. Any guidance or help on right direction would be appreciated
Select
FirstName, LastName,Email, SaleDate, Max(Total)as HighestTotal
From
Sale, Employee
where
month(SaleDate) = 12
Group by
SaleDate, FirstName, LastName, Email
Use TOP 1 with ORDER BY HighestTotal desc to get the highest individual sale for the given month
Also use proper INNER JOIN syntax to join two tables. Considering that both the tables have emp_id as common column
SELECT TOP 1 WITH ties E.FirstName,
E.LastName,
E.Email,
S.SaleDate,
S.Total AS HighestTotal
FROM Sale S
INNER JOIN Employee E
ON E.emp_id = S.Emp_id
WHERE Month(S.SaleDate) = 12
ORDER BY HighestTotal DESC
With Ties will bring more than one employee in case of tie in highest individual sale.
1) You have to add a condition for the current year:
where month( SaleDate) = 12 AND year(SaleDate) = 2015
2) You have to add ORDER BY to get the highest value:
ORDER BY HighestTotal DESC
LIMIT 1