Monthwise defaulterlist of students - sql-server

Table name: student
Stu_id Stu_Name
1 ani
2 sudipta
3 debayan
4 achinto
5 dipankar
Table name: trans
stu_id mon amount
1 JANU 500
1 FEB 600
1 APR 500
3 JANU 600
3 MAR 700
result will be below
STU_ID STU_NAME Defaulter month
1 ani MAR,MAY,JUNE,JULY,AUG,SEP,OCT,NOV,DEC
2 SUDIPTA JANU.FEB,MAR,APR,MAY,JUNE,JULY,AUG,SEP,OCT,NOV,DEC
3 debayan FEB,APR,MAY,JUNE,JULY,AUG,SEP,OCT,NOV,DEC
4 achinto JANU.FEB,MAR,APR,MAY,JUNE,JULY,AUG,SEP,OCT,NOV,DEC
5 dipankar JANU.FEB,MAR,APR,MAY,JUNE,JULY,AUG,SEP,OCT,NOV,DEC

Related

How to calculate mean of previous values of other firms for Director ID before he joins the firm

I need to calculate the previous wage of director before he joins a new company.
I have created a simple dataset for one director (in practice I have many observations of director_id). This director with ID = 1 manages 5 firms which he joined in different years (the variable called enter). If director joined firm number 2 in 2011, I need the average of the variable wage for all years before 2011 which he was managing. For the same director = 1, I need a different mean(wage) for firm number 3 which he joined in 2012 (which will include mean(wage) from previous 2 companies that he managed before entering company 3 in 2012).
Below is the data. I would really appreciate your help in coding this problem.
clear
input enter year wage director_id firm_id
2006 2006 6.4790964 1 1
2006 2010 6.4783854 1 1
2006 2011 6.4067149 1 1
2006 2012 6.3716507 1 1
2006 2013 6.2248578 1 1
2006 2014 6.0631728 1 1
2011 2011 5.0127039 1 2
2011 2012 4.9616795 1 2
2011 2013 4.9483747 1 2
2011 2014 5.2612371 1 2
2012 2012 4.5389338 1 3
2012 2013 4.4322848 1 3
2012 2014 4.3223209 1 3
2013 2013 4.336947 1 4
2013 2014 4.27459 1 4
2015 2015 -.60586482 1 5
2015 2016 .085194588 1 5
end
I just need to exclude from mean(wage) all values that happen after he enters, so really need to regard only years before he enters a new company.
A recipe for what I think you seek is that the mean previous wage in other firms =
(SUM of previous wages in all firms MINUS sum of previous wages in this firm) / (COUNT of previous years in all firms MINUS count of previous years in this firm).
Your example is helpful but the wage variable is too irregular to allow easy eyeball checks.
Consider this sequence, where rangestat is from SSC.
clear
input enter year wage director_id firm_id
2006 2006 6.4790964 1 1
2006 2010 6.4783854 1 1
2006 2011 6.4067149 1 1
2006 2012 6.3716507 1 1
2006 2013 6.2248578 1 1
2006 2014 6.0631728 1 1
2011 2011 5.0127039 1 2
2011 2012 4.9616795 1 2
2011 2013 4.9483747 1 2
2011 2014 5.2612371 1 2
2012 2012 4.5389338 1 3
2012 2013 4.4322848 1 3
2012 2014 4.3223209 1 3
2013 2013 4.336947 1 4
2013 2014 4.27459 1 4
2015 2015 -.60586482 1 5
2015 2016 .085194588 1 5
end
sort year firm_id
replace wage = _n
rangestat (sum) SUM=wage (count) COUNT=wage, int(year . -1) by(director_id)
rangestat (sum) sum=wage (count) count=wage, int(year . -1) by(director_id firm_id)
replace sum = 0 if sum == .
replace count = 0 if count == .
gen wanted = (SUM - sum) / (COUNT - count)
list, sepby(year)
+---------------------------------------------------------------------------------+
| enter year wage direct~d firm_id SUM COUNT sum count wanted |
|---------------------------------------------------------------------------------|
1. | 2006 2006 1 1 1 . . 0 0 . |
|---------------------------------------------------------------------------------|
2. | 2006 2010 2 1 1 1 1 1 1 . |
|---------------------------------------------------------------------------------|
3. | 2006 2011 3 1 1 3 2 3 2 . |
4. | 2011 2011 4 1 2 3 2 0 0 1.5 |
|---------------------------------------------------------------------------------|
5. | 2006 2012 5 1 1 10 4 6 3 4 |
6. | 2011 2012 6 1 2 10 4 4 1 2 |
7. | 2012 2012 7 1 3 10 4 0 0 2.5 |
|---------------------------------------------------------------------------------|
8. | 2006 2013 8 1 1 28 7 11 4 5.666667 |
9. | 2011 2013 9 1 2 28 7 10 2 3.6 |
10. | 2012 2013 10 1 3 28 7 7 1 3.5 |
11. | 2013 2013 11 1 4 28 7 0 0 4 |
|---------------------------------------------------------------------------------|
12. | 2006 2014 12 1 1 66 11 19 5 7.833333 |
13. | 2011 2014 13 1 2 66 11 19 3 5.875 |
14. | 2012 2014 14 1 3 66 11 17 2 5.444445 |
15. | 2013 2014 15 1 4 66 11 11 1 5.5 |
|---------------------------------------------------------------------------------|
16. | 2015 2015 16 1 5 120 15 0 0 8 |
|---------------------------------------------------------------------------------|
17. | 2015 2016 17 1 5 136 16 16 1 8 |
+---------------------------------------------------------------------------------+

