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
Related
I have two SQL tables
TABLE A
id | user | embedding
-----------------------
1 Ram [.12,.56]
2 Shyam [.23,.24]
3 Ghanshyam [.23,.39]
4 Balram [.34,.39]
TABLE B
--------------------
id | users
--------------------
1 [Ram,Shyam]
2 [Ram,Ghanshyam]
3 [Ram, Balram]
And I want to have a query that will return essentially table B but with the users replaced by their embeddings.
Desired output
-----------------------------
id | users
-----------------------------
1 [[.12,.56],[.23.,.24]]
2 [[.12,.56],[.23,.39]]
3 [[.12,.56], [.34,.39]]
How can I do this?
how about using unnest and array_agg:
select b.id , array_agg(embedding)
from TableB b
cross join unnest(b.users) c(user)
join TableA a
on c.users = a.user
group by b.Id
I have database where 2 roles can't be associated with each other, and I need to display any users who have conflicting roles.
For example: an (id 2) accountant can't also be a (id 5) trainer
this has to be done without using CTE's
Table a Table b table c
--------------- ------------------- ------------
userID | roleID roleID | conflictID roleID | Role Name
1 2 2 5 1 chef
1 3 2 accountant
1 5 3 driver
2 3 4 barmaid
2 1 5 trainer
3 2
3 3
the result should contain only the userID who has both roles 2 and 5
userID
------
1
Join the b table with the a table twice, to get userID's with conflicting combinations:
select distinct a1.userid
from tableb b
join tablea a1 on b.roleID = a1.roleID
join tablea a2 on b.conflictID = a2.roleID
and a1.userID = a2.userID
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 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
I have a mapping table tableA
key value
-----------
1 "John"
2 "George"
3 "Kate"
4 "loves"
5 "hates"
and another tableB that contains rows based on keys of tableA
col1 col2 col3
------------------
1 5 2
2 4 3
3 4 1
I want to write a selection query which will return rows from table B but replaced with their appropriate values.
e.g. the output has to be:
John | hates | George
George | loves | Kate
Kate | loves | John
Thank you.
SELECT A1.value, A2.value, A3.value
FROM tableB
JOIN tableA as A1 ON tableB.col1 = A1.key
JOIN tableA as A2 ON tableB.col2 = A2.key
JOIN tableA as A3 ON tableB.col3 = A3.key;
You should probably put the last 2 items, 'loves' and 'hates', into a separate table as they represent a different type of data than the other 3.
Here are the tables:
users:
id name
----------
1 John
2 Edward
3 Kate
feelings:
id type
----------
1 love
2 hate
feelings_users:
id user1_id feeling_id user2_id
-----------------------------------
1 1 2 2
2 2 1 3
3 3 1 1
and here's the query:
select `Ua`.`name`, `F`.`type`, `Ub`.`name` from `feelings_users` as `X`
left join `users` as `Ua` on `X`.`user1_id` = `Ua`.`id`
left join `feelings` as `F` on `X`.`feeling_id` = `F`.`id`
left join `users` as `Ub` on `X`.`user2_id` = `Ub`.`id`
and it will output:
name type name
--------------------
John hate Edward
Edward love Kate
Kate love John