Question: Hello! Help with SQL , please! We have following tables from database _ _ adventureworkslt: CREATE TABLE public.customer ( customerid int 4 NOT NULL, namestyle
Hello! Help with SQL please!
We have following tables from database adventureworkslt:
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 public.customeraddress
customerid int NOT NULL,
addressid int NOT NULL,
addresstype varchar NOT NULL,
modifieddate timestamp DEFAULT CURRENTTIMESTAMP NOT NULL,
CONSTRAINT customeraddresspk PRIMARY KEY customerid addressid, addresstype
;
ALTER TABLE public.customeraddress ADD CONSTRAINT customeraddresscfk FOREIGN KEY customerid REFERENCES public.customercustomerid;
ALTER TABLE public.customeraddress ADD CONSTRAINT customeraddressfk FOREIGN KEY addressid REFERENCES public.addressaddressid;
CREATE TABLE public.address
addressid serial NOT NULL,
addressline varchar NOT NULL,
addressline varchar NULL,
city varchar NOT NULL,
postalcode varchar NOT NULL,
spatiallocation varchar NULL,
stateprovinceid int NOT NULL,
countryregioncode varchar NOT NULL,
isonlystateprovinceflag bool NOT NULL,
stateprovincename varchar NOT NULL,
territoryid serial NOT NULL,
territoryname varchar NOT NULL,
CONSTRAINT addresspkey PRIMARY KEY addressid
;
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
;
In the current database the customer table contains information about two categories of buyers
individuals and companies. However, we consider only companies. For the convenience of further
development, fill the company table with data.
Data from the companyname field should be included in the list of companies. The country and the
city should be taken from the address table. Develop a query to load the country table.
Follow these rules in the table below. Use an addresses of Main Office type to find the appropriate
information.
Field Description Rule
id Company key Autogenerated value. No need to populate it manually
cname Company name Company name customercompanyname
countrycode Code of country Head office country addresscountryregioncode
city City name Head office city addresscity
The following tables can be used to form the company table: customer, customeraddress, address.
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
