Can't insert date from table to other with 2 condition - sql

i want insert date from table to other
with 2 condition from same row
(view picture in yellow part)
thanks

The problem is that the IN in the where clause can only have one colymn, not two. You could re-write it as:
from emp e
inner join emp2 e2 on e.Id = e2.Id and e.storeId = e2.storeId
I think that this would get you the effect.

You can declare only one field in "not in subselect" approach.
You should give this workaround a try:
select .... from emp e
where not exists (
select 1 from emp2 e2 where e.id = e2.id and e.storeid = e2.storeid
)
I guess it provides exactly the same result as you need

Related

SQL Filter on Table A using join on table B containing allowed values

I have 2 tables with the following fields:
Employee:
- string: Name
- Integer: Age
AllowedAge:
- Integer: Age
The table AllowedAge contains either:
all the ages that are permitted
OR
the table is empty if all the ages are permitted (which makes sense because the table cannot contain an infinite number of allowed ages).
To query for the employees with the allowed age in a way that covers the case where AllowedAge is empty and we want to get all the Employees we can do:
select e.Name from Employee e
left join AllowedAge aa on aa.Age = e.Age
where (aa.Age IS NOT NULL) or ((select count(*) from AllowedAge) = 0)
Is there a better, more idiomatic way to achieve the same results? Speed of the query is the number one priority and both tables may contain 10s of millions of rows.
Move the conditions to the where clause. I would recommend:
select e.*
from employee e
where not exists (select 1 from AllowedAge aa) or
exists (select 1
from AllowedAge aa
where aa.age = e.age
);
You are in the right direction, just need to use OUTER APPLY and OR condition in the WHERE clause
SELECT e.Name
FROM Employee e
OUTER APPLY(SELECT COUNT(*) tot FROM AllowedAge) t
LEFT JOIN AllowedAge aa on aa.Age = e.Age
WHERE (t.tot = 0 OR aa.Age = e.Age)
You can go for IF ELSE logic.
IF NOT EXISTS (SELECT 1 FROM AllowedAge) -- all ages allowed
BEGIN
SELECT e.Name from Employee e
END
ELSE
BEGIN
select e.Name from Employee e
inner join AllowedAge aa on aa.Age = e.Age
END

Self Join with Correlated Subquery

I need to find an employee with a salary greater than their Manager. For this, I used to following query and it works;
SELECT
e1.EmpID
,e1.EmpName
,e1.EmpSalary
,e1.ManagerID
FROM empsalary e
INNER JOIN empsalary e1 ON e.EmpID = e1.ManagerID
WHERE e1.EmpSalary > e.EmpSalary
But following one is not working. I want to know why it is not? Why it's result is null? What should be correct format?
SELECT *
FROM empsalary as e
WHERE e.empsalary=(
SELECT e1.empsalary
FROM empsalary as e1
WHERE e.EmpID = e1.ManagerID
AND e1.EmpSalary > e.EmpSalary)
Sample data and code here;
EmpID EmpName EmpSalary ManagerID
----------- ---------- -------------------- -----------
1 Neevan 100000 6
2 Mukesh 30000 6
3 Disha 50000 6
4 Martin 90000 6
5 Roy 170000 6
6 Anvesh 168000 NULL
CREATE TABLE empsalary
(
EmpID INT
,EmpName VARCHAR(10)
,EmpSalary BIGINT
,ManagerID INT
)
INSERT INTO empsalary
VALUES
(1,'Neevan',100000,6)
,(2,'Mukesh',30000,6)
,(3,'Disha',50000,6)
,(4,'Martin',90000,6)
,(5,'Roy',170000,6)
,(6,'Anvesh',168000,NULL)
Your correlations are all backwards. The right way is:
SELECT e.*
FROM empsalary e
WHERE e.empsalary > (SELECT m.empsalary
FROM empsalary m
WHERE m.EmpID = e.ManagerID
);
Notice that I have used the m table alias for the manager record. This helps to follow the logic.
There's no need for me to re-write how to do it, as Gordon has done a good enough job already, but I can explain why yours returned null...
For me to answer this, I needed to re-write yours slightly to help me to read it. It is essentially the same as yours. Also, similarly to how Gordon has done, I've substituted e for m and e1 for e. I've also called your tables tblSalaries, so that the table names aren't the same as the column name:
SELECT *
FROM tblSalaries as m
WHERE m.empsalary=(
SELECT e.empsalary
FROM tblSalaries as e
WHERE e.ManagerID = m.EmpID
AND e.EmpSalary > m e.EmpSalary)
If we work backwards and interpret the last part firstly:
...
(
SELECT e.empsalary
FROM tblSalaries as e
WHERE e.ManagerID = m.EmpID
AND e.EmpSalary > m.EmpSalary)
Firstly, WHERE m.ManagerID = e.EmpID is saying 'Find all employees in e who's manager ID is the same as the employee ID in m'. Considering there is only one managerID (6), then records 1-5 from e will all match the manager record, on m (6) on this.
Your next clause AND e.EmpSalary > m.EmpSalary is finding those who's salary is greater than the managers'. Therefore, you are left with record 5 (Roy) as you intended.
Now to return to your main query:
SELECT *
FROM tblSalaries as m
WHERE m.empsalary= (...tblSalaries as e...)
We have established that table e in the brackets has returned Roy, but we have also established that it only matches records in table m, where m is a manager. Ultimately, you are asking then to find where the manager's salary = Roy's salary; the answer, null.

