i'm currently stuck on a sql-query, trying to find a solution, but making me headache for 2 stays now. i've got 3 tables
user-table:
+-----+----------+-----------+
| pid | username | role |
+-----+----------+-----------+
| 1 | user1 | patient |
| 2 | user2 | patient |
| 3 | user3 | doc |
| 4 | user4 | assistant |
| 5 | user5 | patient |
+-----+----------+-----------+
base-dat:
+-----+---------+-------+------------+
| pid | surname | name | birthdate |
+-----+---------+-------+------------+
| 1 | smith | john | 1950-07-31 |
| 2 | jackson | sarah | 1948-08-15 |
+-----+---------+-------+------------+
med-dat:
+-----+-----+---------------+--------+--------+
| mid | pid | dateLastEntry | weight | pulse |
+-----+-----+---------------+--------+--------+
| 1 | 1 | 2017-12-01 | 86 | 65 |
| 2 | 1 | 2017-12-02 | 84 | 70 |
| 3 | 1 | 2017-12-03 | 80 | 67 |
| 4 | 2 | 2017-11-15 | 66 | 60 |
| 5 | 2 | 2017-11-17 | 60 | 64 |
+-----+-----+---------------+--------+--------+
I'm trying to get the max(dateLastEntry) for each user with role patient, showing their pid, name, surname, weight, pulse in a single row - , even if there is no med-data entry for the patient: something like this:
+-----+---------+-------+------------+--------+-------+
| pid | surname | name | lastEntry | weight | pulse |
+-----+---------+-------+------------+--------+-------+
| 1 | smith | john | 2017-12-02 | 84 | 70 |
| 2 | jackson | sarah | 2017-11-17 | 60 | 64 |
| 5 | NONE | NONE | NONE | NONE | NONE |
+-----+---------+-------+------------+--------+-------+
Atm my statement looks like this, but can't get the proper result:
select b.pid, s.surname, s.name, max(m.date) as lastEntry, m.weight, m.pulse
from users b
left join med-dat m on b.pid = m.pid
left join base-dat s on m.pid = s.pid
where b.role = 'Patient'
group by b.pid, s.surname, s.name, m.weight;
You can rewrite your query as below to get the desired output
select b.pid, s.surname, s.name, m.dateLastEntry as lastEntry, m.weight, m.pulse
from users b
left join med_dat m on b.pid = m.pid
left join base_dat s on m.pid = s.pid
left join med_dat m1 on m.pid = m1.pid
and m.dateLastEntry < m1.dateLastEntry
where m1.pid is null
and b.role = 'Patient'
DEMO
Edit from comment, join base_dat using pid from user table
select b.pid, s.surname, s.name, m.dateLastEntry as lastEntry, m.weight, m.pulse
from users b
left join med_dat m on b.pid = m.pid
left join base_dat s on b.pid = s.pid
left join med_dat m1 on m.pid = m1.pid and m.dateLastEntry < m1.dateLastEntry
where m1.pid is null
and b.role = 'Patient'
group by b.pid, s.surname, s.name, m.weight;
Demo
Related
I have six tables in the database, all tables are relative to each other and want to show records in one table.
Following are my tables:
1) mls_stores
*----------------------------*
| store_id | store_title |
*----------------------------*
| 1001 | ajmar-jaipur |
| 1002 | dwarka-delhi |
*----------------------------*
2) mls_category
*-------------------------------------------*
| cat_no | store_id | cat_value | cat_type |
*-------------------------------------------*
| 20 | 1001 | 1 | running |
| 21 | 1001 | 4 | cycling |
| 22 | 1002 | 1 | running |
| 23 | 1002 | 2 | swmining |
*-------------------------------------------*
3) mls_points_matrix
*----------------------------------------*
| store_id | value_per_point | maxpoint |
*----------------------------------------*
| 1001 | 1 | 10 |
| 1001 | 2 | 20 |
| 1002 | 1 | 20 |
| 1002 | 4 | 30 |
*----------------------------------------*
4) mls_user
*--------------------------*
| id | store_id | name |
*--------------------------*
| 1 | 1001 | sandeep |
| 2 | 1001 | jagveer |
| 3 | 1002 | gagan |
*--------------------------*
5) bonus_points
*---------------------------------------------------*
| user_id | store_id | bonus_points | bonus_type |
*---------------------------------------------------*
| 1 | 1001 | 10 | fixed |
| 3 | 1002 | 2 | % |
*---------------------------------------------------*
6) mls_entry
*-------------------------------------------------------*
| user_id | store_id | category | distance | status |
*-------------------------------------------------------*
| 1 | 1001 | 20 | 10 | approved |
| 1 | 1001 | 21 | 40 | approved |
| 1 | 1001 | 20 | 5 | reject |
| 2 | 1001 | 21 | 40 | approved |
| 3 | 1002 | 22 | 10 | approved |
| 3 | 1002 | 23 | 20 | approved |
*-------------------------------------------------------*
Now I want output as below:
*-----------------------------------------------------------------------------------*
| Name | Entries | Points Earned | Bonus Points | Total Points | Total Amount |
*-----------------------------------------------------------------------------------*
| Sandeep | running(1) | 20 | 10 | 30 | 60 |
| | cycling(1) | | | | |
*-----------------------------------------------------------------------------------*
| Jagveer | cycling(1) | 10 | 0 | 10 | 10 |
*-----------------------------------------------------------------------------------*
I am using following code:
SELECT
u.name,
ROUND(COALESCE(t1.points, 0)) AS points,
ROUND(COALESCE(b.bonus_points, 0)) AS bonus_points,
ROUND(COALESCE(t1.points, 0) + COALESCE(b.bonus_points, 0)) AS total_points
FROM mls_user u
LEFT JOIN
(
SELECT e.user_id, e.status, SUM(e.distance / c.cat_value) AS points
FROM mls_entry e
INNER JOIN mls_category c
ON e.store_id = c.store_id AND e.category = c.cat_no
GROUP BY e.user_id
HAVING e.status='approved'
) t1
ON u.id = t1.user_id
LEFT JOIN bonus_points b
ON u.id = b.user_id
WHERE u.store_id = '1001'
ORDER BY
total_points DESC
This SQL query giving me Point earned, bonus points and total points, But I am not able to find Entries And Total Amount and it is giving me wrong Point calculation for Sandeep, As per data one entry is rejected. so it should be 20, not 25.
My total amount will be for Sandeep 30X2(it is coming from point matrix) = 60
same like for jagveer, the total amount for jagveer 10X1 = 10.
I have created tables in DEMO
Try below :
SELECT
u.name,
ROUND(COALESCE(t1.points, 0)) AS points,
ROUND(COALESCE(b.bonus_points, 0)) AS bonus_points,
ROUND(COALESCE(t1.points, 0) + COALESCE(b.bonus_points, 0)) AS total_points,
ROUND(COALESCE(t1.points, 0) + COALESCE(b.bonus_points, 0)) * t1.countId as total_amount,
group_concat(t1.EntriesConcat) as Entries
FROM mls_user u
LEFT JOIN
(
SELECT e.user_id, e.status, SUM(e.distance / c.cat_value) AS points,
concat(c.cat_type, '(',count(e.user_id), ')' ) as EntriesConcat,
count(e.user_id) as countId -- it returns count of records according to group by part
FROM mls_entry e
INNER JOIN mls_category c
ON e.store_id = c.store_id AND e.category = c.cat_no
-- remove HAVING and use WHERE clause
WHERE e.status='approved'
GROUP BY e.user_id
) t1 ON u.id = t1.user_id
LEFT JOIN bonus_points b ON u.id = b.user_id
WHERE u.store_id = '1001'
ORDER BY total_points DESC
group_concat of mysql is useful to concatinating values on Group By
i have this 3 tables in a MySql Database.
users
+----+------+--------+------+
| Id | Name | Status | Role |
+----+------+--------+------+
| 1 | A | Aktiv | Op |
| 2 | B | Aktiv | Op |
| 3 | C | Aktiv | Op |
| 4 | D | Aktiv | Op |
+----+------+--------+------+
cnt
+----+------+------------+------+
| Id | Name | Date | Type |
+----+------+------------+------+
| 1 | A | 2017-11-09 | Web |
| 2 | B | 2017-11-09 | Web |
| 3 | C | 2017-11-09 | Web |
| 4 | C | 2017-11-09 | Inb |
| 5 | A | 2017-11-09 | Web |
+----+------+------------+------+
Lead
+----+------+------------------+------------+
| Id | Name | Date | Type |
+----+------+------------------+------------+
| 1 | A | 2017-11-09 00:24 | Imported |
| 2 | B | 2017-11-09 09:32 | Activation |
| 3 | B | 2017-11-09 10:56 | Activation |
| 4 | D | 2017-11-09 12:21 | Activation |
| 5 | D | 2017-11-10 12:22 | Activation |
+----+------+------------------+------------+
I'm trying to join them in a main table but with no success, the query i'm using is:
SELECT IFNULL(u.Name,'Total') as "Name",
Sum(IF(c.Type = 'Web',1,0)) as "Cnt Web",
Sum(IF(l.Type = 'Activation',1,0)) as "Lead Web"
FROM users u
LEFT JOIN cnt c ON u.Name = c.Name and c.Date = '2017-11-09'
LEFT JOIN lead l ON u.Name = l.Name and l.Date>= '2017-11-09' AND l.Date< '2017-11-10'
WHERE u.Status = 'Aktiv' AND u.Role = 'Op'
GROUP BY u.Name WITH ROLLUP
The result i need is a table like this:
+----+------+--------+---------+
| Id | Name | Cnt Web| Lead Web|
+----+------+------------------+
| 1 | A | 2 | 0 |
| 2 | B | 1 | 2 |
| 3 | C | 1 | 0 |
| 4 | D | 0 | 1 |
+----+------+------------------+
When i try to join the first table with the second or the first with the third, i get the correct result, but i can't get the needed result when i join them all.
Any answer is the most welcomed. Thank you in advance.
Here's a solution using correlated sub-queries
SELECT u.Id,
u.Name,
(SELECT COUNT(Name) FROM cnt WHERE Name = u.name AND type = 'Web' AND Date = '2017-11-09') AS cnt_web,
(SELECT COUNT(Name) FROM lead WHERE Name = u.name AND type = 'Activation' AND Date>= '2017-11-09' AND Date< '2017-11-10') AS cnt_lead
FROM users u
WHERE u.Status = 'Aktiv' AND u.Role = 'Op'
I'm not sure how to join two tables onto my main query. The two tables that determine the accountnumber join to each of the 3 main tables for different situations.
I'm trying to determine the accountnumber based on three things.
An accountnumber is based on a paycode, department and employee type which are three different tables.
Here is how they should be joined.
/* all accountnumbers have a paycode_id*/
accountcodes.paycode_id = employee_pay.id
/* all accountnumbers are assigned to either a certain department or all*/
accountcodes.department_code = department.code
/* the accountnumber can be assigned to one emp_type or both*/
accountcodes.emp_type_id = employee_infos.emp_type_id
/* the accountnumber is in table:lkp_accountcodes, that determines the account number table:accountcodes */
accountcodes.lkp_accountcodes_id = lkp_accountcodes.id
table:accountcodes
-----------------------------------------------------------------------
| ID | lkp_accountcodes_id | paycode_id | department_code | emp_type_id |
|--------------------------|------------|-----------------|-------------|
| 1 | 21 | 15 | 120 | 1 |
| 2 | 22 | 15 | 310 | 1 |
| 3 | 23 | 30 | null | 1 |
| 4 | 24 | 30 | null | 2 |
| 5 | 25 | 55 | 120 | 1 |
| 6 | 26 | 55 | 310 | 2 |
| 7 | 27 | 55 | 120 | 2 |
-----------------------------------------------------------------------
table:lkp_accountcodes
-----------------------------------
| id | company_id | accountnumber |
|-----|------------|----------------|
| 21 | 500 | 5210 |
| 22 | 500 | 6210 |
| 23 | 500 | 2211 |
| 24 | 500 | 2210 |
| 25 | 500 | 5010 |
| 26 | 500 | 6000 |
| 27 | 500 | 5090 |
-----------------------------------
I don't know if I should be doing three left joins, create temporary tables, or one big left outer join like the one below?
Also, I'm not sure how to group it and if the departent code is null then the accountnumber should be detemined by the paycode_id and emp_type_id.
Help me with the Query below.
SELECT i.employee, d.department, e1.paycode, a1.accountnumber
FROM employee_pay e1
INNER JOIN employee_infos i ON e1.emp_info_id = i.id
INNER JOIN department d ON i.department_id = d.id
LEFT OUTER JOIN accountcodes ac ON ac.paycode_id = e1.id
AND ac.emp_type_id = i.emp_type_id
AND ac.department_code = d.code -- if null?
LEFT OUTER JOIN lkp_accountcodes lgc on gp.lkp_gl_code_id = lgc.id
-- group?
expected result
emp_number | emp_type | deptartment | pay_code | account_number
123 | temp | 120 | CPP Ded | 5210
456 | reg | 310 | CPP Ded | 6210
789 | temp | null | ExpReim | 2210
987 | reg | null | ExpReim | 2211
654 | reg | 145 | StatHol | 5010
321 | temp | 145 | StatHol | 5090
333 | temp | 532 | StatHol | 6000
Do the fact you have not aggregation function then if you want avoid duplicated row instead of GROUP BY you should use DISTINCT
SELECT DISTINCT i.employee, d.department, e1.paycode, a1.accountnumber
FROM employee_pay e1
INNER JOIN employee_infos i ON e1.emp_info_id = i.id
INNER JOIN department d ON i.department_id = d.id
LEFT JOIN accountcodes ac ON ac.paycode_id = e1.id
AND ac.emp_type_id = i.emp_type_id
AND ac.department_code = d.code
LEFT JOIN lkp_accountcodes lgc on gp.lkp_gl_code_id = lgc.id
This should be ok.
SELECT i.employee, d.department, e1.paycode, a1.accountnumber
FROM employee_pay e1
JOIN employee_infos i ON e1.emp_info_id = i.id
JOIN department d ON i.department_id = d.id
JOIN accountcodes ac ON ac.paycode_id = e1.id
AND ac.emp_type_id = i.emp_type_id
AND ac.department_code = d.code -- if null?
LEFT OUTER JOIN lkp_accountcodes lgc on gp.lkp_gl_code_id = lgc.id;
I have 3 table
major table:
+----+------------+
| id | major |
+----+------------+
| 1 | Computer |
| 2 | Architect |
| 3 | Designer |
+----+------------+
classroom table:
+----+----------+-------+
| id | major_id | name |
+----+----------+-------+
| 1 | 1 | A |
| 2 | 1 | B |
| 3 | 1 | C |
| 4 | 2 | A |
| 5 | 2 | B |
| 6 | 3 | A |
+----+----------+-------+
and finally, student_classroom table
+----+------------+--------------+----------+
| id | student | classroom_id | status |
+----+------------+--------------+----------+
| 1 | John | 1 | Inactive |
| 2 | Defou | 2 | Active |
| 3 | John | 2 | Active |
| 4 | Alexa | 1 | Active |
| 5 | Nina | 1 | Active |
+----+------------+--------------+----------+
how can I use propel to build query below
select
a.id,
a.major,
b.number_of_student,
c.number_of_classroom
from major a
left join (
select
major.major_id,
count(student_classroom.id) as number_of_student
from major
left join classroom on classroom.major_id = major.id
left join student_classroom on student_classroom.classroom_id = classroom.id
where student_classroom.`status` = 'Active'
group by major_id
) b on b.major_id = a.major_id
left join (
select
major.major_id,
count(classroom.id) as number_of_classroom
from major
left join classroom on classroom.major_id = major.id
group by major_id
) c on c.major_id = a.major_id
Because I want the final result would be something like this, I spend hours trying to figure it out without success.
+----+------------+-------------------+---------------------+
| id | major | number_of_student | number_of_classroom |
+----+------------+-------------------+---------------------+
| 1 | Computer | 4 | 3 |
| 2 | Architect | 0 | 2 |
| 3 | Designer | 0 | 1 |
+----+------------+-------------------+---------------------+
Try this
select
m.id,
m.major,
count(distinct s.id) as number_of_student ,
count(distinct c.id) as number_of_classroom
from major m
left join classroom c on
(m.id = c.major_id)
left join student_classroom s
on (s.classroom_id = c.id and c.major_id = m.id and s.status = 'active')
group by m.id
order by m.id
I'm facing a problem here :
I have two tables :
A users table :
+----+---------------+----------+
| id | username | company |
+----±---------------±----------+
| 1 | John | 0 |
| 2 | Jack | 0 |
| 3 | Casimir | 0 |
±----±---------------±----------±
A orders table :
+----+---------------+----------+--------+
| id | date | iduser | status |
+----±---------------±----------+--------+
| 1 | 2012-05-28 | 1 | 1 |
| 2 | 2012-05-25 | 1 | 1 |
| 3 | 2012-04-28 | 2 | 1 |
| 4 | 2012-03-28 | 1 | 1 |
| 5 | 2012-02-28 | 2 | 0 |
±----±---------------±----------±--------+
What I'm trying to do is to get a result like this :
+----------+---------------+-------------+
| username | COUNT(order) | MAX(date) |
+----------±---------------±-------------+
| John | 3 | 2012-05-28 |
| Jack | 1 | 2012-04-28 |
| Casimir | 0 | NULL |
±----------±---------------±-------------±
Here's the request I have for the moment :
SELECT u.username, COUNT(o.id), MAX(o.date)
FROM users u
INNER JOIN orders ON u.id = o.iduser
WHERE o.status = 1
GROUP BY u.id
This request gives me a result like :
+----------+---------------+-------------+
| username | COUNT(order) | MAX(date) |
+----------±---------------±-------------+
| John | 3 | 2012-05-28 |
| Jack | 1 | 2012-04-28 |
±----------±---------------±-------------±
As you can see, the user Casimir is not shown as he made no order. How can I modify my request to get the result I need please ?
Thanks !
A LEFT JOIN or LEFT OUTER JOIN will include all rows of the inital table, including those where there is no match in the joined-to table
SELECT u.username, COUNT(o.id), MAX(o.date)
FROM users u
LEFT OUTER JOIN orders o ON u.id = o.iduser AND o.status = 1
GROUP BY u.id
You need to use an OUTER JOIN instead of your current INNER JOIN.
Have a look at Jeff's post here to see how they differ:
http://www.codinghorror.com/blog/2007/10/a-visual-explanation-of-sql-joins.html