I want to display the jobs running on different clusters using SQL - sql

There are two tables job table and cluster table.Every cluster will have the different sub clusters.
LIKE
Main cluster
cluster1 cluster2 cluster3
--------------------------------------
job1 job15 job 20
. .
.
.
j14
1st cluster will have 14 jobs
2nd cluster will have 5 jobs
3rd cluster will have 5 jobs
Now I want to display the jobs running on different clusters (Including sub clusters) using PostgreSQL.
My table structure as follows
CREATE TABLE clusterdata(
clusterid bigint NOT NULL,
sourceclustername character varying NOT NULL,
clustername character varying NOT NULL
);
CREATE TABLE job(
clusterid bigint NOT NULL,
jobname character varying NOT NULL,
sourcecluster bigint
);
Please help me...Thanx in advance

Assuming your sentence "1st cluster will have 14 jobs 2nd cluster will have 5 jobs 3rd cluster will have 5 jobs"
you can use something like this:
WITH BASE AS (
SELECT * , ROW_NUMBER() OVER (PARTITION BY sourcecluster, clusterid ORDER BY clusterid, jobname) AS RN
, DENSE_RANK() OVER (PARTITION BY sourcecluster ORDER BY clusterid) * CASE WHEN sourcecluster=0 THEN 0 ELSE 1 END AS RN_GROUP
FROM job )
SELECT A.clusterid AS SOURCE_CLUSTER, A.jobname AS CLUSTER_1
, B.jobname AS CLUSTER_2
, C.jobname AS CLUSTER_3
FROM BASE A
LEFT JOIN BASE B ON B.sourcecluster = A.clusterid AND B.RN_group=1 AND A.RN=B.RN
LEFT JOIN BASE C ON C.sourcecluster = A.clusterid AND C.RN_group=2 AND B.RN=C.RN
WHERE A.sourcecluster=0
ORDER BY A.clusterid, A.RN, B.RN;
Output:
source_cluster cluster_1 cluster_2 cluster_3
1 1 job1 job15 job20
2 1 job2 job16 job21
3 1 job3 job17 job22
4 1 job4 job18 NULL
5 1 job5 NULL NULL
6 4 job41 NULL NULL
7 4 job42 NULL NULL
8 4 job43 NULL NULL
9 4 job44 NULL NULL
10 4 job45 NULL NULL
11 5 job51 job55 NULL
12 5 job52 job56 NULL
13 5 job53 job57 NULL
14 5 job54 job58 NULL
15 5 job55 NULL NULL
Sample data:
INSERT INTO clusterdata VALUES (1, '0', 'clust1');
INSERT INTO clusterdata VALUES (2, 'clust1', 'clust1.1');
INSERT INTO clusterdata VALUES (3, 'clust1', 'clust1.2');
INSERT INTO clusterdata VALUES (4, '0', 'clust2');
INSERT INTO clusterdata VALUES (5, '0', 'clust3');
INSERT INTO clusterdata VALUES (6, 'clust3', 'clust3.1');
INSERT INTO job VALUES (1,'job1',0);
INSERT INTO job VALUES (1,'job2',0);
INSERT INTO job VALUES (1,'job3',0);
INSERT INTO job VALUES (1,'job4',0);
INSERT INTO job VALUES (1,'job5',0);
INSERT INTO job VALUES (2,'job15',1);
INSERT INTO job VALUES (2,'job16',1);
INSERT INTO job VALUES (2,'job17',1);
INSERT INTO job VALUES (2,'job18',1);
INSERT INTO job VALUES (3,'job20',1);
INSERT INTO job VALUES (3,'job21',1);
INSERT INTO job VALUES (3,'job22',1);
INSERT INTO job VALUES (4,'job41',0);
INSERT INTO job VALUES (4,'job42',0);
INSERT INTO job VALUES (4,'job43',0);
INSERT INTO job VALUES (4,'job44',0);
INSERT INTO job VALUES (4,'job45',0);
INSERT INTO job VALUES (5,'job51',0);
INSERT INTO job VALUES (5,'job52',0);
INSERT INTO job VALUES (5,'job53',0);
INSERT INTO job VALUES (5,'job54',0);
INSERT INTO job VALUES (5,'job55',0);
INSERT INTO job VALUES (6,'job55',5);
INSERT INTO job VALUES (6,'job56',5);
INSERT INTO job VALUES (6,'job57',5);
INSERT INTO job VALUES (6,'job58',5);

