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 MohantyRank #112 · 20K reputationPosted Aug 20, 2019, 2:18 AM EDT
Sonu GuptaRank #1,261 · 1.4K reputationPosted Aug 20, 2019, 1:22 AM EDT
Bhuban MagarRank #1,679 · 909 reputationPosted Aug 20, 2019, 1:03 AM EDT
Jes SieRank #1,285 · 1.3K reputationPosted Aug 20, 2019, 6:29 PM EDT
Again, my warmest thanks to all who gave this ideas.
I love this community.