Question: Hello! Help with SQL , please! We have following tables from database _ adventureworkslt: company _ sales ( cid , salesamt,year,quarter _ yr , qr
Hello! Help with SQL please!
We have following tables from database adventureworkslt:
companysales cidsalesamt,"year",quarteryrqrcategoryid,cclsCONSTRAINT companysalespk PRIMARY KEY qr cid, categoryid;
plandata versionidcountry,quarterid,pcid,salesamt,CONSTRAINT planappdatapkey PRIMARY KEY quarterid country, pcid, versionid;
planstatus quarteridstatus,modifieddatetime,author DEFAULT CURRENTUSER NOT NULL,country,CONSTRAINT planstatuspk PRIMARY KEY quarterid country;
company id GENERATED ALWAYScname,countrycode,city,CONSTRAINT dcompanypk PRIMARY KEY id;
MATERIALIZED VIEW country TABLESPACE pgdefault AS SELECT DISTINCT acountryregioncode AS countrycode
FROM customeraddress ca JOIN address a ON caaddressid aaddressid WHERE caaddresstype::text 'Main Office'::text WITH DATA;
Write a function in python to help managers start working with plan data.
You can call con.commit at the end of processing to apply changes to the data after you execute
SQL commands. The con variable here is a connection in psycopg library. If you do not add this
instruction, then the data will be left unmodified.
Write a function startplanningyear quarter, user, pwd where:
year is the target year of the planning period,
quarter is the target quarter of the planning period,
user is the username of the database user,
pwd is this users password
These parameters have the same meaning in the next tasks.
The function should implement the following steps:
Delete plan data from the plandata table related to the target year and quarter
Delete all records related to the target quarter from the planstatus table
Create the necessary planning status records in the planstatus table for the selected quarter. The
number of records added should be equal to the number of countries in which customercompanies shops are situated.
Generate a version N of planning data in the plandata table. Use the calculation algorithm as
described in section
Copy the data from the version N and insert it further to the plandata table changing the version
to P
Store the name of the user who called the function, in the records of the planstatus table
If initial planning data cannot be generated for some record in planstatus eg if no data can be
found in the companysales table then add rows with in salesamt column for related combination
of country and category.
If the function works correctly, the data will appear in the plandata and planstatus tables. The N
and P versions of plan data will be created.
Populate the planstatus table with the following:
ColumnDescriptionRules
quarteridKey of planning quarterString identifier of a quarter with YYYYQ value template
countryCountry of a companycompanycountry
statusPlanning statusR
modifieddatetimeTime when the record was changed or createdcurrenttimestamp
authorUser who changed the recordcurrentuser
Rules for loading data into the plandata tables are shown here:
ColumnDescriptionRules
versionidVersion of plana version of the plan: initial data generation gets N a copy from N gets P
countryCountry of a shop which orders goodscountrycountrycode or company.countrycode
quarteridKey of planning quarterString identifier of a quarter with YYYYQ value template
pcidProduct categorys keyproductpcid
salesamtSales amount before taxesaverage of total quarterly purchase volume in A and B groups calculated based on companysales.salesamt
Call this function on behalf of Ivan. The target planning period is st quarter of
Section To evaluate the expected volume of sales to shops, you need to use actual sales to customers, which
make the most of total yearly sales to companies not individuals
Company shop grouping is conducted with help of ABCanalysis based on sales data of each year.
Only A and B groups of customers are used to prepare the initial plan data.
The planning year is further denoted as y
To determine the initial target sales amount, it is necessary to calculate the quarterly sales amounts in
y and y years excluding customers in the group C based on planning period, quarter and
year of fact data, country and product category. Then calculate quarterly averages for each
combination of planning period, country and product category.
The administrator saves the initial data as an N version of the plan. The same data is copied to the
P version which is supposed to be edited by the managers.
For example, the calculation in the first quarter of for Bikes in US will require data in first
quarter of QUSBikes and the first quarter of QUSBikes The result of
calculations will be equal to QUSBikes QUSBikes
If there were no sales in one of the two years, then do not divide the number by
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
