Row numbering in iOS SQLite - ios

In an SQLite table in my app, I need to have a column with row numbers going 1,2,3,4,5 (or to be able to get a row number for every row). Now I delete rows and add rows all the time, so I cant just put a number every time I add a new row, because when rows are deleted, the numbering will get messed up. So is there a way to get the row number, or create a special column that will act as row numbering? If so, how?

If you want this to be reset every time you add and delete rows, should this really be a column in your table?
For example, if you just want to be able to select the 20th row from the table, you can use the LIMIT and OFFSET clauses of SQL to select a particular row, e.g., to retrieve the 20th row from my news table, I could do:
SELECT * FROM news LIMIT 1 OFFSET 19;
Alternatively, if you really want a column with this row number information, you could just add a new INTEGER column type and then write an Objective C method to update the values for that column, and invoke that method any time you delete or insert rows in your result set.
There are a number of ways of solving your problem. You have to explain what you're trying to accomplish for us to help you out. The typical AUTOINCREMENT answer is what we generally use for table keys, but it sounds like you don't want this to be the key of your table, but rather something else.

If you use ROWID which is an auto-incrementing number, you can compute row numbers by selecting all of your data, ordered by ROWID and then using their index in the returned results as their row number. If you have some data model class you're creating, you could pass that in to its initializer.
If that won't do what you want, then how about adding some more details about what you're trying to do. Are you sure you need a row number?

You don't want to go changing all the existing records in your database when you delete rows that make "holes" in your row-umber scheme.
SQLite already supports what you want, with the ROWID. When you make a select query, just specify it to sort on ROWID, and all your returned records will be sorted in their proper order, and you can iterate over them in their order.
See this page: http://www.sqlite.org/autoinc.html
FWIW, there are certainly places where you should prefer direct SQLite over Core Data. However, those are rare cases. In general, reach for Core Data first, and fall back to SQLite only when CD does not serve your needs.

Just use an autoincrement primary key for the table. then there is always a unique identifier you can access a record.

Related

Is there a formula to resolve duplicates in one column by looking at a second column?

I have a google sheet with 2 columns, A (containing names) and B (containing dates).
there are quite a few duplicates in A which I need resolved by looking at B and selecting the row with the soonest date.
Ideally I need to extract the data to make a list on another sheet with no duplicates, being resolved by the above method. the issue is that there might be new rows added to the sheet, and I need the new rows checked against the existing data to confirm it has a sooner date compared to potential duplicates. how can I formulate this?
There is probably a better way, but I just use the query function to put the results in a different sheet so I don't mess with the source data.
=QUERY()
Here is some examples on how it is used: https://support.google.com/docs/answer/3093343
As for the query itself, something like this should work. A being the first column (Name), and B being the second column (Date).
SELECT A, MAX(B) GROUP BY A

Unique List from one column to a limit row length across multiple columns Google Sheets

So ultimately what i'm trying to do is, copy paste some data into another sheet. What i want to do with that data. Is to remove any duplicate data, and create multiple columns of a fixed length. E.I. I have unique 14000 entries. It will create 3 columns to 5000 of unique entries each.
Short explanation, I have a long ass column of data I want to break up into several columns with a size limit.
So what I was able to figure out so far is
=unique(QUERY(Sheet2!A:I,"Select B limit 5000"))
Would it be something like by chance?
=unique({QUERY(Sheet2!A:I,"Select B limit 5000");A:A;B:B;C:C})
Not sure how it works but got it to work... and i still need to add in the unique aspect to it
=ArrayFormula(TRIM(split(transpose(SPLIT(Query(Sheet2!B2:B30000&","&if(MOD(row(Sheet2!B2:B30000)-row(A2),B2)=0,"|",""),,9^9),"|")),",")))
i found it from here
https://infoinspired.com/google-docs/spreadsheet/split-a-column-into-multiple-n-columns-sheets/

Add new rows when new value exists

I have a dataset I'm importing into my spreadsheet (using ImportRange). I'm wanting to count the amount of each data value imported. So say I have a column of numbers (1 and 2), I'm wanting to count each one. But the dataset is expanding with each user input, so if someone puts a 3, I've been having to add a new row and copy my equation into that so it can handle the new value.
How can I change this so that if a new value is passed in, the spreadsheet will automatically add a new row for that value and update the values?
I'd also like it to be in numerical order if at all possible. I'm using numbers 1-999 and it's difficult to keep track of if it's not in order.
Suppose your data imported is in column A. Then in column B beneath your headers place =sort(unique(A:A)). That will give your sorted list of values. For me the first value was in B3. So next to that I placed =countIF(A:A,B3). That can be dragged down as far as you anticipate you could have values (in your case no more that 1000 rows give or take). That will show 0's beyond the end. If you want to suppress them, wrap it in =IF(countIF(A:A,B3)=0,"",countIF(A:A,B3)) and drag that down instead.