How to track changes in the board of directors within the same firm

In Stata I need to create a new variable "changes in the board of directors" which indicates whether the same directors are observed in the same firm over time. Consider an example below:
clear
input dirid firmid year
1 10 2006
2 10 2006
3 10 2006
1 10 2007
2 10 2007
3 10 2007
1 10 2008
2 10 2008
3 10 2008
4 10 2008
3 10 2009
4 10 2009
end
Directors ID 1, 2, and 3 are in firm 10 in 2006 and in 2007. So there was no change in the board of directors from t-1 to t. The variable "changes in the board of directors" should be 0. However, in 2008 a new director came to the board dirid = 4, so there was a change in the board and the variable should be 1. The same in 2009 because dirid 1 and 2 left the company. So any change, whether the entrance or exit of directors, should be reported with 1 in the new binary variable.
Here's another way to do it. I think it should cope with directors leaving and later coming back.
clear
input dirid firmid year
1 10 2006
2 10 2006
3 10 2006
1 10 2007
2 10 2007
3 10 2007
1 10 2008
2 10 2008
3 10 2008
4 10 2008
3 10 2009
4 10 2009
end
bysort firmid year (dirid) : gen board = strofreal(dirid) if _n == 1
by firmid year : replace board = board[_n-1] + " " + strofreal(dirid) if _n > 1
by firmid year : replace board = board[_N]
by firmid : gen anychange = year != year[_n-1] & board != board[_n-1]
bysort firmid year (anychange) : replace anychange = anychange[_N]
sort firmid year dirid
list, sepby(firmid year)
+--------------------------------------------+
| dirid firmid year board anycha~e |
|--------------------------------------------|
1. | 1 10 2006 1 2 3 1 |
2. | 2 10 2006 1 2 3 1 |
3. | 3 10 2006 1 2 3 1 |
|--------------------------------------------|
4. | 1 10 2007 1 2 3 0 |
5. | 2 10 2007 1 2 3 0 |
6. | 3 10 2007 1 2 3 0 |
|--------------------------------------------|
7. | 1 10 2008 1 2 3 4 1 |
8. | 2 10 2008 1 2 3 4 1 |
9. | 3 10 2008 1 2 3 4 1 |
10. | 4 10 2008 1 2 3 4 1 |
|--------------------------------------------|
11. | 3 10 2009 3 4 1 |
12. | 4 10 2009 3 4 1 |
+--------------------------------------------+
See also [this paper][1] on concatenating rowwise.
[1]: https://journals.sagepub.com/doi/full/10.1177/1536867X20909698
clear
input dirid firmid year
1 10 2006
2 10 2006
3 10 2006
1 10 2007
2 10 2007
3 10 2007
1 10 2008
2 10 2008
3 10 2008
4 10 2008
3 10 2009
4 10 2009
end
bysort firmid year (dirid): gen n = _n
reshape wide n, i(firmid year) j(dirid)
egen all_directors = concat(n*)
bysort firmid (year): gen change = all_directors != all_directors[_n-1] & _n > 1
reshape long
drop if missing(n)
drop all_directors n

First-In-First-Out Stock trading - calculate cumulative P/L

