Join three tables in SQL Server 2008 - sql-server

I have three tables.
A
ID NAME
--- ----
1 abc
2 asd
3 qwe
B
ID INCOME
--- ------
1 2
2 3
1 4
C
NAME TOTAL
---- ----
abc 8
asd 20
I want to join this three tables with a SQL query to produce an output like
ID INCOME TOTAL
---------------
1 6 8
2 3 20
Could anyone help me?

You could try this:
select
t1.id, sum(t2.income) income, sum(t3.total) total
from
table1 t1
join
table2 t2 on t1.id = t2.id
join
table3 t3 on t3.name = t1.name
group by
t1.id
order by
t1.id

Related

Display 0 when join is not successful

I am using SQL Server to solve the following problem.
I have 3 tables T1, T2, T3
T1:
ID Name Country
----------------------
1 PR IN
2 AR US
T2:
ID AGE
------------
1 32
2 36
3 40
T3:
ID T1_ID T2_ID Amount
--------------------------------
1 1 1 100
2 1 2 300
Required output
T1.Name T2.Age T3.Amount
---------------------------------
PR 32 100
PR 36 200
PR 40 0
My query is :
select
T1.name, T2.Age, T3.amount
from
T3
join
T1 on T1.id = T3.T1_ID --fixed a typo here
right join
T2 on T2.id = T3.T2_ID
where
T1.id = 1
My current output is:
T1.Name T2.Age T3.Amount
--------------------------------
PR 32 100
PR 36 200
I would really appreciate any help on this
start the FROM statement on T2 and T1 because you want all records for T2 and T1 as a Cartesian product, limited by WHERE
instead of joining T3 in the FROM, using a subselect for T3 allows you to use ISNULL:
SELECT T1.NAME,
T2.age,
ISNULL((SELECT amount
FROM T3
WHERE T1.id = T3.T1_id
AND T2.id = T3.T2_id), 0) AS Amount
FROM T2 CROSS JOIN
T1
WHERE T1.id = 1

Return sum using aggregate function in SQL Server

I have two tables.
Table1 has following data
Id1 Name Comments
--------------------
1 abc hgdhg
2 xyz mnoph
3 ysdfr jkljk
4 asdf iiuoo
5 pqrs liuoo
Table2 has following data
Id2 Id1 count date
-------------------------------
1 1 18 11/16/2005
2 1 1 11/15/2005
3 1 4 11/25/2005
4 2 4 11/22/2005
5 3 8 11/05/2005
6 3 3 11/30/2005
7 4 2 11/29/2005
8 3 0 11/04/2005
9 2 5 11/02/2005
10 3 9 11/22/2005
11 2 15 11/10/2005
12 5 12 11/19/2005
I want to return output as name, comments, sum of all count since 11/10/2005
I am trying the following query(with out date where condition)
select
Name, Comments, sum(count)
from
Table1 T1
join
Table2 T2 on T1.Id1 = T2.Id1
group by
ID1
But it is throwing error
Name is invalid in the select list because it is not contained in either an aggregate function or the Group by clause.
Can anyone help me with query (with the date where condition)? What's wrong with this?
Thanks in advance
You have to add any columns not contained in the aggregate function, and use where to filter the results:
select Name,
Comments,
sum(count)
from Table1 T1 join Table2 T2 on T1.Id1 = T2.Id1
where T2.[date] >= '11/10/2005'
group by Name, Comments
you can use below query
SELECT T1.Name ,
T1.Comments ,
SUM(T2.[count]) AS [count]
FROM Table1 T1
INNER JOIN Table2 T2 ON T1.Id1 = T2.Id1
WHERE CAST(T2.[date] AS DATE) >= CAST('11/10/2005' AS DATE)
GROUP BY T1.Name ,
T1.Comments
Every column in a select statement without an aggregate function needs to be in the group by sentence too to prevent aggregate errors, about limiting the date, use where clause to define the condition, like shows ahead.
select
Name, Comments, sum(count)
from
Table1 T1
join
Table2 T2 on T1.Id1 = T2.Id1
where
date >= '2005-11-10 00:00:00'
group by
Name, Comments

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

SQL Server Average Query

Can somebody help me about how to query the average of this?
I have this table.
teacher | class | students | rating
-----------------------------------
T1 1 001 6
T1 1 002 6
T1 2 003 1
T1 3 004 1
T2 1 001 6
T2 1 002 6
T2 2 003 1
T2 3 004 1
if I do Select teacher, class, avg(rating) from table group by teacher, class then I get this:
teacher | class | rating
------------------------
T1 1 6
T1 2 1
T1 3 1
T2 1 6
T2 2 1
T2 3 1
what I want to do is get the average rating of that result and group it by teacher which is:
teacher | rating
------------------------
T1 2.66667
T2 2.66667
because if i do Select avg(rating) from table group by teacher, I get
teacher | rating
------------------------
T1 3.5
T2 3.5
You can do this:
SELECT teacher,avg(avgrating) as avgrating
FROM
(Select teacher, class, avg(rating) avgrating
from TableName
group by teacher, class) Temp
GROUP BY teacher
Result:
TEACHER AVGRATING
T1 2.666666
T2 2.666666
Sample result in SQL Fiddle.
OR:
SELECT T1.teacher,avg(T2.avgrating) as rating
FROM TableName T1 JOIN
(SELECT teacher,class,avg(rating) as avgrating
FROM TableName T2
GROUP BY teacher,class) T2 ON T1.teacher=T2.teacher
GROUP BY T1.teacher
SQL Fiddle.
use a nested select:
select teacher , avg(avgr) from
(Select teacher, class, avg(rating) avgr from table group by teacher, class) qry
group by teacher

Using Joins to get all data from left table

I have two tables as shown below.
Id Name Id Status
-------- -------------------
1 A 1 Approved
2 B 6 Approved
3 C 4 Pending
4 D 1 Approved
5 E 1 Pending
6 F 3 Pending
5 Rejected
Now this is how I want the output to be:
Id Name Status
-------------------
1 A Pending
2 B
3 C Pending
4 D Pending
5 E
6 F
I have tried using left join but I am getting multiple rows.
select t1.ID,Name,Status from t1 left join t2 on t1.id=t2.id
and if I add where Status=pending i am getting only ids 1 and 3.
Here are the queries i tried:
select distinct t1.id,name,status from t1 left join t2 on t1.id=t2.id (this gives me duplicate records i.e id 1 is occurs twice with approved and pending)
and also
select distinct t1.id,name,status from t1 left join t2 on t1.id=t2.id where t2.status='pending' (gives me only 1,3 and 4)
Could any one help me,
Thanks in advance.
To include all rows from T1 and only those from T2 with status = 'pending' move the condition into the ON clause SQL Fiddle
SELECT t1.ID,
Name,
Status
FROM t1
LEFT JOIN t2
ON t1.id = t2.id
AND t2.status = 'pending'
To only return one row per id in the event of dupes you could do (Fiddle)
WITH CTE AS
(
SELECT DISTINCT id, Status
FROM T2
WHERE Status = 'Approved'
)
SELECT t1.ID,
Name,
Status
FROM t1
LEFT JOIN CTE
ON t1.id = CTE.id

Resources