Sql query to get counts by quarterly and half yearly

---- Quarterly 
SELECT
    CASE
        WHEN MONTH(date_column) IN (1, 2, 3) THEN 'Q1'
        WHEN MONTH(date_column) IN (4, 5, 6) THEN 'Q2'
        WHEN MONTH(date_column) IN (7, 8, 9) THEN 'Q3'
        WHEN MONTH(date_column) IN (10, 11, 12) THEN 'Q4'
    END AS quarter,
    CONCAT(YEAR(date_column),
        CASE
            WHEN MONTH(date_column) IN (1, 2, 3, 4, 5, 6) THEN '-H1'
            WHEN MONTH(date_column) IN (7, 8, 9, 10, 11, 12) THEN '-H2'
        END) AS half_year,
    COUNT(*) AS count
FROM your_table
GROUP BY quarter, half_year
ORDER BY quarter, half_year;

--- Half Yearly
 SELECT YEAR(date_column),
   CASE
            WHEN MONTH(date_column) IN (1, 2, 3, 4, 5, 6) THEN 'H1'
            WHEN MONTH(date_column) IN (7, 8, 9, 10, 11, 12) THEN 'H2'
        END AS half_year,
    COUNT(*) AS count
FROM your_table
GROUP BY  half_year
ORDER BY  half_year;


Replace `date_column` with the column in your table that contains the date information and `your_table` with the name of your table. This query will group the counts by quarters and half years based on the date column in your table.

Comments

Popular posts from this blog

How to Convert Word Document to PDF using C#

How to Get First Day and Last Day of a Current Quarter in SQL Server