Conditional Query for Employee Training Analysis Query - mysql

I need to run training queries that return results for questions like the following: "Who has completed this training, but not that training?"
In the simplified table below, I would like to know which employee has completed training_id 1 (as indicated by a date in the completed_date field), but has not finished training_id 7.
+-------------+-------------+----------------+
| emp_id | training_id | completed_date |
+-------------+-------------+----------------+
| 1 | 1 | 2010-04-02 |
+-------------+-------------+----------------+
| 1 | 7 | Null |
+-------------+-------------+----------------+
| 2 | 1 | Null |
+-------------+-------------+----------------+
| 2 | 7 | Null |
+-------------+-------------+----------------+
Desired result would be emp_id 1 and we'd like to return his/her completed training and non-completed training per the query parameters:
+-------------+-------------+----------------+
| emp_id | training_id | completed_date |
+-------------+-------------+----------------+
| 1 | 1 | 2010-04-02 |
+-------------+-------------+----------------+
| 1 | 7 | Null |
+-------------+-------------+----------------+
I can't figure out how to do this with a regular query because it seems to need IF logic. Ex: Return rows where this one training is complete and return rows where this second training is not complete BUT ONLY if the first training is complete.
How do I express something like that in SQL?

You can use an EXISTS clause
SELECT t.*
FROM training t
# which employee has completed training_id 1
WHERE t.training_id = 1 and t.completed_date is not null
#but has not finished training_id 7.
AND NOT EXISTS (
SELECT * FROM training t2
where t2.emp_id=t.emp_id
and t2.training_id = 7
and t2.completed_date is not null)
If you wanted to test something more complex, like completed (4,5,6) but not (1,9), then you can use counts:
SELECT t.emp_id
FROM training t
WHERE t.training_id in (4,5,6) and t.completed_date is not null
group by t.emp_id
having count(distinct emp_id) = 3
AND NOT EXISTS (
SELECT * FROM training t2
where t2.emp_id=t.emp_id
and t2.training_id in (1,9)
and t2.completed_date is not null)
And finally if you need the full employee training record
SELECT e.*
FROM
(
SELECT t.emp_id
FROM training t
WHERE t.training_id in (4,5,6) and t.completed_date is not null
group by t.emp_id
having count(distinct emp_id) = 3
AND NOT EXISTS (
SELECT * FROM training t2
where t2.emp_id=t.emp_id
and t2.training_id in (1,9)
and t2.completed_date is not null)
) search
inner join training e on e.emp_id = search.emp_id
order by e.emp_id

select * from training where emp_id in
(
select distinct emp_id from training where completed_Date is not null
)

You could do a self-join on this table ("pretend it is two identical tables and join them"):
SELECT t1.emp_id, t1.training_id, t1.completed_date,
t2.training_id, t2.completed_date
FROM training AS t1 /* the aliases are needed for the self-join */
JOIN training AS t2
ON t1.emp_id = t2.emp_id
AND t2.training_id = 7 /* second training ID */
WHERE t1.training_id = 1 /* first training ID */
That should give you a result like this:
t1.emp_id | t1.training_id | t1.completed_date | t2.training_id | t2.completed_date
1 1 2010-04-02 7 NULL
2 1 NULL 7 NULL
You can then further limit the WHERE query, e.g. with
AND t1.completed_date IS NOT NULL
AND t2.completed_date IS NULL
which will give you the set that you want - training 1 completed, training 7 not.

Related

How do I update scores in table without using a ranking function

