SQL - Converting similar data while keeping different data - sql

I will try to explain this as detailed as I can if the details are insufficient please help edit my question or inquire about the lacking details for me to add in.
Problem Description
I am required to write a SELECT Statement to convert the data within ORDERED_BY from the REQUESTED_AUTHORS table into AUTHOR_NAME data. For example, JJ as shown in ORDERED_BY must be converted into Jack Johnson as shown in AUTHOR_NAME. Therefore the end results will be Jack Johnson instead of JJ. Below shows my 2 tables:
REQUESTED_AUTHORS
+-----------+
| ORDERED_BY|
+-----------+
| JJ |
+-----------+
| AB |
+-----------+
| JonJey |
+-----------+
| Admin |
+-----------+
| Tech Assit|
+-----------+
| Dr.Ob |
+-----------+
| EL |
+-----------+
| TA |
+-----------+
| JD |
+-----------+
| ET |
+-----------+
AUTHOR_LIST
+----------------+---------------------+
| ORDER_INITIAL | AUTHOR_NAME |
+----------------+---------------------+
| JJ | Jack Johnson |
+----------------+---------------------+
| AB | Albert Bently |
+----------------+---------------------+
| AlecBor | Alec Baldwin |
+----------------+---------------------+
| KingSt | KingSton |
+----------------+---------------------+
| GaryNort | Gary Norton |
+----------------+---------------------+
| Prof.Li | Professor Li |
+----------------+---------------------+
| EL | Elton Langsey |
+----------------+---------------------+
| TA | Thomas Alecson |
+----------------+---------------------+
| JD | Johnny Depp |
+----------------+---------------------+
| ET | Elson Tarese |
+----------------+---------------------+
Solution Tried (1)
SELECT ru.*, al.AUTHOR_NAME
FROM REQUESTED_AUTHORS ru, AUTHOR_LIST al
WHERE al.ORDER_INITIAL = ru.ORDERED_BY;
But this did not work as I intended it to, as there are different data in both ORDERED_BY and ORDER_INITIAL. I tried using DECODE function in order to convert it but I am stuck there.
Solution Tried (2)
SELECT ru.ORDERED_BY,
al.ORDER_INITIAL,
DECODE(ru.ORDERED_BY, (ru.ORDERED_BY != al.ORDER_INITIAL), ru.ORDERED_BY,
(ru.ORDERED_BY = al.ORDER_INITIAL), al.AUTHOR_NAME)results
FROM REQUESTED_AUTHORS ru, AUTHOR_LIST al;
What I intend on doing is changing the data with are similar to the other but keep the different data as how they are.
Meaning that the data as shown below are to be kept the same and not converted as there is nothing for it to convert to.
+-----------+
| ORDERED_BY|
+-----------+
| JonJey |
+-----------+
| Admin |
+-----------+
| Tech Assit|
+-----------+
| Dr.Ob |
+-----------+
My Question:
How may I write a query to convert the similar data and keep the different data?

You need an Outer Join (another reason to avoid old-style joins):
SELECT ru.*,
-- if there's a match return AUTHOR_NAME, otherwise keep ORDERED_BY
COALESCE(al.AUTHOR_NAME, ru.ORDERED_BY)
FROM REQUESTED_AUTHORS ru
LEFT JOIN AUTHOR_LIST al
ON al.ORDER_INITIAL = ru.ORDERED_BY;

Use left outer join here
SELECT ru.*, nvl( al.AUTHOR_NAME , ru.ordered_by)
FROM REQUESTED_AUTHORS ru, AUTHOR_LIST al
WHERE ru.ORDERED_BY = al.ORDER_INITIAL(+);

Related

How to search using a delimited string as array in query

I am trying to search for records columns that match a value within a delimited string.
I have two tables that look like this
Vehicles
| Id | Make | Model |
|----|------|-------|
| 1 | Ford | Focus |
| 2 | Ford | GT |
| 3 | Ford | Kuga |
| 4 | Audi | R8 |
Monitor
| Id | Makes | Models |
|----|-------|----------|
| 1 | Ford | GT,Focus |
| 2 | Audi | R8 |
What I'm trying to achieve is the following:
| Id | Makes | Models | Matched_Count |
|----|-------|----------|---------------|
| 2 | Audi | R8 | 1 |
| 1 | Ford | GT,Focus | 2 |
Using the following query I can get matches on singular strings, but I'm not sure how I can split the commas to search for individual models.
select Id, Makes, Models, (select count(id) from Vehicles va where UPPER(sa.Makes) = UPPER(va.Make) AND UPPER(sa.Models) = UPPER(va.Model)) as Matched_Count
from Monitor sa
(I am using a very SQL Server 2016 however I do not have access to create custom functions or variables)
If you are stuck with this data model, you can use string_split():
select m.*, v.matched_count
from monitor m outer apply
(select count(*) as matched_count
from string_split(m.models, ',') s join
vehicles v
on s.value = v.model and m.makes = v.makes
) v;
I would advise you to put your efforts into fixing the data model, though.
Here is a db<>fiddle.

Set inclusion in 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)

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

Primary keys for Join operation?

I read from a classmate post:
“Joins are usually done using primary keys in a good database design.”
Is really using primary keys as predicate necessary for good design. I can't see how.
Thank you for your help!
Use of primary keys for a good database design could be a debate. classically according to RDBMS guideline it is recommended to create primary keys for good database design. but now a days there is a trend not to put much constraints on DB side to improve performance rather do the validations on business layer (not sure if it is true for primary keys as well).
Now coming to your question,
Primary keys are not mandatory for join operations, however it is mandatory to use columns which uniquely identifies the records of master table otherwise it can generate spurious records.
department
| dept| sub_dept | dsc |
| CS | CS | Computer sc.|
| CS | IT | Info Tech. |
student
| Name | age | sex | dept | sub_dept|
| abcd | 025 | M | CS | CS |
| wxyz | 023 | M | CS | IT |
Now if you join the tables on sub_dept you will get correct results.
select s.name, s.age, s.sex, d.dsc from student s, department d where
s.sub_dept = d.sub_dept
| Name | age | sex | dsc |
| abcd | 025 | M | Computer Sc. |
| wxyz | 023 | M | Computer Sc. |
if you join the tables on dept column you will get spurious tuples (2 extra rows)
select s.name, s.age, s.sex, d.dsc from student s, department d where s.dept = d.dept
| Name | age | sex | dsc |
| abcd | 025 | M | Computer Sc. |
| wxyz | 023 | M | Computer Sc. |
| abcd | 025 | M | Info Tech. |
| wxyz | 023 | M | Computer Sc. |

MySQL Results as comma separated list

I need to run a query like:
SELECT p.id, p.name,
(SELECT name
FROM sites s
WHERE s.id = p.site_id) AS site_list
FROM publications p
But I'd like the sub-select to return a comma separated list, instead of a column of data. Is this even possible, and if so, how?
You can use GROUP_CONCAT to perform that, e.g. something like
SELECT p.id, p.name, GROUP_CONCAT(s.name) AS site_list
FROM sites s
INNER JOIN publications p ON(s.id = p.site_id)
GROUP BY p.id, p.name;
Now only I came across this situation and found some more interesting features around GROUP_CONCAT. I hope these details will make you feel interesting.
simple GROUP_CONCAT
SELECT GROUP_CONCAT(TaskName)
FROM Tasks;
Result:
+------------------------------------------------------------------+
| GROUP_CONCAT(TaskName) |
+------------------------------------------------------------------+
| Do garden,Feed cats,Paint roof,Take dog for walk,Relax,Feed cats |
+------------------------------------------------------------------+
GROUP_CONCAT with DISTINCT
SELECT GROUP_CONCAT(TaskName)
FROM Tasks;
Result:
+------------------------------------------------------------------+
| GROUP_CONCAT(TaskName) |
+------------------------------------------------------------------+
| Do garden,Feed cats,Paint roof,Take dog for walk,Relax,Feed cats |
+------------------------------------------------------------------+
GROUP_CONCAT with DISTINCT and ORDER BY
SELECT GROUP_CONCAT(DISTINCT TaskName ORDER BY TaskName DESC)
FROM Tasks;
Result:
+--------------------------------------------------------+
| GROUP_CONCAT(DISTINCT TaskName ORDER BY TaskName DESC) |
+--------------------------------------------------------+
| Take dog for walk,Relax,Paint roof,Feed cats,Do garden |
+--------------------------------------------------------+
GROUP_CONCAT with DISTINCT and SEPARATOR
SELECT GROUP_CONCAT(DISTINCT TaskName SEPARATOR ' + ')
FROM Tasks;
Result:
+----------------------------------------------------------------+
| GROUP_CONCAT(DISTINCT TaskName SEPARATOR ' + ') |
+----------------------------------------------------------------+
| Do garden + Feed cats + Paint roof + Relax + Take dog for walk |
+----------------------------------------------------------------+
GROUP_CONCAT and Combining Columns
SELECT GROUP_CONCAT(TaskId, ') ', TaskName SEPARATOR ' ')
FROM Tasks;
Result:
+------------------------------------------------------------------------------------+
| GROUP_CONCAT(TaskId, ') ', TaskName SEPARATOR ' ') |
+------------------------------------------------------------------------------------+
| 1) Do garden 2) Feed cats 3) Paint roof 4) Take dog for walk 5) Relax 6) Feed cats |
+------------------------------------------------------------------------------------+
GROUP_CONCAT and Grouped Results
Assume that the following are the results before using GROUP_CONCAT
+------------------------+--------------------------+
| ArtistName | AlbumName |
+------------------------+--------------------------+
| Iron Maiden | Powerslave |
| AC/DC | Powerage |
| Jim Reeves | Singing Down the Lane |
| Devin Townsend | Ziltoid the Omniscient |
| Devin Townsend | Casualties of Cool |
| Devin Townsend | Epicloud |
| Iron Maiden | Somewhere in Time |
| Iron Maiden | Piece of Mind |
| Iron Maiden | Killers |
| Iron Maiden | No Prayer for the Dying |
| The Script | No Sound Without Silence |
| Buddy Rich | Big Swing Face |
| Michael Learns to Rock | Blue Night |
| Michael Learns to Rock | Eternity |
| Michael Learns to Rock | Scandinavia |
| Tom Jones | Long Lost Suitcase |
| Tom Jones | Praise and Blame |
| Tom Jones | Along Came Jones |
| Allan Holdsworth | All Night Wrong |
| Allan Holdsworth | The Sixteen Men of Tain |
+------------------------+--------------------------+
USE Music;
SELECT ar.ArtistName,
GROUP_CONCAT(al.AlbumName)
FROM Artists ar
INNER JOIN Albums al
ON ar.ArtistId = al.ArtistId
GROUP BY ArtistName;
Result:
+------------------------+----------------------------------------------------------------------------+
| ArtistName | GROUP_CONCAT(al.AlbumName) |
+------------------------+----------------------------------------------------------------------------+
| AC/DC | Powerage |
| Allan Holdsworth | All Night Wrong,The Sixteen Men of Tain |
| Buddy Rich | Big Swing Face |
| Devin Townsend | Epicloud,Ziltoid the Omniscient,Casualties of Cool |
| Iron Maiden | Somewhere in Time,Piece of Mind,Powerslave,Killers,No Prayer for the Dying |
| Jim Reeves | Singing Down the Lane |
| Michael Learns to Rock | Eternity,Scandinavia,Blue Night |
| The Script | No Sound Without Silence |
| Tom Jones | Long Lost Suitcase,Praise and Blame,Along Came Jones |
+------------------------+----------------------------------------------------------------------------+
Instead of using group concat() you can use just concat()
Select concat(Col1, ',', Col2) as Foo_Bar from Table1;
edit this only works in mySQL; Oracle concat only accepts two arguments. In oracle you can use something like select col1||','||col2||','||col3 as foobar from table1;
in sql server you would use + instead of pipes.
In my case i have to concatenate all the account number of a person who's mobile number is unique. So i have used the following query to achieve that.
SELECT GROUP_CONCAT(AccountsNo) as Accounts FROM `tblaccounts` GROUP BY MobileNumber
Query Result is below:
Accounts
93348001,97530801,93348001,97530801
89663501
62630701
6227895144840002
60070021
60070020
60070019
60070018
60070017
60070016
60070015