How to get results count in SQL - mysql

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

Related

Remove the line with the same name but different value

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;

How to union two tables with group values?

How to union two tables with group values?
My query:
SELECT
employee.employee_name,
count(employee.employee_name) as count,
sum(table1.position) as totalshares
FROM
erom_kmch.table1
LEFT OUTER JOIN
erom.employee
ON employee.bene_type_table1=table1.bene_type
AND employee.bene_stype_table1=table1.bene_stype
WHERE
table1.date = '2016-04-15'
group by
employee.employee_name
UNION
SELECT
employee.employee_name,
count(employee.employee_name) as count,
sum(table2.shares) as totalshares
FROM
erom_kmch.table2
LEFT OUTER JOIN
erom.employee
ON employee.type_table2=table2.type
AND employee.bo_substat_table2=table2.bo_substat
WHERE
table2.date = '2016-04-15'
group by
employee.employee_name
Returns:
employee_name count totalshares
NULL 0 0 21967
Clearing Member 9 9 1386
devloper-php 4 4 1984
devloper-.net 46 46 410713
devloper-.android 2
NULL 4056461 0 117154
devloper-.C#php 2 5 31618309
devloper-.net
Resident Individual 939 25 361020 22762
But I need the output like this:
employee_name count totalshares
NULL 0 139121
Clearing Member 15 5355
devloper-php 9 2293
devloper-.net 46 433475
devloper-.android 2 4056461
devloper-.C# 2 31618
Resident Individual 3668 2662925
Individual- Director 1 100
I am joining and union two tables and I need to group the values of two tables. I am getting two output, I need the sum and count of two tables as one output.
You should use Subquery:
SELECT
[YourField]
, [YourAggregate]
FROM
(SELECT
table1.holder
, table1.bene_type
, table1.bene_stype
, employee.employee_name
, COUNT(employee.employee_name) AS [count]
, SUM(table1.position) AS totalshares
FROM
erom_kmch.table1
LEFT OUTER JOIN
erom.employee
ON employee.bene_type_table1=table1.bene_type AND employee.bene_stype_table1=table1.bene_stype
WHERE
table1.date = '2016-04-15'
GROUP BY
employee.employee_name
UNION
SELECT
table2.cust_name
, table2.type
, table2.bo_substat
, employee.employee_name
, COUNT(employee.employee_name) as [count]
, SUM(table2.shares) as totalshares
FROM
erom_kmch.table2
LEFT OUTER JOIN
erom.employee
ON employee.type_table2=table2.type AND employee.bo_substat_table2=table2.bo_substat
WHERE
table2.date = '2016-04-15'
GROUP BY employee.employee_name
) AS A
GROUP BY
[YourField]
NOTE: You should also make the query easy to read.

Mysql group_concat for count in sub query using parent filed is not allowed

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

SQL - How to calculate column value and join with another table

As I am not good with MySQL query's so I wish someone help me for creating this kind of sql query.
I having two MySQL tables which is describe bellow:
Table Name: rating
-------------------
property_id user_id area_rate_count safety_rate_count friendly_rate_count walkability_rate_count
4 28 1 1 1 2
5 38 2 3 4 1
5 40 2 2 3 1
6 40 2 3 1 4
10 43 2 2 3 1
Table Name: listing
-------------------
property_id title
4 Sample 1
5 Sample 2
6 Sample 3
10 Sample 4
11 Sample 5
12 Sample 6
Now first I want to sum each column and divide. (area_rate_count, safety_rate_count, friendly_rate_count, walkability_rate_count). For example In property_id:5 having two times so first calculate column sum and divide by 2.
After calculation we will get this output:
Table Name: rating (After Calculation)
--------------------------------------
property_id rate
4 5
5 9 (Divided by 2 because this property_id is two times in table)
6 10
10 8
And Finally I want join this result to my listing table and result looks something like this:
Table Name: listing
-------------------
property_id title rate
4 Sample 1 5
5 Sample 2 9 (Divided by 2 becouse property_id is two times in table)
6 Sample 3 10
10 Sample 4 8
11 Sample 5 0
12 Sample 6 0
Thanks.
I think you want the avg() aggregation function along with a join:
select l.property_id, l.title,
coalesce(avg(area_rate_count + safety_rate_count + friendly_rate_count + walkability_rate_count
), 0) as rate
from listing l left outer join
property_id p
on l.property_id = p.property_id
group by l.property_id, l.title ;
If I understood it right I think you need this:
select l.property_id, l.title, coalesce(r.ssum/if(r.ct=0,1,r.ct), 0) as rate
from listing l LEFT JOIN
(select property_id,
sum(area_rate_count+safety_rate_count
+friendly_rate_count+walkability_rate_count) ssum,
count(*) ct
from rating
group by property_id ) r
ON l.property_id = r.property_id
order by l.property_id
See it here on fiddle: http://sqlfiddle.com/#!2/589d6/5
Edit
As OP asked on the comments that he wants all columns from listing here is what he want:
select l.*, coalesce(r.ssum/if(r.ct=0,1,r.ct), 0) as rate
from listing l LEFT JOIN
(select property_id,
sum(area_rate_count+safety_rate_count
+friendly_rate_count+walkability_rate_count) ssum,
count(*) ct
from rating
group by property_id ) r
ON l.property_id = r.property_id
order by l.property_id
CREATE TEMPORARY TABLE IF NOT EXISTS
temp_table ( INDEX(col_2) )
ENGINE=MyISAM
AS (
SELECT
property_id,
AVG(area_rate_count) as area_rate_count,
AVG(safety_rate_count) as safety_rate_count,
AVG(friendly_rate_count) as friendly_rate_count,
AVG(walkability_rate_count) as walkability_rate_count
FROM rating
GROUP BY property_id
)
SELECT * FROM listing L
JOIN temp_table T
ON L.property_id = T.property_id
Use the below statement to get distinct property_id with its own rate
select property_id, sum(separaterating)/count(property_id) from (
select property_id,sum(area_rate_count , safety_rate_count , friendly_rate_count , walkability_rate_count) as separaterating from rating group by property_id AS temp ) group by
property_id
you can then join with the other table to get the final result as below
select * from ( select property_id, sum(separaterating)/count(property_id) from (
select property_id,sum(area_rate_count , safety_rate_count , friendly_rate_count , walkability_rate_count) as separaterating from rating group by property_id AS temp ) group by
property_id) AS A inner join listing AS B on A.property_id = B.property_id
try this:
select a.prop_id as property_id, l.title, a.allratings / b.numberofreviews as rate
from
(
select property_id as prop_id, SUM(coalesce(area_rate_count,0) + coalesce(safety_rate_count,0) + coalesce(friendly_rate_count,0) + coalesce(walkability_rate_count,0)) as allratings
from rating
group by property_id
) a inner join
(
select property_id, count(distinct user_id) as numberofreviews
from rating
group by property_id
) b on a.property_id = b.property_id
inner join listing l on a.property_id = l.property_id
Try This Query
select ls.property_id,ls.title,inr.rate from listing as ls
left join
(select r.property_id as pid,r.rate/r.cnt as rate from
(select property_id,user_id,(area_rate_count+safefty_rate_count+friendly_rate_count+walkability_rate_count) as rate,count(*) as cnt from rating group by property_id) as r) as inr on inr.pid=ls.property_id

SQL COUNT & GROUP BY

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