Table name is: result
ID Name score position
1 John 40 0
2. Ali 79 0
3 Ben 50 0
4 Joe 79 0
How can I update table result to give me the table below without using rank() as it does not support by server. Pls someone should help me with the MySQL code That breaks ties just as in table below.
ID Name score position
1 John 40 4
2. Ali 79 1
3 Ben 50 3
4 Joe 79 1
In MySQL prior to version 8 try using the multiple table update syntax:
UPDATE scores t
LEFT JOIN (
SELECT t1.id, COUNT(*) + 1 AS new_position
FROM scores t1
JOIN scores t2 ON t1.score < t2.score
GROUP BY t1.id
) agg ON t.id = agg.id
SET t.position = COALESCE(agg.new_position, 1)
fiddle
Lots of ways to skin this particular animal. How about...
DROP TABLE IF EXISTS my_table;
CREATE TABLE my_table
(ID SERIAL PRIMARY KEY
,Name VARCHAR(12) NOT NULL
,score INT NOT NULL
);
INSERT INTO my_table VALUES
(1,'John',40),
(2,'Ali',79),
(3,'Ben',50),
(4,'Joe',79);
SELECT id
, name
, score
, FIND_IN_SET(score, scores) rank
FROM my_table
CROSS
JOIN
( SELECT GROUP_CONCAT(score ORDER BY score DESC) scores
FROM my_table
) scores
+----+------+-------+------+
| id | name | score | rank |
+----+------+-------+------+
| 1 | John | 40 | 4 |
| 2 | Ali | 79 | 1 |
| 3 | Ben | 50 | 3 |
| 4 | Joe | 79 | 1 |
+----+------+-------+------+
I've not provided an UPDATE, because you wouldn't normally store derived data.
You can use correlated sub-query as follows:
update your_table t
set t.position = (select count(*) + 1 from your_table tt
where tt.score > t.score)

Select only latest record for every employees and for specific employee in MySQL

I have a MySQL DB and in it there's a table with activity logs of employees.
+-------------------------------------------------+
| log_id | employee_id | date_time | action_type |
+-------------------------------------------------+
| 1 | 1 | 2015/02/03 | action1 |
| 2 | 2 | 2015/02/01 | action1 |
| 3 | 2 | 2017/01/02 | action2 |
| 4 | 3 | 2016/02/12 | action1 |
| 5 | 1 | 2016/10/12 | action2 |
+-------------------------------------------------+
And I would need 2 queries. First, to get for every employee his last action. So from this example table I would need to get row 3,4 and 5 with all columns. And second, get the latest action only for specified employee.
Any ideas how to achieve this? I'm using Spring Data JPA, but raw SQL Query would be also great.
Thank you in advance.
Ready for a fred ed...
SELECT x.*
FROM my_table x
JOIN
( SELECT employee_id
, MAX(date_time) date_time
FROM my_table
GROUP
BY employee_id
) y
ON y.employee_id = x.employee_id
AND y.date_time = x.date_time;
For your first query. Simply
SELECT t1.*
FROM tableName t1
WHERE t1.log_id = (SELECT MAX(t2.log_id)
FROM tableName t2
WHERE t2.employee_id = t1.employee_id)
For the second one
SELECT t1.*
FROM tableName t1
WHERE t1.employee_id=X and t1.log_id = (SELECT MAX(t2.log_id)
FROM tableName t2
WHERE t2.employee_id = t1.employee_id);
You can get the expected output by doing a self join
select a.*
from demo a
left join demo b on a.employee_id = b.employee_id
and a.date_time < b.date_time
where b.employee_id is null
Note it may return multiple rows for single employee if there are rows with same date_time you might need a CASE statement and another attribute to decide which row should be picked to handle this kind of situation
Demo

How to replace a UNION in a query for a view?

