insert new record for date gap in SQL - sql

I have a table :
xx_asg
person_no location org mgr_person_no effective_start_date eff_end_date
1 Mumbai XYZ 101 01-jan-1901 01-jan-1990
1 Mumbai XYZ 101 01-JAN-1990 31-DEC-4712
101 Delhi xyz 201 01-JAN-2005 31-DEC-4712
5 Delhi XYZ 1 01-JAN-1901 31-DEC-4712
In this table each person has a manager whose person record is also ther in this table.
But as seen above there are cases like for person no 1 with effective start date 01-jan-1901 but it has a manager person no. 101 whose effective start date is from 01-jan-2005. so this is invalid as from 1901-2005 this manager did not existed.
Now i want to insert a record for person_no. 1 in this table in such a way that mgr person np. 101 gets deleted from 01-jan-1901 to 31-dec-2004 and there is a new record for person_no.1
from 01-jan-2005 to 31-dec-4712
something like :
person_no location org mgr_person_no effective_start_date eff_end_date
1 Mumbai XYZ 01-jan-1901 01-jan-1990
1 Mumbai XYZ 01-JAN-1990 31-DEC-2004
1 Mumbai XYZ 101 01-JAN-2005 31-DEC-4712
101 Delhi xyz 201 01-JAN-2005 31-DEC-4712
5 Delhi XYZ 1 01-JAN-1901 31-DEC-4712

Related

Use inner join and left join with more than 2 tables? [duplicate]

This question already exists:
Use left join and inner join with more than 2 tables
Closed 4 months ago.
Here you can see I have three tables
Student table
Stud_id Name Br_id Email City_id
1001 Ankit 101 ankit#bmail.com 1
1002 Pranav 105 pranav#bmail.com 2
1003 Raj 102 raj#bmail.com 2
1004 Shyam 112 shyam#bmail.com 4
1005 Duke 112 duke#bmail.com 2
1006 Jhon 102 jhon#bmail.com 3
1007 Aman 101 aman#bmail.com 4
1008 Pavan 111 pavan#bmail.com 13
1009 Virat 112 virat#bmail.com 12
Branch Table
Br_id Br_name HOD Contact
101 CSE SH Rao 22345
102 MECH AP Sharma 28210
103 EXTC VK Reddy 34152
104 CHEM SK Mehta 45612
105 IT VL Shelke 22521
106 AI KH Verma 12332
107 PROD PG Kakde 90876
Address Table
City_id City Pincode
1 Mumbai 400121
2 Pune 450011
3 Lucknow 553001
4 Delhi 443221
5 Kolkata 213445
6 Chennai 345432
7 Nagpur 323451
8 Sri Nagar 321321
I am using here three tables first one is a student table and the second one is a Branch table and the third one is an Address table
So I am writing a query like this here you can see below
select [Name], Br_name, City
from student
inner join Branch on student.Br_id = Branch.Br_id
left join [Address] on student.City_id = [Address].City_id
I have three tables I want to show here the student's name and branch name city name, but I want to show the student who has their branch only. I also want to show the student who has their city as well as show a student who does not have any city.
I wrote the query above but here I am not getting the result. Here student's name who does not have any city what's wrong here in my above join SQL query?
Here as you can see I am getting this result which I do not want to show
Name Br_name City
Ankit CSE Mumbai
Pranav IT Pune
Raj MECH Pune
Jhon MECH Lucknow
Aman CSE Delhi
Why can I not get the students who do not have a city and who have a city? What's wrong here in my join query what I am missing here?
Please let me know what's wrong in my above join query what should I do to get proper result? I have given three tables above
You can see that and why I am not able to get the result what I want to show what is the wrong in the above my join query please let me know
I hope someone will help me. Thank you so much.

I have 3 tables Flight_schedule, Flights and third is Route I need a stored procedure in SQL which give the cheapest flight on a given date

