Select max(date) in Firebird [duplicate] - sql

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.

Related

SQL - How do I return the most recent occurence in a column along with other aggregated columns? [duplicate]

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 7 months ago.
(BigQuery SQL)
I have three tables of Formula 1 data - results, races, drivers. I am trying to compile a summary that includes the driver, a count of their top 5 finishes, a count of their podium finishes, their total wins, most recent race, and most recent win. I am struggling with the most recent win.
The three tables have the following columns:
results:
races:
drivers:
I can get every piece of information I need except for the most recent win with the following query:
SELECT
drivers.driverId,
driverRef,
SUM(IF(results.position <=5,1,0)) AS top_five_finishes,
SUM(IF(results.position <=3,1,0)) AS podium_finishes,
SUM(IF(results.position = 1,1,0)) AS number_of_wins,
MAX(races.date) AS most_recent_race,
FROM `formula1.all_results` AS results
FULL JOIN `formula1.all_drivers` AS drivers ON results.driverId = drivers.driverId
FULL JOIN `formula1.all_races` AS races ON results.raceId = races.raceId
GROUP BY driverRef, driverId
ORDER BY number_of_wins DESC
Which returns:
I am struggling to include the most recent win. I have tried adding the following statement to the above query:
(select max(races.date)
from
`formula1-356612.formula1_project.all_results` AS results
full join `formula1-356612.formula1_project.all_drivers` AS drivers ON results.driverId = drivers.driverId
full join `formula1-356612.formula1_project.all_races` AS races ON results.raceId = races.raceId
where results.position = 1
) as most_recent_win
But that simply returns the most recent race in the entire table. I have tried creating a subquery to find the most recent win but it returns every win, not the most recent:
select
results.driverId as driver_id,
races.date as date_of_win,
races.name as track_name,
results.position as place
from `formula1.all_results` as results
join `formula1.all_races` as races on results.raceId = races.raceId
where results.position = 1
group by driver_id, date_of_win, track_name, place
order by date_of_win desc
And additionally I am unsure how I would join that information to the existing query.
In order to get one most recent win:
SELECT
.....
</ Your query with necessary fields and conditions>
....
ORDER BY date_of_win DESC
LIMIT 1
I've discovered the answer. It's a simple statement:
max(if(results.position = 1, races.date, null)) as most_recent_win

How can I update this column with an aggregate function? [duplicate]

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

how to outer-join Tables using ANSI sql [duplicate]

This question already has answers here:
Oracle outer join syntax
(1 answer)
How do I convert a "legacy" left outer join statement in Oracle?
(1 answer)
Left Outer Join using + sign in Oracle 11g
(3 answers)
Oracle "(+)" Operator
(4 answers)
Closed 5 years ago.
I am working on 2 Tables. One contains 657 rows matching my filter criteria, and the other contains 193 records.
I successfully joined them using Oracle's "AFAIK" syntax:
select
ecp.portfolio_acct, ecp.posn_id cost_posn_id, ecp.asset_id
from MID_COST_POSITION ecp, MID_CASH_POSITION cas
where ecp.portfolio_acct = 10183306
and ecp.portfolio_acct = cas.portfolio_acct(+)
and ecp.asset_id = cas.asset_id(+)
;
However, I cannot seem to replicate these results using ANSI-SQL (or, ISO-sql):
select
ecp.portfolio_acct, ecp.posn_id cost_posn_id, ecp.asset_id
from MID_COST_POSITION ecp
LEFT OUTER JOIN MID_CASH_POSITION cas
on ecp.portfolio_acct = cas.portfolio_acct
where ecp.asset_id = cas.asset_id
and ecp.portfolio_acct = 10183306
;
I have tried various JOINs without success. In the 1st example I had to Outer Join 2 separate fields, which I do not know how to accomplish using ANSI-SQL.
I appreciate any guidance anyone can offer. Thank you!
select
ecp.portfolio_acct, ecp.posn_id cost_posn_id, ecp.asset_id
from MID_COST_POSITION ecp
LEFT OUTER JOIN MID_CASH_POSITION cas
on ecp.portfolio_acct = cas.portfolio_acct
and ecp.asset_id = cas.asset_id
Where ecp.portfolio_acct = 10183306
;
You were turning this into an inner join with the following where condition because it now has to meet that condition in the result set:
ecp.asset_id = cas.asset_id

select in an oracle to return the newest row [duplicate]

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

How do I write the equivalent of a SQL LEFT JOIN in LINQ? [duplicate]

This question already has answers here:
LINQ to SQL Left Outer Join
(6 answers)
left outer join problem
(1 answer)
Closed 9 years ago.
I need to convert this SQL to LINQ, and would really appreciate some help.
SELECT * FROM Adx_eventSet AS es
LEFT JOIN afx_eventsponsor_eventSet AS spon
ON es.Adx_eventId = spon.adx_eventid
Ive tried this but it's not a left join and so only pulled in the one result.
from t in Adx_eventSet
join x in adx_eventsponsor_eventSet on t.Adx_eventId equals x.adx_eventid
select t
You want to use DefaultIfEmpty(). See below.
var leftJoin = from adx_event in Adx_eventSet
join adx_eventsponsor in adx_eventsponsor_eventSet
on adx_event.Adx_eventId equals adx_eventsponsor.adx_eventid into j
from adx_eventsponsor in j.DefaultIfEmpty()
select new
{
Name = adx_event.Name,
Name = adx_eventsponsor != null ? adx_eventsponsor.Name : null
};
Assuming each table has a Name property.