Click here to Skip to main content
15,886,689 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I am trying to get the highest record from the top ten records from SQL server table in vb.net 2017. .

"select max(DAYCLOSE) from Tbl_Back_Data_Nse where DAYCLOSE=(Select TOP(10) DAYCLOSE from Tbl_Back_Data_Nse where Tbl_Back_Data_Nse.ISIN ='INE358A01014' and SERIES ='EQ')")

getting error

Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.

how to correct the query

What I have tried:

"select max(DAYCLOSE) from Tbl_Back_Data_Nse where DAYCLOSE=(Select TOP(10) DAYCLOSE from Tbl_Back_Data_Nse where Tbl_Back_Data_Nse.ISIN ='INE358A01014' and SERIES ='EQ')"
Posted
Updated 24-Feb-23 5:00am
Comments
Richard Deeming 24-Feb-23 10:57am    
Your question makes no sense. The highest record from the top 10 records will be exactly the same as the highest record from all records.

Think about it: there is absolutely no difference between the latest date in the 10 records with dates later than any other record in the table and the latest date in the table.
pravin9455 24-Feb-23 12:11pm    
This is the day closing price of shares related to share market . Actually, I am trying to get the highest, lowest, and average value of DayClosing for the last ten days for analysis.

the day's range can vary from 5,10 ,100 days as per requirement, etc. I don't have much knowledge of Query

First off, if you want to select a TOP set, you need to supply an ORDER BY clause, or SQL is at liberty to return rows in whatever order suits it. Just saying SELECT TOP doesn't apply any ordering.

Secondly, the error is very specific: your subquery (SELECT TOP(10) ... ) will always return multiple values unless there is only one matching row in the table. Since you are comparing the returned rows with a single value, the system has no idea what you mean - you as you don't when I say "give me the fruit that matches these ten pages I ripped out of a book"

To compare, you must provide a single value on each side of the equals sign.

I have no idea what ordering you need, but try something like this:
SQL
SELECT MAX(DayClose) FROM 
  FROM (SELECT TOP (10) DayClose 
          FROM Tbl_Back_Data
          WHERE Tbl_Back_Data_Nse.ISIN ='INE358A01014' 
            AND SERIES ='EQ'
          ORDER BY InsertDate ASC)
 
Share this answer
 
v2
Comments
pravin9455 24-Feb-23 12:15pm    
This is the day closing price of shares related to share market . Actually, I am trying to get the highest, lowest, and average value of DayClosing for the last ten days for analysis.

the day's range can vary from 5,10 ,100 days as per requirement, etc. I don't have much knowledge of Query
Look at the expression at:
SQL
... where DAYCLOSE=(Select TOP(10) ...

The system cannot compare a single variable to a number of records. You need to rethink what you are trying to achieve here.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900