I have a normal table structure. Like the one below
TableName_OID|GlobalKeyColumn(or foreign key)|Column2| ... |ColumnN|CreateBy|CreateDate|ModifyBy|ModifyDate|IsActive|IsUpdated|UpdateOfTableName_OID
I have written a before update trigger which will take the last row (with inserted.TableName_OID) and insert into the table with its [IsUpdated] = 1 and [UpdateOfTableName_OID] = TableName_OID (or I can insert a row that will only have changed columns' values but I dont know how).
For showing the whole history of table wrt a [GlobalKeyColumn] I have something in my mind. If I go with the first option (inserting a whole row), I will have to run a cursor on the resultset (for a certain GlobalKeyColumn) comparing each row with it's previous one and show only the changed columns.
What I need help with both the options is how to compare each column of a row to its previous one's. I mean, I can do it manually, but is there any generic thing in SQL Server that could compare all the columns in a row to it's previous one?
Related
Hey guys, maybe this is a basic SQL qn. Say I have this very simple table, I need to run a simple sql statement to return a result like this:
Basically, the its to dedup Name based on it's row's Value column, whichever is larger should stay.
Thanks!
Framing the problem correctly would help you figure it out.
"Deduplication" suggests altering the table - starting with a state with duplicates, ending with a state without them. Usually done in three steps (getting the rows without duplicates into temp table, removing original table, renaming temp table).
"Removing rows with duplicated column values" also suggests alteration of data and derails train of thought.
What you do want is to get the entire table, and in cases where the columns you care about have multiple values attached get the highest one. One could say... group by columns you care about? And attach them to the highest value, a maximum value?
select id,name,max(value) from table group by id,name
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.
Happy Holidays. I have two dependent tables, [orders] and [reviews], linked by a "one to many relationship". On the [Orders], the PK is [Order#], there is a column for [#_of_reviews_ordered]. On the [reviews] table (the PK is an auto number) the linked field is [order#] and the number of records (records on the table) should equal "[Orders].[#_of_reviews_ordered]".
Is there a simple way to accomplish this without having to do add the records to [reviews] manually?
The only way I can think to do this without VBA is fairly convoluted and would only work if your number of reviews orders fits within a finite (and reasonably small) range. For my explanation I will assume # of review will be between 0 and 3
You would need to create a table called, say, TemplateReviews. This would have at least one field called "KeyNumber", which should not actually be a key. You could also repeat as many fields as desired from Reviews, and use them to store default values for the rows to be inserted.
The important thing about TemplateReviews is that you must set it up in advance to have N rows with KeyNumber=N for each possible value of KeyNumber. For my example, we can have 0 to 3 # of reviews. So TemplateReviews will have:
0 rows with KeyNumber=0
1 row with KeyNumber=1
2 rows with KeyNumber=2
3 rows with KeyNumber=3
Once you have TemplateReviews set up, you need to create an Insert query based on it. The query will insert rows from TemplateReviews into Reviews. But you also have to filter KeyNumber to match the value on the currently selected Order, as in
=Forms!Orders![#_of_reviews]
You then need run Insert query to run using a macro triggered by a button (etc) on the Orders form. This only works the first time you click the button... but you can modify the criteria expression above to subtract the number of existing reviews, as in
=Forms!Orders![#_of_reviews] - DCount("*","Reviews","OrderId=" & Forms!Orders![order#])
Hope this helps. If you got this approach working, you could then replace the button with a single line of VBA code in the Order form AfterUpdate event to trigger the insert query.
I have a table with 4 columns. The first column is unique for each row, but it's a string (URL format).
I want to update my table, but instead of using "WHERE", I want to update the rows in order.
The first query will update the first row, the second query updates the second row and so on.
What's the SQL code for that? I'm using Sqlite.
Edit: My table schema
CREATE table (
url varchar(150),
views int(5),
clicks int(5)
)
Edit2: What I'm doing right now is a loop of SQL queries
update table set views = 5, click = 10 where url = "http://someurl.com";
There is around 4 million records in the database. It's taking around 16 seconds in my server to make the update. Since the loop update the row in order, so the first query update the first row; I'm thinking if updating the rows in order could be faster than using the WHERE clause which needs to browse 4 million rows.
You can't do what you want without using WHERE as this is the only way to select rows from a table for reading, updating or deleting. So you will want to use:
UPDATE table SET url = ... WHERE url = '<whatever>'
HOWEVER... SqlLite has an extra feature - the autogenerated column, ROWID. You can use this column in queries. You don't see this data by default, so if you want the data within it you need to explicitly request it, e.g:
SELECT ROWID, * FROM table
What this means is that you may be able to do what you want referencing this column directly:
UPDATE table SET url = ... WHERE ROWID = 1
you still need to use the WHERE clause, but this allows you to access the rows in insert order without doing anything else.
CAVEAT
ROWID effectively stores the INSERT order of the rows. If you delete rows from the table, the ROWIDs for remaining rows will NOT change - hence it is possible to have gaps in the ROWID sequence. This is by design and there is no workaround short of re-creating the table and re-populating the data.
PORTABILITY
Note that this only applies to SQLite - you may not be able to do the same thing with other SQL engines should you ever need to port this. It would be MUCH better to add an EXPLICIT auto-number column (aka an IDENTITY field) that you can use and manage.
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.