t-sql-assignment

12
T-SQL Introduction Assignment

Upload: nathan-harris

Post on 25-Jan-2017

84 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: T-SQL-Assignment

T-SQL Introduction Assignment

Page 2: T-SQL-Assignment

Part 1:

Code: Web Form

Imports System.DataImports System.Data.SqlClientPartial Class T_SQL_Intro_Assignment Inherits System.Web.UI.Page Private Shared Con As New SqlConnection(("Data Source = cb-ot-devst03.ad.wsu.edu; initial catalog = AdventureWorksDW2012; Persist Security Info = True;User ID = mfstudent;Password=BIanalyst")) Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim TargetMarket As New SqlDataAdapter("SELECT concat([FirstName],' ',[LastName]) as Customer,[EmailAddress],[YearlyIncome],[TotalChildren],[EnglishEducation] as Education,[StateProvinceName] as State_Name,[City] ,[EnglishCountryRegionName] as Region ,SUM([OrderQuantity]) as OrderQuantity ,SUM([SalesAmount]) as Sales_AMT FROM dbo.[DimCustomer] as CUST inner join [dbo].[FactInternetSales] as SALES on CUST.CustomerKey = SALES.CustomerKey inner join [dbo].[DimGeography] as GEO ON GEO.[GeographyKey] = CUST.[GeographyKey] Where [YearlyIncome] > 100000 and [TotalChildren] > 2 and [EnglishEducation] = 'Bachelors' Group by concat([FirstName],' ',[LastName]),[EmailAddress],[YearlyIncome],[TotalChildren], [EnglishEducation],[StateProvinceName],[City],[EnglishCountryRegionName] order by Sales_AMT DESC", Con)

Dim TargetMarketDataTable As New DataTable TargetMarket.Fill(TargetMarketDataTable) GridView1.DataSource = TargetMarketDataTable GridView1.DataBind() End Sub Protected Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click Dim Children As New SqlDataAdapter("SELECT [TotalChildren],SUM([OrderQuantity]) as Order_quantity ,SUM([SalesAmount]) as Sales_AMT ,AVG([SalesAmount]) as AVG_Sales_AMT FROM [dbo].[DimCustomer] as CUST inner join [dbo].[FactInternetSales] as SALES on CUST.CustomerKey = SALES.CustomerKey Group by [TotalChildren] order by [TotalChildren] ASC", Con)

Dim ChildrenDataTable As New DataTable Children.Fill(ChildrenDataTable) GridView1.DataSource = ChildrenDataTable GridView1.DataBind() End Sub Protected Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click

Dim Age As New SqlDataAdapter("SELECT DATEDIFF(Year,[BirthDate],GETDATE()) as Age,SUM([OrderQuantity]) as Order_quantity ,SUM([SalesAmount]) as Sales_AMT ,AVG([SalesAmount]) as AVG_Sales_AMT FROM [dbo].[DimCustomer] as CUST inner join [dbo].[FactInternetSales] as SALES on CUST.CustomerKey = SALES.CustomerKey group by DATEDIFF(Year,[BirthDate],GETDATE())order by Age ASC", Con) Dim AgeDataTable As New DataTable Age.Fill(AgeDataTable) GridView1.DataSource = AgeDataTable GridView1.DataBind()

End Sub

Protected Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click Dim Gender As New SqlDataAdapter("SELECT [Gender] ,SUM([OrderQuantity]) as Order_quantity ,SUM([SalesAmount]) as Sales_AMT ,AVG([SalesAmount]) as AVG_Sales_AMT FROM [dbo].[DimCustomer] as CUST inner join [dbo].[FactInternetSales] as SALES on CUST.CustomerKey = SALES.CustomerKey Group by [Gender] order by Gender ASC", Con) Dim GenderDataTable As New DataTable Gender.Fill(GenderDataTable) GridView1.DataSource = GenderDataTable

Page 3: T-SQL-Assignment

GridView1.DataBind() End Sub

Protected Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click Dim Education As New SqlDataAdapter("SELECT EnglishEducation ,SUM([OrderQuantity]) as Order_quantity ,SUM([SalesAmount]) as Sales_AMT ,AVG([SalesAmount]) as AVG_Sales_AMT FROM [dbo].[DimCustomer] as CUST inner join [dbo].[FactInternetSales] as SALES on CUST.CustomerKey = SALES.CustomerKey Group by EnglishEducation order by EnglishEducation ASC", Con) Dim EducationDataTable As New DataTable Education.Fill(EducationDataTable) GridView1.DataSource = EducationDataTable GridView1.DataBind()

End Sub

Protected Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click Dim MaritalStatus As New SqlDataAdapter("SELECT MaritalStatus ,SUM([OrderQuantity]) as Order_quantity ,SUM([SalesAmount]) as Sales_AMT ,AVG([SalesAmount]) as AVG_Sales_AMT FROM [dbo].[DimCustomer] as CUST inner join [dbo].[FactInternetSales] as SALES on CUST.CustomerKey = SALES.CustomerKey Group by MaritalStatus order by MaritalStatus ASC", Con)

Dim MaritalStatusDataTable As New DataTable MaritalStatus.Fill(MaritalStatusDataTable) GridView1.DataSource = MaritalStatusDataTable GridView1.DataBind() End Sub

