NestJS-CRUD: How to set an alias for column name? - crud

I am using #nestjsx/crud-typeorm to create a CRUD REST API for react-admin. One of the requirements of react-admin is that every item in the response needs to have an id field. The database that I am working with uses item_id as the ID field instead.
How do I create an alias in my CRUD responses such as the item_id field is returned as id.
The SQL equivalent would be:
SELECT item_id as id from TableName

Related

Increment column id based on another id

My table :
project_user_id refers to user's login id.
name refers to project name.
id is an incremented column based on a new name entry and refers to the project id.
Example table. How to make id column increment from 1 based on a different project_user_id and therefore name input? Desired table.
I am using SQLite and db.execute in my Python Flask application.
I would leave id alone, id should be a unique identifier for your row that has no meaning whatsoever, other than used to uniquely identify your row. if you want to display something else use a view. something like
create or replace view myview as
select project_user_id, name,
row_number() over (partition by project_user_id order by null) as id
from mytable

Azure SQL Create new table from merging two others using a custom ID column

Im new to configuring Azure SQL but im trying to create a new table by merging some columns from two tables which both share the same Custom ID column.
For examples my data is like the following
CustomID
InfoColumn1
InfoColumn2
UNIQUEID001
InfoColumnData123
TEST10001afssf
UNIQUEID002
InfoColumnData456
TEST10001acetca
UNIQUEID003
InfoColumnData789
TEST10001bsgtse
And
CustomID
OtherColumn1
OtherColumn2
UNIQUEID001
Blah2142214asfasf
a4a545a545a
UNIQUEID002
Blah436346sadsasf
aa45a4w555a5a
UNIQUEID003
Blah43643373bsegags
asgsgsgsag
And i want to create a table like the following
CustomID
InfoColumn1
OtherColumn1
UNIQUEID001
InfoColumnData123
Blah2142214asfasf
UNIQUEID002
InfoColumnData456
Blah436346sadsasf
UNIQUEID003
InfoColumnData789
Blah43643373bsegags
Am i going about this the right way or do i have to create a PowerAutomate Flow? and use a condition of ID eq ID?
Also my data is quite large (40k unique IDs)

Retrieving entries of linked list in relational database

For my project, I implemented linked list with rdbms. The linked list uses rowid column as a pointer, and contains prior, next and owner pointer(from different table).
The simple example would be like this.
CREATE TABLE EMPLOYEE
(
EMP_ID NUMBER(4) NOT NULL,
OFFICE_CODE CHAR(2),
OFF_EMP_prior ROWID,
OFF_EMP_next ROWID,
OFF_EMP_owner ROWID
);
{EMP1,(NULL,EMP2,OFF1)} - {EMP2,(EMP1,EMP3,OFF1)} - {EMP3,(EMP2,NULL,OFF1)}
Now I have to implement a retrieval function like "Find 'nth(integer)' entry of the list which has 'OFF1' as a owner".
This can be simply done by using loop to traverse the linked list. But this requires too many SQL operations for one retrieval. (I know that using sequence number can be another option, but this is the decision made so far.)
Instead, I found SELECT - CONNECTED BY in oracle SQL, and tried
select * from EMPLOYEE
where OFF_EMP_owner = [OFF_ROWID]
connect by nocycle OFF_EMP_prior = rowid;
This query works for retrieving entries of the list, but the order of the result is not as I expected (something like EMP3-EMP1-EMP2).
Is it possible to retrieve entries of the linked list and sort them by the order of the list with SELECT-CONNECT BY'? Or is there exists more suitable SQL?
select * from EMPLOYEE
where DEPT_EMPLOYEE_owner = [OWNER_ROWID}
start with OFF_EMP_prior is NULL
connect by OFF_EMP_prior = prior rowid;
Solved the problem with the query above. 'prior' should be used instead of nocycle.

Copying dates to a column from another table with criteria

I'm trying to do the following:
One table in the database is called service_state_history, that table has three columns:
service_id - which refers to the id of the service that the service_state is related to. (it's nor foreign key because the relation is done in the JPA code in this case).
state_started column which has DATETIME values, that indicate whenever the state on that row has started.
state column which describes the state of that history entry and has following VARCHAR-values: (STANDBY, IN PROGRESS, DONE)
The other table in the database is called services, that table has two relevant columns:
id BIGINT
done_date DATETIME
Now, what I'm supposed to do is update the done_date column in services from the service_state_history table's column state_started, when the state column's value in the row is DONE and the service_id value in matches id in services.
How does that translate into SQL?
So to get all the records that will be updating the destination table, you will use the results generated from:
SELECT service_id, state_started, state
FROM service_state_history
WHERE state = 'DONE'
To perform the UPDATE you will need to JOIN this result set to the destination table on the identity column and use the state_started in the update. Something like:
UPDATE s
SET s.done_date = ssh.state_started
FROM services s
INNER JOIN service_state_history ssh ON ssh.service_id = s.id
WHERE ssh.state = 'DONE'

How to create a table from the attributes of another table that contain a key word?

I have a SQL table called SCUBA_CLASSES with attributes ID and NAME. In the table are different kinds of scuba diving classes from beginner to advanced levels. There is a second table called REQUIRED_BOOKS that has the attributes ID and TITLE that contain all the books required by different scuba classes.
I'd like to create a new table called INTRO_REQUIREMENTS with an attribute ID. It will contain a list of all the books required by scuba classes whose name starts with the word "Introduction". How would you create this?
So far I have:
CREATE TABLE INTRO_REQUIREMENTS AS
SELECT SCUBA_CLASSES.ID
FROM SCUBA_CLASSES, REQUIRED_BOOKS
WHERE SCUBA_CLASSES.ID = REQUIRED_BOOKS.ID;
I can generate a list of classes with required books but can't figure out how to add the requirement that the name of the class has to start with "Introduction".
add one more condition in where clause using using AND and Search name using LIKE.
CREATE TABLE INTRO_REQUIREMENTS AS
SELECT SCUBA_CLASSES.ID
FROM SCUBA_CLASSES, REQUIRED_BOOKS
WHERE SCUBA_CLASSES.ID = REQUIRED_BOOKS.ID
AND name LIKE "introduction%";