Suppose I've next data
id cid vid firstname lastname dtype dname createdat
1 32 34 ramesh kjk t check 2021-11-02 10:00:51.66395
1 32 34 ramesh kjk t check 2021-11-01 10:00:51.66395
1 32 4 raj kjk c check 2021-11-04 10:00:51.66395
excepted o/p:
id cid vid firstname lastname dtype dname createdat
1 32 34 ramesh kjk t check 2021-11-02 10:00:51.66395
1 32 4 raj kjk c check 2021-11-04 10:00:51.66395
need to fetch only the only latest record of each user(vid) and document type can be c or t
use corelated subquery
select t1.* from table_nam t1
where t1.createdat=(select max(createdat) from table_nam t2 where t1.vid=t2.vid)
also you can use distinct on
select distinct on (vid) as vid,createdat,cid
from table_name
order by vid,createdat desc
Related
I have below table
userid comp_dd coursecode qualification course_id course passyear totalmarks func stream
----------- ----------- ----------- --------------- ----------- -------------------------------------------------- ----------- -----------------
60 26 1 High School 15 Class 10 26 67 2 All Subject
60 26 2 Senior Secondry 15 Class 12 26 85 2 Commerce
60 2010 3 Graduates 4 B.Tech/B.E 2010 54 1 IT/Computers
60 2013 4 Post Graduates 9 M.com 2013 98.5 2 Commerce
i wanted to get the unique record of max coursecode, the output should be
userid comp_dd coursecode qualification course_id course passyear totalmarks func stream
----------- ----------- ----------- --------------- ----------- -------------------------------------------------- ----------- -----------------
60 2013 4 Post Graduates 9 M.com 2013 98.5 2 Commerce
There will be many records of the different userids
I think you want:
select t.*
from t
where t.coursecode = (select max(t2.coursecode)
from t t2
where t2.userid = t.userid
);
You can also do this with window functions, but the correlated subquery is often faster with the right index, which would be on (userid, coursecode).
Sort the table by coursecode descending and get the 1st row:
select top 1 *
from tablename
order by coursecode desc
This will work only if there are no duplicates in the column coursecode since it will fetch only 1 row, but I guess this is the case because you say:
i wanted to get the unique record of max coursecode
I have a similar problem like this thread but is bit different what it required
https://community.oracle.com/thread/4132183
I have the following table:
Table1:
ID empID employeeactive dtefrom dateto mgrid
1 123 1 1/10/2016 113
2 213 0 1/20/2015 1/20/2016 323
3 213 1 1/20/2016 423
4 312 0 1/05/2016 1/30/2017 523
5 512 1 1/30/2017 623
6 812 1 2/30/2017 6543
Table2:
empID emplyactive supid
123 1 -
213 1 -
312 1 -
512 0 -
612 1 -
712 1 -
812 1 872
912 0 222
I have this table instead of - i want to replace with mgrid in table 1.. and table2 have extra data which is not in table1 so i have to ignore the extra data if supid '-' and also want to have emplyactive =1 but some of the emplyactive=1 table 1 has multiple mgr id ...
so I tried this one
select empid , decode(supid,'-',mgrid,supid) from table2,table1 where
empid(+) = empid and emplyactive =1 and employeeactive=1
so I am getthing how to solve this please help me out thank you .. if some thing like and exists will work thanks in advance.
This is what I am trying to insert in a package body oracle.
This is how the output looks like:
empID emplyactive supid
123 1 113
213 1 423
812 1 872
select a.empid, a.emplyactive, max(a.supid) supid
from (
select * from #table2
union
select empid, employeeactive, mgrid from #table1)a
left join #table1 b on a.empid=b.empid and employeeactive=1
join #table2 c on a.empid=c.empid and c.emplyactive=1
where a.emplyactive=1
and a.supid<>0
group by a.empid, a.emplyactive
I would like to know what would be the best way to get the data from a specific row when I use a Group By query. The real query is more complex than the example I'm providing here so I'm looking for something other than a sub-select on the Sales table.
I'm using MSSQL 2008 and I would like something that allow me to get the date field from the Sales record that has the max(amount).
Query
select uid, firstName, lastName, AmountFromTagetedRow, DateFromTargetedRow,
from users u inner join
sales s on u.uid = s.custID
group by uid, firstName, lastName
order by uid
USERS
uid firstName lastName
1 Sam Smith
2 Joe Brown
3 Kim Young
SALES
sid Custid date amount ...
1 1 2016-01-02 100
2 3 2016-01-12 485
3 1 2016-01-22 152
4 2 2016-02-01 156
5 1 2016-02-02 12
6 1 2016-03-05 84
7 2 2016-03-10 68
RESULTS
uid firstName LastName amount date
1 Sam Smith 152 2016-01-22
2 Joe Brown 156 2016-02-01
3 Kim Young 485 2016-01-12
Your posted query doesn't match your amount but something like this should get you pointed in the right direction.
with SortedResults as
(
select uid
, firstName
, lastName
, AmountFromTagetedRow
, DateFromTargetedRow
, ROW_NUMBER() over (partition by u.uid order by AmountFromTagetedRow desc) as RowNum
from users u inner join
sales s on u.uid = s.custID
group by uid
, firstName
, lastName
)
select *
from SortedResults
where RowNum = 1
order by uid
Given is a table with articles. The following exemplary table contains one article in different variations:
ID ARTICLE_NUMBER STORE_ID COUNTRY TYPE VALID_FROM
----------------------------------------------------------------
100 1 22 DE A 2015-11-01
101 1 22 DE A 2015-11-02
102 1 22 DE A 2015-11-03
103 1 22 DE A 2015-11-04
104 1 22 DE B 2015-11-10
105 1 22 DE B 2015-11-11
106 1 22 DE B 2015-11-11
What I need is a query which returns just the ID of the article with
article_number = 1 AND
store_id = 22 AND
country = 'DE' AND
the latest valid_from timestamp.
So far, the query should return ID = 105 or 106 (both have the same valid_from date, but I want only the one or the other in my result, no matter which, but not both). AND: because there are two types for this article (A + B), I also need ID = 103 in my result set.
How must the query look like?
You could try the HAVING parameter in your filter and selecting MAX(ID)
Or with a subselect:
SELECT [Type],(SELECT TOP(1) ID from dbo.articles S WHERE S.[Type] = A.Type AND S.Valid_From = MAX(A.Valid_From))
FROM dbo.articles A
WHERE
ARTICLE_NUMBER = 1
AND STORE_ID = 22
AND Country = 'DE'
-- AND Valid_FROM = (SELECT MAX(VALID_FROM) FROM dbo.articles)
GROUP BY [Type]
I having trouble with this query
it is executing quit well but I cannot make out
how is this select statement working.
Any help or explanation on this problem will be appreciated ..
thank you
these are my tables and query
here am looking for the employee who lives in same city as the the company for which they work
Table:-emp
eid name street city
----------- ---------------- ------------- ------------
1 yeman asd vasai
2 aksh adssd mumbai
3 chintan ghfgh mumbai
4 samual ghfdgh bandra
5 ddlj fghfgh andheri
6 jack fghnfg Bandra
7 bridge gfhfgh vasai
8 rahim ghfgh mumbai
9 chirag fghfghfg bandra
10 mistry hhhty bandra
11 ravi tytey andheri
Table:- company
cid companyname city
----------- ------------------- ------------
1 Vasai Industries vasai
2 Mumbai Pharmacy mumbai
3 bandra loft bandra
4 andheri tactics andheri
Table:= works
eid cid salary
----------- ----------- -----------
1 1 200
2 3 4831
3 4 4457
4 2 20001
5 1 32221
6 2 224
7 3 784
8 1 336
9 3 2489
10 2 4789
11 1 22541
Query
select * from emp
where eid
IN (select eid from works
where cid=(select cid from company
where city=emp.city))
why not use this query with joins and its easy to understand then a bunch of subqueries.
select * from emp
inner join works on works.eid = emp.eid
inner join company on company.city=emp.city
Explanation:
1.select cid from company where city=emp.city
Here you are getting city id regarding cities which are same in emp and company
2.
select eid from works
where cid=(select cid from company
where city=emp.city)
Here you getting collection of id's from works table which cid is same in emp and company
3.
select * from emp
where eid
IN (select eid from works
where cid=(select cid from company
where city=emp.city))
here you are getting all records based on emp id's whose cities are same in emp and city