I am dealing with a table Employee Complaint which has columns EmployeeId ComplaintSeverity and ComplaintByUser. ComplaintSeverity has four level 0,1,2, and 3.
So the table will look like this ,Example
ComplaintId|EmployeeId|ComplaintSeverity|usr_id
-----------------------------------
1 | 1 | 0 | 3
2 | 2 | 1 | 4
3 | 3 | 0 | 5
4 | 1 | 2 | 4
5 | 4 | 1 | 5
6 | 2 | 2 | 2
7 | 2 | 2 | 4
Any user can complaint employee with any of these level
When client search with severitylevel as 0,
The row should fetch as
ComplaintId|EmployeeId|ComplaintSeverity
----------------------------
3 | 3 | 0
for severitylevel as 1,
ComplaintId|EmployeeId|ComplaintSeverity
----------------------------
5 | 4 | 1
for severitylevel as 2,
ComplaintId|EmployeeId|ComplaintSeverity
----------------------------
4 | 1 | 2
6 | 2 | 2
EmployeeId 1 has been complained by 2 user with severitylevel 0,2 but his highest severity level is 2. so while searching for 0 level, 1 should not be displayed.
Can anyone help me?
The question was edited after the previous answer was submitted. The following would therefore be more accurate.
SELECT x.*
FROM my_table x
JOIN
( SELECT employeeid
, MAX(complaintseverity) severity
FROM my_table
GROUP
BY employeeid
) y
ON y.employeeid = x.employeeid
AND y.severity = x.complaintseverity
WHERE complaintseverity = 0 -- optional line
ORDER
BY employeeid;
You can try following query.
SELECT *
FROM
(
SELECT cs.`EmployeeId`, MAX(cs.`ComplaintSeverity`) severity
FROM ComplaintSeverity cs
GROUP BY cs.`EmployeeId`
) csdata
WHERE csdata.severity=1
Replace 1 with the severity level you want.
Related
I thought this was going to be easy until I tackled it.
I'm trying to write a query that will produce a list of users who follow each other (like tiktok). It's not too hard to get the list of users who follow each other but my query shows both ways.
For example, the data look like:
follower_id followee_id
1 2
1 3
1 4
2 1
2 3
2 4
3 1
3 2
3 4
4 2
4 3
Using the above data, the output should be:
follower_id followee_id
1 2
1 3
2 3
2 4
3 4
Note that everybody follows everybody except 4 doesn't follow 1.
The table structure is below
CREATE TABLE follows (
follower_id INT NOT NULL,
followee_id INT NOT NULL
);
The INSERT statement:
INSERT INTO follows(follower_id, followee_id)
VALUES(1,2), (1,3), (1,4), (2,1), (2,3), (2,4), (3,1), (3,2), (3,4), (4,2), (4,3);
I think a self-join can get the users who follow each other, but I get twice as many rows as I want.
mysql> SELECT
-> f1.follower_id AS f1_follower,
-> f1.followee_id AS f1_followee,
-> f2.follower_id AS f2_follower,
-> f2.followee_id AS f2_followee
-> FROM follows f1
-> JOIN follows f2
-> ON f1.follower_id = f2.followee_id
-> AND f1.followee_id = f2.follower_id
-> ORDER BY f1.follower_id, f1.followee_id;
+-------------+-------------+-------------+-------------+
| f1_follower | f1_followee | f2_follower | f2_followee |
+-------------+-------------+-------------+-------------+
| 1 | 2 | 2 | 1 |
| 1 | 3 | 3 | 1 |
| 2 | 1 | 1 | 2 |
| 2 | 3 | 3 | 2 |
| 2 | 4 | 4 | 2 |
| 3 | 1 | 1 | 3 |
| 3 | 2 | 2 | 3 |
| 3 | 4 | 4 | 3 |
| 4 | 2 | 2 | 4 |
| 4 | 3 | 3 | 4 |
+-------------+-------------+-------------+-------------+
10 rows in set (0.00 sec)
In the first row, we see that 1 and 2 follow each other, but the third row also shows that 1 and 2 follow each other, just the reciprocal of the first row. Same goes for rows 2 and 6, rows 4 and 7, rows 5 and 9, and rows 8 and 10. So I really just want to show rows 1, 2, 4, 5, and 8 (ie., only the non-reciprocal rows). Reciprocal probably isn't the right word - the assumption in the output will be that 1, 2 means "users 1 and 2 follow each other."
I'm probably way over-thinking this one but any and all help will be greatly appreciated!
Your original query is fine you just need the additional predicate to only return one version of the relationship -
SELECT f1.*
FROM follows f1
JOIN follows f2 ON f1.follower_id = f2.followee_id AND f1.followee_id = f2.follower_id
WHERE f1.follower_id < f1.followee_id;
I have a table. It has a pk of id and an index of [service, check, datetime].
id service check datetime score
---|-------|-------|----------|-----
1 | 1 | 4 |4/03/2009 | 399
2 | 2 | 4 |4/03/2009 | 522
3 | 1 | 5 |4/03/2009 | 244
4 | 2 | 5 |4/03/2009 | 555
5 | 1 | 4 |4/04/2009 | 111
6 | 2 | 4 |4/04/2009 | 322
7 | 1 | 5 |4/05/2009 | 455
8 | 2 | 5 |4/05/2009 | 675
Given a service 2 I need to select the rows for each unique check where it has the max date. So my result would look like this table.
id service check datetime score
---|-------|-------|----------|-----
6 | 2 | 4 |4/04/2009 | 322
8 | 2 | 5 |4/05/2009 | 675
Is there a short query for this? The best I have is this, but it returns too many checks. I just need the unique checks at it's latest datetime.
SELECT * FROM table where service=?;
First you need find out the biggest date for each check
SELECT `check`, MAX(`datetime`)
FROM YourTable
WHERE `service` = 2
GROUP BY `check`
Then join back to get the rest of the data.
SELECT Y.*
FROM YourTable Y
JOIN ( SELECT `check`, MAX(`datetime`) as m_date
FROM YourTable
WHERE `service` = 2
GROUP BY check) as `filter`
ON Y.`service` = `filter`.service
AND Y.`datetime` = `fiter`.m_date
WHERE Y.`service` = 2
I have the following data in a Mysql table
ID | code | code order
1 | 1 | 1
1 | 2 | 2
1 | 3 | 3
2 | 1 | 1
2 | 2 | 2
2 | 3 | 3
3 | 1 | 1
3 | 4 | 2
3 | 5 | 3
4 | 1 | 1
4 | 4 | 2
4 | 5 | 3
4 | 6 | 4
How would I write a query to return the following results
code pattern 1,2,3 = 2 (count)
code pattern 1,4,5 = 1 (count)
code pattern 1,4,5,6 = 1 (count)
basically I need to find out the most popular code sequence, each sequence is grouped by an unique ID. The order the codes of the is also important. i.e
1,4,5,6 is different to 1,5,4,6
cheers
In MySQL, this is probably most easily done using two aggregation:
select pattern, count(*)
from (select id, group_concat(code order by code) as pattern
from t
group by id
) p
group by pattern;
I want to search user_id of 1 in table below and get other user_ids related to same mid ( in this case mid of 1 & 3)
mid | user_id
1 1
1 2
1 3
2 2
2 3
2 5
3 1
3 5
3 2
The result must be :
mid | user_id
1 1
1 2
1 3
3 1
3 5
3 2
How is it done with MySQL query ?
Assuming I understood correctly, you want to first find all mid values that have a user_id value of 1, then get all user_id values from all those previously gotten mid values.
SELECT mid, user_id from table
where mid IN (SELECT mid FROM table WHERE user_id = 1)
Assuming that you want to get all rows for those mid that matches a user_id this should do what you want:
select * from your_table t1
where exists (
select 1 from your_table t2
where user_id = 1
and t1.mid = t2.mid
)
Sample SQL Fiddle
Result given your sample data:
| MID | USER_ID |
|-----|---------|
| 1 | 1 |
| 1 | 2 |
| 1 | 3 |
| 3 | 1 |
| 3 | 5 |
| 3 | 2 |
I have two tables that I'm attempting to retrieve specific information from (duh I know). The first table seasons is semi-static data storage and the second table users_cards is used to store user choices.
The result I am hoping to achieve would go through each season, assign a "card_total" = 10 for seasons 1-3 and 11 for each season moving forward. The result would look something similar to:
SEASON_ID | TOTAL |
------------ ------------
1 | 123
2 | 234
3 | 345
4 | 456
The abbreviated & pertinent columns / sample data is as follows:
# `seasons`:
ID | ACTIVE | COMPLETE |
---- ----------- ---------------
1 | 0 | 1
2 | 0 | 1
3 | 0 | 1
4 | 1 | 0
5 | 0 | 0
# `users_cards`
# DESC: this table can store up to 10 choices per user for seasons 1-3
# and up to 11 choices for every season thereafter.
USER_ID | SEASON_ID |
------------ ---------------
1 | 1
1 | 1
2 | 1
2 | 1
1 | 2
1 | 2
1 | 2
I've played around with a few variations of this query but nothing seems to be doing the trick. This query returns the total count for each season but it's not based off of the "card_total" I mentioned above.
SELECT
c.season_id AS season_id,
c.card_total AS card_total,
c.total AS total
FROM seasons s
INNER JOIN (
SELECT
uc.season_id,
COUNT(DISTINCT(user_id)) AS total,
CASE WHEN
uc.season_id = 1
OR uc.season_id = 2
OR uc.season_id = 3
THEN 10
ELSE 11
END AS card_total
FROM users_cards uc
GROUP BY uc.season_id
) AS c ON c.season_id = s.id
WHERE s.is_active = 1
OR s.is_complete = 1
Put SUM() around your CASE...END.