Hoe te groeperen op maand vanuit het veld Datum met sql

Hoe kan ik alleen op maand groeperen vanuit een datumveld (en niet groeperen op dag)?

Zo ziet mijn datumveld eruit:

2012-05-01

Hier is mijn huidige SQL:

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

Antwoord 1, autoriteit 100%

Ik zou dit gebruiken:

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;

Dit wordt gegroepeerd op de eerste van elke maand, dus

`DATEADD(MONTH, DATEDIFF(MONTH, 0, '20130128'), 0)` 

geeft '20130101'. Ik geef over het algemeen de voorkeur aan deze methode omdat het datums als datums houdt.

U kunt ook zoiets als dit gebruiken:

SELECT  Closing_Year = DATEPART(YEAR, Closing_Date),
        Closing_Month = DATEPART(MONTH, 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 DATEPART(YEAR, Closing_Date), DATEPART(MONTH, Closing_Date), Category;

Het hangt er echt van af wat je gewenste output is. (Jaar afsluiten is in uw voorbeeld niet nodig, maar als het datumbereik een jaargrens overschrijdt, kan dat wel).


Antwoord 2, autoriteit 33%

Gebruik de functie DATEPART om de maand uit de datum te halen.

Dus je zou zoiets als dit doen:

SELECT DATEPART(month, Closing_Date) AS Closing_Month, COUNT(Status) AS TotalCount
FROM t
GROUP BY DATEPART(month, Closing_Date)

Antwoord 3, autoriteit 13%

Ik heb de functie FORMAT gebruikt om dit te bereiken:

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_Month

Antwoord 4, autoriteit 9%

Door MONTH(date_column) toe te voegen in GROUP 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), Category

Antwoord 5

DATEPART-functie werkt niet op MySQL 5.6, gebruik in plaats daarvan MONTH(‘2018-01-01’)


Antwoord 6

Probeer dit:

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 1

Op deze manier groepeert u op een aaneengeschakelde datumnotatie, samen met een –


Antwoord 7

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;

Antwoord 8

SQL Server 2012-versie hierboven,

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;

Antwoord 9

U kunt dit doen door Year(), Month() Day() en datepart() te gebruiken.

In jouw voorbeeld zou dit zijn:

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), Category

Antwoord 10

Probeer de volgende code

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;

LEAVE A REPLY

Please enter your comment!
Please enter your name here

12 + two =

Other episodes