Protected Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click Dim Title As New SqlDataAdapter("SELECT Title ,SUM([OrderQuantity]) as Order_quantity ,SUM([SalesAmount]) as Sales_AMT ,AVG([SalesAmount]) as AVG_Sales_AMT FROM [dbo].[DimCustomer] as CUST inner join [dbo].[FactInternetSales] as SALES on CUST.CustomerKey = SALES.CustomerKey Group by Title order by Title ASC", Con)

Dim TitleDataTable As New DataTable Title.Fill(TitleDataTable) GridView1.DataSource = TitleDataTable GridView1.DataBind() End Sub End Class

Page 4: T-SQL-Assignment
Page 5: T-SQL-Assignment

Part 2:

Query 1: Aggregate Number of Children

Graph and Data:

Explanation: This graph demonstrates what the correlation between the total amount of children the and the amount of sales that particular group completes. After analyzing the graph you see a trend that people with fewer children seem to be spending a lot more on the internet, this should be noted when developing a plan to seek what type of people to advertise to.

Code:USE [AdventureWorksDW2012];SELECT [OrderQuantity], [TotalChildren],SUM ([OrderQuantity]) as [Order Quantity],SUM ([SalesAmount]) AS [Total Sales],AVG ([SalesAmount]) AS [Average Sales]FROM [dbo].[DimCustomer] AS C inner join [dbo].[FactInternetSales] AS FON C.CustomerKey = F.CustomerKey

GROUP BY [TotalChildren], [OrderQuantity]ORDER BY [TotalChildren] ASC

Page 6: T-SQL-Assignment

Query 2: Aggregate Age

Graph and Data:

Explanation: This graph shows the difference between age and the amount of sales the person has. As you can tell by the graph, people within the 40-60 range spend the most money on sales in this company.

Code:USE [AdventureWorksDW2012];SELECT [OrderQuantity], [BirthDate] ,DateDiff (YEAR, [BirthDate], GETDATE()) AS [Age],SUM ([OrderQuantity]) as [Order Quantity],SUM ([SalesAmount]) AS [Total Sales],AVG ([SalesAmount]) AS [Average Sales]FROM [dbo].[DimCustomer] AS C inner join [dbo].[FactInternetSales] AS FON C.CustomerKey = F.CustomerKey

GROUP BY [BirthDate], [OrderQuantity]ORDER BY [BirthDate] ASC

Page 7: T-SQL-Assignment

Query 3: Aggregate Gender

Data and Graph:

Explanation: This data and graph is aggregated to show the total sales between men and females and the amount of sales that they produce. When examining the data and graph one could see the females slightly own the market but at the same time men are very close and marketing should be focused on both genders.

Code:USE [AdventureWorksDW2012];SELECT [OrderQuantity], [Gender] ,SUM ([OrderQuantity]) as [Order Quantity],SUM ([SalesAmount]) AS [Total Sales],AVG ([SalesAmount]) AS [Average Sales]FROM [dbo].[DimCustomer] AS C inner join [dbo].[FactInternetSales] AS FON C.CustomerKey = F.CustomerKey

GROUP BY [Gender], [OrderQuantity]ORDER BY [Gender] ASC

Page 8: T-SQL-Assignment

Query 4: Aggregate Education Level

Data and Graph:

Explanation: This data and graph shows the distribution of sales based on the markets education. For the most part people with a bachelor’s degree are the highest and marketing tactics should be adjusted for this demographic.

Code:USE [AdventureWorksDW2012];SELECT [OrderQuantity], [EnglishEducation] ,SUM ([OrderQuantity]) as [Order Quantity],SUM ([SalesAmount]) AS [Total Sales],AVG ([SalesAmount]) AS [Average Sales]FROM [dbo].[DimCustomer] AS C inner join [dbo].[FactInternetSales] AS FON C.CustomerKey = F.CustomerKey

GROUP BY [EnglishEducation], [OrderQuantity]ORDER BY [EnglishEducation] ASC

Page 9: T-SQL-Assignment

Query 5: Aggregate Marital Status

Data and Graph:

Explanation: This graph shows the difference between marital status and sales, in general it seems that people who are married usually make up more sales. But I should also be noted that people that are single also make up a large portion of this market.

Code:USE [AdventureWorksDW2012];SELECT [OrderQuantity], [MaritalStatus] ,SUM ([OrderQuantity]) as [Order Quantity],SUM ([SalesAmount]) AS [Total Sales],AVG ([SalesAmount]) AS [Average Sales]FROM [dbo].[DimCustomer] AS C inner join [dbo].[FactInternetSales] AS FON C.CustomerKey = F.CustomerKey

GROUP BY [MaritalStatus], [OrderQuantity]ORDER BY [MaritalStatus] ASC

Page 10: T-SQL-Assignment

Query 6: Aggregate Title

Data and Graph:

Explanation: This graph is meant to show the title of the purchaser and how much they make up in sales. It seems that most people do not use titles anymore should that could be taken into account when one is to try and aggregate this data.

-Code:USE [AdventureWorksDW2012];SELECT [OrderQuantity], [Title] ,SUM ([OrderQuantity]) as [Order Quantity],SUM ([SalesAmount]) AS [Total Sales],AVG ([SalesAmount]) AS [Average Sales]FROM [dbo].[DimCustomer] AS C inner join [dbo].[FactInternetSales] AS FON C.CustomerKey = F.CustomerKey

GROUP BY [Title], [OrderQuantity]ORDER BY [Title] ASC