Você está na página 1de 5

James Jiang

GIS-GM Professor
Niagara College
135 Taylor Road
Niagara-on-the-Lake, ON
L0S 1J0

January 10, 2018

Dear Mr. Jiang,

RE: Deliverable 3: Winery Database T-SQL for GISC9303 GIS Database and Data Warehouse Concepts.

Please accept this letter as my formal submission of Deliverable 3: Winery Database T-SQL for GISC9303 GIS Database

and Data Warehouse Concepts.

The purpose of the assignment is to develop a basic understanding of T-SQL through designing and using a database. The

first part of the assignment is the creation of a winery database used to house winery data. The second part of the assignment

consists of 10 queries developed using T-SQL to perform various tasks within a winery database. The T-SQL document can

be located at:

X:/Students/wdrouin1/GISC9303/Deliverables/D3/DrouinWGISC9303D3Winery/DrouinWGISC9303D3Winery.sql

Should you have any concerns regarding the enclosed documents or if there are any questions please contact me at your

convivence via e-mail at winona.drouin@gmail.com or phone at (905)932-4534. I look forward to hearing from you.

Warm Regards,

Winona Drouin, BSc


GIS-GM Certificate Student
49 Canal Bank Street
Welland, ON
L3B 3M9

W.D./w.d
Enclosures: 1.) Winery Database T-SQL Part 1 (Database Creation) and Part 2 (Database Queries)
-- Author: Winona Drouin
-- Created on: December 22, 2017
-- D3: Winery Database T-SQL for GISC 9303 - GIS Database and Data Warehouse Concepts
-- Purpose: To develop T-SQL statements to create a database and to extract both attribute and spatial
information. PART 1

CREATE DATABASE DrouinWGISC9303D3Winery;


GO

USE DrouinWGISC9303D3Winery;
GO

-- Below is the creation of the winery database tables. Assisted using W3Schools.com

CREATE TABLE tblWinery


( WineID VARCHAR(6) NOT NULL PRIMARY KEY,
WineName VARCHAR(30) NOT NULL,
StreetNum VARCHAR(20) NULL,
StreetName VARCHAR(30) NULL,
StSuffix VARCHAR(20) NULL,
City VARCHAR(30) NULL,
Province VARCHAR(20) DEFAULT 'ONTARIO',
PostalCode VARCHAR(6) NULL,
Country VARCHAR(20) DEFAULT 'CANADA',
Telephone VARCHAR(14) NULL,
Fax VARCHAR(14) NULL,
Email VARCHAR(20) NULL,
Website VARCHAR(20) NULL,
YearFounded DATE NULL,
MedalAwarded BIT NULL,
WineryNotes VARCHAR(100) NULL,
);
GO

CREATE TABLE tblWineryLocation


( LocationID VARCHAR(6) NOT NULL PRIMARY KEY,
WineID VARCHAR(6) NOT NULL FOREIGN KEY REFERENCES tblWinery(WineID),
utmEasting DECIMAL(9,3) NULL,
utmNorthing DECIMAL(10,3) NULL,
Latitude DECIMAL(10,8) NULL,
Longitude DECIMAL(10,8) NULL,
AreaM2 DECIMAL(10,2) NULL,
ElevationM DECIMAL (6,1) NULL,
AccuracyM DECIMAL (4,1) NULL,
CollectorLastName VARCHAR(20) NULL,
CollectorFirstName VARCHAR(20) NULL,
);
GO

CREATE TABLE tblWineTypeLookup


( WineTypeID VARCHAR(4) NOT NULL PRIMARY KEY,
WineType VARCHAR (20) NULL,
WineTypeNotes VARCHAR(100),
);
GO

CREATE TABLE tblWineVariety


( WineVarietyID VARCHAR(5) NOT NULL PRIMARY KEY,
WineVarietyName VARCHAR(20) NULL,
WineTypeID VARCHAR(4) NOT NULL FOREIGN KEY REFERENCES tblWineTypeLookup(WineTypeID),
);
2
GO

CREATE TABLE tblWineProduct


