SQL - Count two columns together - ms-access

I'm trying to count multiple columns as one column. For example:
Table 1 (Books):
ID
BookName
Genre
SubGenre
1
Name1
1
3
2
Name2
2
1
3
Name3
4
2
Table 2 (Genre):
ID
Genre
1
Horror
2
Drama
3
Romance
4
Sci-Fi
I want to be able to count the genre and subgenre as one to create a table of:
Result:
Genre
Count
Horror
2
Drama
2
Romance
1
Sci-Fi
1
Any help would be really appreciated. Thanks

Try below query-
SELECT t.Genre, Sum(t.cg) AS Count
FROM (
SELECT t2.Genre, Count(t1.Genre) AS cg
FROM Table2 as t2 LEFT JOIN Table1 as t1 ON t2.ID = t1.Genre
GROUP BY t2.Genre
UNION ALL
SELECT t2.Genre, Count(t1.SubGenre) AS cg
FROM Table2 as t2 LEFT JOIN Table1 as t1 ON t2.ID = t1.SubGenre
GROUP BY t2.Genre
) as t GROUP BY t.Genre;

Related

SQL Union and Merge Subset Tables

T1
SCHOOL STUDENT TEACHER
1 1 A
1 2 B
1 3 B
1 4 B
T2
SCHOOL STUDENT TEACHER
2 7 A
2 6 A
2 8 B
2 9 B
T3
SCHOOL TEACHER ID
1 A FOX
1 B CAT
2 A DOG
2 B MOUSE
T4
SCHOOL STUDENT TEACHER ID
1 1 A FOX
1 2 B CAT
1 3 B CAT
1 4 B CAT
2 7 A DOG
2 6 A DOG
2 8 B MOUSE
2 9 B MOUSE
I have T1 T2 T3 where I wish to UNION T1 and T2 and afterwards JOIN T3 such as:
SELECT * FROM T1
UNION
(SELECT * FROM T2)
JOIN
(SELECT SCHOOL, TEACHER, ID
WHERE SCHOOL != 10
FROM T3)
BUT I receive error "UNEXPECTED JOIN"
JOIN is a part of a SELECT statement while UNION are not.
So, you need to make UNION as a part of a SELECT statement
select * from (
SELECT * FROM T1
UNION
SELECT * FROM T2) q1
JOIN
(SELECT SCHOOL, TEACHER, ID
FROM T3
WHERE SCHOOL != 10) q2
on /* here goes JOIN condition you need, like q1.school = q2.school*/
Please note:
there is another syntax error in your example: FROM goes always after SELECT and before WHERE
UNION will append result and eliminate duplicate rows which can run slower than UNION ALL. The latter will not check whether there are duplicate rows. So, if you're sure there won't be any duplicates in the result or if it's irrelevant whether you get duplicates or not, you may use UNION ALL

Left join with other table with where clause

I have 2 tables in MySQL database that I would like to join, where I would like to contain all results from table1.
My tables looks like this:
table1
id
name
1
name1
2
name2
3
name3
4
name4
5
name5
6
name6
7
name7
8
name8
table2
id
table1_id
myfield
1
3
test1
2
2
test2
3
1
test1
4
4
test2
5
5
null
6
2
null
What I am trying to achieve is to get a table which contains all the rows from table1 and only the data that is joined from table2.
This is my query:
select * from table1 as t1
left join table2 as t2 on t1.id = t2.table1_id
where myfield="test1"
group by t1.id
But this is not what I want to achieve.
What I want to achieve is to get all records from table1 and to have all related records from table2 where table2.myfield="test1". And for the other for table2.mytable to have null (if they do not fulfil table2.myfield="test1").
Any help is much appreciated!
Thanks!
move the where clause to the on clause:
select * from table1 as t1
left join table2 as t2 on t1.id = t2.table1_id
and myfield="test1"
group by t1.id
BTW: some DBMS does not allow select * with group by. So select the id and some aggregated values or remove group by id

join table for each variable

