Select a row based on where have maximum by one column - sql

I have a table called match_score which have following data
id
participant
round
score
1
gabe
1
100
2
john
1
90
3
duff
1
80
4
vlad
1
85
5
gabe
2
75
6
john
2
70
Let's just say that round 1 is the preliminary round and 2 is the final round
I want to rank the result based on the score and grouped by the participant , if I'm using some normal sql group by participant and order by score desc.
vlad are the 1st, duff 2nd and gabe 3rd, which one is wrong.
what i want is
1st gabe with 75 point in the final round
2nd john with 70 point in the final round
3rd vlad with 85 point in the preliminary round
4th duff with 80 point in the preliminary round

maybe something like this:
select
h.participant,
h.round,
h.score
from
match_score h
where
not exists (
select 1
from
match_score t
where
t.participant = h.participantand t.round > h.round
)
order by
h.round desc,
h.score desc;

Related

label rows in top 20% sql

Assuming this sample data in a sql table:
people income
Mary 100
Joe 60
Sam 70
Bob 85
Sarah 85
I am trying to create a column which will identify each row as within the top 20% of income. So, the expected output is:
people income performers
Mary 100 Y
Joe 60 N
Sam 70 N
Bob 85 Y
Sarah 85 Y
I have tried TOP (20) PERCENT and a CASE statement, no luck.
Just tried even extracting the top 20% and kept getting errors as well.
In Pandas, I got this, no problem. In SQL, it's like underwater brain surgery using oven mitts.
If you're looking to compare each record to the max value of income, you can use a CASE expression to compare the current record:
SELECT people, income,
CASE
WHEN income >= (SELECT MAX(income) FROM Foo) * 0.8
THEN 'Y' ELSE 'N'
END AS performers
FROM Foo;
people income performer
Mary 100 Y
Joe 60 N
Sam 70 N
Bob 85 Y
Sarah 85 Y
You can use a case statement
select
people,
income,
case when income >= (select max(income) from Table) * .8 then 'Yes' else 'No' end as performer
from Table

Function to get rolling average with lowest 2 values eliminated?

This is my sample data with the current_Rating column my desired output.
Date Name Subject Importance Location Time Rating Current_rating
12/08/2020 David Work 1 London - - 4
1/08/2020 David Work 3 London 23.50 4 3.66
2/10/2019 David Emails 3 New York 18.20 3 4.33
2/08/2019 David Emails 3 Paris 18.58 4 4
11/07/2019 David Work 1 London - 3 4
1/06/2019 David Work 3 London 23.50 4 4
2/04/2019 David Emails 3 New York 18.20 3 5
2/03/2019 David Emails 3 Paris 18.58 5 -
12/08/2020 George Updates 2 New York - - 2
1/08/2019 George New Appointments5 London 55.10 2 -
I need to use a function to get values in the current_Rating column.The current_Rating gets the previous 5 results from the rating column for each name, then eliminates the lowest 2 results, then gets the average for the remaining 3. Also some names may not have 5 results, so I will just need to get the average of the results if 3 or below, if 4 results I will need to eliminate the lowest value and average the remaining 3. Also to get the right 5 previous results it will need to be sorted by date. Is this possible? Thanks for your time in advance.
What a pain! I think the simplest method might be to use arrays and then unnest() and aggregate:
select t.*, r.current_rating
from (select t.*,
array_agg(rating) over (partition by name order by date rows between 4 preceding and current row) as rating_5
from t
) t cross join lateral
(select avg(r) as current_rating
from (select u.*
from unnest(t.rating_5) with ordinality u(r, n)
where r is not null
order by r desc desc
limit 3
) r
) r

How do i join the last record from one table where the date is older than other table?

