Sqlserver minus with unmatched columns - sql-server

I have two tables with different columns and want intersect records.
Table1:
Id name age
1 AAA 20
2 AAA 30
3 BBB 25
4 BBB 30
Table2:
name age
AAA 20
BBB 30
Expect Output:(Table2 - Table1)
Id name age
2 AAA 30
3 BBB 25

You can use NOT EXISTS with a correlated subquery to check that no tuple (name, age) with identical values exists.
SELECT *
FROM table1 t1
WHERE NOT EXISTS (SELECT *
FROM table2 t2
WHERE t2.name = t1.name
AND t2.age = t1.age);

Use Right Join
SELECT DISTINCT Table1.Id, Table1.Name, Table1.Age
FROM Table2 RIGHT JOIN Table1 ON Table1.Name = Table2.Name AND Table1.Age = Table2.Age
WHERE Table2.Name IS NULL
FIDDLE DEMO

Related

Data population

I have a scenario like
Table1
Id. Name. Age. City
1. Aaa. 20. Ccc
2. BBB. 12. Ccc
Table 2
Id. Name. Age. City
1. FFF Ccc
Now all I need is based on the city (ccc is common in both )
I will have to change age value in table b and the expected output is
Id. Name. Age. City
1. FFF 20. Ccc
2. FFF 12. Ccc
Just Use a simple INNER JOIN :
SELECT t1.ID, t2.name, t1.Age, t1.City
FROM Table1 t1 JOIN Table2 t2 on ( t1.City = t2.City );
SQL Fiddle Demo
SELECT Table1.Id, Table2.Name, Table1.Age, Table1.City
FROM Table1
INNER JOIN Table2
ON Table1.City=Table2.City
It's easier if you tabulate the table name and the values in your question.
(use code block)

Joining Two Tables, getting Aggregate and unique value of one

This may have been answered previously, but I'm having a difficult time describing my issue.
Let's say I have two tables
Table1
User, CalendarID
Joe 1
Joe 2
Joe 3
Sam 4
Bob 1
Jim 2
Jim 3
Table2
CalendarID, CalendarTime
1 2014-08-18 00:00:00.000
2 2015-01-19 00:00:00.000
3 2015-08-24 00:00:00.000
4 2016-01-18 00:00:00.000
What I would like to do is Join the two tables, only getting a single User Name, and Calendar ID based on what is this highest CalendarTime associated with that CalandarID.
So I would like the query to return
User CalendarID
Joe 3
Sam 4
Bob 1
Jim 3
The closest I've managed is
SELECT t1.User, MAX(t2.CalendarTIme) AS CalendarTime
FROM table1 t1
INNER JOIN table2 as t2
ON t1.CalendarID = t2.CalendarID
Group By t1.User
Which gets me the User and CalendarTime that I want, but not the Calendar ID, which is what I really want. Please help.
Closest to your script and pretty straightforward:
SELECT t1.User, t2.*
FROM table1 t1
INNER JOIN table2 as t2
ON t1.CalendarID = t2.CalendarID
WHERE NOT EXISTS
(
SELECT 1 FROM table1 t1_2
INNER JOIN table2 t2_2
ON t2_2.Calendar_ID = t1_2.Calendar_ID
WHERE t1_2.User = t1.User
AND t2_2.CalendarTime > t2.CalendarTime
)
This can be solved for the top N per group:
using top with ties with row_number():
select top 1 with ties
t1.User, t1.CalendarId, t2.CalendarTime
from table1 t1
inner join table2 as t2
on t1.Calendarid = t2.Calendarid
order by row_number() over (partition by t1.User order by t2.CalendarTime desc)
or using common table expression(or a derived table/subquery) with row_number()
;with cte as (
select t1.User, t1.CalendarId, t2.CalendarTime
, rn = row_number() over (partition by t1.User order by t2.CalendarTime desc)
from table1 t1
inner join table2 as t2
on t1.Calendarid = t2.Calendarid
)
select User, CalendarId, CalendarTime
from cte
where rn = 1

Find Columns whose values are distinct in two tables without union

I have two tables having same columns.I want to get those columns whose values are distinct in both tables.How Can I achieve this?please help.Iam stuck.
imageid is primary key in both tables.Its not necessary that imageids present in first table should be present on second table.
first table
imageid name id
1 priya 001
2 neha 002
3 divya 003
4 santo 004
second table
imageid name id
1 priy 001
2 neha 003
4 santo 004
Result
imageid firstdata seconddata columnname
1 priy priya name
2 002 003 id
I have tried this-
select t1.imageid, t1.name as firstdata, t2.name as seconddata, 'name' as colname
from t1 join
t2
on t1.imageid = t2.imageid
where t1.name <> t2.name
union all
select t1.imageid, t1.id as firstdata, t2.id as seconddata, 'id'
from t1 join
t2
on t1.imageid = t2.imageid
where t1.id <> t2.id;
Actually I Have 136 columns and I want query to be optimized.

SQL Server join two columns both ID of a table with another table's primary Key Column

I was wondering if there is an easy way of joining these two tables.
Table1
Name FromCountryID ToCountryID
------------------------------
sam 1 2
lee 3 4
john 2 1
Table2:
CountryID CountryName
1 USA
2 UK
3 Canada
4 Nepal
You need to join the same table twice with different alias names
select t1.name,
fromTab.countryName as FromCountry,
toTab.countryName as ToCountry
from table1 t1
left join table2 fromTab on fromTab.countryId = t1.fromCountryId
left join table2 toTab on toTab.countryId = t1.toCountryId
SELECT * FROM Table1 INNER JOIN Table2 ON Table2.CountryID = Table1.FromCountryID

Querying SQL Server to obtain sum total using CTE and joins

I have a below query that I am trying since yesterday with some 33 records of Employee with employeeId on various conditions:
With CTE
(
select EmployeeId, and other colums with joins and conditions.
)
Now I want to join this query to obtain sum of invoices of each employee from below tables, table1 and table2.
table1 has employeeid so as my CTE has employeeid I can join it with table1
With CTE
(
select EmployeeId, and other colums with joins and conditions.
)
select *, table1.invoiceId
from CTE
left join table1 on table1.employeeid = CTE.employeeId
left join table2 on table2.invoiceid = table1.invoiceid
groupby
But my table1 only have invoices and for each such invoice there are amount spend in other table i.e table2. table2 has column "amount" that I need to sum up depending upon invoiceid.
For more clarity I am writing the table structure or output as below.
I am trying like above but they are not showing correct results
Assume CTE has
Emplyeeid empName Empaddress empcode
1 john America 121
2 sandy America 122
Now table1 has
InvoiceId EmployeeId RecordId PAyeeid
1 1 223 202
2 1 222 212
3 1 121 378
4 2 229 987
5 2 345 333
table2 has the coulmm amount that we need for each invoice of epmloyee
now table2
InvLine Invoiceid Amount
1 1 30
2 1 30
3 1 20
4 2 10
5 2 10
6 2 10
The output should be as per employe john has two invoices in table1 ie with Id 1 and 2, and for 1 and 2 invoiceds there are amounts that need to be add up
Emplyeeid empName Empaddress empcode Amount
1 john America 121 80
With CTE
(
select EmployeeId, and other colums with joins and conditions.
)
With CTE1
(
select EmployeeId,table1.invoiceid
from cte
left join table1 on table1.employeeid=CTE.employeeId
)
select sum(amount), cte1.employeeId from CTE1
left join table2 on table2.invoiceid = cte1.invoiceid
group by cte1.employeeId
but you can join the table1 in the first cte itself. There is no need to go for second cte if the first cte is simple one.

Resources