I have 3 tables Flight_schedule, Flights and third is Route I need a stored procedure in SQL which give the cheapest flight on a given date.
When the parameter date is passed to procedure suppose 2 February so the result would be the lowest fare flight on the 2 Feb.
Here is the code where I have joined the tables and passed the parameter to stored procedure but when I am confused in the condition part.
Create proc spCheapestFlight
#FLIGHT_DATE DATE
AS
BEGIN
SELECT Flight_schedule.FlightDate,Flight_schedule.Departure,route.source, route.destination,
Flight_schedule.Arrival
,Flight_schedule.Fare,Flights.Flight_name
FROM Flight_schedule
inner join route ON Flight_schedule.Route_id=route.Route_id inner join
Flights on Flights.Flight_id=Flight_schedule.Flight_id
where Flight_schedule.FlightDate = #FLIGHT_DATE
END
Flight_schedule
F_Id
Flight_id
Total_Fare
FlightDate
Departure
Arrival
Route_Id
Fare
10000
1
1001
2022-02-01
09:30:30.0000000
11:20:45.0000000
100
7500.8
10001
2
1002
2022-02-02
09:45:30.0000000
11:55:45.0000000
101
7000.9
10002
3
1003
2022-02-03
10:30:30.0000000
12:20:45.0000000
102
5111.5
10003
4
1004
2022-02-04
11:30:30.0000000
14:20:45.0000000
103
5500.9
10004
5
1005
2022-02-05
12:30:30.0000000
15:20:45.0000000
104
9000.7
10005
1
1006
2022-02-06
13:30:30.0000000
16:20:45.0000000
105
8675.5
10006
2
1007
2022-02-07
14:30:30.0000000
17:20:45.0000000
106
4000.5
10007
3
1008
2022-02-08
15:30:30.0000000
18:20:45.0000000
107
4100.5
10008
4
1009
2022-02-09
16:30:30.0000000
19:20:45.0000000
101
4000.3
10009
2
1006
2022-02-10
06:30:30.0000000
08:20:45.0000000
108
4000.3
Flights
Flight_id
Flight_name
Capacity
1
Vistara
30
2
Indigo
30
3
SpiceJet
30
4
Go_First
30
5
Air India
30
Route
route_id
source
destination
100
Mumbai
Delhi
101
Delhi
Ahmedabad
102
Ahmedabad
Delhi
103
Ahmedabad
Mumbai
104
Chennai
Mumbai
105
Chennai
Goa
106
Chennai
Delhi
107
Goa
Delhi
108
Bangalore
Delhi
109
Hyderabad
Delhi
your data does not show more than 1 flight per day, in addition there is no need for table Flights and table Route to get result, you should join table FlightSchedule with itself in order to get the minimum fare. your problem can be solve with table-value function(TVF). you should change you PROCEDURE as follows:
CREATE PROCEDURE spCheapest_Costliest_Flight (#FlightDate DATE)
AS
SELECT FS.FlightDate,FS.Departure,route.source, route.destination,
FS.Arrival ,T.Fare,Flights.Flight_name
FROM Flight_schedule FS
inner join route ON FS.Route_id=route.Route_id
inner join Flights on Flights.Flight_id=FS.Flight_id
inner join (select FlightDate FlightDate,MIN(Fare) Fare
from Flight_schedule GROUP BY FlightDate) T
ON T.FlightDate=FS.FlightDate and T.FlightDate=#FlightDate and FS.FlightDate=#FlightDate
GO

BD2: SQL _CASE with group by

I have the following tables
---SALARY_ITEMS---
PERSONID | EMPLOYMENT _REF | GROUP1 | CODE | FROM | END | QUANTI
000101 XYX 400 11101 2020-02-12 2020-02-12 12
000101 XYX 300 1100 2020-01-29 2020-02-29 1
000102 XYY 450 11111 2020-02-01 2020-02-12 19
000102 XYY 400 11101 2020-02-02 2020-02-12 82
000103 XYA 500 1100 2020-02-10 2020-02-12 11
000104 XYB 700 1100 2020-01-12 2020-02-12 24
---PERSON ---
PERSONID NAME
000101 Carolina
000102 Helen
000103 Jack
000104 Anna
---EMPLOYMENT---
PERSONID EMPLOYMENT _REF POSITION
000101 XYX doctor
000102 XYY nurse
000103 XYA nurse
000104 XYB Proffesor
----absent---
PERSONID CODE2 FROM END
000101 123 2020-03-01 2020-06-30
000102 120 2020-02-05 2020-02-13
000102 123 2020-03-01 2020-03-28
000103 115 2020-05-05 2020-06-30
000104 123 2020-02-01 2020-05-30
What I tried to do: get all employee that they are doctor and nurse and have certain group with certain code and works over 100 hours in a 2020 -Feb.
The following SQL query give me what i want But i want to add something to my query that is :
create a new column to see if the employee was absent in the same period 2020-feb with absent code 120 or 119 or both.
If he was I will get the 'CODE2' ELSE 'NOTHING'.
How can I do this in DB2?
This is the result I need to get:
PERSONID | NAME | POSITION | QUANTITY |ABSENT (this what i want to have)
000102 Helen NURSE 101 120
Query:
SELECT
S.PERSONID, P.NAME,E.POSTION , sum(S.QUANTITY) as QUANTITY
FROM
SALARY_ITEMS S
LEFT JOIN
PERSON P ON S.PERSONID = P.PERSONID
LEFT JOIN
EMPLOYMENT E ON E.EMPLOYMENT_REF = S.EMPLOYMENT _REF
WHERE
S.group1 IN ('400', '440', '450', '470', '640')
AND S.code IN ('11101', '11111', '11121', '11131', '11141')
AND S.from >= '2020-02-01'
AND S.end <= '2020-02-29'
AND E.POSTION IN ('nurse', 'doctor')
AND (SELECT SUM(S2.QUANTITY) AS QUANTITY2
FROM SALARY_ITEMS S2
WHERE S2.group1 IN ('400', '440', '450', '470', '640')
AND S2.code IN ('11101', '11111', '11121', '11131', '11141')
AND S2.from >= '2020-02-01'
AND S2.end <= '2020-02-29'
AND S.PERSONID = S2.PERSONID) >= '100'
GROUP BY
S.PERSONID, P.NAME, E.POSTION

SPARK SQL query for match output

I have 2 ds as below
ds1:
CustId Name Street1 City
=================================
1 Ron 1 Mn strt Hyd
2 Ashok westend av Delhi
3 Rajesh 5th Cross Mumbai
4 Venki 2nd Main NY
ds2:
Id CustName CustAddr1 City
=========================================
11 Ron 1 Mn Street Hyd
12 Ron eastend avn Patna
13 Rajesh 2nd Main Mumbai
14 Girish 100ft rd BLR
15 Dinesh 60ft Mum
16 Rajesh 1st Cross Mumbai
I am trying to find an exact match like ds1.Name --> ds2.CustName, ds1.city --> ds2.city
Output:
GrpID Rec_Id Count ds1.cond Rec_Id Count ds2.cond
======================================================================
1 1 1 Ron + Hyd 1001 1 Ron + Hyd
2 2 1 Rajesh + Mumbai 1002 2 Rajesh + Mumbai
How to write (SPARK) SQL query for it?
I tried
final Dataset<Row> rslt = spark.sql("select * from ds1 JOIN ds2 ON ds1.Name==ds2.CustName");
(using only name)
but it gives output of mXn for m matching rows in ds1 with n matching rows in ds2.
My first work on this. Any suggestion?

Complicated Sql Query Of Joining Two Tables

I having Following Two Tables and wants the result date and bank wise. The Example is as below.
Table1: Name:- AccountMast
companyID A/CID Name BalDR BalCR
102 14 SHOBHA NULL NULL
102 15 SONKI NULL NULL
102 16 BANK OF INDIA null 12000
Table2 Name :- DeBank
companyID transID date name particulars deposit withdrawal
102 2 12/04/2012 BANK OF INDIA MAHENDRA 1000 NULL
102 4 15/04/2012 CENTRAL BANK MAHENDRA 1000 NULL
102 3 20/05/2012 BANK OF INDIA MAHENDRA 2000 NULL
Result
BANK WISE RESULT BANK OF INDIA
date transID particulars deposit withdrawal BALANCE
01/04/2012 BAL B/F 12000
12/04/2012 2 MAHENDRA 1000 NULL 13000
20/05/2012 3 MAHENDRA 2000 NULL 15000
BANK WISE RESULT CENTRAL BANK OF INDIA
date transID particulars deposit withdrawal BALANCE**
01/04/2012 BAL B/F
15/04/2012 4 MAHENDRA 2000 NULL 2000
So what is the sql statement for it?.