473,699 Members | 2,384 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Creating Line or Row numbers on query results.

12 New Member
I have two select queries that need to have a column of line numbers on them. The numbers need to simply be 1 through however many rows there are in the result set. With the data I'm using the result set usually under 100 rows for each query. I've read solutions that involve using the primary key field and a COUNT function. Those have not worked for me as my data set comes from a linked CSV file with no primary key. I'm using a linked CSV file because the data is updated often. I want to get my CSV file, run my queries on it and get my exported files. To further complicate matters, the data does not contain a field contains unique values that could be a primary key.

The results of these two queries will be exported to CSV files and then imported into another data system.

Any help or ideas would be appreciated.

Chad
Oct 8 '08 #1
14 17074
NeoPa
32,569 Recognized Expert Moderator MVP
I'm afraid this is logically impossible, as there is no definition of the order, and SQL doesn't recognise the concept of ordinal numbers per line, or to put it another way, of record positions.

There are ways of fiddling this in code (VBA procedures) but fundamentally this is not supported.
Oct 9 '08 #2
chadh
12 New Member
OK, so I need to find a way of making a new field that contains a unique value for each line. If I did this with an expression, lets say concatenating two other fields into new one such that it is unique, could I then use that with the count function?

This seemingly-simple thing has turned into a major roadblock for my project.

I've read that record numbers or an auto-incrementing number could be added to a report. This would work, as long as I could export the report to a csv file.

Chad
Oct 9 '08 #3
NeoPa
32,569 Recognized Expert Moderator MVP
Possible, but reports are not laid out in the format you'd need. Exporting a report does just that, the displayed data, not the underlying record source.

I'm afraid to say that people who understand databases never see this as a straightforward issue. It can only seem that way to those who don't appreciate what it is they're trying to do.
Oct 9 '08 #4
chadh
12 New Member
Ok, so lets try a different approach. Could I create a 'temporary' table and populate it with my query? Then, add an auto number (1 through the total number of records) to the table and run a new select query on that temporary table that just returns everything in it (ordered by the new auto number), export that query to the CSV file and then delete the temporary table.

I presume, if this is feasible, it could be scripted in VB and run with as and event.

I understand using an INSERT statement to put the query results into a table. I'm not so sure about setting up the table, getting the correct fields, adding the auto number and then clearing or deleting it after the export. I would want the table cleared or completely deleted so there would not be residual data the next time a user would run the command.

Chad
Oct 10 '08 #5
NeoPa
32,569 Recognized Expert Moderator MVP
It most certainly is (I'm impressed).

There are various reasons why I wouldn't try suggesting this. Among which is the worry that you might still go away with "wrong" ideas about database concepts. Most of the others are about whether someone could get their head around it and various practicalities.

Clearly, your heads already there, so, regardless of the worry about the understanding, I think we can progress along these lines.

As you are clearly with the plot, I suggest leaving you to produce what you can on those lines. If you get stuck, or need help at any phase, come back with the details and we can certainly help.

I will say that you're on a good track here. It certainly can work, so if you get stuck you already know that's all it is. I will await your response, particularly your eventual, and inevitable, success :)
Oct 10 '08 #6
ADezii
8,834 Recognized Expert Expert
Ok, so lets try a different approach. Could I create a 'temporary' table and populate it with my query? Then, add an auto number (1 through the total number of records) to the table and run a new select query on that temporary table that just returns everything in it (ordered by the new auto number), export that query to the CSV file and then delete the temporary table.

I presume, if this is feasible, it could be scripted in VB and run with as and event.

I understand using an INSERT statement to put the query results into a table. I'm not so sure about setting up the table, getting the correct fields, adding the auto number and then clearing or deleting it after the export. I would want the table cleared or completely deleted so there would not be residual data the next time a user would run the command.