( WineID VARCHAR(6) NOT NULL FOREIGN KEY REFERENCES tblWinery(WineID),
WineVarietyID VARCHAR(5) NOT NULL FOREIGN KEY REFERENCES tblWineVariety(WineVarietyID),
VintageYear DATE NULL,
NumBottlesProduced INTEGER NULL,
PricePerBottle MONEY NULL,
PRIMARY KEY (WineID, WineVarietyID)
);
GO

CREATE TABLE tblRecommendedWineVariety


( WineVarietyID VARCHAR (5) NOT NULL FOREIGN KEY REFERENCES tblWineVariety(WineVarietyID),
RecommendedWineVarietyID VARCHAR(5) NOT NULL PRIMARY KEY,
GrapeColour VARCHAR(20) NULL,
VarietyOrigin VARCHAR(20) NULL,
ClusterType VARCHAR(20) NULL,
WingedCluster BIT,
MinBerrySize DECIMAL(4,2) NULL,
MaxBerrySize DECIMAL (4,2) NULL,
BerryShape VARCHAR(20) NULL,
ClusterCompactness VARCHAR(30) NULL,
DiseaseDisorder VARCHAR(30) NULL,
FruitMaturity VARCHAR(30) NULL,
WoodMaturity VARCHAR(30) NULL,
ColdHardiness VARCHAR(30) NULL,
KillTemp DECIMAL (4,2) NULL,
Recommended BIT NULL,
Summary VARCHAR(100) NULL,
);
GO

--Deliverable 3: Winery Database T-SQL for GISC9303


--Created by: Winona Drouin
--Date: January 10, 2018
--Purpose: To design and use a database to perform queries using T-SQL. PART 2

USE Winery
GO

--Question 1. List the top 3 wineries with the [Winery Name] and their [Website], sorted by winery name
is ascending order.

SELECT TOP 3 WineryName, Website


FROM tblWinery
ORDER BY [WineryName] ASC;

--Question 2. How many wineries are there in the database? List only a single column name as [Total
Number of Wineries].

SELECT COUNT (WineryID)


AS [Total Number of Wineries]
FROM tblWinery;
GO

--Question 3. Write a query to list only the [Winery Name] column for those wineries whose names
contain either 'Wine' or 'Vine' letters. Assume the database is not case-sensitive for string
operations.

SELECT WineryName AS [Winery Name]


3
FROM tblWinery
WHERE Wineryname LIKE '%wine%'
OR WineryName LIKE '%vine%';
GO

--Question 4. Write a query to provide the full mailing addresses within a single column named [Mailing
Address], that includes the Winery Name, Street, City, Province, Country, PostalCode fields for the
wineries selected from the previous question (the field is separated by a comma AND a space).

SELECT WineryName+', '+StreetNumber+' '+StreetName+' '+StreetSuffix+', '+City+', '+Province+',


'+Country+', '+PostalCode AS 'Mailing Address'
FROM tblWinery
WHERE WineryName LIKE '%wine%'
OR WineryName LIKE '%vine%';
GO

--Question 5. Summarize each [Wine Variety Name] and its [Total Number of Bottles of Wines Produced]
(non-zero production). List the two columns and sort the [Total Number of Bottles of Wines Produced]
column by descending order.

SELECT WineVarietyName AS [Wine Variety Name],


SUM(BottlesProduced) AS [Total Number of Wine Bottles Produced]
FROM tblWineVariety JOIN tblWineProduct
ON tblWineProduct.WineVarietyID = tblWineVariety.WineVarietyID
GROUP BY tblWineVariety.WineVarietyName
ORDER BY [Total Number of Wine Bottles Produced] DESC;
GO

--Question 6. Which winery has produced the most expensive wine per bottle? Include the [Winery Name],
[Wine Type], [Wine Variety], [Vintage Year] (with only 4-digit year), [Price per Bottle], [UTM
Easting], and [UTM Northing].

SELECT TOP 1 WineryName, WineVarietyName, WineType, YEAR(VintageYear) 'Vintage Year', BottlePrice,


