Vous êtes sur la page 1sur 8

Copyright Shannon Graup /*Created by Shannon Graup December 3, 2013 Purpose: Part 1 - sql statements to create and populate

a database Part 2 - create sql statements to generate 10 queries using the d3RawWinery database*/ -- Use Master database USE master; GO /*Part 1 - Create the Winery database in Microsoft SQL Server using SQL statements.*/ -- Create new database called GraupSGISC9303D3Winery CREATE DATABASE GraupSGISC9303D3Winery; GO -- Use GraupSGISC9303D3Winery database USE GraupSGISC9303D3Winery; GO -- Create winery table CREATE TABLE tblWinery ( -- Set WineryID as Primary Key WineryID VARCHAR(6) PRIMARY KEY, WineryName VARCHAR(40), StreetNumber VARCHAR(6), StreetName VARCHAR(30), StreetSuffix VARCHAR (10), City VARCHAR(20) DEFAULT 'Niagara-on-the-Lake', Province VARCHAR(20), PostalCode VARCHAR(6), Country VARCHAR(20), Telephone VARCHAR(10), Fax VARCHAR(10), Email VARCHAR(50), Website VARCHAR(50), YearFounded DATE, Active BIT, );

Copyright Shannon Graup -- Create winery location table CREATE TABLE tblWineryLocation ( -- Set LocationID as Primary Key LocationID VARCHAR(2) PRIMARY KEY, -- Set WineryID as Foreign Key WineryID VARCHAR(6) NOT NULL FOREIGN KEY REFERENCES tblWinery(WineryID), UTMEasting INT, UTMNorthing INT, Latitude INT CHECK(Latitude>40), Longitude INT CHECK(Longitude<0), AreaM2 INT CHECK(AreaM2>0), ElevationM INT, AccuracyM INT, CollectorLastNamr VARCHAR(20), CollectorFirstName VARCHAR(20) ); -- Create wine type lookup table CREATE TABLE tblWineTypeLookup ( -- Set WineTypeID as Primary Key WineTypeID VARCHAR(4) PRIMARY KEY, WineType VARCHAR(20), WineTypeNotes VARCHAR(255) ); -- Create wine variety table CREATE TABLE tblWineVariety ( -- Set WineVarietyID as Primary Key WineVarietyID VARCHAR(5) PRIMARY KEY, WineVarietyName VARCHAR(20), -- Set WineTypeID as Foreign Key WineTypeID VARCHAR(4) NOT NULL FOREIGN KEY REFERENCES tblWineTypeLookup(WineTypeID) );

Copyright Shannon Graup -- Create wine product table CREATE TABLE tblWineProduct ( -- Set WineryID as Foreign Key WineryID VARCHAR(6) FOREIGN KEY REFERENCES tblWinery(WineryID), -- Set WineVarietyID as Foreign Key WineVarietyID VARCHAR(5) FOREIGN KEY REFERENCES tblWineVariety(WineVarietyID), VintageYear DATE, BottlesProduced INT, BottlePrice SMALLMONEY, -- Set Primary key (composite key) WineryID & WineVarietyID PRIMARY KEY (WineryID, WineVarietyID) ); -- Create reccommended wine variety table CREATE TABLE tblRecommendedWineVariety ( -- Set RecommendedWineVariety as Primary Key RecommendedWineVarietyID VARCHAR(5) PRIMARY KEY, -- Set WineVarietyID as Foreign Key WineVarietyID VARCHAR(5) NOT NULL FOREIGN KEY REFERENCES tblWineVariety(WineVarietyID), GrapeColour VARCHAR(15), -- Set WineTypeID as Foreign Key WineTypeID VARCHAR(4) FOREIGN KEY REFERENCES tblWineTypeLookup(WineTypeID), VarietyOrigin VARCHAR(20), ClusterType VARCHAR(20), WingedCluster BIT, MinBerrySize TINYINT, MaxBerrySize TINYINT, BerryShape VARCHAR(15), ClusterCompactness VARCHAR(30), DiseaseDisorder VARCHAR(20), FruitMaturity VARCHAR(30), WoodMaturity VARCHAR (30), ColdHardiness VARCHAR(20), KillTemp DECIMAL(5,2), Recommended BIT, );

Copyright Shannon Graup /*Part 2 - T-SQL in Microsoft SQL Server: Import the provided d3RawWinery.mdb database into SQL Server and write the SQL */ -- Use d3WineryRaw database USE d3RawWinery; GO /*q1) List the top 3 wineries in the winery table, with the [Winery Name] and their [Website] sorted by winery name in ascending order.*/ -- Q1 List top 3 wineries in the winery table, sort by winery name SELECT TOP 3 WineryName, Website FROM tblWinery ORDER BY WineryName ; /*q2) Retrieve the total number of wineries in the winery table, with only a single column name as [Total Number of Wineries].*/ -- Q2 Retrieve count of total number of wineries SELECT COUNT (WineryName) AS TotalNumberOfWineries FROM tblWinery ; /*q3) Marketing wants to target winery companies for a direct mailing sorted by winery name in ascending order. 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.*/ -- Q3 List wineries that contain 'Wine' or 'Vine' in their name SELECT WineryName FROM tblWinery WHERE WineryName LIKE '%Wine%' OR WineryName LIKE '%Vine%' ;

Copyright Shannon Graup /*q4) What the Marketing Department really wants is 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).*/ -- Q4 List the full mailing address of each winery in column called MailingAddress SELECT WineryName + ', ' + StreetNumber + ', ' + StreetName + ', ' + StreetSuffix + ', ' + City + ', ' + Province + ', ' + Country + ', ' + PostalCode AS MailingAddress FROM tblWinery WHERE WineryName LIKE '%Wine%' OR WineryName LIKE '%Vine%' ; /*q5) Summarize the [Total Number of Bottles of Wines Produced] for each [Wine Variety Name]. List the two columns and sort the [Total Number of Bottles of Wines Produced] column by descending order.*/ -- Q5 List the total number of bottles of wines produced & sort by descending order SELECT tblWineVariety.WineVarietyName, SUM(tblWineProduct.BottlesProduced) AS TotalNumberOfBottlesOfWinesProduced FROM tblWineProduct LEFT JOIN tblWineVariety ON tblWineVariety.WineVarietyID=tblWineProduct.WineVarietyID GROUP BY WineVarietyName ORDER BY TotalNumberOfBottlesOfWinesProduced DESC ;

Copyright Shannon Graup /*q6) Which winery has produced the most expensive wine per bottle? Include the [Winery Name], [Wine Type], [Wine Variety], [Vintage Year] (use only 4-digit year), [Price per Bottle], [UTM Easting], and [UTM Northing]. Keep the UTM coordinates as integers.*/ -- Q6 List the winery that has produced the most expensive bottle of wine SELECT TOP 1 MAX(tblWineProduct.BottlePrice) AS BottlePrice, tblWinery.WineryName, YEAR (VintageYear) AS VintageYear, tblWineTypeLookup.WineType, tblWineVariety.WineVarietyName, tblWineryLocation.UTMEasting, UTMNorthing FROM tblWinery FULL OUTER JOIN tblWineryLocation ON tblWinery.WineryID=tblwineryLocation.WineryID FULL OUTER JOIN tblWineProduct ON tblWinery.WineryID=tblWineProduct.WineryID FULL OUTER JOIN tblWineVariety ON tblWineProduct.WineVarietyID=tblWineVariety.WineVarietyID FULL OUTER JOIN tblWineTypeLookup ON tblWineVariety.WineTypeID=tblWineTypeLookup.WineTypeID GROUP BY WineryName, VintageYear, WineType, WineVarietyName, UTMEasting, UTMNorthing ORDER BY BottlePrice DESC ; /*q7) Retrieve the only wineries which were founded in and after 1994 with three fields: [Winery Name], [Year Founded] (use 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.*/ -- Q7 List wineries that were founded after 1994 SELECT tblWinery.WineryName, YEAR (YearFounded) AS YearFounded, SUM(tblWineProduct.BottlesProduced) AS TotalNumberOfBottlesOfWinesProduced FROM tblWineProduct FULL OUTER JOIN tblWinery ON tblWinery.WineryID=tblWineProduct.WineryID WHERE YearFounded >='1994' GROUP BY YearFounded, WineryName ORDER BY TotalNumberOfBottlesOfWinesProduced DESC ;

Copyright Shannon Graup /*q8) 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 this SQL are [Winery Name], [Wine Variety Name], [Total Number of Bottles of Chardonnay Produced], and [Vintage Year] (use only 4-digit year).*/ -- Q8 List wineries that have produced Chardonnay in 2001 SELECT tblWinery.WineryName, tblWineVariety.WineVarietyName, YEAR (VintageYear) AS VintageYear, SUM(tblWineProduct.BottlesProduced) AS TotalNumberOfBottlesOfChardonnayProduced FROM tblWineProduct FULL OUTER JOIN tblWineVariety ON tblWineVariety.WineVarietyID=tblWineProduct.WineVarietyID FULL OUTER JOIN tblWinery ON tblWinery.WineryID=tblWineProduct.WineryID WHERE WineVarietyName LIKE '%Chardonnay%' AND VintageYear ='2001' GROUP BY WineryName, WineVarietyName, VintageYear ORDER BY TotalNumberOfBottlesOfChardonnayProduced DESC ; /*q9) 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. Also display the currency data in $1,234,567.89 format.*/ -- QRY9 List wineries that have total sales over $1,000,000 SELECT tblWinery.WineryName, SUM(tblWineProduct.BottlesProduced) AS TotalNumberOfBottlesOfWinesProduced, '$' + CONVERT(varchar(12), SUM(BottlesProduced*BottlePrice)/SUM(BottlesProduced), 1) AS WeightedAveragePricePerBottle, '$' + CONVERT(varchar(12), SUM(BottlesProduced*BottlePrice), 1) AS TotalSales FROM tblWineProduct FULL OUTER JOIN tblWinery ON tblWinery.WineryID=tblWineProduct.WineryID GROUP BY WineryName HAVING SUM(BottlesProduced*BottlePrice) >='1000000' ;

Copyright Shannon Graup /*q10) 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. Note: This query is relatively complex and needs some work.*/ -- QRY10 List the two wineries that have the shortest distance between them, use Geography function to get distance from UTM coordinates SELECT TOP 1 (x.WineryID+ ', ' +y.WineryID) AS 'Winery IDs', (x1.WineryName+ ', ' +y1.WineryName) AS 'Winery Names', ROUND(GEOGRAPHY::Point(x.Latitude, x.Longitude, 4326) .STDistance (GEOGRAPHY::Point(y.Latitude, y.Longitude, 4326)) , 0) AS 'Distance Between Two Wineries In Meters' --SQRT(POWER(x.UTMEasting-y.UTMEasting, 2)) + (POWER(x.UTMNorthing-y.UTMNorthing, 2))AS DistanceBetweenTwoWineriesInMeters FROM (tblWineryLocation x FULL OUTER JOIN tblWinery x1 ON x1.WineryID = x.WineryID),(tblWineryLocation y INNER JOIN tblWinery y1 ON y1.WineryID = y.WineryID) WHERE x.WineryID < y.wineryID ORDER BY [Distance Between Two Wineries In Meters] ;

Vous aimerez peut-être aussi