473,388 Members | 1,408 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

Create a dynamically incremented field

Hi,

Does anyone know how i could add a incrementing field to a select statement.

at the moment i have the following:

SELECT X,Y,
(SELECT COUNT(*)
FROM dbo.tbldo e2
WHERE e2.doid <= dbo.tblDO.doID) AS rank
from dbo.tblDO INNER JOIN
dbo.tblMA_XY ON dbo.tblDO.doID = dbo.tblMA_XY.doID INNER JOIN
dbo.tblMA_MA ON dbo.tblMA_XY.MA_MAIN_ID = dbo.tblMA_MA.MA_MAIN_ID
WHERE (dbo.tblMA_XY.purchaseprice > 357000) and (dbo.tblMA_XY.purchaseprice < 500000)
ORDER BY rank


the problem with this statement is that the rank is not sequential because the id i am using in my subquery is not sequential.

for example the results look like this:

X Y rank
-34.020559 18.348137 1
-34.041561 18.369392 1452
-34.035796 18.361704 2470
-34.036907 18.361067 2488
-34.041291 18.369034 2910
-34.040666 18.368614 2946
-34.040167 18.35322 3272
-34.036984 18.363736 4768


instead of:


X Y rank
-34.020559 18.348137 1
-34.041561 18.369392 2
-34.035796 18.361704 3
-34.036907 18.361067 4
-34.041291 18.369034 5
-34.040666 18.368614 6
-34.040167 18.35322 7
-34.036984 18.363736 8
Feb 18 '08 #1
4 1178
ck9663
2,878 Expert 2GB
Hi,

Does anyone know how i could add a incrementing field to a select statement.

at the moment i have the following:

SELECT X,Y,
(SELECT COUNT(*)
FROM dbo.tbldo e2
WHERE e2.doid <= dbo.tblDO.doID) AS rank
from dbo.tblDO INNER JOIN
dbo.tblMA_XY ON dbo.tblDO.doID = dbo.tblMA_XY.doID INNER JOIN
dbo.tblMA_MA ON dbo.tblMA_XY.MA_MAIN_ID = dbo.tblMA_MA.MA_MAIN_ID
WHERE (dbo.tblMA_XY.purchaseprice > 357000) and (dbo.tblMA_XY.purchaseprice < 500000)
ORDER BY rank


the problem with this statement is that the rank is not sequential because the id i am using in my subquery is not sequential.

for example the results look like this:

X Y rank
-34.020559 18.348137 1
-34.041561 18.369392 1452
-34.035796 18.361704 2470
-34.036907 18.361067 2488
-34.041291 18.369034 2910
-34.040666 18.368614 2946
-34.040167 18.35322 3272
-34.036984 18.363736 4768


instead of:


X Y rank
-34.020559 18.348137 1
-34.041561 18.369392 2
-34.035796 18.361704 3
-34.036907 18.361067 4
-34.041291 18.369034 5
-34.040666 18.368614 6
-34.040167 18.35322 7
-34.036984 18.363736 8

I saw this technique somewhere in the web. I have not tried this myself. But by just looking at your query, I don't think whether the ID you're using is sequential or not. I think the reason you're getting those value for your RANK field is because of your INNER JOIN. It's filtering out those records that are not on those two tables that you're "INNER-JOINING" it with.

Good luck.

-- CK
Feb 18 '08 #2
I saw this technique somewhere in the web. I have not tried this myself. But by just looking at your query, I don't think whether the ID you're using is sequential or not. I think the reason you're getting those value for your RANK field is because of your INNER JOIN. It's filtering out those records that are not on those two tables that you're "INNER-JOINING" it with.

Good luck.

-- CK
hi there,

i am not too sure about that for this reason:

X Y rank
-34.020559 18.348137 1
-34.041561 18.369392 1452
-34.035796 18.361704 2470

if you look at the rank the second row has an id that is 1451 bigger that the previous id. The subqeury say "bring back a count of all ids that are less than or equal to the current id"

Am i mistaken?

Thanks
Chris
Feb 19 '08 #3
ck9663
2,878 Expert 2GB
Isolate your problem. Try your query without the outer WHERE first, which would probably look something like this:

Expand|Select|Wrap|Line Numbers
  1. SELECT X,Y,
  2. (SELECT COUNT(*)
  3. FROM dbo.tbldo e2
  4. WHERE e2.doid <= dbo.tblDO.doID) AS rank
  5. from dbo.tblDO 
  6. ORDER BY rank
Analyze the result if it's what you're trying to do. If it gives the proper then add the first INNER JOIN and the related WHERE. Check the result, again. Observe how your resultset are being reduced by the filter.

Happy coding.

-- CK
Feb 19 '08 #4
Hi CK,

thanks for the advice.

I ended up using a temp table to store the returned records and then a neat little trick i found to add a increment which is:

declare @intCounter int
set @intCounter = 0
update #Temp
SET @intCounter = rank = @intCounter + 1

thanks again
Feb 19 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Ryan Hubbard | last post by:
I'm inserting a record into MySQL 4.0 using Visual Basic ADO. When using the AddNew and Update method I am unable to retrieve the value of a Auto incrment field (Yes I know I can MoveLast but this...
7
by: Bil Muh | last post by:
Esteemede Developers, I would like to Thank All of You in advance for your sincere guidances. I am developing a software using Visual C++ .NET Standard Edition with Windows Form (.NET)...
6
by: Robert | last post by:
Hello, Accessors What I would like to do is create a multi-record table update. I have a table and a form for it. I want to modify it so that there is a new field (textbox) (not bound to a...
2
by: Sean | last post by:
Hello all, I may just not be searching for the right thing, but I've been looking for a way to dynamically create controls (and name them) in my code so that I can create only the controls I...
12
by: ilya_slutsker | last post by:
I am having error trying to run some mysql script (back from 2001). It gives me the error like : ERROR 1067 (42000) at line 48: Invalid default value for 'log_id' in the statement : CREATE...
5
by: Phil Latio | last post by:
I have 2 virtually identical tables and wish to move data between them. Basically one table is called "live_table" and the other is named "suspended_table" and the only difference is that the...
5
by: jalley06 | last post by:
Hi all, I have a form with a field called ReportID in aspx on our local Intranet. Everytime this report is loaded into a browser, I need the ReportID field to be incremented by one. I got this...
15
by: lxyone | last post by:
Using a flat file containing table names, fields, values whats the best way of creating html pages? I want control over the html pages ie 1. layout 2. what data to show 3. what controls to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.