SQLite - SELECT DISTINCT of one column and get the others - sqlite

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

Related

How to query sum and distinct in one sqlite query?

I have a table with the following data:
Class Student Score Date
A Tom 10 Aug 1
B Dave 9 Aug 1
A Jan 7 Aug 2
B Jack 8 Aug 2
A Matt 5 Aug 3
B Dave 8 Aug 4
A Matt 8 Aug 4
Each student can only have one score in each day but can have multiple scores for multiple days.
I want to do a query to list total score of each class and number of student in each class. How can I do that with one query?
The expected result:
Class Student_Count Total_Score
A 3 30
B 2 25
You need to group by class, count the distinct number of students and sum the scores of all the students:
SELECT Class,
COUNT(DISTINCT Student) Student_Count,
SUM(Score) Total_Score
FROM tablename
GROUP BY Class;
See the demo.
Use a distinct student count:
SELECT Class,
COUNT(DISTINCT Student) AS Student_Count,
SUM(Score) AS Total_Score
FROM yourTable
GROUP BY
Class;

Lead function group by in oracle

I want to group by lead function by two column. Here is my table data.
Id Name_Id Name Item_Id Item_Name date
1 1 Car 1 SUV 1-Jan-2015
2 1 Car 1 SUV 12-March-2015
3 1 Car 1 SUV 20-April-2015
4 1 Car 2 Sport 23-April-2015
5 2 Bike 1 SUV 18-July-2015
6 2 Bike 1 SUV 20-Aug-2015
7 2 Bike 2 Sport 18-Sept-2015
8 2 Bike 3 Honda 20-OCT-2015
And I need result from above table like.
Id Name_Id Name Item_Id Item_Name start date end date
1 1 Car 1 SUV 1-Jan-2015 20-April-2015
2 1 Car 2 Sport 20-April-2015 23-April-2015
3 2 Bike 1 SUV 18-July-2015 20-Aug-2015
4 2 Bike 2 Sport 20-Aug-2015 18-Sept-2015
5 2 Bike 3 Honda 18-Sept-2015 20-OCT-2015
Any suggestion really appreciated.
I don't think you need to use LEAD here. The CTE below computes, for each Item_Id, the earliest and latest date. This is then joined to your original table to restrict to records corresponding to the earliest Item_Id. At the same time, the end date is also pulled in during the join.
WITH cte AS (
SELECT Name,
Item_Id,
MIN(date) AS start_date,
MAX(date) AS end_date
FROM yourTable
GROUP BY Name, Item_Id
)
SELECT t1.Id, t1.Name_Id, t1.Name, t1.Item_Id, t1.Item_Name,
t2.start_date,
t2.end_date
FROM yourTable t1
INNER JOIN cte t2
ON t1.Item_Id = t2.Item_Id AND
t1.Name = t2.Name AND
t1.date = t2.start_date

Trouble with SQL query using COUNT

list days appearing in reservation where only red
boats have been reserved using COUNT aggregate function.
Here is the table
servant table:
sname rating
Joe 4
Bob 2
Tim 9
Mike 1
Lewis 5
boat table:
bname color rating
Ace orange 6
Bethany red 5
Cruiser green 9
WindySea red 8
reservation table:
sname bname day
Bob Ace Monday
Bob Bethany Wednesday
Bob WindySea Saturday
Tim Ace Sunday
Tim Bethany Wednesday
Tim Cruiser Wednesday
Mike Ace Monday
SELECT r.day
FROM reservation r
JOIN boat b
ON r.bname = b.bname
GROUP BY r.day
HAVING COUNT(CASE WHEN b.color <> 'red' THEN 1 ELSE NULL END) = 0
AND COUNT(CASE WHEN b.color = 'red' THEN 1 ELSE NULL END) > 0 -- optional

Sqlite3 join tables with result

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

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