This is my first post here, and the first problem i havent been able to find a solution to on my own. I have a MainTable that contains the fields: Date, MinutesActiveWork (And other not relevant fields). I have a second table that contains the fields: ID, id_Workarea, GoalOfActiveMinutes, GoalActiveFrom.
I want to make a query that returns all records from MainTable, and the active goal for the date.
Exampel:
Maintable (Date = dd/mm/yyyy)
ID Date ActvWrkMin WrkAreaID
1 01-01-2019 45 1
2 02-01-2019 50 1
3 03-01-2019 48 1
GoalTable:
ID id_Workarea Goal GlActvFrm
1 1 45 01-01-2019
2 2 90 01-01-2019
3 1 50 03-01-2019
What i want from my query:
IDMain Date ActvWrkMin Goal WrkAreaID
1 01-01-2019 45 45 1
2 02-01-2019 50 45 1
3 03-01-2019 48 50 1
The query that i have now is really close to what i want. But the problem is that the query outputs all goals that is less than the date from MainTable (It makes sense why, but i dont know what criteria to type to fix it). Like so:
IDMain Date ActvWrkMin Goal WrkAreaID
1 01-01-2019 45 45 1
2 02-01-2019 50 45 1
3 03-01-2019 48 45 1 <-- Dont want this one
3 03-01-2019 48 50 1
My query
SELECT tblMain.Date, tblMain.ActiveWorkMins, tblGoal.Goal
FROM VtblSumpMain AS tblMain LEFT JOIN (
SELECT VtblGoalsForWorkareas.idWorkArea, VtblGoalsForWorkareas.Goal, VtblGoalsForWorkareas.GoalActiveFrom (THIS IS THE DATE FIELD)
FROM VtblGoalsForWorkareas
WHERE VtblGoalsForWorkareas.idWorkArea= 1) AS tblGoal ON tblMain.Date > tblGoal.GoalActiveFrom
ORDER BY tblMain.Date
(I know i could do this pretty simple with Dlookup, but that is just not fast enough)
Thanks for any advice!
For this, I think you have to use the nested query as I mention below.
select tblMain.id,tblMain.Date,tblMain.ActvWrkMin, tblMain.WrkAreaID,
(select top 1 Goal
from GoalTable as gtbl
where gtbl.id_workarea = 1
and tblmain.[Date] >= gtbl.glActvFrm order by gtbl.glActvFrm desc) as Goal
from Maintable as tblMain
Check the below image for the result which is generated from this query.
I hope this will solve your issue.

SQL Query for a table

I’m looking for a little assistance. I have a table called equipment. One row is an order of some type of equipment.
Here are the fields:
num_id date player_id order_id active jersey comment
BIGINT DATE BIGINT BIGINT CHAR(1) CHAR(3) VARCHAR(1024)
11 2018-01-01 123 1 Y XL
11 2018-01-01 123 2 Y M Purple
11 2018-01-01 123 3 Y L White, Red
13 2018-01-11 456 1 N S Yellow, Light Blue
14 2018-02-01 789 1 Y M Orange, Black
15 2018-02-02 101 1 Y XL Shield
15 2018-02-02 101 2 Y XL Light Green, Grey
I need to write a query that shows one row for each month with the columns
Month
Total Orders
Total Products ordered
And one extra column for a total count of each size sold.
Is this easy? Any help would be appreciated.
EDIT: To answer people's questions below, SQL Server is the dbms. My apologies. As well, I am struggling as I don't know how to get the month from a date. And then adding the column for size counts has me baffled, but I haven't fully investigated that portion. I feel like the rest I have done individually, just never did it in one succinct query.
It looks weird here and I don't know how to add a table to stackoverflow, so I'll try to make it a little more visually appealing here:
The end goal I think would be like this:
Month Total Orders Total Products Ordered Size Count
January 1 3 S-0, M-1, L-1, XL-2
February 3 6 S–1, M–2, L–1, XL–3
Or this:
Month Total Orders Total Products Ordered S Count M Count L Count XL Count
January 1 3 0 1 1 2
February 3 6 1 2 1 3
You need PIVOT.
It basicly turns rows into columns, which exactly is your case.
https://www.codeproject.com/Tips/500811/Simple-Way-To-Use-Pivot-In-SQL-Query

Access SQL - Select only the last sequence

I have a table with an ID and multiple informative columns. Sometimes however, I can have multiple data for an ID, so I added a column called "Sequence". Here is a shortened example:
ID Sequence Name Tel Date Amount
124 1 Bob 873-4356 2001-02-03 10
124 2 Bob 873-4356 2002-03-12 7
124 3 Bob 873-4351 2006-07-08 24
125 1 John 983-4568 2007-02-01 3
125 2 John 983-4568 2008-02-08 13
126 1 Eric 345-9845 2010-01-01 18
So, I would like to obtain only these lines:
124 3 Bob 873-4351 2006-07-08 24
125 2 John 983-4568 2008-02-08 13
126 1 Eric 345-9845 2010-01-01 18
Anyone could give me a hand on how I could build a SQL query to do this ?
Thanks !
You can calculate the maximum sequence using group by. Then you can use join to get only the maximum in the original data.
Assuming your table is called t:
select t.*
from t join
(select id, MAX(sequence) as maxs
from t
group by id
) tmax
on t.id = tmax.id and
t.sequence = tmax.maxs