Sqlite3 join tables with result - sqlite

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

Related

Join wide table from different starting dates

Hey guys I have 2 DBs:
BaseDB:
"id", "Main_event_date"
*id* *Main_event_date*
1 01/01/2017
2 01/07/2018
3 01/11/2017
DB_2
"id","event_date"
*id* *event_date*
1 01/02/2017
1 19/12/2017
1 19/01/2018
2 01/10/2018
2 10/01/2019
DB_2 might be duplicated in "id": every time an "id" has an event, I have a new row with a new "event_date".
I reshaped the DB_2 from long to wide using "reshape", so that now I have:
DB_2_wide
"id", "event_date0", "event_date1"...."event_date100".
I would like to join the BaseDB with DB_2_Wide considering only the first 30 events after the main_event_date for every "id" having a final table of the type:
Final_Table
id, t1, ... t30
*id* *t1* *t2* *t3* ..... *t30*
1 x NA NA
2 NA NA x
3 NA NA NA
Considering also that t1 for 1 is Feb2017,
t1 for 2 is August 2018
t1 for 3 is December 2017
Not sure how to do this because simply using left_join (or merge) I would get a table like:
"id", "event_date0", "event_date1"...."event_date100",
hence for every id I would also get a column for events before the main_event_date, which would be NA.
Not sure this is clear so let me know if have any questions.

How to only show results if count if more than 5 [duplicate]

This question already has answers here:
How to 'group by' using variables in 4gl
(2 answers)
Closed 3 years ago.
Is it possible to only show results where by the same cust-num appears more than 3 times.
I have tried using a variable ( count = count + 1.). but its counting all the results .
icount = kcount + 1.
display
cust-name cust-num state count
Cust Num Name State Count
1001 Apple NH 1
1001 Apple NH 2
1001 Apple NH 3
1002 Orange BD 6
1002 Orange BD 7
1002 Orange BD 8
1002 Orange BD 9
Expected
Cust Num Name State Count
1001 Apple NH 1
1002 Orange BD 2
Ok, first you probably need a temp-table like this:
DEFINE TEMP-TABLE ttCust LIKE customer
FIELD count AS INTEGER.
Then, remove that code you posted and add this:
FIND FIRST ttCust WHERE ttCust.cust-num = customer.cust-num NO-ERROR.
IF NOT AVAILABLE ttCust then DO:
CREATE ttCust.
BUFFER-COPY customer TO ttCust. /* Copies the whole record to your temp-table */
END.
ASSIGN ttCust.count = ttCust.count + 1.
And after the END of your regular FOR EACH customer, add this:
FOR EACH ttCust where ttCust.count >= 3:
DISPLAY ttCust.cust-num ttCust.name ttCust.state ttCust.count.
END.
Hope this helps.

RPostgreSQL- Insert a column into another table according to the ID

I've used RPostgreSQL to connect R and postgresQL, and I'd like to insert a column into another table according to the "pid", please advise how is can be achieved using R command:
>library(RPostgreSQL)
>drv<-dbDriver("PostgreSQL")
>itemlist<- dbGetQuery(con, "SELECT * from project_budget_itemlist")
>View(itemlist)
pid item cost
1 ABC 9
2 ACB 8
3 BAC 7
3 ZZZ 6
and another tables is as follow:
>name<- dbGetQuery(con, "SELECT * from namelist")
>View(name)
pid name
1 Sally
2 Joy
3 Susan
I want to the result to be:
pid item cost name
1 ABC 9 Sally
2 ACB 8 Joy
3 BAC 7 Susan
3 ZZZ 6 Susan
If there are no matching pids in both outputs, the merge will return an empty data frame. If there are, then this should work:
merge(itemList, name)

SQLite - SELECT DISTINCT of one column and get the others

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

How to get the last two rows

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

Resources