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 SQL,please!
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 a_billing.countryregioncode,
extract(day from avg(duration)) avg_duration,
round(sum(
case
when s.onlineorderflag = true then
1 else null end),0) online_country,
round(sum(
case
when s.onlineorderflag = false then
1 else null end),0) offline_country,
round(sum(
case
when s.onlineorderflag = true then
1 else 0 end)
-____(
case
when s.onlineorderflag = true then
1 else 0 end)) over(),2) online_country_dev,
round(sum(
case
when s.onlineorderflag = false then
s.totaldue else 0 end)
-____(
case
when s.onlineorderflag = false then
1 else 0 end)) over(),2) offline_country_dev
from (select *, max(orderdate) over(partition by customerid )
- min(orderdate) over(partition by customerid ) duration
from salesorderheader) s
join address a_billing
on a_billing.addressid = s.billtoaddressid
group by a_billing.countryregioncode;
Database adventurewokslt
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)
);
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.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);

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!