Number increment in Google Sheets formula

In a Google Sheets database, I have a formula which I have built in order to allocate a reference number to a series of companies.
Each company should have its unique number in the form of RET00XX where XX will represent the unique company number. I would like these numbers to be sequential, starting on 1 and going on +1 after that.
Whenever a new company is inserted in the database, the formula should be able to attribute it a reference number. It should also be able to verify if the company already exists in the database and, if so, automatically attribute it the company's unique reference number, instead of creating a new one.
The company names are in cells of column B.
This is the formula I have built (an example of the one in row 2):
=ARRAYFORMULA(IF($B2<>"",IF((COUNTIF($B$1:$B1,$B2)>0),INDEX($A$1:$R2,MATCH($B2,$B$1:$B1,0),12),CONCATENATE("RET00",ROW($B2))),""))
The steps it takes are:
It verifies that column B in the correspondent row is not empty;
With the COUNTIF function, verifies that the company does not exist in any of the previous rows;
If the company does exist, it attributes the correspondent reference number through the INDEX function;
If the company doesn't exist, it attributes the company a new reference number with the CONCATENATE and ROW functions.
The formula is largely working, although there are some problems.
Users adding to this database have the habit of adding entries by inserting rows in the middle of the database. This makes it so, due to the way the formula is built, that company unique reference codes change each time that happens. I believe this is partially due to the fact that I use a ROW function. Also, given that new rows are inserted in the middle of the database, the formula should be able to verify is the company already exists not only by looping through all previous rows but rather through all rows (if a new row is inserted, the formula will only verify previous rows, when the company could be in the rows after the new one).
How can I attribute sequential numbers in a formula without reference to ROW? Also, how can I make sure that the spreadsheet verifies for all rows of column B instead of just the ones before the inserted row?
apply this formula in your sheets,
=ArrayFormula(if(B2:B<>"",row(A2:A)-1,""))
More information regarding this please visit this link : https://infoinspired.com/google-docs/spreadsheet/auto-serial-numbering-in-google-sheets/
Solution that is independent of starting row number
These examples will allow you to generate incrementing values in your formulas.
Incrementing integers, zero based:
The values will be: 0,1,2,3, etc.
Note: The address "$A$2" represents the cell of your top row. It should be changed to whatever cell your actual top row is. The nice thing about this method is it it will not break if you insert new rows above the start position of your formula.
=(ROW()-ROW($A$2))
Integers, one based:
The values will be: 1,2,3,4, etc.
=(ROW()-ROW($A$2) + 1)
Dates:
The values will be: 2000-01-01,2000-01-02,2000-01-03, etc.
=Date(2000,1,1) + (ROW()-ROW($A$2))
All Even Numbers:
The values will be: 0,2,4, etc.
=(ROW()-ROW($A$2) * 2
Short answer
Use Google Apps Script
Explanation
Using spreadsheet functions to set an ID on a live spreadsheet used as a database is very risky as the values will be recalculated when changes be made to the spreadsheet content.
Instead of using a formula use a script to add a "fixed value". Scripts could be called automatically on events like cell edits and row insertion, by using a custom menu or side panel, from the script editor or by time-driven triggers.
The following Q&A from Web Applications shows several ways to set a sequential number:
Can I add an autoincrement field to a Google Spreadsheet based on a Google Form?
This other from SO could be helpful too:
Auto incrementing Job Reference
Insert 1 in the first cell and paste the formula below in the following cells.
=INDIRECT(ADDRESS(ROW()-1,COLUMN())) + 1
Add number on very first row and type the formula from next cell
i used =A1+1 to get incremental number to index tasks on each line.

Automatic "entry" column in LaTeX tables

I was wondering if it is possible with LaTeX to create a table with multiple rows and columns and have an automatic "entry" column in every table? This means I would like the first column of every table saying "entry" and numbering the rows of the column from 1 to n. (I hope that kind of explains what I want to achieve).
I will need to refer to single entries in a column and it would be easier to have that numbering done automatically in case I change the order of the rows later on.
Is there any way to do that or a package to make that work?
I am new to the whole LaTeX thing so please be easy on the technical terms :)
Check out this TeXBlog post.
You want to create a counter using \newcounter, set it to zero using \setcounter, then at the beginning of each row, increment the counter and print out its value. The example I linked to makes use of a nifty feature of the tabular environment where, in specifying the format for a column, you can also specify a piece of code to be included before each cell in that column. This saves you from having to duplicate the code, as is done in this example.

Resources