I have the following inconvenience with SQL.
I have to list the contents reproduced more than 3 times in each month of 2018 and less than 2 in the months that go of the 2019.
The structure for that is the following:
CREATE TABLE content (
content INT NOT NULL,
CONSTRAINT PK_Codcontent PRIMARY KEY(Codcontent)
)
CREATE TABLE Reproduced (
Coduser INT NOT NULL,
Codcontent INT NOT NULL,
CONSTRAINT PK_Coduser PRIMARY KEY(Coduser, Codcontent),
CONSTRAINT FK_Codusr FOREIGN KEY(Coduser) REFERENCES Perfil(Codpuser),
CONSTRAINT FK_Codcont FOREIGN KEY(Codcontent) REFERENCES Contenido(Codcontent)
)
I have created the following view to list the contents, reproductions and months of the whole year, but the thing is that I do not know how to filter those with more than 3 reproductions for each month.
CREATE VIEW TODOSLOSMESES2018
AS
SELECT
R.Codcontenido
, count(*)Reproducciones
, count(Distinct Month(R.fecha)) as 'Meses'
FROM (
SELECT *
FROM Reproduce
WHERE fecha>='20180101' AND fecha<='20181231')R
group by Codcontenido
HAVING count(Distinct Month(R.fecha))=12
Monthin theGroup byand change theHavingtoCount(*) Reproducciones > 3