Question: Hello! Help with SQL , please! We have the following database: advemturewokslt CREATE TABLE public.customer ( customerid int 4 NOT NULL, namestyle bool NOT NULL

Hello! Help with SQL, please!
We have the following database: advemturewokslt
CREATE TABLE public.customer (
customerid int4 NOT NULL,
namestyle bool NOT NULL DEFAULT false,
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) NOT NULL DEFAULT CURRENT_TIMESTAMP,
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)
);
ALTER TABLE public.customeraddress ADD CONSTRAINT customeraddress_c_fk FOREIGN KEY (customerid) REFERENCES public.customer(customerid);
ALTER TABLE public.customeraddress ADD CONSTRAINT customeraddress_fk FOREIGN KEY (addressid) REFERENCES public.address(addressid);
CREATE TABLE public.product (
productid serial4 NOT NULL,
"name" varchar(50) NOT NULL,
productnumber varchar(25) NOT NULL,
color varchar(15) NULL,
listprice numeric(29,3) NOT NULL,
"size" varchar(5) NULL,
sizeunitmeasurecode bpchar NULL,
weightunitmeasurecode bpchar NULL,
weight numeric(8,2) NULL,
productline bpchar NULL,
"class" bpchar NULL,
"style" bpchar NULL,
productsubcategoryid int4 NULL,
productmodelid int4 NULL,
sellstartdate timestamp(6) NOT NULL,
sellenddate timestamp(6) NULL,
discontinueddate timestamp(6) NULL,
CONSTRAINT product_pkey PRIMARY KEY (productid)
);
CREATE INDEX idx_product_color ON public.product USING btree (color);
CREATE TABLE public.productcategory (
productcategoryid serial4 NOT NULL,
"name" varchar(54) NOT NULL,
CONSTRAINT productcategory_pkey PRIMARY KEY (productcategoryid)
);
CREATE TABLE public.productsubcategory (
productsubcategoryid serial4 NOT NULL,
productcategoryid serial4 NOT NULL,
"name" varchar(54) NOT NULL,
CONSTRAINT productsubcategory_pkey PRIMARY KEY (productsubcategoryid)
);
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) NOT NULL DEFAULT 0,
linetotal numeric(38,6) NOT NULL,
rowguid varchar(36) NOT NULL,
modifieddate timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP,
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 TABLE public.salesorderheader (
salesorderid int4 NOT NULL,
revisionnumber int2 NOT NULL DEFAULT 0,
orderdate timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP,
duedate timestamp(6) NOT NULL,
shipdate timestamp(6) NULL,
status int2 NOT NULL DEFAULT 1,
onlineorderflag bool NOT NULL DEFAULT true,
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) NOT NULL DEFAULT 0,
taxamt numeric(19,4) NOT NULL DEFAULT 0,
freight numeric(19,4) NOT NULL DEFAULT 0,
totaldue numeric(19,4) NOT NULL,
"comment" varchar(4000) NULL,
rowguid varchar(36) NOT NULL,
modifieddate timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT salesorderheader_pkey PRIMARY KEY (salesorderid)
);
ALTER TABLE public.salesorderheader ADD CONSTRAINT salesorderheader_fk FOREIGN KEY (customerid) REFERENCES public.customer(customerid);
Write a query to prepare a report containing the following fields. Use lag/lead window functions:
- year of order (due date);
- month of order (due date);
- product category name;
- number of orders in current product category;
- changes in number of orders from preceding month or zero if there is no preceding data;
Customize the results layout so that records are sorted by category and date in chronological order.

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!