How to select last entry per ID in SQL [duplicate] - sql

This question already has answers here:
How to get the last record per group in SQL
(10 answers)
Closed 6 years ago.
I have a big log table with 2 million rows give or take.
I am looking to look for the last entry for each id.
The 3 columns of importance are
Userid
Actiontype
Actiontime
Text2
Some userids show up thousands of times some just show up once. I need the most recent of each userid. I tried to use 'Group By' but it wont work because text2 is different for each entry which is really the data I need. So it needs to be ordered by actiontime, actiontype needs to be 103. I am really at a loss how to do this.
Any help would be appreciated.

Select B.*
From (
Select UserID,ActionTime=max(ActionTime)
From SomeTable
Group By UserID
) A
Join SomeTable B on A.UserID=B.UserID and A.ActionTime=B.ActionTime

Related

How to use max on sum in oracle sql?

I am working in a task and got stuck at particular question. I am new to SQL so I am reaching out to this platform for the support. Below are the 2 tables. 1st is Theatre_play_table and 2nd is Ticketsales table.
Question: List titles, directors and writers of all shows/plays with the highest total sale.
Theatre_play_table
Ticketsales table
I have pasted screenshot of some part of the table. ID column in both the table represents the same information. Last column in Ticketsales table is Totalamount.
I have tried with below query;
Select theatre_play.title, theatre_play.director, theatre_play.writer, sum(totalamount)
from theatre_play, totalsales
where theatre_play.id = totalsales.id
group by theatre_play.title, theatre_play.director, Theatre_play.writer
order by sum(totalamount) desc
fetch first 3 rows only;
The above approach is not useful when data is huge. I wanted to apply max(sum(totalamount)) but oracle threw an error.
Can anyone please help me solve this question?
If I understand you right, the issue is to get the three highest values?
Then try something like this:
select * from (
Select dpro.title, dpro.director, dpro.writer, sum(fth.totalamount)
from dpro
join fth on dpro.id = fth.id
group by dpro.title, dpro.director, dpro.writer
order by sum(totalamount) desc )
where rownum <=3

Select All the similar data in order wise [duplicate]

This question already has answers here:
sql ORDER BY multiple values in specific order?
(12 answers)
Closed 2 years ago.
I have a large amount of data in data base.Consider this i have a table of student records and i need to get all the student with A grade first and then the B grade and so on..
It sounds like you want order by:
select t.*
from t
order by t.grade;

Get Distinct records based on column B [duplicate]

This question already has answers here:
Get top 1 row of each group
(19 answers)
sql server select first row from a group
(2 answers)
Closed 3 years ago.
I have 2 columns in a table where column A has distinct records and column b does not i.e. column b could have multiple items of the same value in each row.
What I require is to only show distinct records based on column B. I do need both columns though as it will be an input in Crystal Reports but I don't care what value column A has.
CreatedBy column has multiple different values and I need to get those distinct values.
Query
SELECT DISTINCT nh.InNeoHistoryId, nh.CreatedBy
FROM NeoHistory nh
GROUP BY nh.CreatedBy, nh.InNeoHistoryId
Result
Try below query
SELECT nh.CreatedBy,min(nh.InNeoHistoryId)
FROM NeoHistory nh group by nh.CreatedBy
Just make a small tweak to your query:
SELECT MIN(nh.InNeoHistoryId), nh.CreatedBy
FROM NeoHistory nh
GROUP BY nh.CreatedBy

Sum of data at each row in different column in sql [duplicate]

This question already has answers here:
Calculate a Running Total in SQL Server
(15 answers)
Closed 4 years ago.
I need to create a different column of total sales which is the total of the sales units till that row. As our sales unit will increase, the total sales will increase row by row. I have attached the image to get the clear idea.
Screenshot
One method is use to use of subquery with correlation approach
select *,
(select sum(sales) from table where product = t.product and ? <= t.?) TotalSales
from table t
However, you would required ? (i.e. rowid or id) column that could specify your column ordering.
You can use the following query, which adds up all the sales upto that rowId and for the specific product.
select (select sum(ids)
from TBL_NAME T1
where T1.rowid <= TBL_NAME.rowid and PRODUCT=TBL_NAME.PRODUCT
) as TotalSales
from TBL_NAME;
Following answer is for Oracle DB, you can understand the query and easily convert it to the product you want.

group rows based on ID and then return the row with the lowest date per ID grouping [duplicate]

This question already has answers here:
How can I remove duplicate rows?
(43 answers)
Closed 8 years ago.
suppose you have a data set having 3 columns: ID, user, date.
is it possible to filter the data based on the minimum date even if some of the rows have identical IDs?
sorry if the question is a bit unclear. hopefully the image below will help clear things.
there are two records having ID=1, with different users as well as dates. what i want to retrieve is the record having an ID=1, USER=A, DATE=2013-01-20 because its date is earlier than that of the second record (ID=1, USER=A, DATE=2013-01-21)
i want to achieve the same effect for the three records having an ID=2. the desired record is ID=2,USER=C,DATE=2013-10-20
basically i want to group these records by their IDs and then from that grouping, get the one with the lowest date
SELECT id, user, date
FROM OriginalData od
WHERE date = (SELECT MIN(date)
FROM OriginalDate od1
WHERE od.id = od1.id)
Select * from table_name where date in
( select MIN(date) from table_name)
You have tyo use Group By clause on Id attribute.
Use the following syntax
select * from tab1 where (id, date) in (select id, min(date) from tab1 group by(id))