Merging two sql queries columns into one - sql

I have two queries that return:
Query 1
ClassName1 Students1 Teachers1
A1 30 3
A2 20 3
A3 35 4
Query 2
ClassName2 Students2 Teachers2
A1 20 3
A2 22 3
A3 20 4
Result
ClassName1 Students1 Teachers1 ClassName2 Students2 Teachers2
A1 30 3 A1 20 3
A2 20 3 A2 22 3
A3 35 4 A3 20 4
I would like to merge the two dataset into one by columns. SQL 2008
Any ideas?

You would normally do this with a join:
select q1.*, q2.*
from (query1) q1 join
(query2) q2
on q1.ClassName1 = q2.ClassName2;

Thanks i worked this out using this :
**/***********Join **************
select emp.EmpID,emp.FirstName ,emp.LastName,sod.EmpID,sod.HireDate from Employee as emp
join
(
select emp2.EmpID,emp2.HireDate from Employee as emp2
--where emp.EmpID=emp2.EmpID
) sod
on sod.EmpID=emp.EmpID
**/***********cross Apply **************
select emp.EmpID,emp.FirstName ,emp.LastName,sod.EmpID,sod.HireDate from Employee as emp
cross apply
(
select emp2.EmpID,emp2.HireDate from Employee as emp2
where emp.EmpID=emp2.EmpID
) as sod
Any other ideas will be great

Related

SQL Query Subtracting Values from Two Tables

I have two tables, demand and fte, that have department, shift and demand or fte. I need to calculate the need (demand - fte). What I get is a table with 9 entries. I've run into this before and for the life of me I cannot remember how I resolved it.
There is also a department table which I neglected. It only holds the departmentID & Name
select
dp.[Department], dm.[shift], (dm.Demand - ft.fte) as Need
from
tblDemand dm
right join
[tblDepartment] dp on dp.[DepartmentID] = dm.[DepartmentID]
right join
[tblActual_FTE] ft on ft.[DepartmentID] = dm.[DepartmentID]
where
dp.Department = 'screw ii'
Demand
department
shift
demand
A1
1
23
A1
2
26
A1
3
21
FTE
department
shift
fte
A1
1
26
A1
2
24
A1
3
18
Expected result:
department
shift
need
A1
1
-3
A1
2
2
A1
3
3
Current results:
department
shift
need
A1
1
-3
A1
2
0
A1
3
-5
A1
1
-1
A1
2
2
A1
3
-3
A1
1
5
A1
2
8
A1
3
3
your join should be based on departmentid and shift :
select
dp.[Department]
, dm.[shift]
, (dm.Demand - ft.fte) as Need
from tblDemand dm
right join [tblDepartment] dp on dp.[DepartmentID] = dm.[DepartmentID]
right join [tblActual_FTE] ft
on ft.[DepartmentID] = dm.[DepartmentID]
and ft.[shift] = dm.[shift]
where dp.Department = 'screw ii'

SQL | Join two tables and get one of the tables column against any of the matching row

Table A stores site-article wise transfer/transit stock.
Table A
site article transfer_Stock transit stock
s1 a1 10 15
s1 a2 20 25
Table B has site-article-storage location wise stock
Table B
site article sloc stock
s1 a1 1000 5
s1 a1 2000 10
s1 a1 3000 15
s1 a2 1000 20
s1 a2 5000 25
I want to get total stock as for site article
Output required -
site article sloc transfer_stock transit_stock stock
s1 a1 1000 10 15 5
s1 a1 2000 0 0 10
s1 a1 3000 0 0 15
s1 a2 1000 20 25 20
s1 a2 5000 0 0 25
I want transfer stock and transit stock against any of the row of sloc.
Table A has 1 billion rows and Table B has 1.5 billion rows.
what is the optimised way to achieve this in sql.
Currently we are achieving this throguh
SELECT A.site,
A.article,
B.sloc,
case when MIN_B.sloc=B.sloc then A.transfer_stock else 0 end as transfer_stock,
case when MIN_B.sloc=B.sloc then A.transit_stock else 0 end as transit_stock,
B.stock
FROM A
LEFT OUTER JOIN B ON A.site = B.site AND A.article = B.article
LEFT OUTER JOIN (select site,article,min(sloc) from B group by site,article) MIN_B on A.site = MIN_B.site AND A.article = MIN_B.article
SELECT A.site, A.article, B.sloc, A.transfer_stock, A.transit_stock, B.stock
FROM A
LEFT JOIN B ON A.site == B.site AND A.article == B.article;

