Unwanted row while querying tables - mysql

I am querying two tables in the same database, I want to have all lines in both database where there is a match but the query should take only row with max date for each file number from file movement table.
File Details Table
File Movement Table
Here is my current Query
query
The result should not contain the 1st row for file 123 as the 2nd row has the larger issue date
enter image description here

Replace b.issue_date at line 1 of your query with MAX(b.issue_date)
append GROUP BY b.file_number after ON condition at last line before semicolon.
SQL query engine generally applies GROUP BY operation before applying any aggregate function on any of its column specified after SELECT clause while retrieving data.
Therefore, the consequence of the two operations are as follows
result grouped by file_number -> result containing only the max value of issue_date column of file_movement table for each file_number

Related

Best way permute values in a unique constrained column in MySQL in a single instruction (or non-complex instruction)

I have a trouble trying to update some data in sql.
I tried to permutate values in a column with unique constraint in a single query.
For example, I have this rows:
id
name
order
300
first
10
500
second
15
The order column has the unique constraint. Now, I want to update the rows to:
id
name
order
300
first
15
500
second
10
In a single query.
I tried to use some stretegies like:
Start transaction.
Save the first order value into a variable A.
Set to null the first order value.
Save the second order value into a variable B.
Update the second order value using the variable A.
Update the first order value using the variable B.
Commit.
But If I have more than once permutation (for example, 10 permutations) the query it's more complex. For that, I want to know if there is another way to do this more generic (for N permutations) and less complex.

SQL loader when clause conditions to satisfy if a column is starting with certain four digits then skip the row

I am trying to create a SQL loader CTL file and required to ignore rows that are starting with specific number in a column. suppose a column record_id =1211.6540D then ignore the row if the record column value starts with 1211.
I tried with position of numbers but the columns values before the record_id are not constant.
Can you please help.

How to insert data in specific row in sql

So here is my problem, I want to add another entry to my database into a specific row.
eg. I have a table "example" then 2 column "name" "age", there are existing data to my table, I have 3 entry
jude,15,
joy,17,
john,16
respectively but I want to insert another entry
jason,18
in the row before "joy,17".
Is it possible or impossible?
Francis, the data in a SQL database has no implicit order. So, you can't count on the data coming out in the same order in which you inserted it. The order in which data comes out of a SQL database can only be guaranteed by using an 'ORDER BY' clause on the SQL statement that brings it out of the DB.
Thus, if you want a different sort order than sorting by the keys you've provided, you probably have to add a 'sort' column to your table and use that column in the ORDER BY portion of your SELECT statements.

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.

Retain The Order while Insert Rows

I put a sort component to sort my data.
and the data was sorted.
but my destination table is unordered!
How can retain the Order of sorted rows while Inserting them into sql Table with ssis?
There is no inherent ordering of rows in a SQL Server table. You'll need to either add a 'sort order' column or write your queries so that they produce properly sorted result sets.
You can use an IDENTITY column as your 'sort order' columns, since it will increment as things get inserted.
Understand that repeated executions of a given query against a sql database are specifically not guaranteed to return results in the same order, so your queries need to do it each and every time.
Rows in a relational database do not have any "order" - they are like water molecules in a bucket! If you need to have an order then you must include another column that you can use to order by - e.g. an autoincrement field, a timestamp, or some column from external data. You can then use that column to order your data when you query it - otherwise you won't get ordered data.