I'm creating a small employment site and am wondering if this is possible in MySQL: I have 3 sample jobs and I want to show all users who applied to job_id = 1 who have an application status of 'pending' while showing the total number of other 'pending' and 'pending' + 'hired' applications each user has.
I've been trying to get my head around this but I'm having problems. Is this something MySQL can do?
users
+----+-------+
| ID | name |
+----+-------+
| 1 | hanna |
| 2 | bob |
| 3 | rick |
+----+-------+
job
+--------+------------+
| job_id | jobname |
+--------+------------+
| 1 | 'waiter'|
| 2 | 'janitor'|
| 3 | 'cook'|
+--------+------------+
applications
+----------+---------+-----------+
| user_id | job_id | status |
+----------+---------+-----------+
| 1 | 1 | 'pending' |
| 1 | 2 | 'pending' |
| 1 | 3 | ' hired' |
| 2 | 1 | 'pending' |
| 3 | 1 | 'removed' |
+----------+---------+-----------+
My result set
+--------+---------+-----------+---------------+--------------------+
| job_id | user_id | status | count_pending | count_pendinghired |
+--------+---------+-----------+---------------+--------------------+
| 1 | 1 | 'pending' | 2 | 3 |
| 1 | 2 | 'pending' | 1 | 1 |
+--------+---------+-----------+---------------+--------------------+
The following query comes close to your suggested output. Note that it doesn't make sense to associate a single job_id with a given user, because a user may have multiple jobs. Likewise, it also doesn't make sense to associate a single status with a given user, since each record represents an aggregation of more than one status.
SELECT user_id,
SUM(CASE WHEN status = 'pending' THEN 1 ELSE 0 END) AS count_pending,
SUM(CASE WHEN status = 'pending' OR status = 'hired'
THEN 1 ELSE 0 END) AS count_pendinghired
FROM applications
GROUP BY user_id
Related
I am not able to figure out how I can get the following result with one MySQL Query:
I have two tables:
shop_items
| id | description | price | active |
+----+-------------+-------+--------+
| 1 | product_1 | 5 | 1 |
+----+-------------+-------+--------+
| 2 | product_2 | 10 | 1 |
+----+-------------+-------+--------+
| 3 | product_3 | 15 | 0 |
+----+-------------+-------+--------+
inventory_items (the shop_items a user purchased)
| id | item_id | user_id | active |
+----+---------+---------+--------+
| 1 | 2 | 1 | 1 |
+----+---------+---------+--------+
| 2 | 1 | 1 | 0 |
+----+---------+---------+--------+
I want to see all shop_items where active = 1 including a row called purchased = 0 or 1 based on inventory_items -> matching user_id (where user_id = something) and active = 1
Example output based on the data from above tables -> where user_id = 1:
| item_id | price | description | purchased |
+---------+-------+-------------+-----------+
| 1 | 5 | product_1 | 0 |
+---------+-------+-------------+-----------+
| 2 | 10 | product_2 | 1 |
+---------+-------+-------------+-----------+
What query do I need for this output?
Please note: I only need the result from ONE user_id which I can change within the query :)
Test
SELECT shop_items.*, COALESCE(inventory_items.active, 0) purchased
FROM shop_items
LEFT JOIN inventory_items ON shop_items.id = inventory_items.item_id
AND user_id = 1
WHERE shop_items.active = 1
Let's say I have two tables:
+------------------------------------+
| `houses` |
+----+-------+-------+-------+-------+
| id | house | room1 | room2 | room3 |
+----+-------+-------+-------+-------+
| 1 | a | 1 | 1 | 2 |
| 2 | b | 1 | 3 | 1 |
| 3 | c | 2 | 2 | 1 |
+----+-------+-------+-------+-------+
+------------------------+
| `status` |
+-------------+----------+
| status_code | status |
+-------------+----------+
| 1 | empty |
| 2 | occupied |
| 3 | full |
+-------------+----------+
Now I want to change room1, room2 and room3's ids to the status from TABLE status like this:
+---------------------------------------------+
| `houses` |
+----+-------+----------+----------+----------+
| id | house | room1 | room2 | room3 |
+----+-------+----------+----------+----------+
| 1 | a | empty | empty | occupied |
| 2 | b | empty | full | empty |
| 3 | c | occupied | occupied | empty |
+----+-------+----------+----------+----------+
I know one solution, but I believe there is an easier way to do the same:
SELECT
h.*,
s1.status AS room1,
s2.status AS room2,
s3.status AS room3
FROM houses h
JOIN status s1 ON h.room1 = s1.status_code
JOIN status s2 ON h.room2 = s2.status_code
JOIN status s3 ON h.room3 = s3.status_code
UPDATE
MySQL pivot table solution is not suitable for me because the above example is a simplified version what I use. There are more than ten different status and writing a 40 line CASE function for a single room is not an opportunity. The status must come from the status table.
You can approach this as
SELECT id,house,
(
CASE
WHEN room1 > 0 THEN (SELECT status FROM status WHERE status_code=room1)
END) AS room1,
(
CASE
WHEN room2 > 0 THEN (SELECT status FROM status WHERE status_code=room2)
END) AS room2,
(
CASE
WHEN room3 > 0 THEN (SELECT status FROM status WHERE status_code=room3)
END) AS room3
FROM houses
Live DEMO
I am struggeling with a database query for 2 Hours now.
There is the following database structure:
article table
+---------------+-------------+
| id | ordernumber |
+---------------+-------------+
| 1 | 3243 |
| 2 | 3344 |
| 3 | 3423 |
| 4 | 7687 |
+---------------+-------------+
variant table
+----+-----------+-------+-------+
| id | articleId | stock | price |
+----+-----------+-------+-------+
| 1 | 1 | 3 | 10,99 |
| 2 | 1 | 0 | 10,99 |
| 3 | 1 | 1 | 10,99 |
| 4 | 2 | 0 | 11,99 |
| 5 | 2 | 0 | 11,99 |
| 6 | 2 | 1 | 11,99 |
+----+-----------+-------+-------+
I want to get all Articles where all but one variant have 0 stock.
Is this even possible with a plain sql statement? I tried with a subquery, but without success, since the subquery gets executed first and I would need to pass values from the current record of the resultset of the outer query.
Any help is much appreciated.
Edit:
Expected Result:
+----+-------------+
| id | ordernumber |
+----+-------------+
| 2 | 3344 |
+----+-------------+
If you want the full information for the variant:
select v.*
from variants v
where v.stock > 0 and
not exists (select 1
from variants v2
where v2.articleID = v.articleID and
v2.stock > 0 and
v2.id <> v.id
);
Note: this assumes that the duplicated "5" is a typo and that the ids really are unique in the table.
This can be done using group by and having.
select articleID
from variants
group by articleID
having count(*) - 1 = count(case when stock = 0 then 1 end)
I'm trying to put together a summary table that has counts of types of mail sent by group.
Hopefully the below is enough to explain what I mean.
Table 1 (senders)
| id | name | group_id |
+----+------+----------+
| 1 | mike | 1 |
| 2 | john | 1 |
| 3 | lucy | 2 |
| 4 | lobo | 3 |
Table 2 (mail)
| id | type | sender_id |
+----+----------+-----------+
| 1 | letter | 1 |
| 2 | postcard | 2 |
| 3 | postcard | 1 |
| 4 | letter | 2 |
| 5 | postcard | 2 |
| 6 | postcard | 4 |
Table 3 (groups)
| id | name | active |
+----+-------+--------+
| 1 | alpha | 1 |
| 2 | black | 1 |
| 3 | cero | 0 |
Ideal result
| group | letter | postcard | parcel |
+-------+--------+----------+--------+
| alpha | 2 | 3 | 0 |
| black | 0 | 0 | 0 |
So I need to get counts per mail type for active groups.
I've been working through examples (only learning MySQL) but when I think of this situation I'm just totally blank.
Have looked at the answers to Joining three tables to get summary data in MySQL but I don't quite understand how to translate the answers to my problem.
Any help is appreciated.
SELECT t.name,
MAX(CASE t.TYPE WHEN 'letter' THEN #CS:=#CS+1 ELSE 0 END ) letter,
MAX(CASE t.TYPE WHEN 'postcard' THEN #CS1:=#CS1+1 ELSE 0 END ) postcard ,
MAX(CASE t.TYPE WHEN 'parcel ' THEN #CS2:=#CS2+1 ELSE 0 END ) parcel
FROM
(SELECT
groups. name,
mail.type
FROM
groups
LEFT JOIN senders ON groups.id = senders.id
LEFT JOIN mail ON senders.id = mail.sender_id ) AS t
,(SELECT #CS:=0) CS ,(SELECT #CS1:=0) CS1 ,(SELECT #CS2:=0) CS2
You put this query
Select count(*) from senders s inner join mail m on s.id = s.sender_id inner join
groups g on s.groups_id = g.id group by m.type
This query isn't returning what I though it should:
Select user_id
from subs
where status = 'Failed'
and next_run_dt = '2016-08-04'
and active = '1'
group by user_id having count(status) = 2
Table structure and sample data:
id__|__status__|__next_run_dt__|__user_id__|__active__|
| | | | |
1 | Failed | 2016-08-04 | 3 | 1 |
___|__________|_______________|___________|__________|
| | | | |
2 | Failed | 2016-08-04 | 4 | 1 |
___|__________|_______________|___________|__________|
| | | | |
3 | Failed | 2016-08-04 | 3 | 1 |
___|__________|_______________|___________|__________|
The query should return the user_id of 3 because that user has two entries with at status of Failed. The query returns the user_id if I remove and next_run_dt = '2016-08-04' (date column).
What am I missing?
EDIT: There also exists the possibility that a user_id can also have a next_run_dt of null as well
Are you sure it's a date column? If it's datetime, you'll have problems.
Your query works for me on SQLFiddle.
http://sqlfiddle.com/#!9/f0bab6/1