Well, in order to get the last entry by date i've done that query :
select cle
from ( select cle, clepersonnel, datedebut, row_number()
over(partition by clepersonnel order by datedebut desc) as rn
from periodeoccupation) as T
where rn = 1
This one is working and give me the last entry by date, so far i'm ok.
But its the first time i work with subquery and i have to make my query way more complex with multiple joins. But i cannot figure out how to make a subquery in a inner join.
This is what i try :
select personnel.prenom, personnel.nom
from personnel
inner join
( select cle, clepersonnel, datedebut, row_number()
over(partition by clepersonnel order by datedebut desc) as rn
from periodeoccupation) as T
ON personnel.cle = periodeoccupation.clepersonnel
where rn = 1
but its not working !
If you have any idea or tips...Thank you !
Simply change
ON personnel.cle = periodeoccupation.clepersonnel
to
ON personnel.cle = T.clepersonnel
The query join has been aliased with T and you have reference the alias as the table in the aliased query is out of scope in your outer statement.
Related
This Query give an error
ORA-00907: Missing right parenthesis
I can't find any parenthesis problem
select
(select PRE_DESIG_ID FROM AUTHORIZATION
WHERE PROJECT_ID = 5 and PRE_DESIG_ID =48 and
ROWNUM=1 order by ID DESC) AS PERPARED_BY
,(Select PRE_LAST_DATE From (Select PRE_LAST_DATE From AUTHORIZATION
Where PROJECT_ID = 5 and PRE_DESIG_ID = 48 Order By ID Desc)
Where ROWNUM = 1) AS PRE_END_DT from AUTHORIZATION au
LEFT join PROJECT p on AU.PROJECT_ID =p.PROJECT_ID
LEFT join DESIGNATION d on au.AU_DESIG_ID=d.DESIGID;
Your problem is this:
ORDER BY id DESC
You can't use ORDER BY in a subquery that acts as a column expression. If you remove it the error you're facing will go away.
But, I would prefer you rewrite your query using a with clause, since both of your sub-query expression fetches the same row.
WITH auth AS ( SELECT *
FROM ( SELECT pre_desig_id AS perpared_by,
pre_last_date AS pre_end_dt
FROM authorization
WHERE project_id = 5
AND pre_desig_id = 48 ORDER BY id DESC )
WHERE ROWNUM = 1 )
SELECT a.perpared_by,a.pre_end_dt
FROM authorization au
LEFT JOIN project p ON au.project_id = p.project_id
LEFT JOIN designation d ON au.au_desig_id = d.desigid
CROSS JOIN auth a;
You need to convert the derived column's (prepared_by) subquery similar to pre_end_dt's subquery as below
Select
(Select pre_desig_id
From (Select pre_desig_id,
row_number() over (order by ID desc) as rn
From Authorization
Where project_id = 5
and pre_desig_id = 48
)
Where rn = 1) as prepared_by,
(Select pre_last_date
From (Select pre_last_date,
row_number() over (order by ID desc) as rn
From Authorization
Where project_id = 5
and pre_desig_id = 48
)
Where rn = 1) as pre_end_dt
From Authorization au
Left Join Project p
on au.project_id = p.project_id
Left Join Designation d
on au.au_desig_id = d.desigid;
where Order By ID Desc part produces the error, that should be wrapped within an extra subquery, otherwise compiler doesn't allow to use Order By directly as this. i.e. restriction (rownum=1) and using order by are not allowed to work at the same level, they need to exist within outer, and inner select statements respectively.
In fact, it's better to use row_number() window analytic function rather than rownum pseudocolumn, which is untrustable for most cases since it is generated before sorting.
Even if using Where rownum = 1 with order by ID desc for inner select statement is enough for you to use, rather make an habit using row_number() function.
I have the following SQL query:
select
id,
name
from
project
inner join job on project.id = job.project_id
where
job.user_id = 'me'
order by
project.modified desc
limit 10
The idea is to get information about the 10 most recently used projects for a given user.
The problem is that this can return duplicates in the case where multiple jobs have the same project. Instead of having duplicates, I want to order all rows by modified desc, remove duplicates based on id and name, then limit to 10.
I've not been able to figure out how to achieve this. Can anyone point me in the right direction?
You are getting duplicates because of the join. As you only want columns from the project table (I assume id and name are from that table), not creating the duplicates in the first place would be better than removing them after the join:
select p.id,
p.name
from project p
where exists (select *
from job job
where job.project_id = p.id
and job.user_id = 'me')
order by p.modified desc
limit 10
try this using row_number - it will work on postgresql
select * from
(select
id,
name,row_number() over(partition by id,name order by modified desc) as rn
from
project inner join job on project.id = job.project_id
where
job.user_id = 'me')a where rn=1 order by modified desc limit 10
Did you try SELECT DISTINCT?
select distinct
id,
name
from
project
inner join job on project.id = job.project_id
where
job.user_id = 'me'
order by
project.modified desc
limit 10
I ended up with the following, which seems to work fine:
select
p.id,
p.name
from
project p
inner join
(
select
j.id,
max(j.modified) as max_modified
from
job j
where
t.user_id = 'me'
group by
j.id
order by
max_modified desc
limit 10
) ids on p.id = ids.id
order by
max_modified desc
I have a table like this:
I need group this table latest date for every ID.
I mean, I want to get last row every ID. Here is my query:
SELECT DISTINCT ch.Date,ID FROM dbo.tblrisk AS rk
inner join (Select TableIdentity, [Date] from tblCommonHistory ) ch
ON ch.TableIDentity = rk.ID order by ID
How can I do what I want?
EDIT: This query worked for me:
SELECT DISTINCT ch.dt,ID FROM dbo.tblrisk AS rk
inner join (Select TableIdentity, max([Date]) as dt from tblCommonHistory group by TableIdentity) ch ON ch.TableIDentity = rk.ID order by ID
Just use aggregation:
select TableIdentity, max([date])
from tblCommonHistory
group by TableIdentity;
Your question only mentions one table. Your query has two; I don't understand the discrepancy.
It's strange that you have duplicated TableIdentity in tblCommonHistory, but otherwise you should not be getting multiple dates for the same ID from your query.
And also, the only reason to join the 2 tables seems to be that you need to skip those ID that are not present in the tblrisk (is it what you need to do?)
In that case, I'd suggest
SELECT max(ch.Date) AS [Date],ID FROM dbo.tblrisk AS rk
inner join tblCommonHistory AS ch ON ch.TableIDentity = rk.ID
group by ID order by ID
The following query selects a students names and their highest score for a particular row. The problem is that the subquery in the left join can't be used.
I get the Teradata data error: "Object Date_View doesn't exist'". How can this be fixed?
Select name, max_score_today.max_score From Student_View
Left Join Date_view
ON Date_View.date=Student_View.date
Left Join (
Select MAX(score) as max_score FROM
Score_View
Where Date_View.start_date=Score_View.date
) max_score_today
ON max_score_today.name=Student_View.name
Move your correlated subquery up into the select statement, such as:
select name,
(select max(score) from score_view where date_view.start_date) = score_view.date) as max_score
from student_view
left join
date_view
on date_view.date = student_view.date;
To get the row with the highest/lowest value you better use RANK or ROW_NUMBER like this:
Select *
From Student_View
Left Join Date_view
on Date_View.date=Student_View.date
Left Join Score_View
on Date_View.start_date=Score_View.date
and Score_View.name=Student_View.name
QUALIFY -- get the highest score for each student/date
ROW_NUMBER() -- maybe RANK
OVER (PARTITION BY Student_View.name, Score_View.date
ORDER BY Score_View.score DESC) = 1
I'm not sure if this the correct result, you might have to change the PARTITION expression...
I am trying to make a view with the following code...
SELECT DISTINCT
TOP (100) PERCENT .dbo.InventoryItems.ItemNo, .dbo.InventoryItems.ItemID, .dbo.InventoryItems.DescriptionMed,
.dbo.PODetails.MostRecentCost, .dbo.UOMs.UOMCode, .dbo.PODetails.BuyUOMID, .dbo.UOMConvert.Factor,
.dbo.PODetails.MostRecentCost * .dbo.UOMConvert.Factor AS [Price Test], .dbo.PO.EntryDate,
.dbo.UOMConvert.UOMID
FROM .dbo.PO INNER JOIN
.dbo.PODetails ON .dbo.PO.POID = .dbo.PODetails.POID INNER JOIN
.dbo.UOMConvert INNER JOIN
.dbo.UOMs ON .dbo.UOMConvert.ToUOMID = .dbo.UOMs.UOMID INNER JOIN
.dbo.InventoryItems ON .dbo.UOMConvert.ItemID = .dbo.InventoryItems.ItemID ON
.dbo.PODetails.ItemNo = .dbo.InventoryItems.ItemNo
ORDER BY .dbo.PO.EntryDate DESC
that gives me all records sorted by the Entrydate but I am only looking to keep the newest record for each ItemNo. I have tried adding group by statements, row numbers and a MAX() statement but I must be entering all of them in wrong as they are all giving me errors due to my limited knowledge of SQL. :( Any help would be greatly appreciated.
Thanks!
use window function.
select * from (
select row_number()over(partition by itemno order by Entrydate desc) Rn,
....
from table1 join ...)
A where rn=1