Oracle Identity column with custom grouping column - java

I am working on a requirement where I need to populate a unique constant identifier on a row which is the manager row and unique in each department. My table structure is
CREATE
TABLE TEST_ORGANIZATION
(
EMPLOYEE_ID NUMBER NOT NULL,
MANAGER_ID NUMBER,
FIRST_NAME VARCHAR2(256),
DEPARTMENT_ID VARCHAR2(28) NOT NULL,
UUID VARCHAR2(28) ,
PRIMARY KEY(UUID)
);
This table contains information as.
UUID
DEPARTMENT_ID
EMPLOYEE_ID
MANAGER_ID
FIRST_NAME
radmon1
finance
employee1
John B
radmon2
finance
employee2
employee1
Michal
radmon3
finance
employee3
employee1
Ronaldo
radmon4
finance
employee4
employee1
Thomas
radmon5
finance
employee5
Percey
radmon6
account
employee6
Stacy
radmon7
account
employee7
Jordan
radmon8
account
employee8
employee6
Micky
radmon9
account
employee9
employee6
Author
radmon10
account
employee10
employee6
Gordan
I would like to add another column to the table to provide a sequence to managers only (where Manager_ID is null). But, the sequence should be grouped with DEPARTMENT_ID
ALTER TABLE TEST_ORGANIZATION ADD SEQUENCE_ID NUMBER
UUID
DEPARTMENT_ID
EMPLOYEE_ID
MANAGER_ID
FIRST_NAME
SEQUENCE_ID
radmon1
finance
employee1
John B
1
radmon2
finance
employee2
employee1
Michal
radmon3
finance
employee3
employee1
Ronaldo
radmon4
finance
employee4
employee1
Thomas
radmon5
finance
employee5
Percey
2
radmon6
account
employee6
Stacy
1
radmon7
account
employee7
Jordan
2
radmon8
account
employee8
employee6
Micky
radmon9
account
employee9
employee6
Author
radmon10
account
employee10
employee6
Gordan
I tried using sequence and identity columns added after oracle 12/19c.
I could it programmatically from backend service and update SEQUENCE_ID using
Select NVL(MAX(SEQUENCE_ID), 0) + 1 FROM TEST_ORGANIZATION WHERE MANAGER_ID is NULL AND DEPARTMENT_ID = ? query. But, I would like to know if there is any function in Oracle 19c to handle this behaviour on the Database side itself.

Try This:
SELECT d1.*,
1 AS f,
CASE WHEN manager_id is null then
RANK() OVER (partition by department_ID order by manager_id nulls first,employee_id)
end as sequenc
FROM (
SELECT 'radmon1' AS UUID,'finance' AS DEPARTMENT_ID,'employee1' AS EMPLOYEE_ID,'' AS MANAGER_ID,'John B' AS FIRST_NAME from dual UNION ALL
SELECT 'radmon2','finance','employee2','employee1','Michal' from dual UNION ALL
SELECT 'radmon3','finance','employee3','employee1','Ronaldo' from dual UNION ALL
SELECT 'radmon4','finance','employee4','employee1','Thomas' from dual UNION ALL
SELECT 'radmon5','finance','employee5','','Percey' from dual UNION ALL
SELECT 'radmon6','account','employee6','','Stacy' from dual UNION ALL
SELECT 'radmon7','account','employee7','','Jordan' from dual UNION ALL
SELECT 'radmon8','account','employee8','employee6','Micky' from dual UNION ALL
SELECT 'radmon9','account','employee9','employee6','Author' from dual UNION ALL
SELECT 'radmon10','account','employee10','employee6','Gordan' from dual
)d1;

Related

how to extract source table using sql?

I'm trying to write a query that extracts and transforms data from a table and then insert those data into target table and that whole table data should be in single column in target table.below is the query i wrote
INSERT INTO Table2(column1) VALUES
(SELECT * FROM Table1);
table 1
id | ename | email | country |
1 d .. ..
2 v .. ..
3 s .. ..
4 n .. ..
in table2
src_data | src_column | src_tablename
1 eid
2 eid
3 eid
4 eid
d ename
v ename
s ename
n ename
email1 email
email2 email
email3 email
email4 email
country1 country
country2 country
country3 country
country4 country
how can i achieve this ...can you plz suggest me to get this
This:
INSERT INTO table2
SELECT id, 'id', 'table1' FROM table1
UNION SELECT ename, 'ename', 'table1' FROM table1
UNION SELECT email, 'email', 'table1' FROM table1
UNION SELECT country, 'country', 'table1' FROM table1
uses hardcoded names of the columns and the table.

