Question

In: Computer Science

Write a SELECT statement that uses an aggregate window function to get the total amount of...

Write a SELECT statement that uses an aggregate window function to get the total amount of each order. Return these columns:

The order_id column from the Order_Items table

The total amount for each order item in the Order_Items table (Hint: You can calculate the total amount by subtracting the discount amount from the item price and then multiplying it by the quantity)

The total amount for each order Sort the result set in ascending sequence by the order_id column.

Part B.

Modify the solution to exercise 8 so the column that contains the total amount for each order contains a cumulative total by item amount. Add another column to the SELECT statement that uses an aggregate window function to get the average item amount for each order. Modify the SELECT statement so it uses a named window for the two aggregate functions.

Database Schema:
------------------------------
Tables: musicians, orders, order_instruments, instruments, categories

Table columns
-----------------------
musicians: musician_id, email_address, password, first_name, last_name, shipping_address_id, billing_address_id

orders: order_id, musician_id, order_date, ship_amount, tax_amount, ship_date, ship_address_id, card_type, card_number, card_expires, billing_address_id

order_instruments: item_id, order_id, instrument_id, item_price, discount_amount, quantity

instruments: instrument_id, category_id, instrument_code, instrument_name, description, list_price, discount_percent, date_added

categories: category_id, category_name

Solutions

Expert Solution

Solution for Part A(not mentioned in question)

SELECT
orders.order_id AS Order_id,
SUM(order_instruments.quantity *
(order_instruments.item_price -
(instruments.discount_percent * order_instruments.item_price)
/ 100
)
) AS Total_amount_of_each_order
FROM
orders
INNER JOIN order_instruments ON
orders.order_id = order_instruments.order_id
INNER JOIN instruments ON
order_instruments.instrument_id = instruments.instrument_id
GROUP BY
orders.order_id
ORDER BY
orders.order_id ASC;

Solution in image

Solution for Part B

SELECT
orders.order_id AS Order_id,
SUM(order_instruments.quantity *
(order_instruments.item_price -
(instruments.discount_percent * order_instruments.item_price)
/ 100
)
) AS Total_amount_of_each_order,
AVG(order_instruments.item_price) AS Average_item_amount
FROM
orders
INNER JOIN order_instruments ON
orders.order_id = order_instruments.order_id
INNER JOIN instruments ON
order_instruments.instrument_id = instruments.instrument_id
GROUP BY
orders.order_id
ORDER BY
orders.order_id ASC;

Solution in image

Please note :

What is  exercise 8, I don't know but what I understand by question I have wrote query for that.
If you any question, then please do comment. I will try to answer your questions.


Related Solutions

Question #11. Write a script that uses the menu function to prompt the user to select...
Question #11. Write a script that uses the menu function to prompt the user to select the current day of the week. Then use the menu function to prompt the user to select their favorite day of the week. Print a message telling the user how many days it will be until their favorite day. The output must include the current day, the favorite day, and the number of days until the favorite day. Debug your programming by running it...
The total amount of stockholders' equity reported on the balance sheet is often: Select one: a....
The total amount of stockholders' equity reported on the balance sheet is often: Select one: a. not accurate due to too many estimates b. all of the above c. not representative of the value of the company to its stockholders d. too hard for the average investor to locate
Use the price function to obtain the total revenue function (TR). Write the TR function then...
Use the price function to obtain the total revenue function (TR). Write the TR function then plot TR on the lower set of axes. Qx = 40000 - 200Px
Write a function in C that uses the Merge Sort sorting algorithm with arrays. The function...
Write a function in C that uses the Merge Sort sorting algorithm with arrays. The function must not be void and must output type int* i.e. it must take the form: int* merge_sort(int a[], int n) where a[ ] is the input matrix and n is the size of the matrix. You may use an auxiliary functions such as "merge." The returned array should be sorted using merge_sort and should not modify the array that was input (a[ ] ).
(In cell B15, enter a formula that uses the IF function and tests whether the total...
(In cell B15, enter a formula that uses the IF function and tests whether the total sales for Q1 (cell B8) is greater than or equal to 1000000. If the condition is true, multiply the total sales for Q1 by 0.18 to calculate a commission of 18%. If the condition is false, multiply the total sales for Q1 by 0.10 to calculate a commission of 10%.)
[Javascript] Create a function that gets the total amount and percentage of covid case data for...
[Javascript] Create a function that gets the total amount and percentage of covid case data for each Age Group in the covid cases data set(which contains the property "Age Group" in each case array). Age Groups: 'younger than 18', 'between 19 to 28 Years', 'between 29 to 38 Years','between 39 to 48 Years','between 49 to 58 Years','between 59 to 68 Years','between 69 to 78 Years','between 79 to 88 Years', and'older than 89' the function getSummaryOfAges(data) should return an Object with...
Find the total amount of ’Deposit’ transactions at the bank Find the list of transactions (statement)...
Find the total amount of ’Deposit’ transactions at the bank Find the list of transactions (statement) of September 2019 (09/01/2019 to 09/30/2019) for account ’1111222233331441’ (note: look at the date format). Find the balance of ’1111222233331441’ before 09/01/2019 ((not including 09/01/2019). Find the name of the customer that deposited the highest amount with one transaction (include the transaction amount). The SQL code to solve these problems is below: DROP DATABASE IF EXISTS Bank; CREATE DATABASE Bank; USE Bank; DROP TABLE...
PYTHON! Exercise 3 - Total Line length Write a python function that will return the total...
PYTHON! Exercise 3 - Total Line length Write a python function that will return the total length of line that passes through any number of provided points ( (x,y) ). The points should be passed as individual tuples or lists. The function should also have a parameter (True or False) to indicate whether the line should start from the origin, and that parameter should default to False. If True, the returned value should include the distance from the origin to...
Your task is to write a program in C or C++ that calculates the total amount...
Your task is to write a program in C or C++ that calculates the total amount of money a person has made over the last year. Your program will prompt the user for dollar amounts showing how much the user has made per job. Some users will have had more than one job, make sure your program allows for this. The program will print out the total made (all jobs) and will print out the federal and state taxes based...
In the space provided below write a C ++ program that computes the total amount of...
In the space provided below write a C ++ program that computes the total amount of money you are depositing in your bank account. Your program does this by asking you to first enter the number and amount of each check you are depositing, and then it asks you to enter the type of cash bills being deposited and how many of each type, also the types of coins being deposited, and the number of each coin type.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT