Question: Hello! Help me with SQL , please! We have following tables from database _ adventureworkslt: plan _ data ( versionid , country,quarterid,pcid,salesamt,CONSTRAINT planapp _ data

Hello! Help me with SQL, please!
We have following tables from database _adventureworkslt:
plan_data (versionid,country,quarterid,pcid,salesamt,CONSTRAINT planapp_data_pkey PRIMARY KEY (quarterid, country, pcid, versionid));
company_abc (cid,salestotal,cls,"year",CONSTRAINT company_abc_pk PRIMARY KEY (cid, year));
company_sales (cid,salesamt,"year",quarter_yr,qr,categoryid,ccls,CONSTRAINT company_sales_pk PRIMARY KEY (qr,cid,categoryid));
salesorderheader (salesorderid,revisionnumber,orderdate DEFAULT CURRENT_TIMESTAMP,duedate,shipdate,status,onlineorderflag bool,salesordernumber,purchaseordernumber,accountnumber,customerid,shiptoaddressid,billtoaddressid,shipmethod,creditcardapprovalcode,subtotal,taxamt,freight,totaldue,"comment",rowguid,modifieddate DEFAULT CURRENT_TIMESTAMP,CONSTRAINT salesorderheader_pkey PRIMARY KEY (salesorderid));
ALTER TABLE salesorderheader ADD CONSTRAINT salesorderheader_fk FOREIGN KEY (customerid) REFERENCES customer(customerid);
company (id GENERATED ALWAYS,cname,countrycode,city,CONSTRAINT d_company_pk PRIMARY KEY (id));
customer (customerid,namestyle bool DEFAULT false NOT NULL,title,firstname,middlename,lastname,suffix,companyname,emailaddress,rowguid,modifieddate DEFAULT CURRENT_TIMESTAMP,salespersonid,gender,totalchildren,birthdate date,datefirstpurchase date,persontype,CONSTRAINT customer_pkey PRIMARY KEY (customerid));
salesorderdetail (salesorderid,salesorderdetailid,orderqty,productid,unitprice,unitpricediscount,linetotal,rowguid,modifieddate DEFAULT CURRENT_TIMESTAMP,CONSTRAINT salesorderdetail_pk PRIMARY KEY (salesorderdetailid));
CREATE INDEX salesorderdetail_productid_idx ON salesorderdetail USING btree (productid, salesorderid);
ALTER TABLE salesorderdetail ADD CONSTRAINT salesorderdetail_fk FOREIGN KEY (productid) REFERENCES product(productid);
ALTER TABLE salesorderdetail ADD CONSTRAINT salesorderdetail_order_fk FOREIGN KEY (salesorderid) REFERENCES salesorderheader(salesorderid);
CREATE MATERIALIZED VIEW product2
TABLESPACE pg_default
AS SELECT pc.productcategoryid AS pcid,p.productid,pc.name AS pcname,p.name AS pname FROM product p
JOIN productsubcategory ps ON p.productsubcategoryid = ps.productsubcategoryid
JOIN productcategory pc ON ps.productcategoryid = pc.productcategoryid
WITH DATA;
Create a materialized view mv_plan_fact_2014_q1 to compare planned and observed sales before taxes
in 1st quarter of 2014. The view itself should show the difference between fact and plan.
The requirements for the report are described in the section 1.6.
Use the classification results from 2013 to find actual sales of A- and B-class companies in 2014.
You can choose one of these options (does not affect your grade):
1. Load data of 2014 into the company_sales table and include this table in the view
2. Calculate actual data using salesorderheader and ordersalesdetail tables without using
company_sales.
Section 1.6.
As soon as a manager has finished editing the plan and approved it, the data becomes available for planfact analysis.
The plan-fact analysis report uses only those plan values that are marked as approved by the manager.
Comparison of planned and actual values is made in terms of year, quarter, country, and product
category. Actual figures are taken from the information about sales in the y year to shops that were
members of groups A or B in the y 1 year.
If the planning period is the 1st quarter of 2020, then the actual data will be selected from the 1st quarter
of 2020 for companies that were in groups A or B as of 2019.
A report template is shown below:
Field|Quarter|Country|Category name|Dev.|Dev.,%||
Number|1|2|3|4|5||
Description|Quarter key in YYYY.Q format|Code of country|Category name|=PlanFact Plan is chosen from the A version of plan data.Fact is from fact data(linetotal)|=(PlanFact)/Plan||
Examples|2014.1,2014.2,...|AU, DE,...|Bikes,...|10000,-3000,...|10%,-30%,...||
If the approved plan data cannot be found, then null should be shown in cells 4 and 5.

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!