To compare the total of approved and rejected publications in each year using SQL Server, you can use a GROUP BY clause to group the records by year and publication status, then use COUNT() to calculate the totals for each status within each year. Assuming you have a table called publications with columns publication_date (or year), and status (with values like 'approved' or 'rejected'), the query would look like this:
SELECT
YEAR(publication_date) AS publication_year,
SUM(CASE WHEN status = 'approved' THEN 1 ELSE 0 END) AS approved_count,
SUM(CASE WHEN status = 'rejected' THEN 1 ELSE 0 END) AS rejected_count
FROM
publications
GROUP BY
YEAR(publication_date)
ORDER BY
publication_year;
Explanation:
YEAR(publication_date): Extracts the year from thepublication_datefield. If you already have ayearcolumn, replaceYEAR(publication_date)withyear.CASE WHEN status = 'approved' THEN 1 ELSE 0 END: This condition checks if thestatusis 'approved'. If true, it returns1(otherwise0), and theSUM()will count how many publications are approved.CASE WHEN status = 'rejected' THEN 1 ELSE 0 END: Similarly, this condition checks for 'rejected' publications and counts them.GROUP BY YEAR(publication_date): Groups the results by the publication year.ORDER BY publication_year: Orders the results by year in ascending order.
Example Output:
| publication_year | approved_count | rejected_count |
|---|---|---|
| 2020 | 120 | 30 |
| 2021 | 110 | 25 |
| 2022 | 140 | 40 |
This query will give you the total count of approved and rejected publications for each year.
No comments:
Post a Comment