t- sql portfolio
TRANSCRIPT
![Page 1: T- SQL Portfolio](https://reader035.vdocuments.site/reader035/viewer/2022081821/55a1e0771a28ab11778b4787/html5/thumbnails/1.jpg)
T-SQL Portfolio
![Page 2: T- SQL Portfolio](https://reader035.vdocuments.site/reader035/viewer/2022081821/55a1e0771a28ab11778b4787/html5/thumbnails/2.jpg)
Table of Contents
p 3. Create a stored procedure to get purchases from a named
vendor for a period in time.
p 4. A simple query to return products with the word Washer in the
product name.
p 5. Create a function to return weekending dates. Use CTEs to get
shipping totals by weekending date. Select the CTEs to create a
PIVOT table that ranks the top 5 total shipping amounts by
weekending date, and lists the totals for that week by shipping
method.
p 6. Create a function to return requested data based on a price
effective date.
p 7. Creating two functions and using the “Cross Apply”.
p 8. The results of the previous query.
2
![Page 3: T- SQL Portfolio](https://reader035.vdocuments.site/reader035/viewer/2022081821/55a1e0771a28ab11778b4787/html5/thumbnails/3.jpg)
Create a stored procedure to get order ship method, freight, and tax amount for a
vendor and order date.
Create Procedure [dbo].[GetVendorOrders]
( @VendorAccountNumber nvarchar(15), @StartDate datetime, @EndDate Datetime)
AS
Begin
Select Vendor.Name AS VendorName, ShipMethod.Name AS ShipMethodName,
Sum(Freight) As TotFreight,
Sum (TaxAmt) As TotTaxAmt, sum(SubTotal) As TotSubTot
From Purchasing.PurchaseOrderHeader AS POH
Join Purchasing.ShipMethod
On POH.ShipMethodID = ShipMethod.ShipMethodID
Join Purchasing.Vendor
ON POH.VendorID = Vendor.BusinessEntityID
Where POH.OrderDate Between @StartDate and @EndDate
and Vendor.AccountNumber = @VendorAccountNumber
Group by Vendor.Name, ShipMethod.Name
End
go
Exec [dbo].[GetVendorOrders] 'ADVANCED0001', '01-01-2003', '12-31-2003'
3
![Page 4: T- SQL Portfolio](https://reader035.vdocuments.site/reader035/viewer/2022081821/55a1e0771a28ab11778b4787/html5/thumbnails/4.jpg)
Simple Query to get order totals for products with washer in the product
name.
Select Production.Product.Name, SUM(POD.LineTotal) as TotDollars
From Purchasing.PurchaseOrderHeader AS POH
join Purchasing.PurchaseOrderDetail as pod
on poh.PurchaseOrderID = pod.PurchaseOrderID
join Production.Product
on Product.productid = pod.productid
where Product.Name like '%Washer%'
and YEAR(OrderDate) = 2003
Group by Product.Name
Order by Totdollars Desc
Name TotDollars
------------------------------------------------- ----------------
Flat Washer 2 2141.055
Keyed Washer 1851.3495
Flat Washer 9 1812.636
Flat Washer 1 1777.3245
Flat Washer 7 1698.291
Flat Washer 6 1695.4245
External Lock Washer 4 1672.461
External Lock Washer 3 1652.8995
Flat Washer 3 1637.244
External Lock Washer 9 1615.0995
Flat Washer 4 1613.5245
External Lock Washer 7 1596.861
External Lock Washer 5 1577.2995
External Lock Washer 6 1539.4995
Flat Washer 5 1531.6245
Flat Washer 8 1483.398
External Lock Washer 1 1378.188
Internal Lock Washer 2 974.7675
Internal Lock Washer 5 948.6225
Internal Lock Washer 10 904.5225
4
![Page 5: T- SQL Portfolio](https://reader035.vdocuments.site/reader035/viewer/2022081821/55a1e0771a28ab11778b4787/html5/thumbnails/5.jpg)
5
--- Create a function that will take the Order Date from the Purchase Order Header and return the week ending (Saturday) date.
Create Function [dbo].[SaturdayDates]
(@OrderDate date)
Returns Date
AS
Begin
Declare @WeekEndDate Date
Set @WeekEndDate = Dateadd(day, (7 - Datepart(weekday,@OrderDate)), @OrderDate)
Return @WeekEndDate
End
GO
-- OrdersCTE will sum the orders by Week ending date- and for 2003 only. OrdersByShipMethod sums the orders by week end date(for 2003) and by ship
method.
; With OrdersByShipMethodCTE
as
( Select dbo.SaturdayDates(OrderDate) AS WeekEnding,
ShipMethodID as ShipMethod,
SUM(TotalDue) As ShipTotalDue
From Purchasing.PurchaseOrderHeader
Where YEAR(OrderDate) = 2003
group by dbo.SaturdayDates(OrderDate),ShipMethodID ) ,
OrdersTotalCTE
as
( Select dbo.SaturdayDates(OrderDate) AS WeekEndDate,
RANK () Over (Order by SUM(TotalDue)Desc) as WeekRank,
SUM(TotalDue) AS GrandTot
From Purchasing.PurchaseOrderHeader
Where YEAR(OrderDate) = 2003
Group by dbo.SaturdayDates(OrderDate) )
Select top(5) WeekEnding, GrandTot, WeekRank,
[1] As ‘ABC', [2] as ‘XYZ', [3] as 'Overseas', [4] as 'Overnight', [5] as 'Cargo'
from OrdersByShipMethodCTE
join OrdersTotalCTE
on OrdersByShipMethodCTE.WeekEnding = OrdersTotalCTE.WeekEndDate
Pivot (Sum(ShipTotalDue) for ShipMethod in ( [1], [2], [3], [4], [5]))
AS TempResults
Order by GrandTot Desc
![Page 6: T- SQL Portfolio](https://reader035.vdocuments.site/reader035/viewer/2022081821/55a1e0771a28ab11778b4787/html5/thumbnails/6.jpg)
Create a function to get the standard cost of a product for a supplied effective date. Then
select products with a standard cost of over $700 with an effective date of Jan. 01, 2010.
Create Function [dbo].[GetStandardCost]
(@EffectiveDate datetime)
Returns Table
as
Return
Select Product.ProductID as ProdID,
Product.ProductNumber as ProductNumber, Product.Name as Name,
ProductCostHistory.StandardCost as STDCost
From Production.Product
Join Production.ProductCostHistory
on Product.ProductID = ProductCostHistory.ProductID
Where ProductCostHistory.StartDate = (Select max (StartDate)
From Production.ProductCostHistory
Where Product.ProductID = ProductCostHistory.ProductID
And StartDate <= @EffectiveDate )
GO
Select ProductNumber, Name, STDCost From [dbo].[GetStandardCost]('2010-01-01')
Where STDCost > '700.00'
Order by STDCost Desc
6
![Page 7: T- SQL Portfolio](https://reader035.vdocuments.site/reader035/viewer/2022081821/55a1e0771a28ab11778b4787/html5/thumbnails/7.jpg)
7
Create Function VendorsTopYProducts(@VendorNum int, @BeginDate datetime, @EndDate datetime, @TOPY int)Returns TableASReturnSelect top(@TOPY) with ties Vendor.Name AS VendorName, Product.Name AS ProductName,Sum(LineTotal) as ProductTotalDue,Dense_Rank() Over( Order BY Sum(LineTotal)Desc) as ProductRank
From Purchasing.PurchaseOrderHeader AS POHeaderJoin Purchasing.PurchaseOrderDetail AS PODetailON POHeader.PurchaseOrderID = PODetail.PurchaseOrderIDJoin Purchasing.VendorON Vendor.BusinessEntityID = POHeader.VendorIDJoin Production.ProductON Product.ProductID = PODetail.ProductIDWhere POHeader.VendorID = @VendorNumAnd POHeader.OrderDate between @BeginDate and @EndDateGroup by Vendor.Name, Product.Name Order by ProductTotalDue Desc GO
Create Function [dbo].[TopVendors](@TOPN int, @BeginDate datetime, @EndDate datetime)Returns Tableas
Return Select TOP(@TOPN)with ties Vendor.BusinessEntityID AS BusinessEntityID, Vendor.Name AS TopVendorName,SUM(POHeader.TotalDue) AS TotalDue,DENSE_RANK () Over (Order BY SUM(POHeader.TotalDue) Desc) as VendorRank
From Purchasing.VendorJoin Purchasing.PurchaseOrderHeader AS POHeaderON POHeader.VendorID = Vendor.BusinessEntityIDWhere POHeader.OrderDate between @BeginDate and @EndDateGroup by Vendor.BusinessEntityID, Vendor.Name Order by VendorRankGO
Declare @TopN int = 5Declare @TOPY int = 5Declare @BeginDate datetime = '2003-01-01'Declare @EndDate datetime = '2004-06-30'
Select VendorName, TotalDue, VendorRank, ProductName, ProductRank, ProductTotalDueFrom [dbo].[TopVendors](@TopN , @BeginDate, @EndDate)
Cross Apply[dbo].[VendorsTopYProducts](BusinessEntityID, @BeginDate, @EndDate, @TOPY)AS TopVendorsAndProductsOrder by VendorRank
![Page 8: T- SQL Portfolio](https://reader035.vdocuments.site/reader035/viewer/2022081821/55a1e0771a28ab11778b4787/html5/thumbnails/8.jpg)
Vendor Name Total Due Vendor Rank ProductName ProductRank ProductTotalDue
Superior Bicycles 3624672.053 1 Front Brakes 1 1640123.1
Superior Bicycles 3624672.053 1 Rear Brakes 1 1640123.1
Professional Athletic Consultants 2500305.684 2 HL Road Tire 1 628978.35
Professional Athletic Consultants 2500305.684 2 ML Road Tire 2 568618.05
Professional Athletic Consultants 2500305.684 2 Touring Tire 3 566475.525
Professional Athletic Consultants 2500305.684 2 LL Road Tire 4 498648.15
Chicago City Saddles 2248030.785 3 HL Mountain Seat/Saddle 1 315234.15
Chicago City Saddles 2248030.785 3 HL Road Seat/Saddle 2 292717.425
Chicago City Saddles 2248030.785 3 HL Touring Seat/Saddle 2 292717.425
Chicago City Saddles 2248030.785 3 ML Mountain Seat/Saddle 3 234384.15
Chicago City Saddles 2248030.785 3 ML Road Seat/Saddle 4 217642.425
Chicago City Saddles 2248030.785 3 ML Touring Seat/Saddle 4 217642.425
Vision Cycles, Inc. 2083263.683 4 HL Crankarm 1 801627.75
Vision Cycles, Inc. 2083263.683 4 ML Crankarm 2 595633.5
Vision Cycles, Inc. 2083263.683 4 LL Crankarm 3 488045.25
Jackson Authority 1974933.461 5 ML Road Pedal 1 743658.3
Jackson Authority 1974933.461 5 HL Mountain Rim 2 558673.5
Jackson Authority 1974933.461 5 LL Road Pedal 3 484938.3