SQLITE3 Insert into the most recent row where 1 column is an exact match - sql

I have this fairly large DB. It contains lots of column. One of the will have a value that I need to select, but the DB has several of that value. How can I insert into a column in the row thats the newest in the DB, with a matching column.

Without knowing the ins and outs of your database, I think you would likely want to select the largest id you have in the auto incrementing row. For instance:
SELECT MAX(UNIQUE_ID) FROM TABLE WHERE MATCHING_COLUMN = MATCHING_VALUE
From there you can take your unique ID and insert into that row.

Related

SQL Insert Row In-between Two Rows

How do I insert a row between here?
Data is not intended to be stored SQL tables in any particular order, so it's not appropriate to insert a row at a particular position. You use an SQL SELECT query to extract the data you want and ORDER BY to specify how it is sorted. If you really want to have this row in a particular position, add an ID column as the primary key and number the ID column values in the sequence that you want. Whatever you are using to view your rows will order them by the ID column by default. However, you're going to experience this same problem every time you want to add a new row as SQL tables are not intended to be used in this way.

Add additional row for each rows with a different value for a specific column

I want to add a new row for each existing rows. The new row will contain a new value for one of the column and same values for the remaining columns.
From the below table, I want to add a new 'ADUser' -> 'manju#apac.corpdir.net' for each 'Tag' value. Lets say, the current number of rows are 4. After the operation, the final number of rows should be 8.
I have been experimenting with some INSERT queries, but I am unsuccessful. Any help is highly appreciated.
Database: Azure SQL Database
Current Table Rows :
Expected Table Rows:
You can use insert:
insert into t (id, tag, aduser)
select id, t.tag, 'manju#apac.corpdir.net'
from t;
I am guessing that created is assigned automatically. If not, you could use:
insert into t (id, tag, aduser, created)
select t.tag, 'manju#apac.corpdir.net', current_timestamp
from t;
It seems odd that you want duplicate values for id, but that is what the question is asking for. This should really identify each row uniquely and be assigned automatically.

how can I insert data in oracle AFTER a particular row that exist in the data table?

i have 10 rows of data in my table. now i want to insert a row of data just after the 9th data and before row 10..if am not mistaken ,in mysql db this thing is possible before there's a before and after clause, how bout in oracle ?
here's my starting insert statement, let's say i want to insert it after the 9th data ,how?
INSERT INTO MYTABLE (TITLE,DESCRIPTION,STATUS) VALUES ('blahblah','descriptionblah',1);
There is no inherrent order to rows in a table, and there is no guarantee a particular ordering will be preserved between selects. Consider adding a column containing the required ordering and when querying the table, order the result set by that column.
To insert a row which should appear at a particular position in the result set, ensure that the value you place in the ordering column is appropriate.

Eliminating Duplicate Records in a DB2 Table

How do delete duplicate records in a DB2 table? I want to be left with a single record for each group of dupes.
Create another table "no_dups" that has exactly the same columns as the table you want to eliminate the duplicates from. (You may want to add an identity column, just to make it easier to identify individual rows).
Insert into "no_dups", select distinct column1, column2...columnN from the original table. The "select distinct" should only bring back one row for every duplicate in the original table. If it doesn't you may have to alter the list of columns or have a closer look at your data, it may look like duplicate data but actually is not.
When step 2 is done, you will have your original table, and "no_dups" will have all the rows without duplicates. At this point you can do any number of things - drop and rename tables, or delete all from the original and insert into the original, select * from no_dups.
If you're running into problems identifying duplicates, and you've added an identity column to "no_dups," you should be able to delete rows one by one using the identity column value.

Universal SQL construct to retrieve the last row inserted

What would be the correct universal SQL construct to get the last row inserted (or it's primary key). The ID might be autogenerated by a sequence but I do not want to deal with the sequence at all! I need to get the ID by querying the table. Alternatively, INSERT might be somehow extended to return the ID. Assume I am always inserting a single row. The solution should work with most RDBMS!
the best way is to depend on the sequence like:
select Max(ID) from tableName
but If you don't want to deal with it, you can add new timestamp column to your table and then select max from that column.
like this way
select Max(TimestampField) from tableName