Solved

SQL Server Select not returning correct values

Posted on 2014-01-16
10
290 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
10 Comments
 
LVL 66

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
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

This is basically a blog post I wrote recently. I've found that SARGability is poorly understood, and since many people don't read blogs, I figured I'd post it here as an article. SARGable is an adjective in SQL that means that an item can be fou…
There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

624 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