How to get rows having uncommon values in two tables in SQL?

Suppose I have a table a with columns A1,A2,A3,A4 and table b with columns B1,B2,B3,B4.
I want to find a records which are having different values in column A1,A2 and B1,B2
Ex.
A1 A2 A3 A4 B1 B2 B3 B4
12 10 10 12 12 10 10 12
14 14 10 12 15 10 10 12
15 10 10 10 15 10 10 10
IT SHOULD RETURN
14 14 10 10
I tried:
SELECT A1,A2
FROM A
EXCEPT
SELECT B1,B2
FROM B;
However, it returned on A1,A2 columns instead of all columns
use left join
select a.* from tableA a
left join tbaleB b
on a.A1=b.B1 and a.A2=b.B2 and a.A3=b.B3 and a.A4=b.B4
where b.B1 is null
You can try below using left join
select * from tableA
left join tableB on A1=B1 and A2=B2
where B1 is null and B2 is null
I would use not exists:
select a.*
from a
where not exists (select 1
from b
where a.a1 = b.b1 and a.a2 = b.b2
);

Records in one table not present in another table

I am trying to use LEFT JOIN, NOT EXISTS and NOT IN, where I want records from pursuit table, expect matching records from condition table using foreign key instance = id. When I execute the query, it shows empty (no records).
LEFT JOIN
SELECT p.id, p.dept
FROM
pursuit p LEFT JOIN condition c USING (dept)
WHERE
p.id = c.instance
NOT EXISTS
SELECT p.id, p.dept
FROM
pursuit p
WHERE
p.id NOT EXISTS IN (SELECT c.instance FROM condiiton c)
NOT IN
SELECT p.id, p.dept
FROM
pursuit p
WHERE
p.id NOT IN (SELECT c.instance FROM condiiton c)
TABLES
condition
id instance dept
1 3 5
5 2 5
2 7 5
3 4 5
4 10 5
5 2 3
6 15 6
pursuit
id name dept
3 C1 5
2 C9 5
7 C77 5
4 C9 5
10 C6 5
19 C23 5
17 C45 5
15 C12 5
23 C33 5
3,2,7,4 = matching records of dept 5
19,17,15,23 = not matching records of dept 5
Output
id name dept
19 C23 5
17 C45 5
15 C12 5
23 C33 5
Edit2:
SELECT p.id, p.dept
FROM pursuit p LEFT JOIN condition c
on (p.id = c.instance and p.dep=c.dep)
WHERE c.instance IS NULL
this returns rows of pursuit not present in condition table
I would use NOT EXISTS instead :
SELECT P.*
FROM pursuit p
WHERE NOT EXISTS (SELECT 1
FROM condition c
WHERE p.id = c.instance
);
The only problem were with NOT EXISTS you haven't established any relation between them.

Taking Sample in SQL Query

I'm working on a problem which is something like this :
I have a table with many columns but major are DepartmentId and EmployeeIds
Employee Ids Department Ids
------------------------------
A 1
B 1
C 1
D 1
AA 2
BB 2
CC 2
A1 3
B1 3
C1 3
D1 3
I want to write a SQL query such that I take out 2 sample EmployeeIds for each DepartmentID.
like
Employee Id Dept Ids
B 1
C 1
AA 2
CC 2
D1 3
A1 3
Currently I am writing the query,
select
EmployeeId, DeptIds, count(*)
from
table_name
group by 1,2
sample 2
but it gives me total two rows.
Any help?
If the number of departments i know and small you could do a stratified sampling:
select *
from table_name
sample
when DeptIds = 1 then 2
when DeptIds = 2 then 2
when DeptIds = 3 then 2
end
Otherwise a combination of RANDOM and ROW_NUMBER:
select *
from
(
sel EmployeeId, DeptIds, random(1,10000000) as rand
from table_name
) as dt
qualify
row_number()
over (partition by DeptIds
order by rand) <= 2