Question

A store maintains data on customers, products and purchase records in three tables: CUSTOMER, PRODUCT, PURCHASE....

A store maintains data on customers, products and purchase records in three tables: CUSTOMER, PRODUCT, PURCHASE. The store manager wants to know which product is on its maximum discount for each category.Write a query to print the following fields for each category, ordered by productID ascending : category, product_id and discount for the product that has the max discount in category. In the case of multiple products having same max discount within a category, print the product with minimum product_id

CUSTOMER
***************************
CUSTOMER_ID INT
CUSTOMER_NAME STRING
CITY STRING
STATE STRING

PRODUCT
****************************
PRODUCT_ID INT
PRODUCT_NAME STRING
CATEGORY STRING
PRICE INT
DISCOUNT INT
AVAILABLE INT

PURCHASE
****************************
ID INT
CUSTOMER_ID INT
PRODUCT_ID INT
PURCHASE_DATE DATE
  

The part I would need help with would be how to compare the max discounts and print the min product_id, if the max discount is equal for each category.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Query to print the following fields for each category, ordered by productID ascending : category, product_id and discount for the product that has the max discount in category. In the case of multiple products having same max discount within a category, print the product with minimum product_id

select Category, DISCOUNT , min(PRODUCT_ID) from
(select p.Category, p.DISCOUNT , PRODUCT_ID from Product p ,
(SELECT CATEGORY , max(DISCOUNT) DISCOUNT FROM Product group by Category) max_discount_cat where p.Category = max_discount_cat.Category and p.DISCOUNT = max_discount_cat.DISCOUNT) group by Category, DISCOUNT order by PRODUCT_ID;

--------------------------------------------------------------------------------------------------------------------------------------------------------------

SELECT CATEGORY , max(DISCOUNT) DISCOUNT FROM Product group by Category returns the Category and maximum discount for that category

(select p.Category, p.DISCOUNT , PRODUCT_ID from Product p ,
(SELECT CATEGORY , max(DISCOUNT) DISCOUNT FROM Product group by Category) max_discount_cat where p.Category = max_discount_cat.Category and p.DISCOUNT = max_discount_cat.DISCOUNT) returns Category, discount and Product Id of all products in each category having the discount as the maximum discount

select Category, DISCOUNT , min(PRODUCT_ID) from
(select p.Category, p.DISCOUNT , PRODUCT_ID from Product p ,
(SELECT CATEGORY , max(DISCOUNT) DISCOUNT FROM Product group by Category) max_discount_cat where p.Category = max_discount_cat.Category and p.DISCOUNT = max_discount_cat.DISCOUNT) group by Category, DISCOUNT order by PRODUCT_ID; returns the Category, Discount and minimum product id from the result set we got in the previous step.