Chad
chadh, I've used this logic several times when I absolutely, positively, needed sequential numbering, for one reason or another. My approach is to:
  1. Temporarily, convert your Query to a Make Table Query, execute it then Delete all the Records in it. You now have your Temp Table, minus the Counter Field. This process is only a 1-Shot deal, and will not be performed again.
  2. This Temp Table also contains a newly added [Rec_Num] {LONG} Field, not AutoNumber, which will be sequentially numbered for each Appended Record.
  3. Create a Recordset based on the Query and pro-grammatically Append each Record to the Temp Table. Increment the [Rec_Num] Field by 1 for each Append Operation:
    Expand|Select|Wrap|Line Numbers
    1. Do While Not Main_RS.EOF         'based on Query
    2.   Recordset_Append.AddNew       'based on Temp Table
    3.      ... Append other Fields here (Main_RS!*) ==>  Recordset_Append![<Field>]
    4.      intCounter = intCounter + 1
    5.      Recordset_Append![Rec_Num] = intCounter
    6.   Recordset_Append.Update
    7.   Main_RS.MoveNext
    8. Loop
  4. Assuming success, the results of your Query exist in the Temp Table and are sequentially numbered.
  5. Export the Temp Table to any Format you so desire.
Oct 11 '08 #7
NeoPa
32,569 Recognized Expert Moderator MVP
As you are clearly with the plot, I suggest leaving you to produce what you can on those lines. If you get stuck, or need help at any phase, come back with the details and we can certainly help.
Personally, I still think you would be better off trying to work something out for yourself first. I think you probably have the right idea and going through that process will help you more than simply picking up a solution (although obviously that's an option).
Oct 11 '08 #8
mshmyob
904 Recognized Expert Contributor
Hello Ade. Question I have because I did this the other day to solve the problem with the Subform calculated value problem that you gave me a better solution to.

I did this solution for the problem I had stated above but my added column was for the calculation and then populated my subform.

The reason I didn't go with this type of solution was I was thinking what might happen in a multi-user environment. I would assume problems would occur with appending to the table and getting wacky results in the query if multiple people were trying to do everything at the same time.

I created a routine that created a temp table name based on a date/time stamp. I thought there had to be another (better) way and you did give me one in the thread.

Am I over thinking the multi user environment and using a temp table with a name that never changes.

cheers,


chadh, I've used this logic several times when I absolutely, positively, needed sequential numbering, for one reason or another. My approach is to:
  1. Temporarily, convert your Query to a Make Table Query, execute it then Delete all the Records in it. You now have your Temp Table, minus the Counter Field. This process is only a 1-Shot deal, and will not be performed again.
  2. This Temp Table also contains a newly added [Rec_Num] {LONG} Field, not AutoNumber, which will be sequentially numbered for each Appended Record.
  3. Create a Recordset based on the Query and pro-grammatically Append each Record to the Temp Table. Increment the [Rec_Num] Field by 1 for each Append Operation:
    Expand|Select|Wrap|Line Numbers
    1. Do While Not Main_RS.EOF         'based on Query
    2.   Recordset_Append.AddNew       'based on Temp Table
    3.      ... Append other Fields here (Main_RS!*) ==>  Recordset_Append![<Field>]
    4.      intCounter = intCounter + 1
    5.      Recordset_Append![Rec_Num] = intCounter
    6.   Recordset_Append.Update
    7.   Main_RS.MoveNext
    8. Loop
  4. Assuming success, the results of your Query exist in the Temp Table and are sequentially numbered.
  5. Export the Temp Table to any Format you so desire.
Oct 11 '08 #9
ADezii
8,834 Recognized Expert Expert
Hello Ade. Question I have because I did this the other day to solve the problem with the Subform calculated value problem that you gave me a better solution to.

I did this solution for the problem I had stated above but my added column was for the calculation and then populated my subform.

The reason I didn't go with this type of solution was I was thinking what might happen in a multi-user environment. I would assume problems would occur with appending to the table and getting wacky results in the query if multiple people were trying to do everything at the same time.

I created a routine that created a temp table name based on a date/time stamp. I thought there had to be another (better) way and you did give me one in the thread.

Am I over thinking the multi user environment and using a temp table with a name that never changes.

cheers,
Good point, how about setting the following Option on the OpenRecordset() Method for the Temp Table, and see what happens?
Expand|Select|Wrap|Line Numbers
  1. 'The dbDenyWrite Option prevents other users from modifying or adding records
  2. '(Microsoft Jet Recordset objects only).
  3. Set MyRS = MyDB.OpenRecordset("tblTemp", dbOpenDynaset, dbDenyWrite)
  4.  
  5. MyRS.AddNew
  6.    ...
  7. MyRS.Update
  8.  
Oct 11 '08 #10

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

Similar topics

7
8220
by: pj | last post by:
Why does M$ Query Analyzer display all numbers as positive, no matter whether they are truly positive or negative ? I am having to cast each column to varchar to find out if there are any negative numbers being hidden from me :( I tried checking Tools/Options/Connections/Use Regional Settings both on and off, stopping and restarting M$ Query Analyer in betwixt, but no improvement.
3
14987
by: Indraneel Sheorey | last post by:
Hello, I want to set up a query in SQL Server that is "grouped by" a variable PRICE. Since PRICE takes on continuous decimal values, I want to create deciles based on this variable and then display the average price in each decile. As background, PRICE is a calculated quantity: I divide a table field SLS_DLRS by a field SLS_UNTS to get it. I also want to include an average SLS_UNTS for each decile. So essentially, I want the result...
2
5965
by: Iain Miller | last post by:
Now this shouldn't be hard but I've been struggling on the best way as to how to do this one for a day or 3 so I thought I'd ask the assembled company..... I'm writing an application that tracks a group of Sales people, the customers they deal with and the business they transact with them. I've got my head around all the tables & some of the basic Query structures OK and am beginning to delve into creating the forms I need to be able...
3
3271
by: Stewart Allen | last post by:
Hi there I'm trying to find part serial numbers between 2 numbers. The user selects a part number from a combo box and then enters a range of serial numbers into 2 text boxes and the resulting query should find every machine that has that part number between the serial number range. The problem is that the serial number stored is a text field and the results are not what they should be.
2
6229
by: Todd_M | last post by:
I was wondering what anyone might suggest as "best practice" patterns for streaming out fixed formatted text files with C#? Let's say we get our data in a dataset table and we need to iterate over thousands..potentially tens of thousands of rows to create a properly formatted text file -- like an ACH file, for example. In there you typically have a header, a body block of detail rows and a trailer. (The trailers usually contain sums of the...
5
1862
by: Kosmos | last post by:
Hey :) hopefully someone can help me with this...I decided to take on the task of programming an access database for my legal co-op/internship...I'm studying law and music production on the side...most of the background I have in programming has to do with music production... The program I'm creating pulls data from an excel sheet (with defined fields) and brings them into an access database. The data being pulled is the following: ...
17
46533
Motoma
by: Motoma | last post by:
This article is cross posted from my personal blog. You can find the original article, in all its splendor, at http://motomastyle.com/creating-a-mysql-data-abstraction-layer-in-php/. Introduction: The goal of this tutorial is to design a Data Abstraction Layer (DAL) in PHP, that will allow us to ignore the intricacies of MySQL and focus our attention on our Application Layer and Business Logic. Hopefully, by the end of this guide, you will...
6
2061
by: hinksta | last post by:
I'm trying to build a football leage table taking it's content from a results database, CREATE TABLE conference ( id int NOT NULL auto_increment primary key, season VARCHAR(50) NOT NULL, gamedate DATE NOT NULL, hometeam VARCHAR(50) NOT NULL, homescore VARCHAR(30) NOT NULL, awayscore VARCHAR(50) NOT NULL, awayteam VARCHAR(30) NOT NULL);
8
3821
by: rottmanj | last post by:
. In order to teach my self more. I have started to convert some of my cf scheduled tasks to perl applications. One area where things are kind of fuzzy is setting up global variables that can be called from any module with in an application. So far I have created a farily standard module that will act as my global config. This module will store variables that are populated from the database. So that I will not be forced to re-create/query...
0
9172
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9032
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8908
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6532
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4374
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3054
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2344
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2008
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.