How to use order by with table to sort the table in hierarchical way - sql-server

I am given with table shown in Image 1
How to use order by statement so that i can get resultant table. I don't know how to solve it. I just try to use order by C, D column but all numm comes upwards irrespective of B Column.
Result given in image 2
Updated
Sorry, I just forgot to mention this table also contain id and this table is already sorted on the basis of id. So i am not even able to sort it by column A. Due to this SQL think whole table is already sorted but I still want to sort on the basis of column

Related

Check if a field value exists in a JSONB array inside another table's field

For simplicity, let's assume that I have two tables: A and B.
Table A has a JSONB field called data with the field columns.
This JSONB array is just a list of ids that are primary keys in Table B.
Table B has the field id (the others don't matter for the question).
The idea is to create a constraint so that it's impossible to delete a row in Table B if this row's id is IN A.data->'columns'.
As far as I know, it's impossible to create such constrains in a conventional way so I have decided that this behavior can be implemented as:
SELECT *
FROM B
WHERE id = ANY (SELECT UNNEST(ARRAY(
SELECT JSONB_ARRAY_ELEMENTS_TEXT(A.data -> 'cols') FROM A)::int[]
)
);
This query supposedly does exactly what I want, but it looks clumsy enough to assume that there must be a fancier way. Constructing an array and then unnesting it doesn't seem to be optimal anyway.
Can you think of a better way to achieve the behavior I described above?

Create a table using T-SQL that uses some sort of timestamp as ID column

I'm trying to create a table that uses some sort of timestamp as the ID column with PK.
The goal is to create a table that can be sorted using the ID column which can be used to order the table using a numbering format searching according to the date and time the data was inserted. I know I can set the ID column using identity but I would like the column to be more useful.
I'm hoping there is an efficient way of doing this which provides a performance gain.
E.G. I know in excel you can put a date/time into a cell and it displays as a number depending on the format used.
Hope this is clear, I'm finding it hard to explain. Thanks,

Do Postgres and Oracle databases maintain insertion order?

I am trying to store a collection (which preserves the order of its elements). I will iterate over the collection and will insert the elements one by one into the DB. Suppose I want to retrieve the elements one by one. Will I be able to retrieve the elements in the order I inserted them?
No, database tables are heap oriented. So theoretically in a single writer mode, when all the rows were of equal size, then this "could" work. But when you leave some free space in some page and then later you insert shorter row it will be put into that page.
So please do not trust insertion order in any database. Including MySQL.
Unfortunately it is not.
If you want to retrieve rows in an order, you should use a sequence ( like Id column, primary key ) and an order by clause regarding to this id column.
Oracle built in rowid pseudo column contains row number information ; but you should not rely on this. Table may enabled row movement property.

How can the date a row was added be in a different order to the identity field on the table?

I have a 'change history' table in my SQL Server DB called tblReportDataQueue that records changes to rows in other source tables.
There are triggers on the source tables in the DB which fire after INSERT, UPDATE or DELETE. The triggers all call a stored procedure that just inserts data into the change history table that has an identity column:
INSERT INTO tblReportDataQueue
(
[SourceObjectTypeID],
[ActionID],
[ObjectXML],
[DateAdded],
[RowsInXML]
)
VALUES
(
#SourceObjectTypeID,
#ActionID,
#ObjectXML,
GetDate(),
#RowsInXML
)
When a row in a source table is updated multiple times in quick succession the triggers fire in the correct order and put the changed data in the change history table in the order that it was changed. The problem is that I had assumed that the DateAdded field would always be in the same order as the identity field but somehow it is not.
So my table is in the order that things actually happened when sorted by the identity field but not when sorted by the 'DateAdded' field.
How can this happen?
screenshot of example problem
In example image 'DateAdded' of last row shown is earlier than first row shown.
You are using a surrogate key. One very important characteristic of a surrogate key is that it cannot be used to determine anything about the tuple it represents, not even the order of creation. All systems which have auto generated values like this, including Oracles sequences, make no guarantee as to order, only that the next value generated will be unique from previous generated values. That is all that is required, really.
We all do it, of course. We look at a row with ID of 2 and assume it was inserted after the row with ID of 1 and before the row with ID of 3. That is a bad habit we should all work to break because the assumption could well be wrong.
You have the DateAdded field to provide the information you want. Order by that field and you will get the rows in order of insertion (if that field is not updateable, that is). The auto generated values will tend to follow that ordering, but absolutely do not rely on that!
try use Sequence...
"Using the identity attribute for a column, you can easily generate auto-
incrementing numbers (which as often used as a primary key). With Sequence, it
will be a different object which you can attach to a table column while
inserting. Unlike identity, the next number for the column value will be
retrieved from memory rather than from the disk – this makes Sequence
significantly faster than Identity.
Unlike identity column values, which are generated when rows are inserted, an
application can obtain the next sequence number before inserting the row by
calling the NEXT VALUE FOR function. The sequence number is allocated when NEXT
VALUE FOR is called even if the number is never inserted into a table. The NEXT
VALUE FOR function can be used as the default value for a column in a table
definition. Use sp_sequence_get_range to get a range of multiple sequence
numbers at once."

Access Database Automatically sorting records! How to stop?

i am building a program that uses a database.
When i enter my data into the database it doesnt stay as entered. It is automatically sorted into ascending order by a field that contains the ID number. The problem is when i create a new record programatically, it creates a record in another table with the same row number.
I need to stop access automatically sorting the records. any ideas?
A relational database does not have the concept of an order of records per se; instead retrieved records are ordered by the query used to retrieve them - either in your code, or behind the scenes in the Access gui. So if you want them to appear in a specific order, then write a query including an ORDER BY clause to suit.
Like in all relational databases, the rows in MS Access don't have a fixed order.
If you select data from a table without specifying a ORDER BY clause in your query, the database will return the rows in random order.
Often the order will look sensible (like in your case, ordered ascending by the ID column), and if you run the same query several times, the order might really be the same.
But there's no guarantee - you can't rely on this order, you have to specify one yourself by ordering by the ID column or any other column.
I think that your problem (besides apparently not knowing how ordering works in a relational database) is this:
The problem is when i create a new record programatically, it creates a record in another table with the same row number.
If I'm understanding you correctly:
When you need the record in the other table to have the same value, just take the value from the ID column after inserting into the first table (given that ID is the primary key) and use that value to save the data into the second table.
Removing any indexing from the table is the easy way. But then you have no index.
I would just create a new field in your table called "PretendRowNumber" and insert the would be row number into it. Then you can at least tie your two tables back to each other.

Resources