Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Hello! Help with SQL , please! We have following tables from database _ _ adventureworkslt: CREATE TABLE company _ abc ( cid int 4 NOT
Hello! Help with SQL please!
We have following tables from database adventureworkslt:
CREATE TABLE companyabc
cid int NOT NULL,
salestotal numeric NULL,
cls varchar NULL,
year int NOT NULL,
CONSTRAINT companyabcpk PRIMARY KEY cid year
;
CREATE TABLE public.salesorderheader
salesorderid int NOT NULL,
revisionnumber int DEFAULT NOT NULL,
orderdate timestamp DEFAULT CURRENTTIMESTAMP NOT NULL,
duedate timestamp NOT NULL,
shipdate timestamp NULL,
status int DEFAULT NOT NULL,
onlineorderflag bool DEFAULT true NOT NULL,
salesordernumber varchar NULL,
purchaseordernumber varchar NULL,
accountnumber varchar NULL,
customerid int NOT NULL,
shiptoaddressid int NULL,
billtoaddressid int NULL,
shipmethod varchar NOT NULL,
creditcardapprovalcode varchar NULL,
subtotal numeric DEFAULT NOT NULL,
taxamt numeric DEFAULT NOT NULL,
freight numeric DEFAULT NOT NULL,
totaldue numeric NOT NULL,
"comment" varchar NULL,
rowguid varchar NOT NULL,
modifieddate timestamp DEFAULT CURRENTTIMESTAMP NOT NULL,
CONSTRAINT salesorderheaderpkey PRIMARY KEY salesorderid
;
ALTER TABLE public.salesorderheader ADD CONSTRAINT salesorderheaderfk FOREIGN KEY customerid REFERENCES public.customercustomerid;
CREATE TABLE public.customer
customerid int NOT NULL,
namestyle bool DEFAULT false NOT NULL,
title varchar NULL,
firstname varchar NULL,
middlename varchar NULL,
lastname varchar NULL,
suffix varchar NULL,
companyname varchar NULL,
emailaddress varchar NULL,
rowguid varchar NOT NULL,
modifieddate timestamp DEFAULT CURRENTTIMESTAMP NOT NULL,
salespersonid int NULL,
gender varchar NULL,
totalchildren int NULL,
birthdate date NULL,
datefirstpurchase date NULL,
persontype varchar NULL,
CONSTRAINT customerpkey PRIMARY KEY customerid
;
CREATE TABLE company
id int NOT NULL GENERATED ALWAYS AS IDENTITY,
cname varchar NOT NULL,
countrycode varchar NULL,
city varchar NULL,
CONSTRAINT dcompanypk PRIMARY KEY id
;
CREATE TABLE public.salesorderdetail
salesorderid int NOT NULL,
salesorderdetailid int NOT NULL,
orderqty int NOT NULL,
productid int NOT NULL,
unitprice numeric NOT NULL,
unitpricediscount numeric DEFAULT NOT NULL,
linetotal numeric NOT NULL,
rowguid varchar NOT NULL,
modifieddate timestamp DEFAULT CURRENTTIMESTAMP NOT NULL,
CONSTRAINT salesorderdetailpk PRIMARY KEY salesorderdetailid
;
CREATE INDEX salesorderdetailproductididx ON public.salesorderdetail USING btree productid salesorderid;
ALTER TABLE public.salesorderdetail ADD CONSTRAINT salesorderdetailfk FOREIGN KEY productid REFERENCES public.productproductid;
ALTER TABLE public.salesorderdetail ADD CONSTRAINT salesorderdetailorderfk FOREIGN KEY salesorderid REFERENCES public.salesorderheadersalesorderid;
CREATE MATERIALIZED VIEW public.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;
Calculate quarterly sales amount before taxes in and individually. Fill the companysales
table using data about orders, companies, and classification results in the respective year.
The table below contains comments on filling the fields of the companysales table.
Field Description Rules
cid Company key company.id Find company.id through customer and company tables
salesamt Total amount sold before taxes Purchase volume of each product category made per quarter by
each company Calculate as sumsalesorderdetaillinetotal for combinations of year&quarter, company, and category. The salesorderdetail table contains product sales data in quantity and cash.
year Numeric year of orderDate Numeric year of salesorderheader.orderdate
quarteryr Numeric quarter number of orderDate Numeric quarter number of salesorderheader.orderdate
qr Quarter in text format. Eg or String representation of quarter in form of YYYYQ extracted from
salesorderheader.orderdate.
categoryid Product categorys key productpcid. Connect product with salesorderdetail
ccls Companys class name. Eg AB or C String name of a class to which a company belongs. Use companyabc.cls for the year from salesorderheader.orderdate
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started