In SQL, create the query using the following entity-relationship model: Retrieve
ID: 3874214 • Letter: I
Question
In SQL, create the query using the following entity-relationship model:
Retrieve a sales report for the years 2009 and 2010 summarizing PVFC sales by customer state. Only include customer states with multiple orders in a given year in the sales report. The query should output the year, customer state, number of orders in the year, the average order total, and total sales for the year. Sort the results first in ascending year, then ascending customer state. Hint: use the year function to return the year for a given date (year(date) returns year; e.g. year('9/1/2009') returns 2009).
FIGURE 2-22 Data model for Pine Valley Furniture Company in Microsoft Visio notation SALESPERSON TERRITOR SIN PK Salesperson ID Serves PK Territory ID Salesperson Name Salesperson Telephone Salesperson Fax Territory Name CUSTOMER PRODUCT LINE PK Customer ID PK Product Line ID Customer Name Customer Address Customer Postal Code Product Line Name Submits Includes ORDER PK Order ID Order Date DUC ORDER LINE PK Product ID Product Description Product Finish Product Standard Price Ordered Quantity VENDOR USES PRODUCED IN PK Vendor ID Vendor Name Vendor Address Goes Into Quantity RAW MATERIAL WORK CENTER PK Material ID PK Work Center ID Material Name Material Standard Cost Unit Of Measure Work Center Location Supply Unit Price ls Supervised By EMPLOYEE WORKS IN SupervisesPEmployee ID HAS SKILL SKILL Employee Name Employee Address PK SkillExplanation / Answer
Create the query using the following entity-relationship model:
Following Query, will join tables Order, Customer and Orderline tables and do a grouping based on the Year and customer address to retrive the results.
Query:-
Select
Year(ORDER.Order_Date) As [Year], /*Derive date from order list*/
CUSTOMER.Customer_Address As State,
Count(ORDER.Order_Date) As NumberOfOrders,
Avg(ORDERLINE.Ordered_Quantity) as AverageOrderTotal, /*Average of total order*/
Sum(ORDERLINE.Ordered_Quantity) as TotalOrders /*Total number of orders*/
from
/*customerId from ORDER and CUSTOMER table check for getting order */
ORDER JOIN CUSTOMER ON ORDER.Customer_Id = CUSTOMER.Customer_Id
JOIN ORDERLINE ON ORDERLINE.ORDER_ID = ORDER.ORDER_ID
WHERE Order_Date between '01/01/2009' and '12/31/2010'
Group by Year(Order_Date), Customer_Address
ORDER BY 1 ASC, 2 ASC
For ex:- Consider given data
Output
[YEAR] State NumberOfOrders AverageOrderTotal TotalOrders 2009 California 75000 200000 225000 2009 Florida 50000 100000 150000 2009 Texas 10000 250000 255000 2010 Florida 65000 150000 175000Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.