Show the last record if meets certain conditions - oracle

I have two tables for example USER and AUDIT, where the join is user.id = audit.record:
USER:
ID, USER, NAME, LAST_NAME
205, USER1, PEDRO, PEREZ
206, USER2, JUAN, PEREZ
AUDIT:
ID, ACTION, RECORD, FIELD, DATE
1, MODIFY, 205, LAST_NAME, 08/11/12
2, MODIFY, 205, NAME, 25/09/12
3, MODIFY, 206, NAME, 08/11/12
I need a query where the result shows all the users that his last registry entry have a modification in its name. In the example the result would be just USER2
I'm using Oracle 8, can i use a simple query or do i need to create a function?
Thanks in advance.

Maybe this will do it:
select id from user u,
audit a
where u.id=a.record
and a.action='MODIFY'
and a.field='NAME'
and a.id = (select max(id)
from audit a2
where a2.record=u.id)

Here is my solution... MAX (FIELD_NAME) KEEP (DENSE_RANK FIRST ORDER BY OPERATION_DT)

Related

Oracle SQL - How to add a filter on a case field that I created?

I'm relatively new to Oracle SQL and have run into an issue where I'm trying to filter a report to only return records logged by a specific list of user names.
They are currently stored in the system in fields user.first_name and user.surname and I've created the following CAST field in the coding to join the two together:
CAST(USER.FIRST_NAME||' '||USER.SURNAME as VARCHAR (25)) as CUSTOMER
What I want to do now though is restrict it so that my query will only return records where the customer is in a pre-determined list that I can hard core into the SQL.
eg I only want to see records for :
Joe Bloggs,
John Doe,
A Nother
How do I do this in Oracle SQL?
Thanks
One option (as you said you hardcoded it) is
select *
from your_table
where customer in ('Joe Bloggs', 'John Doe', 'A Nother');
A better one is to store those customers into a separate table and join it with your_table:
insert into separate_table (name) values ('Joe Bloggs'); -- do the same for the rest
select *
from your_table y join separate_table s on s.name = y.first_name ||' '||i.surname;
Even better, use their IDs (because, there could be two John Doe persons; which one will you take)?
insert into separate_table (id) values (1123); -- this is Joe Bloggs
select *
from your_table y join separate_table s on s.id = y.id;

How to use GROUP BY clause with COUNT(*)

I have two tables on Oracle database, one is named departments_table and the other is locations_table. The departments.table has dep_id, dep_name, location_id, staff_id, employer_id. The locations table consists of location_id, city_id, streetname_id and postcode_id. How do I calculate the number of departments that each location has?
This is the code below is what I have tried to replicate but have been unsuccessful. The error message below that is what shows once the code has submitted.
SELECT dep_name, location_id,
COUNT(*)
FROM departments_table
WHERE location_id => 1
GROUP BY dep_name;
The results of this is an error, " not a single group function "
If you want to count how many departments are in each location, then you must group by location, not by department name, right? Let's start with that.
Then, you don't need ANYTHING about the individual departments in the output of the query, do you? You just need the location id and the count of departments.
select location_id, count(*) as cnt
from departments_table
group by location_id
;
This does most of the work. You may want to add the location name (city, address, etc.), which is/are stored elsewhere - in the locations_table. So you will need a join. And there may be locations in that table that are not, in fact, the location of any department (their id doesn't appear in the departments_table at all). If so, you would need an OUTER join. Also for those departments you probably want to show a count of 0 (rather than null) - you can "fix" that with the nvl() function. So you will end up with something like
select l.*, nvl(g.cnt, 0) as department_count
from locations_table l
left outer join
( select location_id, count(*) as cnt
from departments_table
group by location_id
) g
on l.location_id = g.location_id
;
SELECT l.location_id, l.city, COUNT(d.DEPARTMENT_ID)
FROM OEHR_LOCATIONS l, OEHR_DEPARTMENTS d WHERE l.location_id = d.location_id
GROUP BY l.location_id, l.city ORDER BY l.city;
This method works. I created aliases and made minor changes. OEHR stands for the table names so ignore that.

Hive query to find the ip address and country which is not present

I have a Hive table named 'Login'. It contains the following columns :-
UserID | UserName | UserIP | UserCountry | Date
On a particular day (all that logins of that day), I want to find out the UserIDs, which has been accessed from a country (UserCountry) from where the user has never accessed his account from OR the IPs (UserIP) from which the account has never been accessed before.
I would start with an except where I remove prior countries and IPs
select userid, usercountry, userip
from table
where date=xx
except
select userid, usercountry, userip
from table
where date<xx
I think that the best way is the GROUP clause!
You say "has never been accessed before", means COUNT = 1.
To find the IP use only once :
select UserId, UserIP, COUNT(UserIP) FROM Login WHERE Date = yourdate GROUP BY UserIP, UserId HAVING COUNT(UserIP) = 1
To find the country use only once :
select UserId, UserCountry, COUNT(UserCountry) FROM Login WHERE Date = yourdate GROUP BY UserCountry, UserId HAVING COUNT(UserCountry) = 1
Left Outer Join will be able to satisfy your requirement in HIVE.
select t1.userid, t1.usercountry, t1.userip
from table t1
LEFT OUTER JOIN
from table t2
ON (t1.userid=t2.userid)
WHERE t1.date=xx and
t2.data < xx and
(t2.usercountry IS NULL or
t2.userip IS NULL);
Hope this helps...

Why is "group by" giving only one column as output?

I have a table something like this:
ID|Value
01|1
02|4
03|12
01|5
02|14
03|22
01|9
02|32
02|62
01|13
03|92
I want to know how much progress have each id made (from initial or minimal value)
so in sybase I can type:
select ID, (value-min(value)) from table group by id;
ID|Value
01|0
01|4
01|8
01|12
02|0
02|10
02|28
02|58
03|0
03|10
03|80
But monetdb does not support this (I am not sure may be cz it uses SQL'99).
Group by only gives one column or may be average of other values but not the desired result.
Are there any alternative to group by in monetdb?
You can achieve this with a self join. The idea is that you build a subselect that gives you the minimum value for each id, and then join that to the original table by id.
SELECT a.id, a.value-b.min_value
FROM "table" a INNER JOIN
(SELECT id, MIN(value) AS min_value FROM "table" GROUP BY id) AS b
ON a.id = b.id;

Getting latest record for each userid in rails 3.2

I have user with name, location, created_at as important fields in table.
I want to retrieve for each user the latest location,i.e, I want something like this:
username location created_at
abc New York 2012-08-18 16:18:57
xyz Mexico city 2012-08-18 16:18:57
abc Atlanta 2012-08-11 16:18:57
only input is UId(1,2) array of userids.please help me to accomplish this.I just want to know how to write query using active record query interface.
Generally, this should be a standard way to solve this kind of problems:
SELECT l1.user, l1.location
FROM locations l1
LEFT JOIN locations l2 ON l1.user = l2.user AND l2.created_at > l1.created_at
WHERE l2.id IS NULL
The idea is to join the table with itself, and find those rows which don't have any row with the same user and greater created_at.
Of course, you should have (user, created_at) index on your table.
Now you should see how would that be represented in AR interface.
When
u_id
is the array of user ids, then
u_id.map{|i| User.find(i).location}
should be an array of the users locations.
You can Use
User.where(:uid => [1,2,3]).maximum('location')
which will create something like
SELECT MAX(`users`.`location`) AS max_id FROM `users` WHERE `users`.`id` IN (1, 2,3)

Resources