Question: Hello! Help with SQL , please ! Consider a query written to generate a report with the following structure: - customer's country code ( billing
Hello! Help with SQLplease
Consider a query written to generate a report with the following structure:
customer's country code billing address;
average duration of interaction with customer measured in days between first and last order;
number of online orders created by customers of the country;
difference between number of online orders and respective arithmetic mean among countries;
difference between number of offline orders and respective arithmetic mean among countries.
What code should be written in place of
select abilling.countryregioncode,
extractday from avgduration avgduration,
roundsum
case
when sonlineorderflag true then
else null end onlinecountry,
roundsum
case
when sonlineorderflag false then
else null end offlinecountry,
roundsum
case
when sonlineorderflag true then
else end
case
when sonlineorderflag true then
else end over onlinecountrydev,
roundsum
case
when sonlineorderflag false then
stotaldue else end
case
when sonlineorderflag false then
else end over offlinecountrydev
from select maxorderdate overpartition by customerid
minorderdate overpartition by customerid duration
from salesorderheader s
join address abilling
on abilling.addressid sbilltoaddressid
group by abilling.countryregioncode;
Database adventurewokslt
CREATE TABLE public.customer
customerid int NOT NULL,
namestyle bool NOT NULL DEFAULT false,
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 NOT NULL DEFAULT CURRENTTIMESTAMP,
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 public.product
productid serial NOT NULL,
"name" varchar NOT NULL,
productnumber varchar NOT NULL,
color varchar NULL,
listprice numeric NOT NULL,
"size" varchar NULL,
sizeunitmeasurecode bpchar NULL,
weightunitmeasurecode bpchar NULL,
weight numeric NULL,
productline bpchar NULL,
"class" bpchar NULL,
"style" bpchar NULL,
productsubcategoryid int NULL,
productmodelid int NULL,
sellstartdate timestamp NOT NULL,
sellenddate timestamp NULL,
discontinueddate timestamp NULL,
CONSTRAINT productpkey PRIMARY KEY productid
;
CREATE INDEX idxproductcolor ON public.product USING btree color;
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 NOT NULL DEFAULT
linetotal numeric NOT NULL,
rowguid varchar NOT NULL,
modifieddate timestamp NOT NULL DEFAULT CURRENTTIMESTAMP,
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 TABLE public.salesorderheader
salesorderid int NOT NULL,
revisionnumber int NOT NULL DEFAULT
orderdate timestamp NOT NULL DEFAULT CURRENTTIMESTAMP,
duedate timestamp NOT NULL,
shipdate timestamp NULL,
status int NOT NULL DEFAULT
onlineorderflag bool NOT NULL DEFAULT true,
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 NOT NULL DEFAULT
taxamt numeric NOT NULL DEFAULT
freight numeric NOT NULL DEFAULT
totaldue numeric NOT NULL,
"comment" varchar NULL,
rowguid varchar NOT NULL,
modifieddate timestamp NOT NULL DEFAULT CURRENTTIMESTAMP,
CONSTRAINT salesorderheaderpkey PRIMARY KEY salesorderid
;
ALTER TABLE public.salesorderheader ADD CONSTRAINT salesorderheaderfk FOREIGN KEY customerid REFERENCES public.customercustomerid;
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
