Sir, I need your help to find the second highest value related to this query.
Here I writing query to find the customerName, ProductName and ProductPrice.
select A.customerName,B.productName,B.unitPrice from tbl_Customer as A
inner join tbl_custProductSales on A.customerId=tbl_custProductSales.customerId inner join
tbl_Product as B on B.productId=tbl_custProductSales.productId
Like this I want to get the second highest productPrice, customer and productName
My result would be:
customer product second_highest_productRate
-----------------------------------------------------
Loading

Jignesh TrivediPosted Nov 28, 2013, 12:35 AM
hi
Try...
select A.customerName,B.productName,B.unitPrice, (RANK() OVER
(ORDER BY A.customerName,B.productName,B.unitPrice DESC)) AS rnk
from tbl_Customer as A
inner join tbl_custProductSales on A.customerId=tbl_custProductSales.customerId inner join
tbl_Product as B on B.productId=tbl_custProductSales.productId
Group by A.customerName,B.productName,B.unitPrice
) AS A
WHERE A.rnk = 2
also refer
http://www.programmerinterview.com/index.php/database-sql/find-nth-highest-salary-sql/
hope this will help you.
Sanjeeb LenkaPosted Nov 28, 2013, 12:31 AM
try something like this
select A.customerName,B.productName,B.unitPrice from tbl_Customer as A
inner join tbl_custProductSales c on A.customerId=c.customerId inner join
tbl_Product as B on B.productId=c.productId
where MAX(B.unitPrice)<(select MAX(unitPrice) from tbl_Product)