I have 2 tables like this:
Table_1 Table_2
--------------- -------------------
id bk_title bk_no bk_isbn
--------------- ---------------------
1 A_Book 1 ISBN0001
2 B_Book 1 ISBN0002
2 ISBN0003
I want to fetch records as:
BK_Title Num_Copies
----------------------------
A_Book 2
B_Book 1
Any SQL example would be of great help for me.
This is a simple INNER JOIN and GROUP BY with a COUNT:
Select T1.bk_title, Count(*) As Num_Copies
From Table_1 T1
Join Table_2 T2 On T1.id = T2.bk_no
Group by T1.bk_title
I have 3 tables just like:-
Table_1 Table_2 Table_3
------------------------ -------------------- --------------------
id bk_title strm_id bk_no bk_isbn s_id strm_name
----------------------- --------------------- -----------------------
1 A_Book 3 1 ISBN0001 3 Science
2 B_Book 4 1 ISBN0002 4 History
2 ISBN0003
I want to fetch records as
BK_Title Num_Copies Stream
---------------------------------------
A_Book 2 Science
B_Book 1 History
How do i do so.Please advice.
Try this:
SELECT BK_Title, COUNT(Table_2.bk_isbn) AS Num_Copies, Table_2.strm_name AS Stream
FROM Table_1
JOIN Table_2 on (Table_1.id = Table_2.bk_no)
JOIN Table_3 on (Table_1.strm_id = s_id)
GROUP BY BK_Title, strm_name
ORDER BY BK_Title
I have two tables. I want to combine this tables to get the results like below:
Table_1
RegNo Class_id Name Address
------------------------------------------------
ABC/R/13-14-1 1 Name1 Address1
ABC/R/13-14-2 2 Name1 Address1
ABC/R/2014-15-1 1 Name1 Address1
ABC/R/2014-15-2 3 Name1 Address1
ABC/R/13-14-3 1 Name1 Address1
------------------------------------------------
Table_2
Class_id Class
----------------------
1 IA
2 IB
3 IC
----------------------
i need the result like:
Class 2013 2014
--------------------
IA 2 1
IB 1 0
IC 0 1
--------------------
I am using Group by to get the results in a separately for each year, but I am not able to get the year in column-wise format like the requirement. Any help is highly appreciated.
Thanks in advance.
If condition you used in your query works for your data then you can use this query:
select t2.class,
sum(case when regno like 'ABC%13-14%' then 1 else 0 end) c2013,
sum(case when regno like 'ABC%14-15%' then 1 else 0 end) c2014
from table_1 t1
join table_2 t2 on t1.class_id = t2.class_id
group by t2.class
Output:
CLASS C2013 C2014
----- ---------- ----------
IA 2 1
IB 1 0
IC 0 1
Table 1 (T1)
VchID CustomerID Amt
1 ---- 1 ---- 100.00
2 ---- 1 ---- 200.00
3 ---- 2 ---- 250.00
2 ---- NULL ---- 200.00
Table2 (T2)
CustID CustomerName
1 ---- ABC
2 ---- XYZ
I want the following result :
VchID CustomerName
1 ---- ABC
2 ---- ABC
3 ---- XYZ
2 ---- NULL
When i use the following query, the Null row is left out. How to include that.
Select T1.VchID, T2.CustomerName
FROM Table1 T1, Table2 T2
WHERE T1.CustomerID = T2.CustID
Any Suggestions?
Use an outer join instead:
SELECT T1.VchID, T2.CustomerName
FROM Table1 T1
LEFT JOIN Table2 T2 ON T1.CustomerID = T2.CustID
SQL Fiddle Demo
A Visual Explanation of SQL Joins
Hi I am having a problems using Group By and joins between 3 tables.
I have a project table with various fields and a projectcode fields. I then have an invoice table and an hours table and each can have multiple rows per project. Both of these table have project code also.
The two SUM values are not calculating correctly and I am realy struggling to see where the issue is.
Here the sql I am using:
SELECT dbo.project.projectcode,
dbo.project.client,
dbo.project.project,
dbo.project.budget,
dbo.project.budget * 80 AS value,
SUM(dbo.harvest.hours) AS hourslogged,
SUM(dbo.salesforce.value) AS invoiced
FROM dbo.salesforce
RIGHT OUTER JOIN dbo.project
ON dbo.salesforce.projectcode = dbo.project.projectcode
LEFT OUTER JOIN dbo.harvest
ON dbo.project.projectcode = dbo.harvest.projectcode
GROUP BY dbo.project.projectcode,
dbo.salesforce.projectcode,
dbo.harvest.projectcode,
dbo.project.project,
dbo.project.client,
dbo.project.budget
Any help or tips on this would be much appreciated!
Whenever each of the two tables, dbo.salesforce and dbo.harvest, have more than 1 match for every projectcode, a mini-Cartesian product happens. Here's a simple illustration. Suppose there are tables A and B, like this:
Table A:
AID AVALUE
--- -------
1 ValueA1
2 ValueA2
Table B:
BID BVALUE AID
--- ------- ---
1 ValueB1 1
2 ValueB2 1
3 ValueB3 2
Now if we performed this join:
SELECT * FROM A JOIN B ON A.AID = B.AID
the result would be:
AID AVALUE BID BVALUE AID
--- ------- --- ------- ---
1 ValueA1 1 ValueB1 1
1 ValueA1 2 ValueB2 1
2 ValueA2 3 ValueB3 2
Enter table C:
CID CVALUE AID
--- ------- ---
1 ValueC1 1
2 ValueC2 1
3 ValueC3 1
And the join now is this:
SELECT * FROM A JOIN B ON A.AID = B.AID JOIN C ON A.AID = C.AID
What would be the result? Here:
AID AVALUE BID BVALUE AID CID CVALUE AID
--- ------- --- ------- --- --- ------- ---
1 ValueA1 1 ValueB1 1 1 ValueC1 1
1 ValueA1 1 ValueB1 1 2 ValueC2 1
1 ValueA1 1 ValueB1 1 3 ValueC3 1
1 ValueA1 2 ValueB2 1 1 ValueC3 1
1 ValueA1 2 ValueB2 1 2 ValueC3 1
1 ValueA1 2 ValueB2 1 3 ValueC3 1
As you can see, every match from B is repeated three times, for how many matches C has got. And, similarly, every match from C is repeated twice, because that is how many matches there are in B. The 'luckiest', of course, is the row from A, because it is repeated 2 × 3 = 6 times. That is a Cartesian join for you. And that's just what happens in your case too.
Not sure whether it is considered typical, but in such cases I would often group each table separately by the joining expression(s), then join the result sets. Your query would then look like this:
SELECT
p.projectcode,
p.client,
p.project,
p.budget,
p.budget * 80 AS value,
h.hourslogged,
s.invoiced
FROM dbo.project p
LEFT JOIN (
SELECT
projectcode,
SUM(dbo.salesforce.value) AS invoiced
FROM dbo.salesforce
GROUP BY projectcode
) s ON p.projectcode = s.projectcode
LEFT JOIN (
SELECT
projectcode,
SUM(dbo.harvest.hours) AS hourslogged
FROM dbo.harvest
GROUP BY projectcode
) h ON p.projectcode = h.projectcode
I'd suggest to avoid mixing right and left outer join.
Your central table is Project, so use it first.
SELECT dbo.project.projectcode,
dbo.project.client,
dbo.project.project,
dbo.project.budget,
dbo.project.budget * 80 AS value,
SUM(dbo.harvest.hours) AS hourslogged,
SUM(dbo.salesforce.value) AS invoiced
FROM dbo.project
LEFT OUTER JOIN dbo.salesforce
ON dbo.salesforce.projectcode = dbo.project.projectcode
LEFT OUTER JOIN dbo.harvest
ON dbo.project.projectcode = dbo.harvest.projectcode
GROUP BY dbo.project.projectcode,
dbo.project.project,
dbo.project.client,
dbo.project.budget
But the error come from the GROUP BY. You don't have to group by the two tables on which you are doing the aggregate, else your aggregate will not be good !