Question: 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 company_abc (
cid int4 NOT NULL,
salestotal numeric NULL,
cls varchar(1) NULL,
year int4 NOT NULL,
CONSTRAINT company_abc_pk PRIMARY KEY (cid, year)
);
CREATE TABLE public.salesorderheader (
salesorderid int4 NOT NULL,
revisionnumber int2 DEFAULT 0 NOT NULL,
orderdate timestamp(6) DEFAULT CURRENT_TIMESTAMP NOT NULL,
duedate timestamp(6) NOT NULL,
shipdate timestamp(6) NULL,
status int2 DEFAULT 1 NOT NULL,
onlineorderflag bool DEFAULT true NOT NULL,
salesordernumber varchar(25) NULL,
purchaseordernumber varchar(25) NULL,
accountnumber varchar(15) NULL,
customerid int4 NOT NULL,
shiptoaddressid int4 NULL,
billtoaddressid int4 NULL,
shipmethod varchar(50) NOT NULL,
creditcardapprovalcode varchar(15) NULL,
subtotal numeric(19,4) DEFAULT 0 NOT NULL,
taxamt numeric(19,4) DEFAULT 0 NOT NULL,
freight numeric(19,4) DEFAULT 0 NOT NULL,
totaldue numeric(19,4) NOT NULL,
"comment" varchar(4000) NULL,
rowguid varchar(36) NOT NULL,
modifieddate timestamp(6) DEFAULT CURRENT_TIMESTAMP NOT NULL,
CONSTRAINT salesorderheader_pkey PRIMARY KEY (salesorderid)
);
ALTER TABLE public.salesorderheader ADD CONSTRAINT salesorderheader_fk FOREIGN KEY (customerid) REFERENCES public.customer(customerid);
CREATE TABLE public.customer (
customerid int4 NOT NULL,
namestyle bool DEFAULT false NOT NULL,
title varchar(8) NULL,
firstname varchar(50) NULL,
middlename varchar(50) NULL,
lastname varchar(50) NULL,
suffix varchar(10) NULL,
companyname varchar(128) NULL,
emailaddress varchar(50) NULL,
rowguid varchar(36) NOT NULL,
modifieddate timestamp(6) DEFAULT CURRENT_TIMESTAMP NOT NULL,
salespersonid int4 NULL,
gender varchar(1) NULL,
totalchildren int4 NULL,
birthdate date NULL,
datefirstpurchase date NULL,
persontype varchar(3) NULL,
CONSTRAINT customer_pkey PRIMARY KEY (customerid)
);
CREATE TABLE company (
id int4 NOT NULL GENERATED ALWAYS AS IDENTITY,
cname varchar(200) NOT NULL,
countrycode varchar(10) NULL,
city varchar(30) NULL,
CONSTRAINT d_company_pk PRIMARY KEY (id)
);
CREATE TABLE public.salesorderdetail (
salesorderid int4 NOT NULL,
salesorderdetailid int4 NOT NULL,
orderqty int2 NOT NULL,
productid int4 NOT NULL,
unitprice numeric(19,4) NOT NULL,
unitpricediscount numeric(19,4) DEFAULT 0 NOT NULL,
linetotal numeric(38,6) NOT NULL,
rowguid varchar(36) NOT NULL,
modifieddate timestamp(6) DEFAULT CURRENT_TIMESTAMP NOT NULL,
CONSTRAINT salesorderdetail_pk PRIMARY KEY (salesorderdetailid)
);
CREATE INDEX salesorderdetail_productid_idx ON public.salesorderdetail USING btree (productid, salesorderid);
ALTER TABLE public.salesorderdetail ADD CONSTRAINT salesorderdetail_fk FOREIGN KEY (productid) REFERENCES public.product(productid);
ALTER TABLE public.salesorderdetail ADD CONSTRAINT salesorderdetail_order_fk FOREIGN KEY (salesorderid) REFERENCES public.salesorderheader(salesorderid);
CREATE MATERIALIZED VIEW public.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;
Calculate quarterly sales amount before taxes in 2012 and 2013 individually. Fill the company_sales
table using data about orders, companies, and classification results in the respective year.
The table below contains comments on filling the fields of the company_sales 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 sum(salesorderdetail.linetotal) 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||
quarter_yr | Numeric quarter number of orderDate | Numeric quarter number of salesorderheader.orderdate||
qr | Quarter in text format. E.g.,2022.1 or 2023.4| String representation of quarter in form of YYYY.Q extracted from
salesorderheader.orderdate.||
categoryid | Product categorys key | product2.pcid. Connect product2 with salesorderdetail||
ccls | Companys class name. E.g., A,B or C.| String name of a class to which a company belongs. Use company_abc.cls for the year from salesorderheader.orderdate||

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!