March 9, 2017 at 7:29 pm
1 - Write a query witch returns the following columns:
2 - a- OrderID
b- AverageUnitPrice (per order)
c- TotalOrderAmount (per order) - Total order amount will be (Price * Quantity * (1 - Discount))
3 - Using a SELECT from task 1 create:
4- a- A view - Name it "Sales.v_OrderInfo"
b - A table valued function - Name it "Sales.tf_OrderInfo". Function should accept a parameter "OrderID"
5- Using a SELECT from task 1 and the objects you created in task 2, please write a select statement which will return the following columns:
6 a- OrderID - the id of the order
b-LastName - Last name of the Contact name of the Customer
c- FirstName - First Name of the Contact name of the Customer
d - ProductName - The name of the product
e -Unitprice - The price of the product
f- OrderDate - The date of the order (date only)
g - AverageUnitPrice - Average unit price (for all orders) - use subquery
h - DT_AverageUnitPrice - Average unit price PER ORDER using derived table
i - DT_TotalOrderAmount - Total Order Amount PER ORDER using derived table
j- CTE_AverageUnitPrice - Average unit price PER ORDER using Common Table Expression (CTE)
k- CTE_TotalOrderAmount - Total Order Amount PER ORDER using Common Table Expression (CTE)
l - V_AverageUnitPrice - Average unit price PER ORDER using view
m - V_TotalOrderAmount - Total Order Amount PER ORDER using view
n - SUB_AverageUnitPrice - Average unit price PER ORDER using corelated sub query
o- SUB_TotalOrderAmount - Total Order Amount PER ORDER using corelated sub query
p -TF_AverageUnitPrice - Average unit price PER ORDER using table valued function
q - TF_TotalOrderAmount - Total Order Amount PER ORDER using table valued function
Filter the result set to include only orders name in the year 2006 where the category of the product is "Produce"
To get First and Last name of the contact you will have to "split" the string "ContactName". Use comma (,) as a delimiter
Write as script which will clean up the objects you created!
So, to recap:
Once I run your script:
The view and the function will be created
The select will run and I will get the result set with 17 columns and 26 rows
Task 1 should not return a result set. It is just a preparation step. Make sure you comment it out
The view and the function will be dropped
March 9, 2017 at 9:08 pm
sergehermane - Thursday, March 9, 2017 7:29 PM1 - Write a query witch returns the following columns:
2 - a- OrderID
b- AverageUnitPrice (per order)
c- TotalOrderAmount (per order) - Total order amount will be (Price * Quantity * (1 - Discount))
3 - Using a SELECT from task 1 create:
4- a- A view - Name it "Sales.v_OrderInfo"
b - A table valued function - Name it "Sales.tf_OrderInfo". Function should accept a parameter "OrderID"
5- Using a SELECT from task 1 and the objects you created in task 2, please write a select statement which will return the following columns:
6 a- OrderID - the id of the order
b-LastName - Last name of the Contact name of the Customer
c- FirstName - First Name of the Contact name of the Customer
d - ProductName - The name of the product
e -Unitprice - The price of the product
f- OrderDate - The date of the order (date only)
g - AverageUnitPrice - Average unit price (for all orders) - use subquery
h - DT_AverageUnitPrice - Average unit price PER ORDER using derived table
i - DT_TotalOrderAmount - Total Order Amount PER ORDER using derived table
j- CTE_AverageUnitPrice - Average unit price PER ORDER using Common Table Expression (CTE)
k- CTE_TotalOrderAmount - Total Order Amount PER ORDER using Common Table Expression (CTE)
l - V_AverageUnitPrice - Average unit price PER ORDER using view
m - V_TotalOrderAmount - Total Order Amount PER ORDER using view
n - SUB_AverageUnitPrice - Average unit price PER ORDER using corelated sub query
o- SUB_TotalOrderAmount - Total Order Amount PER ORDER using corelated sub query
p -TF_AverageUnitPrice - Average unit price PER ORDER using table valued function
q - TF_TotalOrderAmount - Total Order Amount PER ORDER using table valued functionFilter the result set to include only orders name in the year 2006 where the category of the product is "Produce"
To get First and Last name of the contact you will have to "split" the string "ContactName". Use comma (,) as a delimiterWrite as script which will clean up the objects you created!
So, to recap:
Once I run your script:
The view and the function will be created
The select will run and I will get the result set with 17 columns and 26 rows
Task 1 should not return a result set. It is just a preparation step. Make sure you comment it out
The view and the function will be dropped
Please, do your own homework. You're the one working for the grade and maybe a career. At least try.
--Jeff Moden
Change is inevitable... Change for the better is not.
March 9, 2017 at 9:10 pm
I already did this but i got stucked in the middle
please help
SELECT
o.orderid,
AVG(d.qty * d.unitprice) AS avgunitpriceperorder,
SUM(d.qty * d.unitprice * (1-discount)) AS totalordeamountperorder
FROM Sales.Orders AS o
INNER JOIN Sales.OrderDetails AS d ON d.orderid = o.orderid
GROUP BY o.orderid;
SELECT
o.orderid,
AVG(d.qty * d.unitprice) AS avgunitpriceperorder,
SUM(d.qty * d.unitprice * (1-discount)) AS totalordeamountperorder
FROM Sales.Orders AS o
INNER JOIN Sales.OrderDetails AS d ON d.orderid = o.orderid
GROUP BY o.orderid;
GO
CREATE VIEW sales.v_orderinfo AS
SELECT
o.orderid,
AVG(d.qty * d.unitprice) AS avgunitpriceperorder,
SUM(d.qty * d.unitprice * (1-discount)) AS totalordeamountperorder
FROM Sales.Orders AS o
INNER JOIN Sales.OrderDetails AS d ON d.orderid = o.orderid
GROUP BY o.orderid;
GO
CREATE FUNCTION Sales.tf_orderinfo
(@orderID AS INT) RETURNS TABLE
AS
RETURN
SELECT
o.orderid,
AVG(d.qty * d.unitprice) AS avgunitpriceperorder,
SUM(d.qty * d.unitprice * (1-discount)) AS totalordeamountperorder
FROM Sales.Orders AS o
INNER JOIN Sales.OrderDetails AS d ON d.orderid = o.orderid
GROUP BY o.orderid;
March 10, 2017 at 8:27 am
sergehermane - Thursday, March 9, 2017 9:10 PMI already did this but i got stucked in the middle
please help
Why did you got stuck? What don't you understand?
Viewing 4 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply