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
Related
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 an inner join on 2 tables.
Consider
Table1
sid name
1 abc
2 xyz
Table2
sdid sid detailname
1 1 a
2 1 b
3 2 x
So my query looks like below
SELECT *
FROM table1 t1
INNER JOIN table2 t2
ON t1.sid=t2.sid
the result i get is
sid name sdid sid detailname
1 abc 1 1 a
1 abc 2 1 b
2 xyz 3 2 x
I want to modify this query to get the highest 'sdid' from table 2
my end result should look like
sid name sdid sid detailname
1 abc 2 1 b
2 xyz 3 2 x
Include one more subquery in the join to get the max sdid for each sid from table2.
SELECT t1.sid,t1.name,t2.sdid,t2.sid,t2.detailname
FROM table1 t1
INNER JOIN table2 t2 ON t1.sid=t2.sid
INNER JOIN (select max(sdid) as maxsdid, sid from table2 group by sid) t21
ON t21.sid=t2.sid and t21.sdid = t2.sdid
I have table like below one -
Name Null Type
--------- -------- ------------
ID NOT NULL NUMBER
Name VARCHAR2(20)
PARENT_ID NUMBER
Table contents
ID Name PARENT_ID
--------- -------- ------------
1 Ramesh null*
2 Ajay 1
I want to find out best SQL join query where I can populate the results like below.
For each row I want to know the ParentName not ID. How can I do that ?
ID Name ParentName
--------- -------- ------------
1 Ramesh null*
2 Ajay Ramesh
*null or blank
This is not example of the my requirement.
I have tried below SQL with left join but I am not sure if its the proper way.
SELECT S1.ID,S1.CRID AS PARENT_CRID,S2.CRID AS CRID FROM DAJ_JOINS S1
left JOIN DAJ_JOINS S2
ON S1.ID=S2.PARENT_ID
order by id asc;
Your query is close but the joining clause needs to be reversed as
select
t1.ID,
t1.Name,
t2.Name as PARENT_Name
from DAJ_JOINS t1
left join DAJ_JOINS t2 on t1.PARENT_ID = t2.ID
order by t1.ID
With SQL Server 2005+, you can try:
SELECT *
FROM DAJ_JOINS D
OUTER APPLY (SELECT Name As Parent_Name FROM DAJ_JOINS WHERE ID = D.Parent_ID) A
I have three tables: Table1, Table2, Table1Table2Mapping.
Each tabel1 data having multiple data in tabel1, this table1 relation table2 is done through the mapping table the Primary key of the table1 and table2 is put in mapping table.
Table1:
Table1_ID Name ,Other columns
--------- ---- ------
1 Name1
2 Name2
3 Name3
Table2:
Table2_ID Title
--------- -----
101 Title1
102 Title2
103 Title3
104 Title4
105 Title5
Table1Table2Mapping:
Table1_ID Table2_ID
--------- ------------
1 101
1 102
2 103
3 104
3 105
I am getting all the related rows from table1 and its relation in table1 through mappping table
I need to select single row for each Table1 row.
Like this
Table1_ID Name Title
--------- ------- -----
1 Name1 Title1
2 Name2 Title3
3 Name3 Title4
The CROSS APPLY will do the trick:
SELECT
T1.Table1_ID
, T1.Name
, TMP.Title
FROM
Table1 T1
CROSS APPLY (
SELECT TOP(1)
T2.Title
FROM
Table2 T2
INNER JOIN Table1Table2Mapping TTM
ON T2.Table2_ID = TTM.Table2_ID
WHERE
TTM.Table1_ID = T1.Table1_ID
ORDER BY
TTM.TAble2_ID ASC -- You can change this order to what you want
) TMP
You can change the order of the subquery.
SQL Fiddle
Using CROSS APPLY in TechNet
EDIT
IF it is enough, you can use aggregation too:
SELECT
T1.Table1_ID
, T1.Name
, MIN(T2.Title) -- You can use MAX
FROM
Table1 T1
INNER JOIN Table1Table2Mapping TTM
ON TTM.Table1_ID = T1.Table1_ID
INNER JOIN Table2 T2
ON T2.Table2_ID = TTM.Table2_ID
GROUP BY
T1.Table1_ID
, T1.Name
(This gives the same result for the provided dataset, but in real life, the result of this and the previous solution mostly different!
use outer apply in sql server 2005 and later
Select Table1.*, ttt.Title From Table1 Outer Apply
(Select Top 1 Table2.* From Table2 Inner Join Table1Table2Mapping
On Table2.Table2_Id = Table1Table2Mapping.Table2_Id
Where Table1.Table1_Id = Table1Table2Mapping.Table1_Id) ttt
I have one complicated question. I'll try to explain it with example:
have one table that have primary key, and I want to join other table there the first's table primary key is foreign key, and I want If in the second table there is duplicate foreign key to select the number of repeatability. For example:
1-st table:
id name
--- -----
1 Greg
2 Alan
3 George
4 John
5 Peter
2-nd table
id aid data
--- ----- -------
1 2 CCCV
2 2 VVVV
3 3 DDDDD
4 3 SSSS
5 4 PPPPP
I want the result of the join to be:
id(1st table) aid name Data Number
----------- ---- ----- ----- -----
1 null Greg null 1
2 1 Alan CCCV 1
2 2 Alan VVVV 2
3 3 George DDDDD 1
3 4 George SSSS 2
4 5 John PPPPP 1
5 null Peter null 1
I searched a lot, I couldn't find anything. Maybe I do not know how search, or there is no such thing as what I want to do.
SELECT Table1.id, Table2.id as aid, Table1.name, Table2.data,
GREATEST(1, (SELECT COUNT(*)
FROM Table2 t2
WHERE t2.aid = Table1.id
AND t2.id <= Table2.id))
AS number
FROM Table1
LEFT JOIN Table2
ON Table2.aid = Table1.id
ORDER BY id, aid;
works in both MySQL and PostgreSQL.
As per my comment, you've tagged this both MySQL and PostgreSQL.
This answer is for PostgreSQL.
SELECT
table1.id,
table2.aid,
table1.name,
table2.data,
ROW_NUMBER() OVER (PARTITION BY table1.id ORDER BY table2.aid) AS number
FROM
table1
LEFT JOIN
table2
ON table1.id = table2.aid
Queries for PostgreSQL 8.3 which has no window functions.
With bigger tables it is regularly much faster to use a JOIN instead of a correlated sub-query.
The first query aggregates values for Table2 before joining to Table1, which should befaster, too:
SELECT t1.id, t2.aid, t1.name, t2.data, COALESCE(t2.ct, 1) AS number
FROM Table1 t1
LEFT JOIN (
SELECT x.aid, x.data, count(y.aid) + 1 AS ct
FROM Table2 x
LEFT JOIN Table2 y ON x.aid = y.aid AND x.id > y.id
GROUP BY x.aid, x.data
) t2 ON t2.aid = t1.id
ORDER BY t1.id, t2.ct;
And ORDER BY should be fixed.
Alternative without sub-query. Might be faster, yet:
SELECT t1.id, t2.aid, t1.name, t2.data, count(*) + count(t3.id) AS number
FROM Table1 t1
LEFT JOIN Table2 t2 ON t2.aid = t1.id
LEFT JOIN Table2 t3 ON t3.aid = t2.aid AND t3.id < t2.id
GROUP BY t1.id, t2.aid, t1.name, t2.data
ORDER BY t1.id, count(t3.id);
Not sure, didn't test with a bigger set. Test performance with EXPLAIN ANALYZE. Could you report back your results?