I have created the following query to use in a view
SELECT
*
FROM
customers c
JOIN
customer_business cb
ON
c.customer_id = cb.customer_id
union
SELECT
*
FROM
customers c
LEFT JOIN
customer_business
ON
business_id=NULL;
It makes his work perfectly. It shows all customers with the business associated, and at the end, shows all customers with the info of the business in null.
customer_id | business_id
--------------------------------
1 | 1
2 | 1
2 | 2
1 | NULL
2 | NULL
3 | NULL
But the problem es that the UNION makes the view has very poor performace.
I tryed to do it with LEFT JOIN but doesnt shows al the customers with business in null, just the ones without any businesses associated
I know that the solution to speed up my view is to remove that UNION, but i cant figure out how.
Can anyone help me?
Thanks
EDIT
Here's an example
Customer Table
customer_id | name
--------------------------------
1 | test1
2 | test2
3 | test3
Customer_business Table
customer_business_id | customer_id | business_id
----------------------------------------------------------
1 | 1 | 1
2 | 1 | 2
3 | 1 | 3
4 | 2 | 1
5 | 2 | 2
Expected query result:
name | customer_id | business_id
----------------------------------------------------------
test1 | 1 | 1
test1 | 1 | 2
test1 | 1 | 3
test2 | 2 | 1
test2 | 2 | 2
test1 | 1 | NULL
test2 | 2 | NULL
test3 | 3 | NULL
Updating it based on the comments below and the output you want.
Note that I have used UNION ALL which is faster than UNION as UNION uses DISTINCT to get unique records which in your case doesn't apply. Also, make sure customer_id is PK in Customer table and try adding non-unique index on customer_id in Customer_Business table and it should help with performance.
SELECT name,
C.customer_id,
business_id
FROM Customer C
INNER JOIN Customer_Business CB
ON C.customer_id = CB.customer_id
UNION ALL
SELECT name,
C.customer_id,
NULL
FROM Customer C
Excluding the union which we know that is not performant the other thing that slows down you query is the statement in the second query ON idbusiness = NULL.
I propose to edit you query like this and see the performance as a view:
SELECT c.customer_id, idbusiness
FROM customers c
JOIN customer_business cb ON c.customer_id = cb.customer_id
UNION
SELECT customer_id, NULL
FROM customers c
EDIT:
Looking for an alternative you could try this, it should return the same output (i've changed null values with 0) but i don't think it's faster:
SELECT c.customer_id, idbusiness
FROM customers c
INNER JOIN (
SELECT customer_id, idbusiness
FROM customer_business
UNION
SELECT 0 , 0
)b ON ( c.customer_id = b.customer_id )
OR (
b.idbusiness =0
)
Eventually you could try to put into a view only the subquery b or delete the union by putting the values 0,0 as a record in table customer_business.

Mysql - Select at least one or select none

I have a table as so...
----------------------------------------
| id | name | group | number |
----------------------------------------
| 1 | joey | 1 | 2 |
| 2 | keidy | 1 | 3 |
| 3 | james | 2 | 2 |
| 4 | steven | 2 | 5 |
| 5 | jason | 3 | 2 |
| 6 | shane | 3 | 3 |
----------------------------------------
I'm running a select like so:
SELECT * FROM table WHERE number IN (2,3);
The problem im trying to solve is that I want to only grab get results from groups that have 1 or more rows of each number. For instance the above query is returning id's 1-2-3-5-6, when I'd like the results to exclude id 3 since the group of '2' can only return 1 result for the number of '2' and not for BOTH 2 and 3, since there's no row with the number 3 for the group 2 i'd like it to not even select id 3 at all.
Any help would be great.
Try it this way
SELECT *
FROM table1 t
WHERE number IN(2, 3)
AND EXISTS
(
SELECT *
FROM table1
WHERE number IN(2, 3)
AND `group` = t.`group`
GROUP BY `group`
HAVING MAX(number = 2) > 0
AND MAX(number = 3) > 0
)
or
SELECT *
FROM table1 t JOIN
(
SELECT `group`
FROM table1
WHERE number IN(2, 3)
GROUP BY `group`
HAVING MAX(number = 2) > 0
AND MAX(number = 3) > 0
) q
ON t.`group` = q.`group`;
or
SELECT *
FROM table1
WHERE `group` IN
(
SELECT `group`
FROM table1
WHERE number IN(2, 3)
GROUP BY `group`
HAVING MAX(number = 2) > 0
AND MAX(number = 3) > 0
);
Sample output (for both queries):
| ID | NAME | GROUP | NUMBER |
|----|-------|-------|--------|
| 1 | joey | 1 | 2 |
| 2 | keidy | 1 | 3 |
| 5 | jason | 3 | 2 |
| 6 | shane | 3 | 3 |
Here is SQLFiddle demo
On this, you can approach from a fun way with multiple joins for what you WANT qualified, OR, apply a prequery to get all qualified groups as others have suggested, but readability is a bit off for me..
Anyhow, here's an approach going through the table once, but with joins
select DISTINCT
T.id,
T.Name,
T.Group,
T.Number
from
YourTable T
Join YourTable T2
on T.Group = T2.Group AND T2.Group = 2
Join YourTable T3
on T.Group = T3.Group AND T3.Group = 3
where
T.Number IN ( 2, 3 )
So on the first record, it is pointing to by it's own group to the T2 group AND the T2 group is specifically a 2... Then again, but testing the group for the T3 instance and T3's group is a 3.
If it cant complete the join to either of the T2 or T3 instances, the record is done for consideration, and since indexes work great for joins like this, make sure you have one index for your NUMBER criteria, and another index on the (GROUP, NUMBER) for those comparisons and the next query sample...
If doing by more than this simple 2, but larger group, prequery qualified groups, then join to that
select
YT2.*
from
( select YT1.group
from YourTable YT1
where YT1.Number in (2, 3)
group by YT1.group
having count( DISTINCT YT1.group ) = 2 ) PreQualified
JOIN YourTable YT2
on PreQualified.group = YT2.group
AND YT2.Number in (2,3)
Maybe this,if I understand you
SELECT id FROM table WHERE `group` IN
(SELECT `group` FROM table WHERE number IN (2,3)
GROUP BY `group`
HAVING COUNT(DISTINCT number)=2)
SQL Fiddle
This will return all ids where BOTH numbers exist in a group.Remove DISTINCT if you want ids for groups where just one numbers is in.

Understanding NOT EXISTS

I'm trying to get the hang of NOT EXISTS and am having some trouble.
Say I have a 2 tables.
Employees:
+------+------+
| eid | name |
+------+------+
| 1 | Bob |
| 2 | Alice|
| 3 | Jill |
+------+------+
Transactions:
+----------+----------+----------+-----------+
| tid | eid | type | amount |
+----------+----------+----------+-----------+
| 1 | 1 | Deposit | 50 |
| 2 | 1 | Open | 500 |
| 3 | 3 | Open | 200 |
| 4 | 2 | Withdraw | 25 |
| 5 | 2 | Open | 100 |
+----------+----------+----------+-----------+
Let's say I want to find the names of all the employees that have not opened any account with the amount of $250 or higher. This means that I only want the rows where an employee has opened an account of amount < $250.
Right now I have something like this...
SELECT name FROM Employees e
WHERE NOT EXISTS (
SELECT * FROM Transactions t
WHERE t.type <> 'Open' AND t.amount >= 250 AND t.eid = e.eid);
This is obviously wrong and I don't really understand why.
You need to combine an EXISTS with a NOT EXISTS since you "only want the rows where an employee has opened an account of amount < $250.":
SELECT name FROM Employees e
WHERE EXISTS (
SELECT 1 FROM Transactions t
WHERE t.amount < 250 AND t.type='Open' AND t.eid = e.eid)
AND NOT EXISTS (
SELECT 1 FROM Transactions t
WHERE t.amount >= 250 AND t.eid = e.eid);
You need the EXISTS to ensure that only employee are returned which have an open account with amount < 250 at all. The NOT EXISTS is required to ensure that not employee are included which have additional accounts with amount >= 250.
Here's a sql-fiddle demo
The only issue I see - is that you've used <> for transaction type, not =
SELECT name FROM Employees e
WHERE NOT EXISTS (
SELECT null FROM Transactions t
WHERE t.transaction_type = 'Open' AND t.amount >= 250 AND t.eid = e.eid);
After you edited your question the answer would be:
SELECT name FROM Employees e
WHERE EXISTS (
SELECT null FROM Transactions t
WHERE t.transaction_type = 'Open' AND t.amount < 250 AND t.eid = e.eid);
I'd recommend using an LEFT JOIN instead of a sub select.
SELECT name FROM Employees e
LEFT JOIN Transactions t
ON e.eid = t.eid
WHERE t.tid IS NULL
OR t.type <> 'Open'
OR t.amount <= 250;
This will join all transaction records, and then only include records where a transaction does not exist, the user has a non-open transaction, or the amount doesn't meet the reuiqred $250