deleting a row in legacy SQL - sql

SELECT * FROM [dataset.HistoricalDashboard]
OMIT RECORD IF IncidentNumber = 'INC0887'
I want to delete a row with that Incident number. Does this query deletes the row? Legacy SQL does not have delete function. so trying to figure it out

OMIT RECORD does not delete the row from the table. Instead, it populates whole results except for the records which are satisfying the given condition. You can use the query(given in question) and store results in another table.
For more information, please refer to the google doc:
https://cloud.google.com/bigquery/docs/reference/legacy-sql#omit

Related

Filter out rows in a SQL Server table based on added-on-date

I have a table in my SQL Server which I update on a monthly basis with new data from the Client. Unfortunately, there is no timestamp which tells me when the rows were added or modified. This becomes a problem when sometimes the same data gets appended twice, and I need to manually check and delete the repeated rows. Is there a way to write a query like below:
DELETE FROM (table)
WHERE (date_time_when_row_is_added) <= (manually_specified_datetime)

Deleting records based on a criteria within the table and a field in another table

Created the below query using the design view in Access 2010.
The query runs and brings the correct records but when trying to run it to delete the records, I receive the error message:
Could not delete from specified table.
I am trying to delete the records with status "I" or "p" AND the year is equal to the year stored in another table.
Table W: Name, status, year
Table Year: the current year
I really appreciate any help you could provide.
I have tried building the query in different ways, I searched for solutions similar to the one I have. I am new to Access and never used SQL before. I am just learning it
DELETE Tbl_W.*, Tbl_W.Status, Tbl_Year.[Current Year]
FROM Tbl_W, Tbl_Year
WHERE (((Tbl_W.Status)="p" Or (Tbl_W.Status)="i") AND ((Tbl_AGIFYear.[Current Year])=[current year]));
To be able to delete the records identified by the query.
In MS Access, you can only delete records from one table at a time. It is also unnecessary to specify fields which should be deleted, as only entire records can be deleted.
Given the above information, your delete query could become:
delete from tbl_w
where
tbl_w.status in ("i", "p") and
tbl_w.year in (select tbl_year.[current year] from tbl_year)
Or, if you are only using the tbl_year to provide a record containing the current year, you could alternatively use the Year and Date functions in the following way:
delete from tbl_w
where tbl_w.status in ("i", "p") and tbl_w.year = year(date())

Updating records based on the result of the same record

I have a table where I have duplicate lines of the same Item_number.
I am wanting to do an update query where by I wish to update each line of the item_number based on the result of one of the item_number current records within the table.
My current query looks like this.
UPDATE RESOURCE
SET [resource code]='TOOL-G'
WHERE [resource code]='FILLTOOL' AND OPERATION_DESCRIPTION='FILL MOULD TOOL' AND [set up]=2.5
This where clause will be the same for all of the queries as they are all based on that record currently in the system.
This however will only update 1 line of the 4 duplicate records for that item_number where by the query criteria matches.
What I am wanting to do is do update each individual row with different values apart from the one which matches the where clause in the query.
I.e The third duplicate record of a specific item_number has the information I wish to base my query on. However I want to update the first duplicate record in the list and so on.
I expect this will involve a sub query.
Hope my explanation is easy to understand. I am using Access 2007.

Updating rows in order with SQL

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 to only insert unique data into an access database

I have an access database that I need to update only if my information is unique. Is there a simple sql statement to accomplish this? Will 'insert ignore' work with access?
Example: I have the info stored in an array ('bob','34','hair'). If my database contains a record that matches on those three columns I would not want it to be inserted. If it was found to be unique I would like it to be inserted.
I am writing this in cold fusion but just cant seem to get the sql right.
Before doing an insert, do a select for those values. If you don't get a record back then you know it is safe to insert. Just use two separate queries, one for the check, and if no record found, then the insert.
A unique index is always a good idea if a field or set of fields should be unique. If you have a unique index in Access on the three fields, an insert will fail.
It is possible is to create a single query that only inserts a record where a matched record is not found, for example:
INSERT INTO Shows (ForeName,Reviews,Musical)
SELECT "bob" As ForeName,"34" As Reviews,"hair" As Musical
FROM (SELECT Count(*) As ExistsCount
FROM Shows
WHERE ForeName = "bob",Reviews = "34",Musical = "hair") AS e
WHERE e.ExistsCount=0