Latest row per person per month [closed] - sql

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I need to get one row per person per month and that row should be maximum date in that month.
Data available:
person date value
a jan/1/2019 10
a jan/2/2019 20
a feb/3/2019 30
b jan/10/2019 20
b jan/20/2019 30
b feb/1/2019 40
b feb/12/2019 30
Desired output:
a jan/2/2019 20
a feb/3/2019 30
b jan/20/2019 30
b feb/12/2019 30
I am not able to figure out how to achieve this. Any help is highly appreciated.

You can use an aggregation within the subquery and connect them by in operator :
select *
from tab
where (person,date) in
(
select person,max(date)
from tab
group by person,month(date),year(date)
)
Demo

Related

Make top 50 users who received the most points [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
madam,
I would like to make a top 50 of users who received the most points.
So my database table looks like this:
points table:
pid | uid_give | uid | amount | time
(every row indicates an amount given)
Now I want to make a top 50 of users who received the most amount.
I had the sql query but dont remeber to do this.
Can you help? (group by?)
Regards,
in mysql
select id, uid_give, uid, amount, time from points order by amount desc limit 50;
in sql server
select top 50 id, uid_give, uid, amount, time from points order by amount desc ;
Oracle 12 and onward
select id, uid_give, uid, amount, time from points order by amount desc
FETCH FIRST 50 ROWS ONLY;

SQL diff between rows [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have a table with following structure
ID newSatus modifiedDate
252123_001 Closed 2020-10-07 20:14:57.477
252123_001 Shipped 2020-10-07 09:24:12.693
252309_001 Closed 2020-10-08 18:51:34.810
252309_001 Shipped 2020-10-07 09:22:33.537
252404_001 Closed 2020-10-07 12:25:10.270
252404_001 Shipped 2020-10-07 09:29:02.363
Basically what i would like to do is have an access query that calculates the date difference for consecutive records between different newStatus but for the same ID.
The expected result would be !!
ID diffHours diffMinutes diffSeconds
252123_001 10h:50m:45s 650 39045
252309_001 1 day 9h:29m:01s 2009 120541
252404_001 2h:56m:08s 176 10568
And finally calculates de median of time between these results.
You want to aggregate your rows so as to get one result row per ID. "Per ID" translates to GROUP BY id in SQL.
select
id,
datediff ("h", min(modifieddate), max(modifieddate)) as diff_hours,
datediff ("n", min(modifieddate), max(modifieddate)) as diff_minuts,
datediff ("s", min(modifieddate), max(modifieddate)) as diff_seconds
from mytable
group by id
order by id;
I must admit that I don't know how to get the median from this in MS Access, as this is the RDBMS farthest from the SQL standard that I know of.
Can you try to ungroup the data so that you have seperate column for status Closed and Shipped? The you can use simple subtraction to get the difference in time, and from that you can calculate all the things you mentioned.

Get a value based on another column's value [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
It's a pretty basic sql statement I imagine, but I can't for the life of me figure out how to do.
So I have a table where I have three columns : Amount of car sold (AOCS), Day of the week (DOTW), Previous day of the week (PDOTW).
I'd like to have the amount of car sold for the previous day in a fourth column. For example, let's say I have sold 4 cars on Monday, 5 cars on Tuesday, 3 cars on Wednesday.
On the Tuesday line, I'd like to have '4' written, as it is the amount of cars sold on Monday.
I'm not sure if I make sense, let me know. Thank you for your help!
You just have to join your table on itself
SELECT A.DOTW, A.AOCS, A.PDOTW, B.AOCS
FROM
MY_TABLE A
LEFT JOIN
MY_TABLE B
ON
A.PDOTW=B.DOTW

How to display two expression result in different column in oracle sql [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have a table ACCOUNT(ACCOUNT_NUMBER,BRANCH_NAME,BALANCE)
Now I need to find in Oracle SQL : all accounts with balances over R.100000 receive 6 percent interest whereas all others receive 5 percent.
I need to display the balance with 6% and balance with 5% in two different column.
What is the SQL query for it?
Maybe this is what you want:
select
account_number,
case
when balance > 100000 then balance*1.06
end AS With6PercentInterest,
case
when balance <= 100000 then balance*1.05
end AS With5PercentInterest
from ACCOUNT;
Sample SQL Fiddle

Pull Records every 90 days in SQL Server Query [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
How can I fetch records every 90 days based on criteria in a case statement?
SELECT
LB.bayview_loan_number Loan_Number,
CASE WHEN DATEDIFF(day, OL.LOAN_ADDED_DT, PH3.SKIPTR) >= 120
AND DATEDIFF(day, OL.LOAN_ADDED_DT, GETDATE()) > 150
AND Con2.LastCon IS NULL THEN **--repeat this step every 90 days** 'LMSKIP'
END AS ST
FROM
CAPMKTS.dbo.Loan_Bayview LB
INNER JOIN
LDG.dbo.LDG_ORIGINAL_LOAN OL ON LB.bvln = OL.ln_no
The best way to run SQL process on a timed basis is by creating scheduled jobs in the SQL Server Agent.