Need help on a query

I have a table employees that looks like this:
Id Name Manager_Id
1 ABC 4
2 DEF 20
3 GHI 30
4 JKL 40
The below query does not return any results. I was expecting that it would return "JKL". What am I missing here?
select e1.Name from Employees e1 where e1.id =
(select e2.manager_id from employees e2 where e2.id = e1.id);
If you break this query down from the outside in, you're expecting to find a record where e1.id = e2.manager_id but also where e2.id = e1.id. In other words, you're querying for a record where id = manager.id, which simply does not exist.
What I assume you're trying to do is select all the records where the id exists as a manager_id in the table.
This can be done much more simply with an in operator:
SELECT name
FROM employees
WHERE id IN (SELECT manager_id FROM employees)
use in, not = :
select e1.Name from Employees e1 where e1.id IN
(select e2.manager_id from employees e2 where e2.id = e1.id);
The = operator is for testing if scalars are the same. IN tests set membership.

SQL Server fetch alias name for query

Please check fiddle: myFiddle
Query:
create table Emp(empId int primary key, EmpName varchar(50),MngrID int)
insert into Emp(empId,EmpName,MngrID)values(1,'A',2)
insert into Emp(empId,EmpName,MngrID)values(2,'B',null)
A has mngr B but A has no mngr, so while fetching the record from query it shows me:
EmpId EmpName MngrName(alias MngrName)
1 A B
2 B null
How to fetch the above data using a query?
For some reason it doesn't work in SQLFiddle, but i ran it in my own instance of SQL Server to verify it does work:
SELECT e1.EmpID, e1.EmpName, e2.EmpName
FROM emp e1 LEFT OUTER JOIN emp e2
ON e1.MngrID = e2.EmpID
Basically, you're doing a 'self join' by declaring two instances of the table (e1 and e2), and then joining the first instance's MngrID to the second instance's EmpID.
You need to LEFT JOIN table to itself:
select A.empID, A.empName, b.empName as mgrName
from emp A left join emp B on A.mngrID = b.empID
http://sqlfiddle.com/#!3/184dc/8
select empId,EmpName,(SELECT EmpName FROM emp WHERE MngrID = amp1.MngrID) AS Manager from emp as amp1

SQl to select from multiple tables

I have two table emptable1(empid,status) emptable2(empid,week)
i want to select all the empid whose status is 0 in emptable1 and from that list of empid i need to select empid whose week is 7 from the table emptable2
Please help :-)
Not knowing your table structure in detail, but this ought to work:
SELECT
(fields)
FROM
dbo.emptable1 e1
INNER JOIN
dbo.emptable2 e2 ON e1.empid = e2.empid
WHERE
e1.status = 0
AND e2.week = 7
How about using join? Join the two, then do your logic implementation altogether.