This question already has answers here:
SQL update query using joins
(13 answers)
Closed 2 years ago.
I calculated a sum for OREB that is unique to every pair of GameId and TeamId
using this query,
Query Image.
select DGAM.GameID, HomeTeamID, SUM(OREB) as OREB_Home
from dimGames as DGAM
join dimPerformance as DPERF on DGAM.GameID = DPERF.GAME_ID and DGAM.HomeTeamID = DPERF.TEAM_ID
group by DGAM.GameID, HomeTeamID
Using the results of this query I want to update the null column in this table
Table called dimGames.
How can I write an update statement to accomplish this?
Adapting this answer, I think you want:
with agg as (
select DGAM.GameID, HomeTeamID, SUM(OREB) as OREB_Home
from dimGames as DGAM
join dimPerformance as DPERF on DGAM.GameID = DPERF.GAME_ID and DGAM.HomeTeamID = DPERF.TEAM_ID
group by DGAM.GameID, HomeTeamID
)
UPDATE dimGames dg
SET dg.OREB_home = agg.OREB_Home
FROM agg
WHERE dg.GameId = agg.GameId and dg.HomeTeamId = agg.HomeTeamId
Related
This question already has answers here:
SQL Server dynamic PIVOT query?
(9 answers)
Closed 1 year ago.
I have this query:
SELECT Students.StudentNumber,
Students.StudentSurname,
Students.StudentFirstNames,
Students.SchoolYear,
Students.Class,
Cycle,
Section,
MarksEntry.SubjectCode,
MarksEntry.AssessmentPeriod,
MarksEntry.SubjectMaxima,
MarksObtained,
Subject.SubjectName
FROM Students,
MarksEntry,
Subject
WHERE Students.StudentNumber = MarksEntry.StudentNumber
AND MarksEntry.SubjectCode = Subject.SubjectCode
AND Students.Class = MarksEntry.Class
AND MarksEntry.SchoolYear = '2020-2021'
AND MarksEntry.Class = '1ere LIT'
AND MarksEntry.AssessmentPeriod = '2è P'
ORDER BY Students.StudentSurname;
I get the results this way:
How can I modify this query so that I can get the data listing only the students with no duplication and the score marks under each subject, with the name of the subject as column name.
The subjects names are not the same, they differ from classes, so they have to be read dynamically from the subjectName column based on the query condition.
Something like this:
You can use LEFT JOIN with the table MarksEntry multiple times, where each MarksEntry corresponds to a different subject.
For example,
SELECT s.StudentNumber,
rel.MarksObtained ReligionMarks,
bio.MarksObtained BiologyMarks
FROM Students s
LEFT JOIN MarksEntry rel
ON s.StudentNumber = rel.StudentNumber
AND rel.SubjectName = 'Religion'
LEFT JOIN MarksEntry bio
ON s.StudentNumber = bio.StudentNumber
AND bio.SubjectName = 'Biology'
This will only work if there aren't any duplicated students in the same course, otherwise, each register will appear multiple times.
I'd also recommend you take a look at the GROUP BY clause in case there are duplicates
This question already has answers here:
Select first row in each GROUP BY group?
(20 answers)
Firebird Query- Return first row each group
(4 answers)
Closed 5 years ago.
I have a table in a Firebird 2.5 Database where I have to find out the latest entry for one specific articel no. Therefore the table has a colum "date".
I tried several queries but I always get more than 1 entry for the article no.
I used the following query:
select max(lp.datum) as Wareneingangsdatum, lp.nr, lp.artikelnr, lp.belegnr, lf.nr, lf.name, lf.plz, lf.ort, lf.laenderkuerzelnr, lae.bezeichnung
from lagerprotokoll lp
left join waeinpos we on we.idnr = lp.belegposidnr
left join waein we1 on we1.nr = we.nr
left join liefrant lf on lf.nr = we1.lieferantnr
left join laender lae on lae.nr = lf.laenderkuerzelnr
where lp.belegtyp = 14
group by 2,3,4,5,6,7,8,9, 10
order by 1 desc
How can I achieve the result so that I can only get the latest entry for one specific article in the table.
This question already has answers here:
Create a SQL query to retrieve most recent records
(5 answers)
Closed 7 years ago.
I have a ticketing system (itsm) and I need to return the newest record from a table among many other records of the same ticket.
On the picture above, you can have a view of the output of select (I will paste it below).
Realize that PBTI_WORKORDER_ID column shows the ticket ID and what I am trying to do is just show the greatest request_id (the first row on image).
The select is:
SELECT
MAX(REQUEST_ID),
(PBTI_WORKORDER_ID),
PBTI_IDREQUISICAO,
TO_CHAR(SECS_TO_DATE(PBTI_DTABERTURA),'DD/MM/YYYY HH24:MI:SS') AS DATA_CRIACAO,
PBTI_GRUPOSUPORTEATUAL AS GRUPO_ATUAL,
TO_CHAR(SECS_TO_DATE(PBTI_DATAENTRADAGRUPO),'DD/MM/YYYY') AS DATA_ENTRADA,
TO_CHAR(SECS_TO_DATE(PBTI_DATASAIDAGRUPO),'DD/MM/YYYY') AS DATA_SAIDA_GRUPO,
PBTI_PROXIMOGRUPOSUPORTE AS PROXIMO_GRUPO,
REQUEST_ASSIGNEE,
CATEGORIZATION_TIER_1,
CATEGORIZATION_TIER_2,
CATEGORIZATION_TIER_3,
CUSTOMER_ORGANIZATION,
PBTI_MCU_ORG
FROM PBTI_TABELA_INDICADORES
WHERE PBTI_GRUPOSUPORTEATUAL = 'CENTRAL HD - TRATAMENTO'
GROUP BY (REQUEST_ID),
(PBTI_WORKORDER_ID),
(PBTI_IDREQUISICAO),
(PBTI_DTABERTURA),
(PBTI_GRUPOSUPORTEATUAL),
(PBTI_DATAENTRADAGRUPO),
(PBTI_DATASAIDAGRUPO),
(PBTI_PROXIMOGRUPOSUPORTE),
(REQUEST_ASSIGNEE),
(CATEGORIZATION_TIER_1),
(CATEGORIZATION_TIER_2),
(CATEGORIZATION_TIER_3),
(CUSTOMER_ORGANIZATION),
(PBTI_MCU_ORG)
ORDER BY PBTI_DATAENTRADAGRUPO DESC;
The MAX (used on select clause) should only bring the first row, does not?
tks
One method to accomplish this is to generate a set of data consisting of the max request_ID for each ticket (B). Then, join that set back to the base set (A) with all records, thereby using the join to only retain the relevant "newest" record per ticket / request.
Something like this..
SELECT A.*
FROM PBTI_TABELA_INDICADORES A
INNER JOIN (SELECT max(request_ID) Request_Id, PBTI_WorkORder_Id, PBTI_IDREQUISICAO
FROM PBTI_TABELA_INDICADORES
GROUP BY PBTI_WorkORder_Id, PBTI_IDREQUISICAO) B
on A.Request_Id = B.Request_Id
and A.PBTI_WorkORder_Id = B.PBTI_WorkORder_Id
and A.PBTI_IDREQUISICAO = B.PBTI_IDREQUISICAO
This question already has answers here:
Fetch the rows which have the Max value for a column for each distinct value of another column
(35 answers)
Closed 8 years ago.
I have this query and i need to get the maxvalue of the CCSEQ camp, i have tried using this query but it doesn´t work, if anyone can help me i will be very grateful.
the query
select max(cc.CCSEQ), cc.ccline4, cc.ccstreetno, cccity
from ccontact cc
where customer_id = '724609' and ccbill = 'X';
EDIT
I have resolved the issue with this query
select cc.ccline4, cc.ccstreetno, cccity
from ccontact cc
where CC.customer_id = '724609' and CC.ccbill = 'X'
AND cc.CCSEQ = (SELECT MAX(C1.CCSEQ) FROM ccontact c1
WHERE CC.customer_id = C1.customer_id)
Best wishes
Don't mix plain columns with aggregates if you don't group by those plain columns:
select max(cc.CCSEQ)
from ccontact cc
where customer_id = '724609' and ccbill = 'X';
It's difficult to determine the absolute right query without knowing the schema, but my guess is that you want the Max CCSEQ and the respective ccline4, ccstreetno, and cccity for your selection. There are a few ways to do this. With a subquery it would look like:
SELECT
cc.ccseq, cc.ccline4, cc.ccstreetno, cccity
FROM
(SELECT max(cc.CCSEQ) AS maxccseq FROM ccontact WHERE customer_id = '724609' and ccbill = 'X') as ccmax
INNER JOIN ccontact cc ON
ccmax.maxccseq = cc.CCSEQ
This question already has answers here:
ORA-00979 not a group by expression
(10 answers)
Closed 5 years ago.
how to use group by for 3 columns it is showing an error that it is not a group by expression.
here is the query.
select
brewer.name as brewer,beer.name as beer,count(ratings.score) as LowestRating
from ratings,beer,brewer
where ratings.beer=beer.id and beer.brewer=brewer.id
group by brewer,beer;
You should GROUP BY by columns, not whole tables:
select
brewer.name as brewer,
beer.name as beer,
count(ratings.score) as LowestRating
from
ratings,
beer,
brewer
where
ratings.beer = beer.id
and beer.brewer = brewer.id
group by brewer.name, beer.name;