Related

GROUP BY A, B, C, and else for counting data?

I want to try group by with data with certain types and etc.
CREATE TABLE dbo.T_TEST(
TYPE nchar(1) NOT NULL,
Qty int NOT NULL
)
INSERT INTO [T_TEST] VALUES ('A','2');
INSERT INTO [T_TEST] VALUES ('A','1');
INSERT INTO [T_TEST] VALUES ('A','2');
INSERT INTO [T_TEST] VALUES ('B','22');
INSERT INTO [T_TEST] VALUES ('B','21');
INSERT INTO [T_TEST] VALUES ('C','2');
INSERT INTO [T_TEST] VALUES ('C','2');
INSERT INTO [T_TEST] VALUES ('C','2');
INSERT INTO [T_TEST] VALUES ('C','2');
INSERT INTO [T_TEST] VALUES ('D','2');
INSERT INTO [T_TEST] VALUES ('D','2');
INSERT INTO [T_TEST] VALUES ('D','2');
INSERT INTO [T_TEST] VALUES ('E','1');
INSERT INTO [T_TEST] VALUES ('E','1');
INSERT INTO [T_TEST] VALUES ('E','4');
INSERT INTO [T_TEST] VALUES ('F','5');
The result I am wondering is like this.
TYPE Count
A 3
B 2
C 4
etc 7
I could do with get total count and subtract count A,B,C.
But I believe it's not a good logic, Please help me with this.
You may try the following and see working fiddle:
SELECT
CASE WHEN TYPE IN ('A','B','C') THEN TYPE ELSE 'etc' END as [Type],
COUNT(Type) as [Count]
FROM
T_TEST
GROUP BY
CASE WHEN TYPE IN ('A','B','C') THEN TYPE ELSE 'etc' END
GO
Type | Count
:--- | ----:
A | 3
B | 2
C | 4
etc | 7
db<>fiddle here
You need a basic GROUP BY query:
SELECT TYPE, COUNT(*) AS Count
FROM dbo.T_TEST
GROUP BY TYPE
ORDER BY TYPE;

SQL (PLSQL) number a set of rows

I attended an interview recently and the interviewer asked me number the occurrences of 'A', 'B', 'C' and so on. To put in table and columns - there is a table tab with column as col. The values in col is 'A', 'B', 'C' etc.
create table tab226 (col varchar2(3) );
insert into tab226 VALUES ('A');
insert into tab226 VALUES ('B');
insert into tab226 VALUES ('C');
insert into tab226 VALUES ('B');
insert into tab226 VALUES ('A');
insert into tab226 VALUES ('C');
insert into tab226 VALUES ('C');
insert into tab226 VALUES ('A');
insert into tab226 VALUES ('B');
The expected output is :
Interviewer told me I can use SQL or PLSQL to achieve it. I thought about it for almost 10 mins but couldn't come up with a plan let alone the solution. Does anyone know if this can be achieved in Oracle SQL or PLSQL?
Doesn't make much sense to me, but - would this do?
SQL> select col,
2 count(*) over (partition by col order by rowid) exp_output
3 from tab226
4 order by rowid;
COL EXP_OUTPUT
--- ----------
A 1
B 1
C 1
B 2
A 2
C 2
C 3
A 3
B 3
9 rows selected.
SQL>
As written, you cannot accomplish -- consistently -- what they are asking for. The problem is that SQL tables represent unordered sets. There is no way to run a query on the original data and preserve the ordering.
However, the final column appears to simply be an enumeration, so you can use row_number() for that:
select col, row_number() over (partition by col order by NULL)
from tab226;
But if you have an ordering column -- say id in this example -- then you would do:
select col, row_number() over (partition by col order by NULL)
from tab226;
Here is a db<>fiddle.

Creating a time table using SQL

