The table is
I need to get the last two event associates for each event
event_id event_date event_associate
1 2/14/2014 ben
1 2/15/2014 ben
1 2/16/2014 steve
1 2/17/2014 steve // this associate is the last but one for event 1
1 2/18/2014 paul // this associate is the last for event 1
2 2/19/2014 paul
2 2/20/2014 paul // this associate is the last but one for event 2
2 2/21/2014 ben // this associate is the last for event 2
3 2/22/2014 paul
3 2/23/2014 paul
3 2/24/2014 ben
3 2/25/2014 steve // this associate is the last but one for event 3
3 2/26/2014 ben // this associate is the last for event 3
I need to find out who was the last but one event_associate for each event . The result should be
event_id event_associate rn
1 steve 2
1 paul 1
2 paul 2
2 ben 1
3 steve 2
3 ben 1
I tried
SELECT t.* , ROW_NUMBER() OVER (PARTITION BY event_associate ORDER BY event_date DESC) rn
FROM mytable t
QUALIFY rn < 3
"for each event" -> PARTITION BY event_id
"last but one" -> ORDER BY event_date DESC
you have to count the number of row in your data base and then use this query
sql=sql+" LIMIT "+NumberOfRowsToShowInTables+" OFFSET "+(countrow- NumberOfRowsToShowInTables);
where countrow is he number of row in your database
NumberOfRowsToShowInTables equal 2 as you mentioned
sql is your normal query without limitation
Related
We have a daily meeting when participants nominate each other to speak. The first person is chosen randomly.
I have a dataframe that consists of names and the order of speech every day.
I have a day1, a day2 ,a day3 , etc. in the columns.
The data in the rows are numbers, meaning the order of speech on that particular day.
NA means that the person did not participate on that day.
Name day1 day2 day3 day4 ...
Albert 1 3 1 ...
Josh 2 2 NA
Veronica 3 5 3
Tim 4 1 2
Stew 5 4 4
...
I want to create two analysis, first, I want to create a dataframe who has chosen who the most times. (I know that the result depends on if a participant was nominated before and therefore on that day that participant cannot be nominated again, I will handle it later, but for now this is enough)
It should look like this:
Name Favorite
Albert Stew
Josh Veronica
Veronica Tim
Tim Stew
...
My questions (feel free to answer only one if you can):
1. What code shall I use for it without having to manunally put the names in a different dataframe?
2. How shall I handle a tie, for example Josh chose Veronica and Tim first the same number of times? Later I want to visualise it and I have no idea how to handle ties.
I also would like to analyse the results to visualise strong connections.
Like to show that there are people who usually chose each other, etc.
Is there a good package that is specialised for these? Or how should I get to it?
I do not need DNA sequences, only this simple ones, but I have not found a suitable one yet.
Thanks for your help!
If I am not misunderstanding your problem, here is some code to get the number of occurences of who choose who as next speaker. I added a fourth day to have some count that is not 1. There are ties in the result, choosing the first couple of each group by speaker ('who') may be a solution :
df <- read.table(textConnection(
"Name,day1,day2,day3,day4
Albert,1,3,1,3
Josh,2,2,,2
Veronica,3,5,3,1
Tim,4,1,2,4
Stew,5,4,4,5"),header=TRUE,sep=",",stringsAsFactors=FALSE)
purrr::map(colnames(df)[-1],
function (x) {
who <- df$Name[order(df[x],na.last=NA)]
data.frame(who,lead(who),stringsAsFactors=FALSE)
}
) %>%
replyr::replyr_bind_rows() %>%
filter(!is.na(lead.who.)) %>%
group_by(who,lead.who.) %>% summarise(n=n()) %>%
arrange(who,desc(n))
Input:
Name day1 day2 day3 day4
1 Albert 1 3 1 3
2 Josh 2 2 NA 2
3 Veronica 3 5 3 1
4 Tim 4 1 2 4
5 Stew 5 4 4 5
Result:
# A tibble: 12 x 3
# Groups: who [5]
who lead.who. n
<chr> <chr> <int>
1 Albert Tim 2
2 Albert Josh 1
3 Albert Stew 1
4 Josh Albert 2
5 Josh Veronica 1
6 Stew Veronica 1
7 Tim Stew 2
8 Tim Josh 1
9 Tim Veronica 1
10 Veronica Josh 1
11 Veronica Stew 1
12 Veronica Tim 1
I have a data frame named as Records having 2 vectors Rank and Name
Rank Name
1 Ashish
1 Ashish
2 Ashish
3 Mark
4 Mark
1 Mark
3 Spencer
2 Spencer
1 Spencer
2 Mary
4 Joseph
I want that every name should be placed in either 1, 2 ,3 or 4 tag depending on their occurrence and uniqueness:
I want to create a new vector which will be named as Tagging
So The output should be:
Rank 1 has three unique elements Mark Spencer and Ashish so the tag is 1 for all three.
Rank 2 has one unique records which is Mary as Ashish has already been assigned tag 1 so Mary is tagged as 2.
Rank 3 has no unique records as Spencer and Mark has already been assigned 1 so I cannot tag 3 to anybody.
Rank 4 has one unique record Joseph so he gets tagged as 4.
Let me know which function can help me do this.
I do not want to use looping as this is 1000000 row database
The below solution follows the principle that the highest Rank of a person is going to be that person's tag too.
tbl <- read.table(header=TRUE, text='
Rank Name
1 Ashish
1 Ashish
2 Ashish
3 Mark
4 Mark
1 Mark
3 Spencer
2 Spencer
1 Spencer
2 Mary
4 Joseph
')
Ordering the 'tbl' dataframe by Rank
tbl_ord <- tbl[with(tbl,order(Rank)),]
Removing multiple occurrence of name within same Rank
> name_ord<- tbl_ord[duplicated(tbl_ord$Rank),]
> name_ord
Rank Name
2 1 Ashish
6 1 Mark
9 1 Spencer
8 2 Spencer
10 2 Mary
7 3 Spencer
11 4 Joseph
Displaying unique Names
#name_ord[unique(name_ord$Name),] #this will work too
> name_ord[!duplicated(name_ord$Name),]
Rank Name
2 1 Ashish
6 1 Mark
9 1 Spencer
10 2 Mary
11 4 Joseph
Using the setkey function of data.table package and unique:
library(data.table)
dt<-data.table(Rank=c(1,1,2,3,4,1,3,2,1,2,4), Name=c(rep("Ashish", 3), rep("Mark", 3), rep("Spencer", 3), "Mary", "Joseph"))
setkey(dt, Rank, Name)
dt<-unique(dt)
setkey(dt, Name)
dt<-unique(dt) # works because of the above setkey call which sorted it
setkey(dt, Rank) # if you want to order them by Rank again
I have created 2 tables,
1 reservations (timestamp,name,phone,date,time,tableNo)
2 tables (tableId, TableNo)
Users are able to add and select entries from lists in a form
but when they reach table list i want to run a query which will check saved reservations (table1 time & tableNO) and their time is greater than a given variable, so the table2 will have to show next to table number the name and time of a saved reservation,
Hope this makes some sense:
reservations tables
name date time tableNO 1
John 20/5 12:30 5 2
Mary 20/5 15:00 2 3
4
5
What i want to achieve is:
SELECT FROM tables .... WHERE time > 13:00
reservations tables
name date time tableNO 1
John 20/5 12:30 5 2 Mary
Mary 20/5 15:00 2 3
4
5
This has been touched on before on this website. I want distinct on group but I also want to get the other fields too. what I need is the lowest id of each group, but instead I get the highest. I've tried variod SQL queries and the nearest 2 that work are
1)
select *
from reminder
group by Eventgroup
order by autoid
2)
SELECT distinct Autoid,EventDate,Subject,birthdate,Eventgroup
from reminder
group by Eventgroup
order by autoid
Data:
EventDate Subject birthdate Eventgroup autoid
09/10/2017 Joes Birthday 09/10/1995 4 9
13/07/2017 Bill Birthday 13/07/1999 2 8
04/04/2017 Tony Birthday 04/04/1993 3 7
09/10/2016 Joes Birthday 09/10/1995 4 6
13/07/2016 Bill Birthday 13/07/1999 2 5
04/04/2016 Tony Birthday 04/04/1993 3 4
09/10/2015 Joes Birthday 09/10/1995 4 3
13/07/2015 Bill Birthday 13/07/1999 2 2
04/04/2015 Tony Birthday 04/04/1993 3 1
both of these queries return
09/10/2017 Joes Birthday 09/10/1995 4 9
13/07/2017 Bill Birthday 13/07/1999 2 8
04/04/2017 Tony Birthday 04/04/1993 3 7
what I want is the earliets dates such as
09/10/2015 Joes Birthday 09/10/1995 4 3
13/07/2015 Bill Birthday 13/07/1999 2 2
04/04/2015 Tony Birthday 04/04/1993 3 1
Join the table with a subquery that finds the earliest date for each event group.
SELECT a.*
FROM reminders a
JOIN (SELECT eventgroup, MIN(eventdate) mindate
FROM reminders
GROUP BY eventgroup) b
ON a.eventgroup = b.eventgroup AND a.eventdate = b.mindate
This is the same structure as the second query in this answer in the duplicate question.
DEMO
Suppose I have an employee table “DB_EMPLOYEE” with the following values for EMPID and NAME
EMPID NAME
0 Bob
1 Joe
2 Carl
3 Wendy
Next, I have another table listing audits, “ICQA_AUDITS” with the following values for the columns RECORD_ID, AUDITOR_ID, PACKER_ID, SHIPPER_ID
RECORD_ID AUDITOR_ID PACKER_ID SHIPPER_ID
0 0 1 3
1 1 2 3
2 3 1 2
I can write the following query to get the list of audits with the auditor name substituted for the AUDITOR_ID:
SELECT
emp.NAME,
aud.PACKER_ID,
aud.SHIPPER_ID
FROM
ICQA_AUDITS aud, DB_EMPLOYEE emp
WHERE
aud.AUDITOR_ID = emp.EMPID
The formatted output would look something like this:
RECORD_ID NAME PACKER_ID SHIPPER_ID
0 Bob 1 3
1 Joe 2 3
2 Wendy 1 2
My question is this: How might I also get the PACKER_ID and SHIPPER_ID replaced by the appropriate names? Desired output would be the following:
RECORD_ID NAME PACKER_ID SHIPPER_ID
0 Bob Joe Wendy
1 Joe Carl Wendy
2 Wendy Joe Carl
I'm trying to format a report region in APEX and I'm not seeing the easy way to do this. Any help will be much appreciated.
Thanks!
Edit
Expanded example: Suppose that instead of just having one NAME attribute, the DB_EMPLOYEE table has FIRSTNAME and LASTNAME, as follows:
EMPID FIRSTNAME LASTNAME
0 Bob Bobbington
1 Joe Josephson
2 Carl Carlton
3 Wendy Van Dorfenstein
I can write the example query as:
SELECT
emp.LASTNAME || ', ' || emp.FIRSTNAME as "Auditor Name",
aud.PACKER_ID,
aud.SHIPPER_ID
FROM
ICQA_AUDITS aud, DB_EMPLOYEE emp
WHERE
aud.AUDITOR_ID = emp.EMPID
The output would then be:
RECORD_ID Auditor Name PACKER_ID SHIPPER_ID
0 Bobbington, Bob 1 3
1 Josephson, Joe 2 3
2 Van Dorfenstein, Wendy 1 2
The desired output would then be:
Record Auditor Name Packer Name Shipper Name
0 Bobbington, Bob Josephson, Joe Van Dorfenstein, Wendy
1 Josephson, Joe Carlton, Carl Van Dorfenstein, Wendy
2 Van Dorfenstein, Wendy Josephson, Joe Carlton, Carl
Try this query:
SELECT
a.RECORD_ID as id,
b.name as auditor_name,
c.name as packer_name,
d.name as shiper_name
FROM ICQA_AUDITS a
LEFT JOIN DB_EMPLOYEE b ON b.EMPID = a.AUDITOR_ID
LEFT JOIN DB_EMPLOYEE c ON c.EMPID = a.PACKER_ID
LEFT JOIN DB_EMPLOYEE d ON d.EMPID = a.SHIPPER_ID
WHERE a.RECORD_ID = 1
The response must be like this:
id auditor_name packer_name shiper_name
1 Joe Carl Wendy
Change "WHERE" statement to your needs. Good luck.