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:
plandata versionidcountry,quarterid,pcid,salesamt,CONSTRAINT planappdatapkey PRIMARY KEY quarterid country, pcid, versionid;
companyabc cidsalestotal,cls"year",CONSTRAINT companyabcpk PRIMARY KEY cid year;
companysales cidsalesamt,"year",quarteryrqrcategoryid,cclsCONSTRAINT companysalespk PRIMARY KEY qrcid,categoryid;
salesorderheader salesorderidrevisionnumber,orderdate DEFAULT CURRENTTIMESTAMP,duedate,shipdate,status,onlineorderflag bool,salesordernumber,purchaseordernumber,accountnumber,customerid,shiptoaddressid,billtoaddressid,shipmethod,creditcardapprovalcode,subtotal,taxamt,freight,totaldue,"comment",rowguid,modifieddate DEFAULT CURRENTTIMESTAMP,CONSTRAINT salesorderheaderpkey PRIMARY KEY salesorderid;
ALTER TABLE salesorderheader ADD CONSTRAINT salesorderheaderfk FOREIGN KEY customerid REFERENCES customercustomerid;
company id GENERATED ALWAYS,cname,countrycode,city,CONSTRAINT dcompanypk PRIMARY KEY id;
customer customeridnamestyle bool DEFAULT false NOT NULL,title,firstname,middlename,lastname,suffix,companyname,emailaddress,rowguid,modifieddate DEFAULT CURRENTTIMESTAMP,salespersonid,gender,totalchildren,birthdate date,datefirstpurchase date,persontype,CONSTRAINT customerpkey PRIMARY KEY customerid;
salesorderdetail salesorderidsalesorderdetailid,orderqty,productid,unitprice,unitpricediscount,linetotal,rowguid,modifieddate DEFAULT CURRENTTIMESTAMP,CONSTRAINT salesorderdetailpk PRIMARY KEY salesorderdetailid;
CREATE INDEX salesorderdetailproductididx ON salesorderdetail USING btree productid salesorderid;
ALTER TABLE salesorderdetail ADD CONSTRAINT salesorderdetailfk FOREIGN KEY productid REFERENCES productproductid;
ALTER TABLE salesorderdetail ADD CONSTRAINT salesorderdetailorderfk FOREIGN KEY salesorderid REFERENCES salesorderheadersalesorderid;
CREATE MATERIALIZED VIEW product
TABLESPACE pgdefault
AS SELECT pcproductcategoryid AS pcid,pproductid,pcname AS pcname,pname AS pname FROM product p
JOIN productsubcategory ps ON pproductsubcategoryid psproductsubcategoryid
JOIN productcategory pc ON psproductcategoryid pcproductcategoryid
WITH DATA;
Create a materialized view mvplanfactq to compare planned and observed sales before taxes
in st quarter of The view itself should show the difference between fact and plan.
The requirements for the report are described in the section
Use the classification results from to find actual sales of A and Bclass companies in
You can choose one of these options does not affect your grade:
Load data of into the companysales table and include this table in the view
Calculate actual data using salesorderheader and ordersalesdetail tables without using
companysales.
Section
As soon as a manager has finished editing the plan and approved it the data becomes available for planfact analysis.
The planfact 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 year.
If the planning period is the st quarter of then the actual data will be selected from the st quarter
of for companies that were in groups A or B as of
A report template is shown below:
FieldQuarterCountryCategory nameDevDev
Number
DescriptionQuarter key in YYYYQ formatCode of countryCategory namePlanFact Plan is chosen from the A version of plan data.Fact is from fact datalinetotalPlanFactPlan
ExamplesAU DEBikes
If the approved plan data cannot be found, then null should be shown in cells and
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