Add a comment
Know the answer?
Add Answer to:
A store maintains data on customers, products and purchase records in three tables: CUSTOMER, PRODUCT, PURCHASE....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • CREATE TABLE customer (    id INTEGER PRIMARY KEY,    customer_name TEXT,    contact_number TEXT );...

    CREATE TABLE customer (    id INTEGER PRIMARY KEY,    customer_name TEXT,    contact_number TEXT ); CREATE TABLE supplier (    id INTEGER PRIMARY KEY,    supplier_name TEXT UNIQUE,    contact_number TEXT ); CREATE TABLE product (    id INTEGER PRIMARY KEY,    supplier_id INTEGER REFERENCES supplier(id),    product_name TEXT,    product_price INTEGER,    UNIQUE(supplier_id, product_name) ); CREATE TABLE purchase (    id INTEGER PRIMARY KEY,    customer_id INTEGER REFERENCES customer(id),    purchase_date REAL,    store_id INTEGER REFERENCES store(id) );...

  • A database used for a toy store that keeps track of the inventory and purchase orders...

    A database used for a toy store that keeps track of the inventory and purchase orders from the store. All clients will have their membership ID card to shop. This database will help the store to keep track of their orders. It will also help the store to know how many products are available in the inventory. It is necessary to have a membership to purchase a product. The customer can also bring coupons to get discount. Key attributes: Salesman:...

  • A database used for a toy store that keeps track of the inventory and purchase orders...

    A database used for a toy store that keeps track of the inventory and purchase orders from the store. All clients will have their membership ID card to shop. This database will help the store to keep track of their orders. It will also help the store to know how many products are available in the inventory. It is necessary to have a membership to purchase a product. The customer can also bring coupons to get discount. Key attributes: Salesman:...

  • An online company sells hundreds of office supply products on its e-Commerce store. It has asked...

    An online company sells hundreds of office supply products on its e-Commerce store. It has asked you to design and implement a sales order processing system which will be used by users in our Operations department to process sales orders. Here is an overview of the sales order process. Customers make purchases by placing orders. Each customer has a customer number and profile (such as name, shipping address). To simplify the matter, each time an order is placed, only one...

  • Questions are based on the Northwind database. a. A data dictionary (Excel file) that describes each...

    Questions are based on the Northwind database. a. A data dictionary (Excel file) that describes each of the tables. (Attached in question) b.    Write good, clean SQL that answers the following questions. c.     Separate your queries as I have done in the following example. End each query with a semicolon (;) and insert the word "go" between each query. Queries can span multiple lines if required. Select CustomerID from Customers; go Select Count(*) from Employees; go Select max(productID) from Products; 18. Produce...

  • Customer (CustomerId, CustomerName) Employee (EmployeeId, EmployeeName, Salary, SupervisorId) Product(ProductId, ProductName, ListPrice) Orders (OrderId, OrderDate, CustomerId, EmployeeId,...

    Customer (CustomerId, CustomerName) Employee (EmployeeId, EmployeeName, Salary, SupervisorId) Product(ProductId, ProductName, ListPrice) Orders (OrderId, OrderDate, CustomerId, EmployeeId, Total) OrderedProduct (OrderId, ProductId, Quantity, Price) Write the code to complete the methods in OrderJDBC.java (look for TODO items). <---**IN BOLD** throughout code. /* OrderJDBC.java - A JDBC program for accessing and updating an order database on MySQL. */ import java.io.File; import java.math.BigDecimal; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.SQLException; import java.sql.Statement; import java.util.Scanner; /** * An application for...

  • Write in java language ================== Office Supplies Inc., an office supply store, services many customers. As...

    Write in java language ================== Office Supplies Inc., an office supply store, services many customers. As customers’ orders for office supplies are shipped, information is entered into a file. Office Supplies bills their customers once each month. At the end of each month, the Chief Executive Officer requests a report of all customers sorted by their customer id (from lowest to highest). The report includes their bill balance and tax liability. Write a program to produce the outstanding balance report...

  • You will develop an E-Commerce database used to maintain customers, products and sales information. You are...

    You will develop an E-Commerce database used to maintain customers, products and sales information. You are required to 1) gather and analyze requirements 2) design logical structure of the database 3) create stored procedures to develop the tables and insert the data 4) write SQL statements for data extraction and reporting. Throughout the course of this semester you have analyzed the requirements for an eCommerce database, designed and developed your database. As a class we have gone through the process...

  • CellTell, a cellular phone service provider, offers cell phone service contracts to their customers. They have an information system that stores the details about these contracts, selected plans, phones, and accessories

    ERD and Schema for the model is:CUSTOMER ( ID, Lname, Fname, Street, City, ZipCode, State, Phone, CreditScore, Credit_Org, CS_Date)PLAN ( Plan_ID, Start_Date, End_Date, BasePrice, Plan_Type)VOICE ( vPlan_ID@, NumMin, HasVoiceMail, HasThreeWay, OverageFee)TEXT ( tPlan_ID@, TextLimit, HasMMS, MmsOverage, TextOverage)DATA ( dPlan_ID@, DataLimit, NetworkTechnology, OverageFee)CONTRACT ( Contract_ID, Customer_ID@, vPlan_ID@, tPlan_ID@, dPlan_ID@, Start_Date, End_Date, DiscountPerc)MANUFACTURER ( Manuf_ID, Manuf_Name, Contact_Lname, Contact_Fname, Contact_Phone, Contact_Email)PRODUCT ( Product_ID, Name, Manuf_ID@, CostPaid, BasePrice, Type)CELLPHONE ( Phone_ID@, Model, NetworkTechnology, OS)ACCESSORY ( Access_ID@, Category)PHONE_ACCESS ( Phone_ID@, Access_ID@)CONTRACT_CELLPHONE ( Contract_ID@, Phone_ID@, Seq#, PaidPrice, ESN)1. CellTell would like to add website information about the manufacturers of products they sell. Add a new column named “Website” to the manufacturer table. Then,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT