Make top 50 users who received the most points [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 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;

Related

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 minimum time and max time for each day in sql 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 2 years ago.
Improve this question
I have an data which includes Processeddate, start time and end time. I need to get minimum starttime and max endtime for each day.
Process date should be the first coulmn
Just use min() and max() function as shown below.
;WITH cte
AS (
SELECT Cast(processedDate AS DATE) AS processedDate
,min(startTime) minStartTime
,max(endTime) maxEndTime
FROM mytable
GROUP BY Cast(processedDate AS DATE)
)
SELECT *
FROM cte
WHERE month(processedDate) = 1
AND year(processedDate) = 2020
ORDER BY processedDate
db<>fiddle

Latest row per person per month [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 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

How to use group and sum same data based on different criteria on same table [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 4 years ago.
Improve this question
I am trying to sum data in a table.
Data in my table:
Month Osl Sale
10 8-02-01-01 38440.5
10 8-02-01-03 14961
10 8-03-02-01 10388.3
10 8-05-04-01 81666.6
10 8-05-04-05 29431.8
10 8-07-01-09 9821.4
10 8-09-01-01 7567.5
And my expected output is:
I think union all is the simplest method:
select month, osl, sale
from t
union all
select month, left(osl, 7), sum(sale)
from t
group by month, left(osl, 7);
Not all databases support left(). In those that don't, either substr() or substring() can extract the first seven characters.
Unsure of your RDMS, but I would suggest something along the lines of:
select * from table1
union all
select t.month, left(t.osl, len(t.osl)-3), sum(t.sale)
from table1 t
group by t.month, left(t.osl, len(t.osl)-3)
Change table1 to your table name as appropriate.

Get Sum & Average of each month using SQL [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 4 years ago.
Improve this question
I have below database table,In here i need group some data and get sum and avg of some data.
In here i need to calculate Item marketers monthly sales(Each Month)
As a Example :
I need to get a Patric Newton's Sales in Each months.
And also i need to calculate AvarageFactor of each month Each employee.
AvgFactor = SUM(Daily Sales)/ SUM(Value Factor) * 100( Each Month )
Database Table
I have tried it like below,
SELECT ItemMarketerName,DailySales,ValueFactor,[Month],[Year]
FROM [SR_Hotel].[dbo].[Table_1]
WHERE ItemMarketerName IS NOT NULL
Group by ItemMarketerName,DailySales,ValueFactor,[Month],[Year]
Order by ItemMarketerName
You should not include your aggregated columns in your GROUP BY.
Try:
SELECT ItemMarketerName, [Month], [Year], SUM(DailySales), SUM(DailySales) / SUM(ValueFactor) * 100
FROM [SR_Hotel].[dbo].[Table_1]
WHERE ItemMarketerName IS NOT NULL
GROUP BY ItemMarketerName, [Month], [Year]
ORDER BY ItemMarketerName