I have this table
---ID----NAME----INVITED_BY---
1 A
2 B 1
3 C 1
4 D 2
all I want is to get the result:
---ID----NAME------INVITES---------
1 A 2 (COUNT OF INVITED_BY)
2 B 1
3 C 0
4 D 0
You could do a self join to count the number of persons invited:
select yt.id
, yt.name
, count(distinct inv_by.id) as invites
from YourTable yt
left join
YourTable inv_by
on yt.id = inv_by.invited_by
group by
yt.id
, yt.name
Related
So basically I run this code in sql:
SELECT p.Nome as Nome_pub, a.Nome as Nome_area, COUNT(*) as total
FROM publicacao p, emprestimo e, area_tematica a
WHERE p.Id=e.Publicacao_Id and p.Area_Tematica_Id=a.Id and (Data_hora>='2021-01-01' AND Data_de_devolucao<='2021-06-31')
GROUP by p.Nome
ORDER BY Nome_area;
and I get the following output
Nome_pub
Nome_area
total
name1
a
1
name2
b
1
name3
c
1
name4
d
3
name5
d
2
name6
d
2
name7
e
1
but I want an output that gives me the max word3 based on word2 basically: (in my case remove line with "name5" and "name6"
Nome_pub
Nome_area
total
name1
a
1
name2
b
1
name3
c
1
name4
d
3
name7
e
1
is there any way to do this??
Thanks in advance!!
If ROW_NUMBER is available in your version.
SELECT Nome_pub, Nome_area, total
(
SELECT
p.Nome as Nome_pub
, a.Nome as Nome_area
, COUNT(*) as total
, ROW_NUMBER() OVER (PARTITION BY a.Nome ORDER BY COUNT(*) DESC) as RN
FROM publicacao p
JOIN emprestimo e
ON p.Id = e.Publicacao_Id
JOIN area_tematica a
ON a.Id = p.Area_Tematica_Id
WHERE ( Data_hora >= DATE '2021-01-01' AND
Data_de_devolucao < DATE '2021-07-01')
GROUP by p.Nome, a.Nome
) q
WHERE RN = 1
ORDER BY Nome_area;
I have 3 tables like:
owner_details:-
owner_id owner_name
---------------------
1 A
2 B
3 C
-------------------
vehicle_owner:-
v_id vehicle_id owner_id
-------------------------
1 1 1
2 2 2
3 4 1
4 3 1
5 5 3
transaction:-
id v_id amount transaction_type
--------------------------------
1 1 100 0
2 2 250 1
3 1 150 1
4 3 450 1
5 1 200 0
6 4 300 1
7 5 150 0
8 5 200 1
transaction_type= 0 then (-) transaction_type=1 then (+)
Owner A (1) have 3 vehicles with v_id (1,3,4) in table vehicle_owner.
v_id (1,3,4) have 5 entries in table transaction (1,3,4,5,6) with sum of amount 600 (-100+150+450-200+300)
Now I want listing like this:-.
owner_id owner_name amount
---------------------
1 A 600
2 B 250
3 C 50
-------------------
You can use the following query:
SELECT od.owner_id, od.owner_name, SUM(t.amount) AS amount
FROM owner_details od INNER JOIN vehicle_owner vo ON od.owner_id = vo.owner_id
INNER JOIN `transaction` t ON vo.v_id = t.v_id
GROUP BY od.owner_id
If you want to use the additional transaction_type you can use the following:
SELECT od.owner_id, od.owner_name, SUM(CASE WHEN t.transaction_type = 0 THEN t.amount * -1 ELSE t.amount END) AS amount
FROM owner_details od INNER JOIN vehicle_owner vo ON od.owner_id = vo.owner_id
INNER JOIN `transaction` t ON vo.v_id = t.v_id
GROUP BY od.owner_id
demo: http://sqlfiddle.com/#!9/c5f8d/1/1
Try this:
SELECT A.owner_id, A.owner_name, SUM(IFNULL(amount,0)) AMOUNT
FROM owner_details A LEFT JOIN
vehicle_owner B
ON A.owner_id=B.owner_id
LEFT JOIN `transaction` C
ON C.v_id=B.v_id
GROUP BY A.owner_id, A.owner_name;
It works for me
SELECT od.owner_id,
od.owner_name,
Sum(t.amount) AS amount
FROM owner_details od
INNER JOIN vehicle_owner vo
ON od.owner_id = vo.owner_id
INNER JOIN (SELECT v_id,
Coalesce(Sum(CASE
WHEN type = 0 THEN -amount
ELSE +amount
end), 0.0) AS amount
FROM `transaction`
GROUP BY v_id) t
ON vo.v_id = t.v_id
GROUP BY od.owner_id
Thanks Sebastian Brosch for quick response !!!
I need to process album count for each of the country per artist; however, I have a problem once I do group_concat for count in mysql, I search a bit in stackoverflow, I found I have to do sub select for group_concat. The problem is once I do the sub select in from I can not use a.id from the parent from filed table. I got error like following Unknown column 'a.id' in 'where clause'
This is the query:
SELECT a.seq_id, a.id
(SELECT GROUP_CONCAT(cnt) AS cnt FROM (
SELECT CONCAT_WS('-', mgr.country_code, count(mgr.media_id)) AS cnt
FROM music_album_artists AS ma
JOIN media_geo_restrict AS mgr ON ma.album_id = mgr.media_id
WHERE ma.artist_id = a.id
GROUP BY mgr.country_code
) count_table
) AS album_count
FROM music_artist AS a
WHERE a.seq_id > 0 and a.seq_id < 10000
The sample data in tables:
music_artists:
seq_id id name
1 1 Hola
2 2 Vivi
music_album_artists:
id artist_id album_id
1 1 1
2 1 2
3 1 5
4 1 10
5 2 2
6 2 10
6 2 1
media_geo_restrict:
album_id country_code
1 BE
1 CA
1 DE
1 US
2 CH
2 CA
2 CH
5 DE
10 US
The result I would like to have
seq_id id album_count
1 1 BE--1,CA--2,CH--1,DE--1,US--1
2 2 CA--1,US--2,CH--1
Here is what you need:
select seq_id, id, group_concat(concat(country_code, '--', qtd))
from (
select ma.seq_id, ma.id,
mgr.country_code, count(*) qtd
from music_artists ma
inner join music_album_artists maa
on ma.id = maa.artist_id
inner join media_geo_restrict mgr
on maa.album_id = mgr.album_id
where ma.seq_id > 0 and ma.seq_id < 10000
group by ma.seq_id, ma.id, ma.name,
mgr.country_code
) tb
group by seq_id, id
Here is the working sample: http://sqlfiddle.com/#!9/ff8b5/8
Try this and tell me:
SELECT a.seq_id, a.id, GROUP_CONCAT(cnt) AS cnt
FROM music_artist AS a,
(
SELECT ma.artist_id, CONCAT_WS('-', mgr.country_code, count(mgr.media_id)) AS cnt
FROM music_album_artists AS ma
JOIN media_geo_restrict AS mgr ON ma.album_id = mgr.album_id
GROUP BY mgr.country_code
) AS count_table
WHERE a.seq_id > 0 and a.seq_id < 10000
and a.id=count_table.artist_id
group by a.id
I have two tables:
TB_Departament
(
id_dep, nome
1 , RRHH
2 , Security
3 , logistics
)
TB_Incident
(
id_tipo, id_dep
1 , 1
2 , 3
1 , 3
2 , 1
1 , 3
2 , 1
2 , 1
)
If I count the records department get this:
select d.nome, count(i.id_tipo) as Num from TB_Departament d, TB_Incident i
where i.id_dep = d.id_dep
group by d.nome
resp:
RRHH 4
logistics 3
but I need this:
RRHH 4
Security 0
logistics 3
How can i modify the query to get the right answer?
Try:
SELECT d.nome, count(i.id_tipo)
FROM TB_Departament d LEFT JOIN
TB_Incident i
ON d.id_dep = i.id_dep
GROUP BY d.nome
ON MSSQL, this would be as follow, including an order by clause in case you want the results to come out by id_dep #
SELECT x.name,x.Num FROM (
select d.id_dep, d.name AS name, count(i.id_tipo) AS Num from TB_Department d
LEFT OUTER JOIN TB_Incident i
ON i.id_dep= d.id_dep
group by d.name, d.id_dep) AS x
ORDER BY x.id_dep
I've got a budget table:
user_id product_id budget created
-----------------------------------------------------------------
1 1 300 2011-12-01
2 1 400 2011-12-01
1 1 500 2011-12-03
2 2 400 2011-12-04
I've also got a manager_user table, joining a manager with the user
user_id manager_id product_id
------------------------------------
1 5 1
1 9 2
2 5 1
2 5 2
3 5 1
What I'd like to do is grab each of the user that's assigned to Manager #5, and also get their 'budgets'... but only the most recent one.
Right now my statement looks like this:
SELECT * FROM manager_user mu
LEFT JOIN budget b
ON b.user_id = mu.user_id AND b.product_id = mu.product_id
WHERE mu.manager_id = 5
GROUP BY mu.user_id, mu.product_id
ORDER BY b.created DESC;
The problem is it doesn't pull the most recent budget. Any suggestions? Thanks!
To accomplish your task you can do as follows:
select b1.user_id,
b1.budget
from budget b1 inner join (
select b.user_id,
b.product_id,
max(created) lastdate
from budget b
group by b.user_id, b.product_id ) q
on b1.user_id=q.user_id and
b1.product_id=q.product_id and
b1.created=q.lastdate
where b1.user_id in
(select user_id from manager_user where manager_id = 5);
I'm assuming here that your (user_id, product_id, created) combination is unique.
For what it's worth, here's the code that returned what I was looking for:
SELECT DISTINCT(b1.id),mu.user_id,mu.product_id,b1.budget,b1.created
FROM budget b1
INNER JOIN (
SELECT b.user_id, b.product_id, MAX(created) lastdate
FROM budget b
GROUP BY b.user_id, b.product_id) q
ON b1.user_id=q.user_id AND
b1.product_id=q.product_id AND
b1.created=q.lastdate
RIGHT JOIN manager_user mu
ON mu.user_id = b1.user_id AND
mu.product_id = b1.product_id
WHERE mu.manager_id = 5;
Thanks for the help Andrea!