Question: Hello! Help me with SQL , please! We have following tables from database _ adventureworkslt: CREATE TABLE plan _ status ( quarterid varchar ( 6

Hello! Help me with SQL, please!
We have following tables from database _adventureworkslt:
CREATE TABLE plan_status (
quarterid varchar(6) NOT NULL,
status varchar(10) NOT NULL,
modifieddatetime timestamp DEFAULT CURRENT_TIMESTAMP NOT NULL,
author varchar(20) DEFAULT CURRENT_USER NOT NULL,
country varchar(5) NOT NULL,
CONSTRAINT plan_status_pk PRIMARY KEY (quarterid, country)
);
CREATE TABLE country_managers (
username varchar(30) NOT NULL,
country varchar(5) NOT NULL,
CONSTRAINT country_managers_pk PRIMARY KEY (username, country)
);
CREATE TABLE public.plan_data (
versionid varchar(1) NOT NULL,
country varchar(5) NOT NULL,
quarterid varchar(6) NOT NULL,
pcid int4 NOT NULL,
salesamt numeric(18,2) NULL,
CONSTRAINT planapp_data_pkey PRIMARY KEY (quarterid, country, pcid, versionid)
);
Write a function in python - accept_plan(year, quarter, user, pwd). The function will copy the modified
data into the actual version of the plan.
The function is expected to select the records from the existent version of plan_data table, change the
versionid there and then change the status in the plan_status table when the records in the original
versions (P and R respectively) meet the following requirements:
Planning quarter (quarterid column) is equal to combination of year and quarter from the
function's arguments.
Version equals to 'P'(which refers to a modified version of the plan).
Data status (in plan_status table) equals to 'R'.
The current user has a permission to access the plan data according to the settings in the
country_managers table.
Implement these processing steps in the accept_plan function:
Clear the A version of plan data for specific quarter and countries accessible to the current user
Read data available to the current user from the version P and save its copy as the version A
Change the status of the processed from 'R' to 'A'
When updating the status, also save a timestamp in the modifiedtimestamp column.
Thus, after the function is applied, the tables should get their versionid and status updated to A as well
as the author and modifieddatetime columns in plan_status should show the info of who and when made
the latest changes.
Use the developed function to approve the plan of Q12014 on behalf of each manager. Check whether
the data is visible through the v_plan view:
The administrator has access to the entire plan
Manager can view the only data he/she is permitted to read and change.
Details on how each column can be populated are shown in the tables below.
Table Rules for changing records in the plan_status table
Field |Description |Rules||
quarterid |Key of planning quarter |No Changes||
country |Country of a company |No Changes||
status |Planning data status |A||
modifieddatetime |Time when the record was changed or created |current_timestamp||
author |User who changed the record |current_user||
Table Rules for loading data in the plan_data table
Field |Description |Rules||
versionid |Version of plan |A||
country |Country of a shop which orders goods |Copy from version P||
quarterid |Key of planning quarter |Copy from version P||
pcid |Product categorys key |Copy from version P||
salesamt |Sales amount before taxes |Copy from version P||

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!