Question

In: Computer Science

From the following mySql query create a function that returns a nth value of a table:...

From the following mySql query create a function that returns a nth value of a table:

SELECT name, (SUM(price * quantity)) AS TotalPrice

FROM Items NATURAL JOIN Inventory

GROUP BY name

ORDER BY (TotalPrice) DESC

LIMIT 1, OFFSET 1;

Shema:

Inventory(name: String,item:String,quantity:integer)

Items(item: String, brand: String, price:double)

Solutions

Expert Solution

MySQL Function to return the nth value of table:

Syntax to create MySQL Function:
CREATE FUNCTION function_name(param1,param2,…)
RETURNS datatype
BEGIN
    SQL statements
END;

MySQL Function to return the nth value

CREATE FUNCTION GET_NthVal(n INT) RETURNS DOUBLE
BEGIN
   /*VARIABLE DECLARATION TO STORE Nth VALUE*/
   DECLARE NVal DOUBLE DEFAULT 0;

  DECLARE tName VARCHAR(25);


   SELECT name, (SUM(price * quantity)) AS TotalPrice INTO tName,NVal
   FROM Items NATURAL JOIN Inventory
   GROUP BY name
   ORDER BY (TotalPrice) DESC
   LIMIT 1, OFFSET n-1;

   RETURN NVal;
END;//

The function named 'GET_NthVal' returns a 'double' value which is the value(total price) of the nth record of the given query.
TotalPrice of nth record will be stored to the variale 'NVal' and returned to the caller funtion/ place.
Here OFFSET is set to n-1 and LIMIT to 1 and it is to skip the first n-1 records and starts from n th record. In this way you will get the nth value of any table/ result set.

Kindly Note:
Feel free to comment if you have any doubts regarding the solution.


Related Solutions

Create a ‘Student’ table using SQL query. The table must have at least five attributes from...
Create a ‘Student’ table using SQL query. The table must have at least five attributes from your choice with different data types.
Create a query in Access that uses two tables, the Patient table and the Session table. Add
Create a query in Access that uses two tables, the Patient table and the Session table. Add the LastName, FirstName, and SessionDate fields to the query grid. Run the query. How many records are displayed? Delete the join line between the field lists in Query Design View. Rerun the query. How many records are now displayed? Why are the results different? You do not need to save the queries. 
I have the following question: Write a recursive function to find the Nth element from the...
I have the following question: Write a recursive function to find the Nth element from the top of a stack. For example, if N is 3 the function should return the third element in the stack. Use the following header: template <class Type> Type getNth( stack<Type> & s, int n) Although your function may push and pop values from the stack, it must eventually leave the stack in its original state. Your function may NOT use a help stack or...
Using the world_x database you installed on your MySQL Workbench, create a new table named “independence”...
Using the world_x database you installed on your MySQL Workbench, create a new table named “independence” with the following attributes (columns): A field named “id” which has data type auto_increment, A field named “country_name” which has data type varchar(50), and A field named “independence_date” which has type “date.” After you create the table, run the following SQL command: INSERT INTO independence(country_name, independence_date) VALUE (‘United States’,’1776-07-04’) Submit a 1-page Microsoft Word document that shows the following: The SQL command you used...
Describe in words what the function of each line is in the following SQL query The...
Describe in words what the function of each line is in the following SQL query The lyrics database is provided under question 3 for context 1. select studioID, studioname, base from salespeople sa inner join studios st on (sa.salesID = st.salesid) where base < 300 2. SELECT artistName FROM Artists WHERE artistID IN (SELECT artistID FROM Titles) 3. select m.lastname, m.firstname, s.lastname         from members m inner join salespeople s using (salesID)         order by m.lastname asc; The lyrics database...
The ProjectsAndLineItems query currently uses the ProjectID field from the Projects table. Explain why that field cannot be edited in Query Datasheet View.
The ProjectsAndLineItems query currently uses the ProjectID field from the Projects table. Explain why that field cannot be edited in Query Datasheet View. 
1. Write a query to: a. select data from INVOICES table as follows: Invoice date in...
1. Write a query to: a. select data from INVOICES table as follows: Invoice date in MM/DD/YYYY format Invoice Date in DD-Mon-YYYY format Invoice Total rounded to the nearest dollar Note: you can alias columns as you sit fit b. select data from VENDORS table as follows: Vendor Name Concatenate Vendor Name with the string ‘s Address Concatenate Vendor City, Vendor State and Vendor Zip Code (alias this) Your output should look like this (this is just an example of...
QUESTION 34 seqAID is a valid Oracle sequence: select seqAID.nextval from dual; the above query returns...
QUESTION 34 seqAID is a valid Oracle sequence: select seqAID.nextval from dual; the above query returns 1500, what the following query will return? Select seqAID.nextval From dual; A. 1499 B. 1500 C. 1501 D. 1502 QUESTION 35 after running the query in previous Question, what the following query will return? Select seqAID.nextval-1 From dual; A. 1499 B. 1500 C. 1501 D. 1502 QUESTION 36 after running queries in previous two Questions (34 & 35), what the following query will return?...
Python. Create a function that receives the name of an auto maker and returns a slice...
Python. Create a function that receives the name of an auto maker and returns a slice of the dataframe with the cars from that auto maker. For instance, it may receive 'ford' and return a slice with all the cars produced by 'ford' meaning they have 'ford' in their name. Call the function for 'ford' to see if it works properly. Hint: You may create a list comprehension producing a list of all the index labels that include the auto-maker's...
Python pls create a function called search_position. This function returns a list. team1 = {'Fiora': {'Top':...
Python pls create a function called search_position. This function returns a list. team1 = {'Fiora': {'Top': 1, 'Mid': 4, 'Bottom': 3},'Olaf': {'Top': 3, 'Mid': 2, 'Support': 4},'Yasuo': {'Mid': 2, 'Top': 5},'Shaco': {'Jungle': 4, 'Top': 2, 'Mid': 1}} def search_position(team1): returns [(5, [('Top', ['Yasuo'])]), (4, [('Mid', ['Fiora']), ('Support',['Olaf']), ('Jungle',['Shaco'])])   (3, [('Bottom', ['Fiora']), ('Top', ['Olaf'])]), (2, [('Mid', ['Olaf','Yasuo']), ('Top', ['Shaco'])]), (1, [('Mid', ['Shaco'])])]
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT