cursor- how to compare and select unique records - plsqldeveloper

IF i have to compare two cursors and retrun unique vals how do i do that
example
CURSOR c_stock_option IS
Select empid, name, ssn, isenrolled
from employee
where isenrolled=1
CURSOR c_espp_option IS
Select empid, name, ssn, isenrolled
from employee
where isenrolled=2
Now i want to reject all the recs in second cursors that are in cursor 1 select, how do i do that

Ummm.....by definition, all of the rows where isenrolled=2 does not overlap with the rows where isenrolled=1. But I think you're asking a more general question about how to exclude rows from one result set that are in another.
If this is the case, you could take a few different approaches:
1)
CURSOR c_stock_option IS
Select empid, name, ssn, isenrolled from employee where isenrolled=1
MINUS
Select empid, name, ssn, isenrolled from employee where isenrolled=2
2)
CURSOR c_stock_option IS
Select empid, name, ssn, isenrolled from employee
where isenrolled=1
and empid not in (
Select empid, name, ssn, isenrolled from employee where isenrolled=2)
3)
CURSOR c_stock_option IS
Select empid, name, ssn, isenrolled from employee e
where isenrolled=1
and not exists(
Select 1 from employee where e.empid = employee.empid and isenrolled=2)
Which you choose depends on your situation, data model, indexing etc.

Related

I want select all duplicate records from my table on the basis of student name and father name in mysql database

select student_name
from thewings_clients_temp
where concat(student_name,father_name) IN (
select concat(student_name,father_name) from thewings_clients_temp group by student_name HAVING COUNT(concat(student_name,father_name)) > 1
)
This will give you the students whose name is repeated more than once.
SELECT student_name,
father_name,
COUNT(*)
FROM thewings_clients_temp
GROUP BY student_name, father_name
HAVING COUNT(*) > 1

How to select rows without duplicates and partition only by some columns?

I want to create a SELECT that neglects duplicates. The duplicates should be detected by only some columns, while I still want to select all columns of one row.
Example:
CREATE TABLE employee (
id integer,
firstname varchar(100),
lastname varchar(100),
country varchar(100),
salary interger,
//many more fields...
);
select * from employee GROUP BY firstname, lastname, country;
Of course that's invalid sql, but it shows my intention:
If any combination of (firstname, lastname, country) forms a duplicate key, then I only want to select one of those duplicate rows, but all columns of it.
Preferably, out of the duplicates I would want to select the row with the highest value in salary column.
I'm using mysql 8
You can use ROW_NUMBER() to do this. Essentially what you have posted for your grouping instead becomes your partition, and you can then chose how each group is sorted (in the case of the below salary):
select *
from (select *,
row_number() over(partition by firstname, lastname, country
order by salary desc) AS rownum
from employee) AS e
where e.rownum = 1;
You can use row_number() to select unique combinations of firstname, lastname, country:
select t.*
from (select t.*,
row_number() over (partition by firstname, lastname, country order by id) as seqnum
from t
) t
where seqnum = 1;
If you want one row only where there are duplicates, then include a count():
select t.*
from (select t.*,
row_number() over (partition by firstname, lastname, country order by id) as seqnum,
count(*) over (partition by firstname, lastname, country) as cnt
from t
) t
where seqnum = 1 and cnt > 1;

JDBC MySQL trying to insert multiple rows with select subquery

Currently error 1242 happens saying that I'm getting multiple results from the query. I want all the results to be put into the equipmenthistory table though. I want to label a few items with the same bagId, and upon running the insert it does all the items at once to the same eid.
My current insert statement is below.
into equipmenthistory (assetId, checkedOutBy, operation) values ((select assetId from equip where bagId = '1'), (select eid from employees where employeeNum = '1'),'checkedOut');
Tables:
equip
assetId,
asset,
bagId
employees
eid,
employeeNum
equipmenthistory
eid,
assetId,
operation
Preferred outcome:
equipmenthistory:
eid - 1 assetId 1 operation checkedOut
eid - 1 assetId 2 operation checkedOut
You need to construct ONE select with all your values, not a collection of selects
insert into equipmenthistory
(assetId, checkedOutBy, operation)
values
select assetId, employeeNum, 'checkedOut'
from equip
join employees using (...)
where ....

how does the select statement works or retrieve the data from database

here is the query
select emp1.EmployeeId as empss, EmployeeName, DepartmentId, IsActive, CreatedDate, Address, salary
FROM employee as emp1
where 4=(select count(emp.EmployeeId) as con from employee as emp where emp.EmployeeId < emp1.EmployeeId);
here is the table name employee where i am getting the number 5 row without limit,for writing this query i took some help over net but my question is how does the select statments work ??
1) does it select the column first then filter the where clause ?
2) does it filter the where clause then select column ??
please explain
thanks so much in advance
In MySQL Where conditions (filtration) executes before select columns. Following is the order of execution for various clauses present in a SELECT statement
FROM (including joins)
WHERE
SELECT (select columns)
GROUP BY
ORDER BY
LIMIT
Can validated by following SQL
SELECT 1 as cnt FROM employee WHERE cnt = 1;
This statement will throw error as the cnt is defined in the SELECT clause which executes after the WHERE but following SQL
SELECT 1 as cnt FROM employee GROUP BY cnt;
will work.
select * from employee;
this would return all rows from the table 'employee'
select id from employee;
this would return all rows but only the id column from the table 'employee'
select id, name from employee where id in (1,2);
this would return only matching rows for employee.id in (1,2) and the columns would be id and name
The 'from' will direct the db to goto that table and the conditions would filter out the needed rows - reducing scope further by traversing through and only printing the asked-for columns

Mysql retriving the nth record

Suppose I have a table named EMPLOYEE containing the following attributes
(EMPLOYEE_ID, LAST_NAME, FIRST_NAME, MIDDLE_NAME, JOB_ID, MANAGER_ID, Salary)
Can I
Display the Nth highest salary drawing employee details
Please help
ORDER BY and LIMIT where 10 is n + 1:
SELECT
*
FROM
employees
ORDER BY
Salary DESC
LIMIT
10, 1
(If you want the first record, use LIMIT 0, 1. For the tenth, use LIMIT 9, 1 etc.)
try this
put n > 1 to get corresponding results
n=3 must give you second highest salary
SELECT * --This is the outer query part
FROM Employee Emp1
WHERE (N-1) = ( /* Subquery starts here */
SELECT COUNT(DISTINCT(Emp2.Salary))
FROM Employee Emp2
WHERE Emp2.Salary > Emp1.Salary)
Test Table
CREATE TABLE Test
(ID INT IDENTITY(1,1),
Salary INT)
INSERT INTO Test
VALUES (100), (200), (300), (400), (500)
SELECT * FROM Test
Query
SELECT TOP 1 Salary
FROM
(SELECT TOP 3 Salary FROM Test ORDER BY Salary DESC)q
ORDER BY Salary ASC
In your Sub-query SELECT TOP Nth the rest remains the same and it will get you the desired results