I want to create a time table for my university's modules in SQL Server Management Studio using a query.
This is my first time programming in SQL and don't know much about it. I have created the database as well as the tables I want to use, using the following code:
USE master
GO
IF EXISTS (SELECT * FROM sys.databases WHERE name = 'myTimetable')
DROP DATABASE myTimetable
GO
CREATE DATABASE myTimetable
GO
USE myTimetable
GO
CREATE TABLE DayTable
(
WeekDay_ID int Identity (1,1) PRIMARY KEY NOT NULL,
Day_Name varchar(10) NOT NULL
)
GO
CREATE TABLE TimeRangeTable
(
DayTime_ID int Identity (1,1) PRIMARY KEY NOT NULL,
TimeInterval varchar(20) NOT NULL
)
GO
CREATE TABLE SubjectTable
(
Course_ID int Identity (1,1) PRIMARY KEY NOT NULL,
CourseCode varchar (10),
CourseName varchar (255) NOT NULL
)
GO
CREATE TABLE ScheduleTable
(
WeekDay_ID int references DayTable(WeekDay_ID),
DayTime_ID int references TimeRangeTable(DayTime_ID),
Course_ID int references SubjectTable(Course_ID),
)
GO
The tables was created correctly and I managed to insert the correct data into the tables, except for my ScheduleTable (the last table created in the above sample code).
Here is the SQL code I used to insert the data:
insert into DayTable values ('Monday')
insert into DayTable values ('Teusday')
insert into DayTable values ('Wensday')
insert into DayTable values ('Thursday')
insert into DayTable values ('Friday')
insert into DayTable values ('Saterday')
insert into DayTable values ('Sunday')
insert into TimeRangeTable values ('07:30 - 08:20')
insert into TimeRangeTable values ('08:30 - 09:20')
insert into TimeRangeTable values ('09:30 - 10:20')
insert into TimeRangeTable values ('10:30 - 11:20')
insert into TimeRangeTable values ('11:30 - 12:20')
insert into TimeRangeTable values ('12:30 - 13:20')
insert into TimeRangeTable values ('13:30 - 14:20')
insert into TimeRangeTable values ('14:30 - 15:20')
insert into TimeRangeTable values ('15:30 - 16:20')
insert into TimeRangeTable values ('16:30 - 17:20')
insert into TimeRangeTable values ('17:30 - 18:20')
insert into SubjectTable values ('WTW115','Discrete Mathematics')
insert into SubjectTable values ('INF214','Database Design')
insert into SubjectTable values ('INL210','Information Seeking and Retreival')
insert into SubjectTable values ('INL240','Social and Ethical Impact')
insert into SubjectTable values ('INF271','System Analysis and Design')
insert into SubjectTable values ('INF154','Introduction to Programming')
-- Struling from this point onward...
insert into ScheduleTable values('1','1','1')
insert into ScheduleTable values('1','2','2')
insert into ScheduleTable values('1','3','3')
insert into ScheduleTable values('1','4','3')
insert into ScheduleTable values('1','5','3')
insert into ScheduleTable values('2','4','1')
insert into ScheduleTable values('2','5','2')
insert into ScheduleTable values('2','6','2')
insert into ScheduleTable values('2','9','4')
insert into ScheduleTable values('2','10','2')
insert into ScheduleTable values('3','1','5')
insert into ScheduleTable values('3','2','5')
insert into ScheduleTable values('3','6','1')
insert into ScheduleTable values('3','7','3')
insert into ScheduleTable values('4','1','4')
insert into ScheduleTable values('4','3','5')
It all executes and inserts the data, but when I display the data for ScheduleTable, is shows the data as Follow:
WeekDay_ID DayTime_ID Course_ID
-------------------------------------------
1 1 1 1
2 1 2 2
3 1 3 3
4 1 4 3
5 1 5 3
6 2 4 1
7 2 5 2
8 2 6 2
9 2 9 4
10 2 10 2
11 3 1 5
12 3 2 5
13 3 6 1
14 3 7 3
15 4 1 4
16 4 3 5
Where I wanted it to show the data instead of just the codes, example of what I wanted:
WeekDay_ID DayTime_ID Course_ID
--------------------------------------------
1 Monday 07:30 - 08:20 WTW115
2 Monday 08:30 - 09:20 INF214
3 Monday 09:30 - 10:20 INL210
4 Monday 10:30 - 11:20 INL210
5 Monday 11:30 - 12:20 INL210
etc...
I know it has something to do with my Schedule table but that is all I know I don't know how to display it in this way as in the example.
Any help will be appreciated.
Time for a few joins:
select --*
d.Day_Name,
tr.TimeInterval,
sbj.CourseCode, sbj.CourseName
from ScheduleTable as sch
join DayTable as d on sch.WeekDay_ID = d.WeekDay_ID
join TimeRangeTable as tr on sch.DayTime_ID = tr.DayTime_ID
join SubjectTable as sbj on sch.Course_ID = sbj.Course_ID;
You could create a view (for the aforementioned statement) for convenience:
create view TimeScheduleView
as
select
d.Day_Name,
tr.TimeInterval,
sbj.CourseCode, sbj.CourseName
from ScheduleTable as sch
join DayTable as d on sch.WeekDay_ID = d.WeekDay_ID
join TimeRangeTable as tr on sch.DayTime_ID = tr.DayTime_ID
join SubjectTable as sbj on sch.Course_ID = sbj.Course_ID;
go
select *
from TimeScheduleView;

