How to write a correlated subquery for the below one? - subquery

Can anyone help me to write co-related subquery for the below non-correlated subquery?
SELECT e.emp_no,e.last_name FROM employees e,dept_emp d
WHERE e.emp_no = d.emp_no
AND d.dept_no = (SEELCT dept_no FROM dept_emp WHERE emp_no =
(SELECT emp_no FROM employees WHERE first_name = "Margareta" AND last_name = "Markovitch"));

This the answer for the above query :
Select E.Emp_No,E.Last_Name From Employees E Where Exists
( Select Dept_No From Dept_Emp D Where E.Emp_No=D.Emp_No)
And emp_no In (Select emp_no From Employees Where First_Name='Margareta' And Last_Name='Markovitch');

Related

Cognos 11 - filter between query subjects

Given Table A with columns: ColA1, ColA2, ColA3
And a Table B with columns: ColB1
I want to restrict the data that can be returned from Table A based on data in Table B, like:
ColA1 not in ColB1
Ideally, some way incorporate SQL queries in the filter with select statements
What you want is
SELECT a.ColA1
, a.ColA2
, a.ColA3
FROM TableA a
LEFT OUTER JOIN TableB b on b.ColB1 = a.ColA1
WHERE b.ColB1 IS NULL
So...
Query1 contains ColA1, ColA2, and ColA3 from TableA.
Query2 contains ColB1 from TableB.
Query3
joins Query1 and Query2 on ColA1 1..1 = 0..1 ColB1
Data Items: ColA1, ColA2, ColA3
Filter: ColB1 IS NOT NULL
not exists is probably what you are looking for
Try something like this
select * from TableA as T1
where not exists
(select * from TableB as T2
where t1.key1 = t2.key1 and T1.key2 = t2.key2)

How do I optimize MariaDB query with subqueries in FROM clause?

imagine these two tables.
Table A
ID col1 col2 col3
1 foo baz bar
2 ofo zba rba
3 oof abz abr
Table B
A_ID field_name field_value
1 first Jon
1 last Doe
2 first Adam
2 last Smith
etc..
Now I would like to have a query (current one looks like this)
SELECT
a.id,
a.col1,
a.col2,
(SELECT field_value FROM B WHERE A_ID = a.id AND field_name = 'first') as first_name,
(SELECT field_value FROM B WHERE A_ID = a.id AND field_name = 'last') as last_name
FROM A a
WHERE (SELECT COUNT(*) FROM B WHERE A_ID = a.id) = 2;
This query is working. What I would like to achieve would be something like this.
SELECT
a.id,
a.col1,
a.col2,
(SELECT field_value FROM b WHERE b.field_name = 'first') as first_name,
(SELECT field_value FROM b WHERE b.field_name = 'last') as last_name
FROM
A a,
(SELECT field_value, field_name FROM B WHERE A_ID = a.id) b
WHERE (SELECT COUNT(*) FROM b) = 2;
How would my approach look correctly? Is there any other way to get rid of the multiple queries of the table B?
Thank you!
I would replace your correlated subqueries with joins:
SELECT
a.id,
a.col1,
a.col2,
b1.field_value AS fv1,
b2.field_value AS fv2
FROM A a
LEFT JOIN B b1
ON a.id = b1.A_ID AND b1.field_name = 'first'
LEFT JOIN B b2
ON a.id = b2.A_ID AND b2.field_name = 'last';
This answer assumes that a left join from a given A record would only match at most one record in the B table, which, however, is a requirement anyway for your correlated subqueries to only return a single value.

Unpivot and Pivot does not return data

I'm trying to return data as columns.
I've written this unpivot and pivot query:
`select StockItemCode, barcode, barcode2 from (select StockItemCode, col+cast(seq as varchar(20)) col, value from (
select
(select min(StockItemCode)
from RTLBarCode t2
where t.StockItemCode = t2.StockItemCode) StockItemCode,
cast(BarCode as varchar(20)) barcode,
row_number() over(partition by StockItemCode order by StockItemCode) seq
from RTLBarCode t) d unpivot(
value
for col in (barcode) ) unpiv) src pivot ( max(value) for col in (barcode, barcode2)) piv;`
But the problem is only the "Barcode2" field are returning a value (the barcode field returns a null when in fact there is a value.
SAMPLE DATA
I have a Table called RTLBarCode
It has a field called Barcode and a field called StockItemCode
For StockItemCode = 10 I have 2 rows with a Barcode value of 5014721112824 and 0000000019149.
Can anyone see where I am going wrong?
Many thanks
You are indexing your barcode in unpiv.
This results in col's-values barcode1 and barcode2.
But then you are pivoting on barcode instead of barcode1. No value is found and the aggregate returns null.
The correct statement would be:
select StockItemCode, barcode1, barcode2 from
(
select StockItemCode, col+cast(seq as varchar(20)) col, value
from
(
select
(select min(StockItemCode)from RTLBarCode t2 where t.StockItemCode = t2.StockItemCode) StockItemCode,
cast(BarCode as varchar(20)) barcode,
row_number() over(partition by StockItemCode order by StockItemCode) seq
from RTLBarCode t
) d
unpivot(value for col in (barcode)) unpiv
) src
pivot (max(value) for col in (barcode1, barcode2)) piv

Give the employee(fname and lname) that make the most money in each dept

SELECT max(salary), dept
FROM Employees
GROUP BY dept;
How to add fname and lname without the errors? If I add them in group by, I no longer get the max salary.
This is how I would do it in SQL Server... You need a subquery that returns the max salary for each department, then join that back to the employee table to figure out which employee has that salary for that department:
SELECT D.Dept, D.MaxSalary, Employees.FName, Employees.LName
FROM (
SELECT MAX(Salary) AS MaxSalary, Dept
FROM Employees
GROUP BY dept
) D
INNER JOIN Employees
ON Employees.Dept = D.Dept
AND Employees.Salary = D.MaxSalary

How to use union in sub query with Access database

I'm working on a WinForms application with an Access database.
Please correct me.
Select *
from EMP
where Empid in
(Select id
from Test1
where pin=4
UNION
Select id
from Test2
where pin=4)
When I try to execute this on the Access database, it gave me this error:
Operations are not allowed in subquery
You can also use two standard SQL where conditions, as per below. I have tested this approach and it appears to work as expected
SELECT x, y, z
WHERE x NOT IN (SELECT x FROM b)
AND NOT IN (SELECT x FROM c)
I tried as below and its worked.
Select *
from EMP where Empid in (Select id from Test1 where pin=4 ) UNION
Select *
from EMP where Empid in(Select id from Test2 where pin=4)
Really late but this works
Select *
from EMP
where Empid in
(SELECT id FROM (
Select id from Test1 where pin=4
UNION Select id from Test2 where pin=4 ) )
How about something like
Select e.*
from EMP e INNER JOIN
(
Select id
from Test1
where pin=4
UNION
Select id
from Test2
where pin=4
) subSelect ON e.Empid = subSelect.id
as far as i know union operations are not allowed in subqueries,instead you can create a named union query and include in the subquery expression

Resources