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
;
Split the companies into three groups according to algorithm in section below for and
Populate the companyabc table after grouping the companies. All calculations should be done in one
query. The detailed calculation rules are given below.
Field Description Rule
cid Company key Key from company table. Find the companyname from customer in the company table to retrieve its key
salestotal Sales total per year Calculated as sumsubtotal from salesoderheader table
cls Company class The class should be calculated following an algorithm in the section
year Year Get year of orderdate field from salesoderheader table
Section Company classification by annual orders
The assessment is done quarterly in terms of product categories and the countries in which the stores
are located. In this task the planning year is
It is necessary to split the buyer companies into three groups by their volume of orders in y
and y years. As a result, two ratings of companies for each year will be created. The
following algorithm should be used to define members of the three groups for one year:
Calculate the total volume of sales to companies before taxes. Denote the value as S
Calculate the upper boundary values for inclusion in the groups A and B:
Sa S
Sb S
Sort companies in descending order by the total volume of their respective purchases made per
year STi
Calculate a running total of STi and denote these values as SRTi. SRTi of a single company
should be equal to the sum of the accumulated purchase volume made by this company per year
and the aggregated purchase volumes made by all the companies that are higher in the rating.
Assign a class to eacht company i according to the following conditions:
a Class A if SRTi does not exceed Sa
b Class B if SRTi does not exceed Sb
c Class C in any other case.
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