Skip to content
Advertisement

SQL group by (count,sum)

Hello i need help abou SQL a have this table

My table

and i need this result.

Result waned

thanks for your attention and your answers.

select 
to_char(r.date,'yyyy') YEAR,
sum(1),
sum(r.cafe) cafe,
sum(r.drink) drink,
sum(r.tea) tea
from ucet a
join  drinks r on a.IDT=r.IDT
join feat ce on a.IDT=ce.IDT
join dance c on a.ID= c.ID

group by to_char(r.date,'yyyy')

Advertisement

Answer

You can use a sum and group by as below:

SELECT
 DISTINCT YEAR(r.date) year,
 COUNT(DISTINCT(number)) number,
 SUM(r.cafe) cafe,
 SUM(r.drink) drink,
 SUM(r.tea) tea
FROM drinks r
JOIN ucet a on a.IDT = r.IDT
GROUP BY YEAR(r.date)

Please also bear in mind that date is a reserved word for a datatype in many RDBMS systems, as such I would suggest naming your columns as something more appropriate, avoiding reserved words.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement