Hide org src dbpassword header to other place - sql

I am learning through postgres with "Practical SQL"
#+begin_src sql :engine postgresql :dbuser org :dbpassword 1618 :database analysis
select * from teachers
order by hire_date desc
limit 3;
#+end_src
#+RESULTS:
| id | first_name | last_name | school | hire_date | salary |
|----+------------+-----------+---------------------+------------+--------|
| 1 | Janet | Smith | F.D. Roosevelt HS | 2011-10-30 | 36200 |
| 4 | Samantha | Bush | Myers Middle School | 2011-10-30 | 36200 |
| 6 | Kathleen | Roush | F.D. Roosevelt HS | 2010-10-22 | 38500 |
Reference to the header of :dbpassword,
Is it possible to save it other places like the header of files so as to avoid exposing it as header in src?

You should be able to use :dbpassword (read-passwd "Password: "). The (...) is just ordinary Emacs Lisp code, you're free to use others.

Related

How do I identify distinct combinations across array-columns and then unnest in sql presto

I have a database called programs created as
CREATE TABLE programs (
name varchar(200) NOT NULL,
role varchar(200) NOT NULL,
section text[] NOT NULL,
sub_section text[] NOT NULL,
title text[] NOT NULL
);
INSERT INTO programs (name, role, section, sub_section, title) VALUES
('John','Lead','{"VII","VII","VII"}','{"A","A","C"}','{"STUDY","STUDY","STUDY"}'),
('Olga','Member','{"VII","VII"}','{"A","A"}','{"STUDY","STUDY"}'),
('Ben','Co-Lead','{"XI","X"}','{"A","B"}','{"STUDY","TRAVEL"}'),
('Ana','Member','{"VII","II","VI"}','{"A","ALL","B"}','{"STUDY","STUDY","TRAVEL"}');
Here's what the table looks like
| name | role | section | sub_section | title |
| ---- | ------- | ------------ | ----------- | ------------------------ |
| John | Lead | VII,VII,VII | A,A,C | STUDY,STUDY,STUDY |
| Olga | Member | VII,VII | A,A | STUDY,STUDY |
| Ben | Co-Lead | XI,X | A,B | STUDY,TRAVEL |
| Ana | Member | VII,II,VI | A,ALL,B | STUDY,STUDY,TRAVEL |
I want to identify distinct combinations across the section, sub-section, and title columns, as well as unnesting to get this as output
| name | role | section.sub_section | title |
| ---- | ------- | ------------------- | ------------------------ |
| John | Lead | VII.A | STUDY
| John | Lead | VII.C | STUDY
| Olga | Member | VII.A | STUDY
| Ben | Co-Lead | XI.A | STUDY
| Ben | Co-Lead | X.B | TRAVEL
| Ana | Member | VII.A | STUDY
| Ana | Member | II.ALL | STUDY
| Ana | Member | VI.B | TRAVEL
I'm fairly new to SQL and I'm really struggling with getting desired output. Your help would be very much appreciated.
You desired data does not show "combinations across the section, sub-section, and title columns", it seems that you require to match corresponding array based on positions, so you can just unnest and group by fields which you want to distinct on.
Assuming that corresponding columns contain arrays of varchars (if not - you will need to use some string functions to convert them):
-- sample data
WITH dataset (name, role, section, sub_section, title) AS (
VALUES ('John','Lead',array['VII','VII','VII'],array['A','A','C'],array['STUDY','STUDY','STUDY']),
('Olga','Member',array['VII','VII'],array['A','A'],array['STUDY','STUDY']),
('Ben','Co-Lead',array['XI','X'],array['A','B'],array['STUDY','TRAVEL']),
('Ana','Member',array['VII','II','VI'],array['A','ALL','B'],array['STUDY','STUDY','TRAVEL'])
)
--query
select name,
role,
sec || '.' || sub_sec "section.sub_section",
t title
from dataset
cross join unnest(section, sub_section, title) as t(sec, sub_sec, t)
group by name, role, sec, sub_sec, t
order by name
Output:
name
role
section.sub_section
title
Ana
Member
VII.A
STUDY
Ana
Member
II.ALL
STUDY
Ana
Member
VI.B
TRAVEL
Ben
Co-Lead
XI.A
STUDY
Ben
Co-Lead
X.B
TRAVEL
John
Lead
VII.A
STUDY
John
Lead
VII.C
STUDY
Olga
Member
VII.A
STUDY

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)

Oracle 12c modifying records in child table that includes insert, update, delete

My first post here. Here's what i'm trying to do and i'm simplifying for this question. I have 2 tables.
Companies
--------------------------------
| COMPANY_ID | NAME |
--------------------------------
| 1 | Google |
| 2 | Santas Factory|
| 3 | Pied Piper |
--------------------------------
Employees
------------------------------------------------------
| EMPLOYEE_ID | COMPANY_ID | NAME |
------------------------------------------------------
| 1 | 1 | Larry Page |
| 2 | 1 | Sergey Brin |
| 3 | 2 | Santa Claus |
------------------------------------------------------
I have a PLSQL that will insert with type objects. My question is if i have an type object of the same company what is the best practice in possibly updating, deleting or inserting to the child table?
Example payload:
Company_id: 1
Name: Google
Employees:
|__ Name: Larry Single Page
|__ Name: Sundar Pichai
How should I,
update larry Page
delete sergey brin (sorry sergey)
insert sundar pichai
or is it even possible?
Thanks,
Allen

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;

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. |