Selecting by ID over a table with 2 entries results in a single output result.
Here's the example table:
CREATE TABLE IF NOT EXISTS Person(
id int,
city_id int,
name varchar,
age int,
company varchar,
PRIMARY KEY (id, city_id)
);
INSERT INTO Person (id, name, city_id) VALUES (1, 'John Doe', 3);
INSERT INTO Person (id, name, city_id) VALUES (1, 'John Dean', 4);
The following queries were executed in the same order:
"SELECT * FROM Person" returns both rows - expected
"SELECT * FROM Person WHERE age is null" returns both rows - expected
"SELECT * FROM Person WHERE id = 1" returns only the first row, when it was expected to return both rows.
Would you be able to help me understand what is going on here?
Thanks!
Edit
This is an active critical issue being tracked here: https://issues.apache.org/jira/browse/IGNITE-12068
The issue has been fixed by the community and will be available in the upcoming release shortly:
https://issues.apache.org/jira/browse/IGNITE-12068
Thanks for reporting.
Related
Say I have two tables role and roleApp defined like this:
create table #tempRole(roleId int);
insert into #tempRole (roleId) values (1)
insert into #tempRole (roleId) values (2)
create table #tempRoleApp(roleId int, appId int);
insert into #tempRoleApp (roleId, appId) values (1, 26)
insert into #tempRoleApp (roleId, appId) values (2, 26)
insert into #tempRoleApp (roleId, appId) values (1, 27)
So, from #tempRoleApp table, I want to get only the rows that matches all the values of the #tempRole table (1 and 2), so in this case the output needs to be 26 (as it matches both 1 and 2) but not 27 as the table does not have 2, 27).
#tempRole table is actually the output from another query so it can have arbitrary number of values.
I tried few things like:
select *
from #tempRoleApp
where roleId = ALL(select roleId FROM #tempRole)
Which does not give anything... tried few more things but not getting what I want.
I believe this gives what you were looking for.
select tra.appId
from #tempRoleApp as tra
join #tempRole as tr on tra.roleId = tr.roleId
group by tra.appId
having count(distinct tra.roleId) = (select count(distinct roleId) from #tempRole)
It uses count distinct to get the total unique roleId's in the tempRole table and compares that with the unique count of these per appId, after confirming the roleIds match between the tables.
As you clarified in the comment, once you add another tempRole roleId, now no entry has all of the Ids so no rows are returned.
hello I used to work with Postgres and there if I want to see the affected rows after a manipulation I used the key word RETURNING
Example to show all the columns of the affected row(s):
UPDATE tblName
SET colName='something'
WHERE colName='something'
RETURNING *;
can anyone tell me how I do the same thing with DB2 ?
Example for DB2:
CREATE TABLE MYTABLE (
ID INTEGER GENERATED ALWAYS AS IDENTITY,
NAME CHAR(30),
AGE SMALLINT,
)
SELECT ID, NAME, AGE
FROM FINAL TABLE
(
INSERT INTO MYTABLE (NAME, AGE)
VALUES('Jon Smith', 35)
)
Result:
ID NAME AGE
1 Jon Smith 35
For my task i need to add a main entry and then add some additional, using the id of the added record.
Now i use request with "returning id":
with rows as (
insert into "Contact"(name, gender, city, birthdate)
values
('Name', 1, 'City', '2000-02-03')
returning id
)
insert into "Education"(user_id, place, degree, endyear)
select id , 'some_place', 'some_state', 1990 from rows
This way I can add one additional entry, but I need several. If I try to do the second insert query - postgre loses relation "rows"
with rows as (
insert into "Contact"(name, gender, city, birthdate)
values
('Name', 1, 'City', '2000-02-03')
returning id
)
insert into "Education"(user_id, place, degree, endyear)
select id , 'some_place', 'some_state', 1990 from rows
insert into "Status"(user_id, status)
select id , 'val' from rows
ERROR: relation "rows" does not exist
LINE 11: select id , 'val' from rows
^
SQL state: 42P01
Character: 373
is there any way to fix this?
I would suggest to put all statements in an anonymous code block. You could use the RETURNING into a variable and from there execute the following inserts, e.g.
DO $$
DECLARE returned_id int;
BEGIN
INSERT INTO contact
(name, gender, city, birthdate) VALUES
('Name', 1, 'City', '2000-02-03')
RETURNING id INTO returned_id;
INSERT INTO education
(user_id, place, degree, endyear) VALUES
(returned_id, 'some_place', 'some_state', '1990-01-01');
INSERT INTO status
(user_id, status) VALUES
(returned_id, 'val');
END;
$$;
Demo: db<>fiddle
I was given a spreadsheet of students where 500 students don't have student ID's. I imported this into SQL as a table, I'll call it 'table a'.
I have another table with all students and their ID's, complete with birthdate etc. I'll call it 'table b'.
Goal:
Copy the student ID's from table b into table a. To do this, I think I need to update table a based on the student name and birthdate.
Problem:
My update query is duplicating student id's to students who have the same birthdate, but they have different last names. So the result is two different students with the same birthdate end up having the same student id.
How can I update table a with the correct student ID's please?
My current update statement that's putting duplicate ID's on the same student:
UPDATE table a
SET EMPStudentID = CAStudentID
FROM #students
WHERE EmpStudentName = CA_STUNAME
AND EMP_DOB = CA_DOB
Thank you.
Screenshot of sample data:
Code of sample data:
CREATE TABLE #students (
EMPStudentID int
, EmpStudentName varchar(30)
, EMP_DOB DATE
, CA_DOB DATE
, CA_STUNAME VARCHAR(30)
, CAStudentID int
)
INSERT INTO #students (EmpStudentName ,EMP_DOB ,CA_DOB ,CA_STUNAME ,CAStudentID)
VALUES
('Brothers, John', '20000309', '20000309', 'Brothers, John', 1111111),
('Campbell, Thomas', '20000107', '20000107', 'Campbell, Thomas', 2222222),
('Echols, Terry', '20000309', '20000309', 'Echols, Terry', 3333333),
('Jones, Bruce', '20000518', '20000518', 'Jones, Bruce', 4444444),
('Maxwell, Lauren', '20000728', '20000728', 'Maxwell, Lauren', 5555555),
('Feldler, John', '19991026', '19991026', 'Feldler, John', 6666666),
('Jenkins, Michael', '19990322', '19990322', 'Jenkins, Michael', 7777777),
('Taylor, Greg', '20000428', '20000428', 'Taylor, Greg', 8888888),
('Williams, Gene', '20000105', '20000105', 'Williams, Gene', 9999999),
('Wynn, Charles', '20000111', '20000111', 'Wynn, Charles', 1233211)
SELECT * FROM #students
Try this below
UPDATE S
SET EMPStudentID = A.CAStudentID
FROM #students S
INNER JOIN #TableA A
ON S.EmpStudentName = A.CA_STUNAME
AND S.EMP_DOB = A.CA_DOB
SELECT * FROM #students
I hope everyone is fine and learning more. I need some advice on select statement and on its fine tuning. I am using Oracle 11gR2. Please find below table and data scripts.
create table employee (emp_id number, emp_name varchar2(50), manager_id number);
create table department (dept_id number, dept_name varchar2(50), emp_name varchar2(50), manager_level varchar2(20));
create table manager_lookup (manager_level_id number, manager_level varchar2(20));
insert into employee values (1, 'EmpA',3);
insert into employee values (2, 'EmpB',1);
insert into employee values (3, 'EmpC',1);
insert into employee values (4, 'EmpD',2);
insert into employee values (5, 'EmpE',1);
insert into employee values (6, 'EmpF',3);
insert into department values (1, 'DeptA','EmpD','Level3');
insert into department values (2, 'DeptB','EmpC','Level2');
insert into department values (3, 'DeptC','EmpA','Level1');
insert into department values (4, 'DeptD','EmpF','Level1');
insert into department values (5, 'DeptD','EmpA','Level3');
insert into department values (6, 'DeptA',NULL,'Level3');
insert into manager_lookup values (1, 'Level1');
insert into manager_lookup values (2, 'Level2');
insert into manager_lookup values (3, 'Level3');
commit;
Below query is returning me dept_id by passing some emp_name. I need those dept_id where manager_level is same as emp_name passed but don't need to have that same emp_name in result data set.
SELECT b.dept_id
FROM (SELECT DISTINCT manager_level
FROM department dpt
WHERE emp_name = 'EmpA'
and emp_name is not null) a,
department b
WHERE a.manager_level = b.manager_level
AND NVL (b.emp_name, 'ABC') <> 'EmpA';
Above query is returning me data set as below:
dept_id
--------
1
4
6
I want same result set but need to rewrite above query in a way to avoid department table scan two times. This is just sample query but in real time scanning big table two times is giving performance issues. I want to re-write this query in a way to perform better and avoid same table scan two times.
Can you please help on providing your wonderful suggestions or solutions? I will really appreciate all responses.
Thank you for going over the question.
If you want your query to be more efficient, then use indexes:
create index idx_department_name_level on department(emp_name, manager_level)
and
create index idx_department_name_level on department(manager_level, emp_name, dept_id)
also, you have a redundant null check which may be avoiding indexes...
SELECT b.dept_id
FROM (SELECT manager_level
FROM department dpt
WHERE emp_name = 'EmpA') a,
department b
WHERE a.manager_level = b.manager_level
AND NVL (b.emp_name, 'ABC') <> 'EmpA';
post your explain plan for more help
This should work:
SELECT a.*
FROM
(
SELECT d.*,
SUM(CASE WHEN emp_name = 'EmpA' THEN 1 ELSE 0 END)
OVER (PARTITION BY manager_level) AS hits
FROM department d
) a
WHERE hits > 0
AND NVL(emp_name, 'Dummy') <> 'EmpA'
ORDER BY dept_id
;
The query does the following:
Calculate how many times EmpA appears in a given manager_level
Keep all records with a manager_level that has at least one occurrence of EmpA in it
Excluding the EmpA records themselves
SQL Fiddle of the query in action: http://sqlfiddle.com/#!4/a9e03/7
You can verify that the execution plan contains only one full table scan.