Need to get value from a lookup table

I have a table-X with ecode,emp ID ( some values)
37,10
47,20
57,30
There are 2 lookup tables
lookup table 1 has just the emp ID details( which am interested in)
10
20
so when i join..i get all the values needed (thats one part)
my result will be
37 10
47 20
second part is,
the ones which doesnt satisfy the join condition should lookup on table 2 which has
2 columns
ecode, other_codes
37 xxx
47 YYY
57 AAA
So when 30 comes in , i want to return AAA and my final dataset should be,
37, 10
47,20
57 AAA
appreciate any help!
Thanks
You can left join both tables and use CASE statement for selecting a value from one table or the other.
I've created a db-fiddle which I think exemplifies your situation based on your description: https://www.db-fiddle.com/f/ujW8Unf44CqbsiJZXqXXtK/0
Here is the code for posterity. To set up your tables:
CREATE TABLE tableX (
eCode int,
employeeId int
);
INSERT INTO tableX (eCode, employeeId) VALUES (37, 10);
INSERT INTO tableX (eCode, employeeId) VALUES (47, 20);
INSERT INTO tableX (eCode, employeeId) VALUES (57, 30);
CREATE TABLE employeeIds (employeeId int);
INSERT INTO employeeIds (employeeId) VALUES (10);
INSERT INTO employeeIds (employeeId) VALUES (20);
CREATE TABLE otherCodes (
eCode int,
other_codes varchar(10)
);
INSERT INTO otherCodes (eCode, other_codes) VALUES (37, 'XXX');
INSERT INTO otherCodes (eCode, other_codes) VALUES (47, 'YYY');
INSERT INTO otherCodes (eCode, other_codes) VALUES (57, 'AAA');
The query based on this schema:
SELECT
tx.eCode,
CASE WHEN ei.employeeId IS NULL THEN oc.other_codes ELSE ei.employeeId END as 'result'
FROM tableX tx
LEFT JOIN employeeIds ei ON tx.employeeId = ei.employeeId
LEFT JOIN otherCodes oc ON tx.eCode = oc.eCode;

How to generate batch number in SQL Server

Any idea on how to generate batch numbers into a batch_number column in SQL Server table from 1 to 3 and repeat it again as shown below in the script?
Thanks in advance
Declare #mTestTable table
(
id int,
city varchar(50),
batch_number int
)
--Insert some sample data
Insert into #mTestTable values (1, 'London')
Insert into #mTestTable values (2, 'Karachi')
Insert into #mTestTable values (3, 'New York')
Insert into #mTestTable values (4, 'Melbourne')
Insert into #mTestTable values (5, 'Beijing')
Insert into #mTestTable values (6, 'Tokyo')
Insert into #mTestTable values (7, 'Moscow')
I need to generate batch number from 1 to 3 and repeat it again.
Id City Batch_Number
1 London 1
2 Karachi 2
3 New York 3
4 Melbourne 1
5 Beijing 2
6 Tokyo 3
7 Moscow 1
Use row_number() and some arithmetic:
with toupdate as (
select t.*, 1 + ((row_number() over (order by id) - 1) % 3) as new_batch_no
from testtable
)
update toupdate
set batch_number = new_batch_number;
update #mTestTable
set batch_number = (id-1) % 3 + 1
If the IDs won't be sequential, then one way to do it is using a cursor passing through it. Simply count the rows one by one and update the appropriate batch number.