I have a SQL Server 2005 table in which I store the book exchanges that take place between two students.
ExchangeID BookID ExchangeDate FromPersName ToPersName
1 23 23.12.2011 John Matt
2 22 15.02.2012 Billy Ken
3 23 27.12.2011 Matt Riddley
5 23 05.03.2012 Riddley Josh
6 22 08.03.2012 Ken Rachel
7 23 19.03.2012 Josh Laura
8 23 15.01.2013 Laura Mike
9 22 17.01.2013 Rachel Stephanie
I want to generate a report for a specified year that looks like this:
Year:2012
BookID PersonName ReceivingDate DeliveryDate
23 Matt 01.01.2012 27.02.2012
23 Riddley 27.02.2012 05.03.2012
23 Josh 05.03.2012 19.03.2012
23 Laura 19.03.2012 31.12.2012
22 Ken 01.01.2012 08.03.2012
22 Rachel 08.03.2012 31.12.2012
This is half solution.You need to change it a litle bit to work. I will not make whole query for Your homework. Put some effort!
http://sqlfiddle.com/#!3/39d73/17
table can join it self
select ex1.BookID,ex1.toPersName,ex1.ExchangeDate as DeliveryDate,Exchanges.ExchangeDate
as ReceivingDate from Exchanges as ex1 inner join Exchanges on
ex1.toPersName=Exchanges.FromPersName
Related
Name Canada US Euro
John 10 50 60
Mindy 5 60 100
Joe 20 15 55
Alan 10 30
Visual of original table
Into The below Format (effectively a concatenation of the first column and the first row into one column and then taking the array of number values corresponding to the person and geography into one column).
Visual of Desired Table
Name Fields
John Canada 10
John US 50
John Euro 60
Mindy Canada 5
Mindy US 60
Mindy Euro 100
Joe Canada 20
Joe US 15
Joe Euro 55
Alan Canada 10
Alan US 30
I am able to get it to work without the concatenation of the first column and first row.
I'm trying to write a recursive CTE for a table that does not have a hierarchy. Meaning that there is no NULL in the family of IDs that are related.
For example table looks like this:
So it looks like this:
AccountID Account_RelationshipID
--------------------------------
1 2
2 4
4 6
6 11
11 1
15 17
17 19
19 15
So 1 relates to 2. 2 relates to 4. 4 relates to 6. 6 relates to 11. And then 11 loops back to ID of 1.
Then there is a new family. 15 relates to 17. 17 relates to 19. and then 19 goes back to 15.
There is also a separate Account_Detail table that has the account date:
AccountID AccountName AccountDate
-------------------------------------
1 Dave 1/1/2012
2 Dave 1/1/2013
4 Dave 1/1/2014
6 Dave 1/1/2015
11 Dave 1/1/2016
15 Paul 7/1/2015
17 Paul 7/1/2016
19 Paul 7/1/2017
I tried writing this as my code:
WITH C AS
(
SELECT
AR.AccountID,
AR.Account_RelationshipID,
AD.AccountDate
FROM
Account_Relationship AR
INNER JOIN
Account_Detail AD ON AD.AccountID = AR.AccountID
UNION ALL
SELECT
AR2.AccountID,
AR2.Account_RelationshipID,
AD.AccountDate
FROM
Account_Relationship AR2
INNER JOIN
Account_Detail AD2 ON AD2.Account_ID = AR2.Account_ID
INNER JOIN
C ON C.AccountID = AR2.Account_relationshipID
WHERE
AD.AccountDate < AD2.AccountDate
)
Obviously this code is totally wrong. This is as far as I've gotten. This code will loop infinitely.
I was thinking I could break the loop by adding a function that states when the AccountDate of the next AccountID in the loop is less than the AccountDate of the last AccountID, to break the loop and go to the next one.
Also, how do you get it to go to the next "family" of accountIDs in the loops (Paul in this case)? All the videos and tutorials I've seen about Recursive CTEs just teach how to do it for one family - usually with a hierarchical structure that breaks at NULL as well.
Help!!
I'm stuck with a new problem
I have two tables:
BudgetNumber Name Remaing(h) EndDate
BU145122 Jhon 5 2018/11/15
BU145123 Jhon 20 2018/12/01
BU145124 Lewis 10 2018/11/15
BU145125 Lewis 50 2018/12/15
Actuals:
Actuals(h) Name DATE
5 Jhon 2018/11/01
6 Jhon 2018/11/10
5 Jhon 2018/11/25
5 Lewis 2018/12/10
So in my final table, I'd like something like:
Name Remaining(h) Date Budget
Jhon 5 2018/11/01 BU145122
Jhon 14 2018/11/01 BU145123
Jhon 9 2018/11/25 BU145123
Lewis 45 2018/12/10 BU145125
So if the date of my budget is lower than my actual date, my budget is lost
If it's not the case, I substract my Actuals from my remaing(Table B)
I can get a table with a windows table but I'm stuck with the date
Is there another way to do that than a loop?
Thanks for your help
Assume my data (df) looks something like this:
Rank Student Points Type
3 Liz 60 Junior
1 Sarah 100 Junior
10 John 40 Senior
2 Robert 70 Freshman
13 Jackie 33 Freshman
11 Stevie 35 Senior
I want to sort the data according to the Points, followed by Rank column in descending and ascending order, respectively, so that it looks like this:
Rank Student Points Type
1 Sarah 100 Junior
2 Robert 70 Freshman
3 Liz 60 Junior
10 John 40 Senior
11 Stevie 35 Senior
13 Jackie 33 Freshman
So I did this:
df[order(df[, "Points"], df[, "Rank"]), ]
Resulted in this:
Rank Student Points Type
1 Sarah 100 Junior
10 John 40 Senior
11 Stevie 35 Senior
13 Jackie 33 Freshman
2 Robert 70 Freshman
3 Liz 60 Junior
Question: How do I fix this?
I'm trying to use the column headers because the column length/width may change which can affect my sorting if I use physical locations.
FYI: I've tried so many suggestions and none seems to work:
one, two, three and four...
Try this:
df[order(df$Points,decreasing=T,df$Rank),]
Rank Student Points Type
2 1 Sarah 100 Junior
4 2 Robert 70 Freshman
1 3 Liz 60 Junior
3 10 John 40 Senior
6 11 Stevie 35 Senior
5 13 Jackie 33 Freshman
going back to the basics :) http://www.statmethods.net/management/sorting.html
so, your code should be:
df <- df[order(-Points, Rank),]
Like m0nhawk pointed out in his comment, you probably have the data as strings. String characters are ordered one at a time.
You need to convert them to numeric first. Also, for decreasing order you need the argument decreasing = TRUE.
df[, "Rank"] <- as.numeric(df[, "Rank"])
df[, "Points"] <- as.numeric(df[, "Points"])
df[order(df[, "Points"], decreasing = TRUE, df[, "Rank"]), ]
If the data type is 'factor' this will not work, though. You can try the following:
df <- as.data.frame(df, stringsAsFactors = FALSE)
And then the three lines above will work.
I have two tables.
Table1:
Label Date CT
A 2014-01-01 19
A 2014-02-01 10
A 2014-03-01 19
A 2014-04-01 18
B 2014-01-01 20
B 2014-02-01 16
B 2014-03-01 14
B 2014-04-01 16
C 2014-01-01 13
C 2014-02-01 12
C 2014-03-01 19
C 2014-04-01 14
Table2 :
Label Date CT
D 2014-01-01 19
D 2014-02-01 10
D 2014-03-01 19
D 2014-04-01 18
E 2014-01-01 20
E 2014-02-01 16
E 2014-03-01 14
E 2014-04-01 16
F 2014-01-01 13
F 2014-02-01 12
F 2014-03-01 19
F 2014-04-01 14
Desired Output :
Label Jan'14 Feb'14 Mar'14 Apr'14 Total
A 19 10 19 18 66
B 20 16 14 16 66
C 13 12 19 14 58
D 19 10 19 18 66
E 20 16 14 16 66
F 13 12 19 14 58
I'm new to PostgreSQL.
I wanted to take the unique values of Label column from both the table.
And produce the sum total of count to their respective label.
I can combine both the tables in a straight forward method using UNION ALL.
But that'll not give me the desired output or the view like a pivot.
I did google on this but nothing could help me out.
Came across this in SO. And I'm still trying on with it.
But I actually don't have a clue whether it can be done or not.
Can someone help me in getting the desired output.
Thanks in advance!!
Try Like This
select *,("Jan ''14" + "Feb ''14" + "Mar ''14" +"Apr ''14") as total
from crosstab($$
select id,to_char(da,'Mon ''yy') as tt,no from t2
union all
select id,to_char(da,'Mon ''yy') as tt,no from "T1"
$$,$$values ('Jan ''14'), ('Feb ''14'),('Mar ''14'),('Apr ''14') $$) as at
(id text, "Jan ''14" integer,"Feb ''14" integer,"Mar ''14" integer,
"Apr ''14" integer) order by id