¿Cómo puedo agrupar solo por mes desde un campo de fecha (y no agrupar por día)?
Así es como se ve mi campo de fecha:
2012-05-01
Aquí está mi SQL actual:
select Closing_Date, Category, COUNT(Status)TotalCount from MyTable
where Closing_Date >= '2012-02-01' and Closing_Date <= '2012-12-31'
and Defect_Status1 is not null
group by Closing_Date, Category
sql
sql-server
tsql
usuario1858332
fuente
fuente

SELECT STUFF(SUBSTRING(CONVERT(VARCHAR, CURRENT_TIMESTAMP, 6), 4, 6), 4, 1, '-');Closing_Date =es solo el alias de la columna, es lo mismo que tenerAS Closing_Datedespués de la expresión. Es completamente subjetivo pero personalmente encuentro laalias =notación mucho más fácil de leer queAS Alias. Para obtener más información sobre por qué lo prefiero, lea este artículo de Aaron Bertrand.Utilice la función DATEPART para extraer el mes de la fecha.
Entonces harías algo como esto:
SELECT DATEPART(month, Closing_Date) AS Closing_Month, COUNT(Status) AS TotalCount FROM t GROUP BY DATEPART(month, Closing_Date)fuente
He utilizado el FORMATO función de lograr esto:
select FORMAT(Closing_Date, 'yyyy_MM') AS Closing_Month , count(*) cc FROM MyTable WHERE Defect_Status1 IS NOT NULL AND Closing_Date >= '2011-12-01' AND Closing_Date < '2016-07-01' GROUP BY FORMAT(Closing_Date, 'yyyy_MM') ORDER BY Closing_Monthfuente
Por adición
MONTH(date_column)deGROUP BY.SELECT Closing_Date, Category, COUNT(Status)TotalCount FROM MyTable WHERE Closing_Date >= '2012-02-01' AND Closing_Date <= '2012-12-31' AND Defect_Status1 IS NOT NULL GROUP BY MONTH(Closing_Date), Categoryfuente
La función DATEPART no funciona en MySQL 5.6, en su lugar use MONTH ('2018-01-01')
fuente
Prueba esto:
select min(closing_date), date_part('month',closing_date) || '-' || date_part('year',closing_date) AS month, Category, COUNT(Status)TotalCount FROM MyTable where Closing_Date >= '2012-02-01' AND Closing_Date <= '2012-12-31' AND Defect_Status1 is not null GROUP BY month, Category, ORDER BY 1De esta manera, está agrupando por un formato de fecha concatenado, unido por un -
fuente
SELECT to_char(Closing_Date,'MM'), Category, COUNT(Status) TotalCount FROM MyTable WHERE Closing_Date >= '2012-02-01' AND Closing_Date <= '2012-12-31' AND Defect_Status1 IS NOT NULL GROUP BY Category;fuente
Versión de SQL Server 2012 anterior,
SELECT format(Closing_Date,'yyyy-MM') as ClosingMonth, Category, COUNT(Status) TotalCount FROM MyTable WHERE Closing_Date >= '2012-02-01' AND Closing_Date <= '2012-12-31' AND Defect_Status1 IS NOT NULL GROUP BY format(Closing_Date,'yyyy-MM'), Category;fuente
Puede hacer esto usando Year (), Month () Day () y datepart ().
En su ejemplo, esto sería:
select Closing_Date, Category, COUNT(Status)TotalCount from MyTable where Closing_Date >= '2012-02-01' and Closing_Date <= '2012-12-31' and Defect_Status1 is not null group by Year(Closing_Date), Month(Closing_Date), Categoryfuente
Pruebe el siguiente código
SELECT Closing_Date = DATEADD(MONTH, DATEDIFF(MONTH, 0, Closing_Date), 0), Category, COUNT(Status) TotalCount FROM MyTable WHERE Closing_Date >= '2012-02-01' AND Closing_Date <= '2012-12-31' AND Defect_Status1 IS NOT NULL GROUP BY DATEADD(MONTH, DATEDIFF(MONTH, 0, Closing_Date), 0), Category;fuente