Set inclusion in SQL - sql

The quest is to check if one set fully includes another. As simplified example we can take four tables:
worker (id, name),
worker_skills (worker_id, skill),
job (id, type)
job_required_skills (job_id, skill)
I want to match the worker to the job but only if job required skills are fully match worker skills, i. e. if worker has some skills which are not required on job it's ok, but if job has at least one skill which worker doesn't then they don't match.
All I can think of includes ridiculous amount of joins and can't be used as a serious solution, so any advices are highly appreciated. Database is postgres 9.6. Thanks!
EDIT:
Some sample data:
+------+---------------+
| name | worker_skills |
+------+---------------+
| John | java |
| John | sql |
| John | ruby |
| Jane | js |
| Jane | html |
+------+---------------+
+---------------------+-------------+
| type | job_skills |
+---------------------+-------------+
| Writing_queries | sql |
| Writing_queries | black_magic |
| Generic_programming | java |
| Frontend_stuff | js |
| Frontend_stuff | html |
+---------------------+-------------+
Result:
+------+---------------------+
| John | Generic_programming |
+------+---------------------+
| Jane | Frontend_stuff |
+------+---------------------+
John is perfectly qualified for Generic_programming (the only needed skill is in his skillset) but can't do Writing_queries as it requires some black_magic; Jane can do Frontend_stuff as she has both required skills.

You can use a left join and aggregation:
select jrs.id, ws.id
from job_required_skills jrs left join
worker_skills ws
on jrs.skill = ws.skill
group by jrs.id, ws.id
having count(*) = count(ws.skill)

Related

SQL - specific requirement to compare tables