lets say i have 2 tables:
table 1:
PersonID PersonName
1 Micheal
2 Edward
3. Nord
4. Stephanie
Table 2
PurchaseID PurchaseItem. PersonID
1 Rack 1
2 Desk 1
3. Lamp 2
4. Table 3
with standard join, query result can return
1 Micheal Rack
2. Micheal Desk
3 Edward Lamp
4 Nord Table
but i need the result to be shown as:
1 Micheal Rack, Desk
2 Edward Lamp
3 Nord Table
You could group by the PersonName and use group_concat to concatenate the PurchaseItems:
SELECT t1.PersonId, PersonName, GROUP_CONCAT(PurchaseItem, ', ')
FROM t1
JOIN t2 ON t1.PersonId = t2.PersonId
GROUP BY t1.PersonId, PersonName
select t1.personid, t1.personname,
group_concat(t2.PurchaseItem)
from t1
join t2 on t1.personid = t2.personid
group by t1.personid, t1.personname
You can try something like this:
Select t1.personid, t1.personname, group_concat(t2.purchase_item)
from table1 t1 join table2 t2
on t1.personId = t2.personId
group by personID;

mysql count children of selected rows having selected rows as parent?

Table structure(representative)
ID NAME PARENT
--------------------
1 cat1 0
2 cat1 1
3 cat2 1
4 cat1 2
5 cat2 2
6 cat3 2
7 cat1 3
8 cat2 3
9 cat3 3
10 cat1 1
FOREIGN TABLE data for foreign_sub_category_count
id_parent name
-----------------------
2 a
2 b
2 c
3 a
3 b
3 c
categories may have sub categories.
SELECT t.name,t.id
FROM TABLE_NAME AS t
WHERE t.parent = SOME_ID
SOME_ID = 1
gives me the name,id of all categories with SOME_ID parent id
what i want is to get a count of all sub categories of each row in above result set besides the name
WHERE t.id is parent of sub categories and get count of categories from another table which has the same t.id as parent
EXPECTED RESULT
t.id t.name sub_category_count foreign_sub_category_count
2 cat1 3 3
3 cat2 3 3
10 cat1 0 0
I suspect that you are looking for a recursive query - available in MySQL 8.0:
with recursive cte as (
select id root_id, id from mytable
union all
select c.root_id, t.id from cte c inner join mytable t on t.parent = c.id
)
select
t.*,
(select count(*) - 1 from cte c where c.root_id = t.id) no_children
from mytable t
This adds one column to your original table, which contains the number of direct and indirect descendants of the current row.
Try this:
select
tab1.id,
tab1.name,
coalesce(tab2.counts,0) as sub_category_count,
coalesce(tab3.counts,0) as foreign_sub_category_count
from
(select id,name,parent from representative) tab1
left join
(select t1.parent tab_id, count(*) as counts from representative t1 inner join representative t2 on t1.parent=t2.id group by t1.parent) tab2
on tab1.id=tab2.tab_id
left join
(select parent_id,count(*) as counts from foreign_table group by parent_id) tab3
on tab1.id=tab3.parent_id
where tab1.parent=1 --SOME_ID
you can change the parent_id in where tab1.parent=1 of you choice
Example on DB-FIDDLE

Select all IDs that have that value in MySQL

I have these two tables:
Table 1:
ID ITEM
--------------------
1 AB
1 22S
1 AB
2 F45
2 BB
3 1
3 1
3 AA
3 F45
3 F67
3 A
......
Table 2:
ITEM COUNTRY
---------------
0 usa
1 italy
2 mexico
3 mexico
A greece
AA malta
AB usa
AC pakistan
B uk
BB france
BA france
BB russia
F45 uk
And I use the following query to get the Items that are like "A%" for each ID:
SELECT GROUP_CONCAT(ITEM)
FROM (SELECT Table1.ID, Table1.ITEM, Table1.date
FROM Table2 Table2 INNER JOIN Table1 Table1
ON (Table2.ITEM = Table1.ITEM) WHERE table2.ITEM like "A%"
ORDER BY Table1.id, Table1.date, Table2.ITEM) as temp
GROUP BY ID
ORDER BY ID
I want to find a way to edit my query so I can get the above only for the IDs that happen to have item "F45" in them (but I don't want to use this item, just select the IDs that have it)..
Any help will be appreciated
I guess
SELECT GROUP_CONCAT(ITEM) AS group_item
FROM (SELECT Table1.ID, Table1.ITEM, Table1.date
FROM Table2 Table2 INNER JOIN Table1 Table1
ON (Table2.ITEM = Table1.ITEM) WHERE table2.ITEM like "A%"
ORDER BY Table1.id, Table1.date, Table2.ITEM) as temp
GROUP BY ID
HAVING group_item LIKE '%F45%'
ORDER BY ID
Or adjust the subquery to select only needed rows (here I'm not sure what are you trying to achieve)