Below is my table in SQL SERVER. What I want is to extract the lowest partsCost and the latest partsCost inserted into the table:
Below is my stored procedure but does not work:
- ALTER PROCEDURE [dbo].[spClaims_GetLowestLatestPrice] --'honda','civic','1'
- -- Add the parameters for the stored procedure here
- @carMake nvarchar(50),
- @carModel nvarchar(50),
- @partsCode int
- AS
- BEGIN
- -- SET NOCOUNT ON added to prevent extra result sets from
- -- interfering with SELECT statements.
- SET NOCOUNT ON;
- -- Insert statements for procedure here
- with query1 as
- (SELECT ISNULL(partsCost,0) AS LowestPrice
- FROM claimsPartsMasterList
- WHERE carMake = @carMake AND carModel = @carModel AND partsCode = @partsCode AND partsCost = (SELECT MIN(partsCost) FROM claimsPartsMasterList AS LowestPrice)),
- query2 as
- (SELECT ISNULL(partsCost,0) AS LatestPricePurchase
- FROM claimsPartsMasterList
- where carMake = @carMake AND carModel = @carModel AND partsCode = @partsCode AND getLogs = (SELECT MAX(getLogs) FROM claimsPartsMasterList AS LatestPricePurchase))
- SELECT LowestPrice, LatestPricePurchase from query1, query2
- END
Hope someone can help with this. Thanks in advance.

Amit MohantyPosted Aug 20, 2019, 6:18 AM
Jes SiePosted Aug 20, 2019, 10:29 PM
Again, my warmest thanks to all who gave this ideas.
I love this community.
Sonu GuptaPosted Aug 20, 2019, 5:22 AM
Bhuban MagarPosted Aug 20, 2019, 5:03 AM