How to choose a specific record in a duplicate record query - duplicates

In my MS Access database, some of my data have the same Date, Time and StationID but have a different value in the last field (Communication field)
MS ACCESS DATABASE
How can I choose a specific record with "VHF" in Communication field?
I try to use SELECT DISTINCT, but it removed the communication field from my query.

The DISTINCT function will return all rows with unique values in a specific column. What you are looking for is the WHERE clause:
SELECT * FROM myTable WHERE Communication = 'VHF'
The above will return only records with VHF in Communications.

Related

MS Access: Update a table field from a query field

I have a field in a table called "CaseDelay" and a field in a query called "Delay." Both are numbers.
Don't know the best procedure to get the query to run and the field value to update the table field.
Additionally, the field in the query is a calculated field, and I read on Microsoft's website that syntax such as UPDATE won't work with calculated fields.
The expressions that are in the query are difficult nested IIF statements that I couldn't figure out how to write in VBA, but could do it in a query expression.
You'll first need to specify how the records in the query relate to those in the table, such that an update query can unambiguously identify & update the appropriate record in your table for each record in your query.
Typically this would be achieved using unique keys present in both the table & query which may be used to pair up the records in the two datasets.
Once this is ascertained, assuming that the query does not use aggregation, you can use a straightforward update query with a join between your existing query & table, e.g.:
update YourTable inner join YourQuery on YourTable.ID = YourQuery.ID
set YourTable.CaseDelay = YourQuery.Delay

Query-getting last inserted row id in mysql using query

I want to get last inserted row id in mysql any one help me
Table field is
If you want to get the id just after the insertion, use LAST_INSERT_ID():
SELECT LAST_INSERT_ID();
This will return the AUTO_INCREMENT value of the last inserted element (in your current connection).
If you want to know which is the last inserted value in a determined table, use any of the queries provided in the other answers.
You can use LAST_INSERT_ID(), but you should be aware that (you not only should have the AUTO_INCREMENENT), but it does operates in at connection level. This is, it will return the last_insert_id() for the current connection and not the last_insert_id() that another connection --that might be happening meanwhile-- generated.
Example (lets say the operations arrive the database in the following order):
-Connection A : user A -> insert order (id = 1000), lets say $20
-Connection B : user B -> insert order (id = 1001), lets say $50
.... multiple orders from other users are placed
-Connection B : you retrieve last_insert_id (value 1001) and you retrieve the order amount, and update the user B profile with the total revenue generated (+$50).
-Connection A : you retrieve last_insert_id (value 1000) and you retrieve the order amount, and update the user A profile with the total revenue generated (+$20).
You should be aware that last-insert_id() operates on a connection level, therefore you want to keep the connection open until you are finished. You should not use things like do a max on the database, because on a web environment you don't have control on how many users will use your app at same time, and in which order the operations will be executed and most important you want to keep your database consistent.

MySQL Query Show Rows with columns data even if not exists

I have a MySQL Table with the following columns
id
action
ip
The action column can contain several values such as ( edit , delete, add etc)
I want to perform a MySQL Query and get some data based on the action value i will provive no matter if the action value exists or not.
For example the following query
SELECT ip,COUNT(*) as total_actions FROM table
WHERE action IN ('edit','delete') group by ip;
This query returns data based on the action column and for example if there is no entry for edit action it will not be printed in the results.
Instead, for the values i write in the IN statement i want to get their COUNT Values and for the values that don't exists the count should be 0.
How i can do that?
It sounds like you want to count ips that match everything in the where clause. Well, you can't quite do that with the where clause.
You can do what you want with conditional aggregation:
SELECT ip,
SUM(action IN ('edit', 'delete'))
FROM table
GROUP BY ip;

Calculate date difference for the same table column in two consecutive records

I'm writing a formula in Access where I want to minus date in the current row to the date in the upper row in the same column. As I'm very new to Access, I'm not able to figure out how can I assign a cell reference to it.
For example, like in Excel, we can put cell reference =a3-a2, how can we do it in Access.
You cannot do that in Access. A database table is not worksheet. You can only work record-wise in a database. What you can do is to join the table to itself. But this works only if you have an appropriate field that tells you how the records are related, for instance like a record number
Table:
RecordNo, Date
The query goes like this
SELECT
DateDiff('d', t1.Date, t2.Date) As Days
FROM
myTable t1
INNER JOIN myTable t2
ON t1.RecordNo + 1 = t2.RecordNo
One basic rule in databases is that the table records have no natural order. I.e. they do not have a line number bound to them. The database engine can reorder the rows when the database is compacted. Therefore a table must always have a primary key that allows you to identify a record. If you need a specific order, you must have a column that reflects this order and that you can use in an ORDER BY clause in a query.

MySQL - get and ID from a lookup table and insert if needed

I have a looklup table, its got an ID and a NAME field. The process I'm currently going through is:
Lookup Name. If the row count equals 1, return the ID. If the row count is 0, insert the new name and return the new ID. If there is more than 1 row, log the error and return the 1st ID.
This consists of a SELECT then an INSERT in the case of a new name. Is there a way to combine this so there is only a single SQL statement executed?
Even if that is possible which I'm confident it's not it should be illegal. Your cramming way too much logic into the SQL code.