Solved

SQL Server Select not returning correct values

Posted on 2014-01-16
10
286 Views
Last Modified: 2014-01-16
This should be pretty straight forward and easy...I need another set of eyes to look at this as I'm not getting any where.

I have the following code:
Declare  @Market Table(High int, low int, Question_Num smallint, limit int)
Declare @intQuestion int, @intBuy int, @intSell int, @fltLImit float, @Count int,@high float, @low float

 Insert into @Market (High,low,Question_Num,limit)
 Values
 (115,114,1,5),
 (115,114,2,5),
 (113,112,3,5),
 (111,110,4,5)
 

Set @intQuestion =4
Set @fltLimit= 5
Set @Count=0
		
 Select TOP 1 High +@Count
,Low+@Count
,@intQuestion as Question_Num
,Limit=@fltLimit
From @Market 	
Order by Question_Num Desc

Open in new window


It should return the results as (111, 110, 4,5); however it returns (115,114,4,5,).  If I modify my code so @intQuestion is commented out:
 Select TOP 1 High +@Count
,Low+@Count
--,@intQuestion as Question_Num
,Limit=@fltLimit
From @Market 	
Order by Question_Num Desc

Open in new window


I get the correct values of (111,110,5).  Anyone have any idea's what's going on here...?

Thanks!
0
Comment
Question by:badrhino
  • 5
  • 4
10 Comments
 
LVL 65

Expert Comment

by:Jim Horn
ID: 39785869
<knee-jerk reaction>

>Insert into @Market
Change this temp table to #Market, and rerun.

>High +@Count
Not that it affects the return set, as @Count = 0, but what's the purpose of this?
0
 
LVL 1

Author Comment

by:badrhino
ID: 39785882
Knee-jerk
@Market is actually a table in my db (dbo.Market), I just made it a table variable for ease of posting...Correct me if I'm wrong, it shouldn't matter if the data is coming from a table, temp table or a table variable...

@Count normally isn't zero, but I zeroed it out to try and debug things....This is a segment of my code that is causing me the problem.
0
 
LVL 4

Expert Comment

by:ravikantninave
ID: 39785889
Change
--,@intQuestion as Question_Num to any other variable
0
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
LVL 1

Author Comment

by:badrhino
ID: 39785902
ravikantninave
Changed @intQuestion to @fltLimit  and it does the same thing.
Select TOP 1 High +@Count
,Low+@Count
,@fltLimit as Question_Num
,Limit=@fltLimit
,high
,@low
From Market_Simulation 	
Order by Question_Num Desc

Open in new window

0
 
LVL 4

Expert Comment

by:ravikantninave
ID: 39785907
Change this
as Question_Num to Question_abcd
0
 
LVL 1

Author Comment

by:badrhino
ID: 39785914
Ravik....
That worked.  What is going on?  I know that the order by clause is the last to execute, but why is this causing a problem?
0
 
LVL 4

Accepted Solution

by:
ravikantninave earned 500 total points
ID: 39785919
conflict with your db field Question_Num
0
 
LVL 4

Expert Comment

by:ravikantninave
ID: 39785927
Try to Change the db field Question_Num to anything else and try your first code it will run perfectly

Declare  @Market Table(High int, low int, Question_Numx smallint, limit int)
Declare @intQuestion int, @intBuy int, @intSell int, @fltLImit float, @Count int,@high float, @low float

 Insert into @Market (High,low,Question_Numx,limit)
 Values
 (115,114,1,5),
 (115,114,2,5),
 (113,112,3,5),
 (111,110,4,5)
 

Set @intQuestion =4
Set @fltLimit= 5
Set @Count=0
		
 Select TOP 1 High +@Count
,Low+@Count
,@intQuestion as Question_Num
,Limit=@fltLimit
From @Market 	
Order by Question_Numx Desc

Open in new window

0
 
LVL 1

Author Closing Comment

by:badrhino
ID: 39785936
Thanks!  Learn something every day!
0
 
LVL 4

Expert Comment

by:ravikantninave
ID: 39785940
):
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question