I'm trying to merge 2 queries into 1 (cuts the number of daily queries in half): I have 2 tables, I want to do a query against 1 table, then the same query against the other table that has the same list just less entries.
Basically its a list of (let's call it for obfuscation) people and hobby. One table is ALL people & hobby, the other shorter list is people & hobby that I've met. Table 2 would all be found in table 1. Table 1 includes entries (people I have yet to meet) not found in table 2
The tables are synced up from elsewhere, what I'm looking to do is print a list of ALL people in the first column then print the hobby ONLY of people that are on both lists. That way I can see the lists merged, and track the rate at which the gap between both lists is closing. I have tried a number of SQL combinations but they either filter out the first table and match only items that are true for both (i.e. just giving me table 2) or just adding table 2 to table 1.
Example of what I'm trying to do below:
+---------+----------+--+----------+---------+--+---------+----------+
| table1 | | | table2 | | | query | |
+---------+----------+--+----------+---------+--+---------+----------+
| name | hobby | | activity | person | | name | hobby |
| bob | fishing | | fishing | bob | | bob | fishing |
| bill | vidgames | | hiking | sarah | | bill | |
| sarah | hiking | | planking | sabrina | | sarah | hiking |
| mike | cooking | | | | | mike | |
| sabrina | planking | | | | | sabrina | planking |
+---------+----------+--+----------+---------+--+---------+----------+
Normally I'd just take the few days to learn SQL a bit better however I'm stretched pretty thin at work as it is!
I should mention the table 2 is flipped and the headings are all unique (don't think this matters)!
I think you just want a left join:
select t1.name, t2.activity as hobby
from table1 t1 left join
table2 t2
on t1.name = t2.person;

Query M:N contains

I am trying to filter a set of tables that includes an M:N junction table in Android Room (SQLite).
An image can have many subjects. I'd like to allow filtering by a subject, so that I get a row with complete image information (including all subjects). So if an image had (National Park, Yosemite) filtering for either would result in one row with both keywords. Unless I messed something up, a typical join will result in multiple rows such that matching Yosemite would get the right image, but you'd be lacking National Park. I came up with this:
SELECT *,
(SELECT GROUP_CONCAT(name)
FROM meta_subject_junction
JOIN subject
ON subject.id = meta_subject_junction.subjectId
WHERE meta_subject_junction.metaId = meta.id) AS keywords,
(SELECT documentUri
FROM image_parent
WHERE meta.parentId = image_parent.id ) AS parentUri
FROM meta
Now this gets me the complete rows, but I think at this point I'd need to:
WHERE keywords LIKE(%YOSEMITE%)
and I think the LIKE is less than ideal, not to mention an imprecise match. Is there a better way to accomplish this? Thanks, this is bending my novice SQL brain.
Further details
meta
+----+----------+--+
| id | name | |
+----+----------+--+
| 1 | yosemite | |
| 2 | bryce | |
| 3 | flowers | |
+----+----------+--+
subject
+----+---------------+--+
| id | name | |
+----+---------------+--+
| 1 | National Park | |
| 2 | Yosemite | |
| 3 | Tulip | |
+----+---------------+--+
junction
+--------+-----------+
| metaId | subjectId |
+--------+-----------+
| 1 | 1 |
| 1 | 2 |
| 2 | 1 |
| 3 | 3 |
+--------+-----------+
Although I may have done something wrong, as far as I can tell Android Room doesn't like:
+----+-----------+---------------+
| id | name | subject |
+----+-----------+---------------+
| 1 | yosemite | National Park |
| 1 | yosemite | Yosemite |
+----+-----------+---------------+
so I'm trying to reduce the rows:
+----+-----------+-------------------------+
| id | name | subject |
+----+-----------+-------------------------+
| 1 | yosemite | National Park, Yosemite |
+----+-----------+-------------------------+
which the above query does. However, I also want to query for a subject. So that National Park filter will yield:
+----+-----------+-------------------------+
| id | name | subject |
+----+-----------+-------------------------+
| 1 | yosemite | National Park, Yosemite |
| 2 | bryce | National Park |
+----+-----------+-------------------------+
I'd like to be more precise/efficient than LIKE with the already 'concat' subject. Most of my attempts end up with no results in Room (multi-row) or reducing the subject to only the filter keyword.
Update
Here's a test I've been using to compare the actual SQL results from a query to what Android Room ends up with:
http://sqlfiddle.com/#!7/0ac11/10/0
That join query is interpreted as four objects in Android Room, so I'm trying to reduce the rows, but retain the full subject results while filtering for any image containing the subject keyword.
If you want multiple keywords, then where and group by and having can be used:
select image_id
from image_subject
where subject_id in ('a', 'b', 'c') -- whatever
group by image-id
having count(distinct subject_id) = 3; -- same count as in `where`
This gets the result I need, though I'd love to hear a better option if this is particularly inefficient.
SELECT meta.*,
(SELECT GROUP_CONCAT(name)
FROM junction
JOIN subject
ON subject.id = junction.subjectId
WHERE junction.metaId = meta.id) AS keywords,
junction.subjectId
FROM meta
LEFT JOIN junction ON junction.metaId = meta.id
WHERE subjectId IN (1,2)
GROUP BY meta.id
+----+----------+------------------------+-----------+
| id | name | keywords | subjectId |
+----+----------+------------------------+-----------+
| 1 | yosemite | National Park,Yosemite | 2 |
| 2 | bryce | National Park | 1 |
+----+----------+------------------------+-----------+
http://sqlfiddle.com/#!7/86a76/13

Postgres: How to combine columns into the same row value

How would I combine multiple columns that could fit into the same row instead of having the same row display many times?
flight | Manager | Lead | Worker
---------------------|-----------|-------|--------
Arizona_BGS_Flight_2 | John | |
Arizona_BGS_Flight_2 | | Will |
Arizona_BGS_Flight_2 | | | James
Utah_UTS_Flight_5 | John | |
Into:
flight | Manager | Lead | Worker
---------------------|-----------|-------|--------
Arizona_BGS_Flight_2 | John | Will | James
You can use aggregation:
select flight, max(manager) as manager, max(lead) as lead, max(worker) as worker
from t
group by flight;

Increment value when the field is the same

First, I'm sorry for the ambiguous title.
Here's my problem :
I'm using Access and I have this table :
+--------+-----------+
| PARENT | CHILD |
+--------+-----------+
| JOHN | TANIA |
| JOHN | ROBERT |
| JOHN | APRIL |
| HELEN | TOM |
| HELEN | GABRIELLE |
+--------+-----------+
And I would like to add a column like this with queries or VBA code :
+--------+-----------+---------+
| PARENT | CHILD | LIST |
+--------+-----------+---------+
| JOHN | TANIA | CHILD 1 |
| JOHN | ROBERT | CHILD 2 |
| JOHN | APRIL | CHILD 3 |
| HELEN | TOM | CHILD 1 |
| HELEN | GABRIELLE | CHILD 2 |
+--------+-----------+---------+
I want to do this because at the end, I want to run a cross tab query. I'm only missing that last column to create that query.
I tried to do it in a recordset, but my database starts bloating after a couple of rst.Update (I have 700k+ rows)
I created a temporary table and used UPDATE queries but it just takes too much time.
I think there might be a SQL code that would do what I need, but I just can't figure it out. I hope you could help me, thanks :)
You can do something like the below, but it would be much better with some sort of IDs:
SELECT Parent.PARENT,
Parent.CHILD,
(SELECT Count(*)
FROM Parent p
WHERE p.Parent=Parent.Parent
AND p.Child<=Parent.Child) AS ChildNo
FROM Parent
ORDER BY Parent.PARENT, Parent.CHILD;
Parent is the name of the table.

considering using Cross Apply but not sure

This is my first question on stackoverflow and am looking forward to everyone's feedback and solutions.
I would put my current SQL skills at the lower end of intermediate.
Simple one for most of you: I need to write a query in an oracle SQL environment that returns all transactions after the active employees departure date.
Table looks like this:
| Name | dept | departure date |
| John | Sales | 3.12.2014 |
| David | IT | 7.27.2014 |
| Gary | Audit | 12.5.2013 |
Transaction table
| TransID | Emp Name | Amount | TransDate |
| 1 | John | 25.00 | 3.31.2014 |
| 2 | David | 30.00 | 8.13.204 |
| 3 | Gary | 15.00 | 1.1.2014 |
I'm trying to avoid a UNION ALL since the table has over 100+ employee records. On researching the use of CROSS APPLY it seemed like it could fit the situation. Any ideas are appreciated. Thanks!
Josh
You can just use a join:
select t.*
from employees e join
transactions t
on e.emp = t.emp and e.date < t.transdate;
You could write this using apply, but I think a join makes the intention more clear.