Android : SQLITE Select Query

I have one table of student_fee where columns are :
id_fee
id_student
student_name
fee_month
Here i am inserting data based on students with their monthly tuition fee payment.
From this easily i can query which students paid their monthly fees, suppose January fee by Query Like : Select * From student_fee Where fee_month = "January";
But how can i search which students not paid their monthly fee only in January?
normaly you should have normalized database tables to avoid storing the student name over and over and over again:
<!-- sql -->
CREATE TABLE students ( id INTEGER, name varchar(60));
INSERT INTO students ( id , name ) VALUES (1, 'stud_1');
INSERT INTO students ( id , name ) VALUES (2, 'stud_2');
INSERT INTO students ( id , name ) VALUES (3, 'stud_3');
INSERT INTO students ( id , name ) VALUES (4, 'stud_4');
INSERT INTO students ( id , name ) VALUES (5, 'stud_5');
CREATE TABLE student_fee (id INTEGER, studentId INTEGER, month varchar(20));
INSERT INTO student_fee (id , studentId , month ) VALUES (1, 1, 'january');
INSERT INTO student_fee (id , studentId , month ) VALUES (2, 2, 'january');
INSERT INTO student_fee (id , studentId , month ) VALUES (4, 2, 'february');
INSERT INTO student_fee (id , studentId , month ) VALUES (5, 3, 'february');
INSERT INTO student_fee (id , studentId , month ) VALUES (6, 4, 'february');
And you would query it like so:
select *
from students
where not exists (
select 1
from student_fee
where students.id = studentid
and month = 'january')
Output:
id name
3 stud_3
4 stud_4
5 stud_5
(Sql Syntax is mysql, not sure about sqlite)

Joining db table

I have 2 db tables:
courses
id|name|teacher_id
teachers
id|first_name|last_name|email
I want to show id, name from courses table and first_name, last_name from teachers table.
I used full join method:
select name,
first_name,
last_name
from courses
full join teachers on teachers.id = courses.teacher_id;
And I'm getting the error below :
Unknown column 'courses.teachers_id' in 'on clause'
You don't have FULL JOINS on MySQL, but achieve this as below:
In case you intend to do FULL OUTER JOIN:
select name, first_name, last_name from courses left join teachers on teachers.id = courses.teacher_id;
union all
select name, first_name, last_name from courses right join teachers on teachers.id = courses.teacher_id;
For INNER JOIN
select name, first_name, last_name from courses left join teachers on teachers.id = courses.teacher_id;
Try this one:
select name, first_name, last_name from courses
inner join
teachers on teachers.id = courses.teacher_id;

How to get max value from the oneToMany table using hibernate query language

The following is my schemma structure
sub_roles
id
name
role_id
parent_id
roles
id
name
user_id
last_updated
user
id
name
type
I want to fetch the users with recently modified role information based on parent_id
I wrote the following query to fetch the users based on parent_id as follows
select sr.role.user, sr.parent, sr.role from SubRole sr where sr.parent.id in (:parentIds)
I want to get recently updated user role. How to achieve this by modifying the above query
select user.id as id, SUBSTRING_INDEX( GROUP_CONCAT(CAST(user.name AS CHAR) ORDER BY rl.last_updated desc), ',', 1 ) as name, SUBSTRING_INDEX( GROUP_CONCAT(CAST(user.type AS CHAR) ORDER BY rl.last_updated desc), ',', 1 ) as type from sub_roles srl INNER JOIN roles rl on srl.role_id = rl.id INNER JOIN user on rl.user_id = user.id where parent_id in (:parentIds) group by user.id;
P.S. : Not Tested
Try like this
select sr.parent, sr.role, u from SubRole sr JOIN sr.role.user u WHERE sr.parent.id IN (:parentIds)

Selecting a data from column

My table is as below
ID Name City
-----------------
1 abc Mumbai
2 def Delhi
3 xyz bangalore
I want to write a query as
String query= select Name from table where (?)
what should come in the where clause if I want to select city Delhi if the input is an ID?
You should frame it as ,
String query= "select City from table where ID=2";
The general syntax select (COLUMN 1 , COLUMN 2) from table where condition; also you can use * to select the entire row.
will return Delhi in the resultset. hope you are using jdbc
select Name from table where id=2;
or
select City from table where id=2;
Where id = IdInputVar
Should work

Categories