UTMEasting, UTMNorthing
FROM tblWinery
RIGHT JOIN tblWineryLocation ON tblWinery.WineryID=tblWineryLocation.WineryID
RIGHT JOIN tblWineProduct ON tblWineryLocation.WineryID=tblWineProduct.WineryID
RIGHT JOIN tblWineVariety ON tblWineProduct.WineVarietyID=tblWineVariety.WineVarietyID
RIGHT JOIN tblWineTypeLookup ON tblWineVariety.WineTypeID=tblWineTypeLookup.WineTypeID
ORDER BY BottlePrice DESC;
GO

--Question 7. Retrieve the only wineries which were founded in and after 1994 with three fields:
[Winery Name], [Year Founded] (with only 4-digit year), and [Total Number of Bottles of Wines
Produced]. The queried records are sorted by the [Total Number of Bottles of Wines Produced] by
descending order.

SELECT WineryName, YEAR(YearFounded) 'Year Founded', SUM(BottlesProduced) 'Total Bottles Produced'


FROM tblWinery
LEFT JOIN tblWineProduct ON tblWinery.WineryID=tblWineProduct.WineryID
WHERE YearFounded >= '1994'
GROUP BY WineryName, YearFounded
ORDER BY [Total Bottles Produced] DESC;
GO

--Question 8. Derive those wineries which have produced Chardonnay in 2001 and sort the [Total Number
of Bottles of Chardonnay Produced] in descending order. Fields included in the result are [Winery
Name], [Wine Variety Name], [Total Number of Bottles of Chardonnay Produced], and [Vintage Year] (use
only 4-digit year).

4
SELECT WineryName, WineVarietyName, BottlesProduced AS [Total Bottles of Chardonnay Produced],
YEAR(VintageYear) 'Vintage Year'
FROM tblWinery
INNER JOIN tblWineProduct ON tblWinery.WineryID=tblWineProduct.WineryID
INNER JOIN tblWineVariety ON tblWineProduct.WineVarietyID=tblWineVariety.WineVarietyID
WHERE WineVarietyName
LIKE '%Chardonnay%' AND VintageYear LIKE '%2001%'
ORDER BY [Total Bottles of Chardonnay Produced] DESC;
GO

--Question 9. Extract 4 pieces of information: [Winery Name], [Total Number of Bottles of Wines
Produced], [Weighted Average Price per Bottle], and [Total Sales] for those wineries whose total sales
are over 1 million dollars, ordered by the [Total Sales] in descending order. Assume that each winery
has sold out every bottle of their wines in stock at the price that was initially marketed.

SELECT WineryName, Sum(BottlesProduced) [Total Bottles of Wine Produced], CONVERT(VARCHAR(15),


SUM(BottlePrice*BottlesProduced)/SUM(BottlesProduced), 1)
AS [Weighted Average of Bottle Price], SUM(BottlesProduced*BottlePrice) As [Total Sales]
FROM tblWinery
FULL OUTER JOIN tblWineProduct ON tblWinery.WineryID=tblWineProduct.WineryID
GROUP BY WineryName
ORDER BY 'Total Sales' DESC;
GO

--Question 10. Extract two wineries, including the columns of the [Winery ID], [Winery Name], and the
[Distance between Two Wineries in Metre]. These two wineries are the closest to each other in spatial
distance, i.e. they have the shortest distance in between. Display the wineries within a single record
and round the distance to the nearest integer in metre.

SELECT TOP 1
(A.WineryID+', ' + B.WineryID) AS [Winery ID], A1.WineryName+', '+ B1.WineryName) As [Winery Name],
ROUND ((SQRT((SQUARE(A.UTMNorthing-B.UTMNorthing))+SQUARE (A.UTMEasting-B.UTMEasting))), 0)
AS [Metre Distance Between Wineries]
FROM (tblWineryLocation 1 FULL OUTER JOIN tblWinery A1 ON A1.WineryID=A.WineryID), (tblWineryLocation 2
FULL OUTER JOIN tblWinery B1 ON B1.WineryID=B.WineryID)
WHERE A.WineryID<B.WineryID
ORDER BY [Metre Distance Between Wineries] ASC;
GO

Você também pode gostar