Select row with max value with condition - sql

I have a table that contains results of some sports competition. Here it is:
And I need to get a table with winner teams. It means, from rows with same MatchIds select entries where Score is maxium for these MatchIds.
Result should look like this:
I have no idea of correct SQL query.
I'm using MSSQL Server 2018. Thank you.

One method is a correlated subquery:
select t.*
from t
where t.score = (select max(t2.score) from t t2 where t2.matchid = t.matchid);

Related

How can I make selection based on conditions on SQL?

There is a table based on ID an those ID's status keys:
The table
I need to write query that will bring higher status key of the same ID. For example; query will bring only the row with status key number 9 for ID number 123. But it will bring the row with status key number 2 for ID number 156.
Hope I managed to explain myself clearly. Please help me with this query.
Use max() aggregation
select id, max(status_key)
from tablename
group by id
You didn't tag your backend, this would work with many backends and older versions of many backends (assuming you have other columns too in your table - otherwise do only group by):
select myTable.*
from myTable
inner join
(select id, max(statusKey) as statusKey
from myTable
group by id) tmp on myTable.id = tmp.id and myTable.statusKey = tmp.statusKey;

how to sum result of count in sql query from one table and one column

I need to sum the result of count of a column in one query.
Is it possible to have like this query?
SELECT sum(count(pro_id)) from jalasat group by pro_id
You have not mentioned which SQL database you are using so you may modify this slightly to fit it to what you are using:
SELECT SUM(cnt) FROM (SELECT COUNT(pro_id) as cnt
FROM jalasat
GROUP BY continent) as t1

SQL Query for ID grouped AND WHERE otherfield has certain value

I have a table in Access that is set up where there are multiple records with the same ID, they correspond to each other.
I'd like to find certain records that have a specific date value. However, I want all the corresponding information WITH that ID (i.e. all the other records with the same ID). I've tried things like this:
SELECT *
FROM myTable
WHERE LEFT(Field1,7) = '2016-11' IN (SELECT ID
FROM myTable
GROUP BY ID
HAVING COUNT(*)>1)
and
SELECT *
FROM myTable
WHERE ID = (SELECT * FROM myTable WHERE LEFT(Field1,7) = '2016-11'
Neither of these are giving me the proper output. I think I may need a For loop of some sort but don't have much experience doing this with SQL. That way I can loop through all IDs that are returned with that date-part. Any suggestions? I would put the table format in the post but the table formatting isn't working for me for some reason. The frustration is real!
Haha thanks ahead of time for taking the time to even read my question. Much appreciated.
EDIT
Here is a visual of what my table is like:
ExampleTable
I'd like to choose all the records that occur during November, but also get the corresponding information (i.e. records with same ID number as the November records).
Consider adding WHERE condition in subquery:
SELECT *
FROM myTable
WHERE ID IN (SELECT ID FROM myTable
WHERE LEFT(Field1, 7) = '2016-11');
Alternatively to avoid subquery, try an INNER JOIN on a filtered self join by ID:
SELECT myTable.*
FROM myTable
INNER JOIN
(SELECT ID FROM myTable
WHERE LEFT(Field1, 7) = '2016-11') sub
ON sub.ID = myTable.ID

Adding count in select query

I am trying to find a query that would give me a count of another table in the query. The problem is that I have no idea what to set where in the count part to. As it is now it will just give back a count of all the values in that table.
Select
ID as Num,
(select Count(*) from TASK where ID=ID(Also tried Num)) as Total
from ORDER
The goal is to have a result that reads like
Num Total
_________________
1 13
2 5
3 22
You need table aliases. So I think you want:
Select ID as Num,
(select Count(*) from TASK t where t.ID = o.ID) as Total
from ORDER o;
By the way, ORDER is a terrible name for a table because it is a reserved work in SQL.
You can do it as a sub query or a join (or an OVER statement.)
I think the join is clearest when you are first learning SQL
Select
ID as Num, count(TASK.ID) AS Total
from ORDER
left join TASK ON ORDER.ID=TASK.ID
GROUP BY ORDER.ID

How can I select an entire row without selecting rows where a certain column value is duplicated?

How can I select an entire row without selecting rows where a certain column value is duplicated?
EDIT: Sorry my question is a bit vague. Assuming i have a table with two columns: names and score. There are duplicate values in names. I want to select distinct names and also select their scores.
Based on the edited information given, this will use the GROUP_CONCAT function to produce the distinct names and a comma-delimited list of scores. If more appropriate, you could substitute another aggregate function (e.g., MIN, MAX, SUM) for the GROUP_CONCAT.
SELECT Name, GROUP_CONCAT(Score)
FROM YourTable
GROUP BY Name
Using the following example data...
name score
----- -----
James 10
James 12
Lisa 45
John 42
...the following queries should return the third and fourth row.
select name, score
from table
where name in(select name
from table
group by name having count(*) = 1);
...less clear, but probable more efficient on MySQL.
select t1.name, t1.score
from (select name
from table
group by name having count(*) = 1
) t1
join table t2 on(t1.name = t2.name)
Try using the DISTINCT clause on the columns you don't want duplicates of.
based on your latest edit, try this:
select
name, SUM(score) AS TotalScore
from YourTable
group by name