I want a sql-server query to calculate cumulative P/L on stock trading (FIFO based calculation).
Input table :
EXECTIME
share_name
Quantity
Price
Buy/Sell
2013-01-01 12:25
abc
100
100
B
2013-01-01 12:26
abc
10
102
S
2013-01-01 12:27
abc
10
102
S
2013-01-01 12:28
abc
10
95
S
2013-01-01 12:29
abc
10
99
S
2013-01-01 12:30
abc
10
105
S
2013-01-01 12:31
abc
100
102
B
2013-01-01 12:32
abc
150
101
S
OUTPUT :
EXECTIME
Cumualative P/L
Winning Streak
Lossing Streak
2013-01-01 12:26
20
1
0
2013-01-01 12:27
40
1
0
2013-01-01 12:28
-10
0
1
2013-01-01 12:29
-20
0
2
2013-01-01 12:30
30
1
0
2013-01-01 12:32
-20
0
1
Explanation :
1st row - 10 shares sold at 102 which were purchased at 100. So profit = (102-100) * 10 = 20
6th row - 150 shares sold at 101,
50 were purchased at 100 - 1st row( 50 already sold above, 50 left)
100 were purchaed at 102 - 7th row
150 * 101 - [(50 * 100)+(100 * 102)] = -50
cumaltive p/l = 30 + (-50) = -20
Winning streak - 1 for positive
Lossing streak - 1,2,... for continuous loss. reset again after profit

Create a Column that shows the day of the month based on a date column

I am attempting to return day of the week (i.e. Monday = 1, Tuesday = 2, etc) based on a date column ("Posting_date"). I tried a for loop but got it wrong:
#First date of table was a Sunday (1 March 2019) => so counter starts at 6
posting_df3['Day'] = (posting_df3['Posting_date'] - dt.datetime(2019,3,31)).dt.days.astype('int16')
# Start counter on the right date (31 March 2019 is a Sunday)
count = 7
for x in posting_df3['Day']:
if count != 7:
count = 1
else:
count = count + 1
posting_df3['Day'] = count
Not sure if there are other ways of doing this. Attached is an image of my database structure:
level_0 Posting_date Reservation date Book_window ADR Day
0 9 2019-03-31 2019-04-01 -1 156.00 0
1 25 2019-04-01 2019-04-01 0 152.15 1
2 11 2019-04-01 2019-04-01 0 149.40 1
3 42 2019-04-01 2019-04-01 0 141.33 1
4 45 2019-04-01 2019-04-01 0 159.36 1
... ... ... ... ... ... ...
4278 739 2020-02-21 2019-04-17 310 253.44 327
4279 739 2020-02-22 2019-04-17 310 253.44 328
4280 31 2020-03-11 2019-04-01 345 260.00 346
Final output should be 2019-03-31 Day column should return 7 since it is a Sunday
and 2019-04-01 Day column should return 1 since its Monday etc
You can do it this way
df['weekday']=pd.to_datetime(df['Posting_date']).dt.weekday+1
Input
level_0 Posting_date Reservation_date Book_window ADR Day
0 9 3/31/2019 4/1/2019 -1 156.00 0
1 25 4/1/2019 4/1/2019 0 152.15 1
2 11 4/1/2019 4/1/2019 0 149.40 1
3 42 4/1/2019 4/1/2019 0 141.33 1
4 45 4/1/2019 4/1/2019 0 159.36 1
Output
level_0 Posting_date Reservation_date Book_window ADR Day weekday
0 9 3/31/2019 4/1/2019 -1 156.00 0 7
1 25 4/1/2019 4/1/2019 0 152.15 1 1
2 11 4/1/2019 4/1/2019 0 149.40 1 1
3 42 4/1/2019 4/1/2019 0 141.33 1 1
4 45 4/1/2019 4/1/2019 0 159.36 1 1

JFreeChart: How to define a DataSet that groups by year, month and day?

I have a SQL query already that gets the data I need but I'm struggling to figure out how to get that into a chart. This is sample data as result of my query:
year month day mode amount duration
2013 2 22 0 1 36001
2013 7 7 1 1 55062
2015 12 23 1 6 13
2015 12 23 4 4 11
2015 12 23 7 31 104
2015 12 23 8 2 4
2015 12 23 12 11 21
2015 12 23 13 3 8
2016 3 24 1 207 519
If I wanted to graph lets say amount grouped per year, month and day how would that be done in JFreeChart?

Resources