I have a log table over all kind of events have been logged during the past years. Is it possible to create a sql query which tells me how many events have been logged per month, BUT only counting maximum one event per day?
Example for a few dates in October:
2022-10-12 12:11:32.570
2022-10-12 12:11:44.050
2022-10-12 14:19:55.027
2022-10-12 14:20:07.763
2022-10-26 09:54:39.020
2022-10-26 09:54:49.333
2022-10-26 09:54:54.353
2022-10-26 09:55:53.030
2022-10-26 09:55:59.363
2022-10-26 09:56:28.783
2022-10-26 09:56:36.797
2022-10-26 09:57:01.200
2022-10-26 10:00:21.663
2022-10-26 10:00:26.820
2022-10-26 10:00:42.777
2022-10-26 10:00:50.520
2022-10-26 10:01:20.777
2022-10-26 10:02:14.833
2022-10-26 10:02:23.460
2022-10-27 08:57:02.830
2022-10-27 08:57:33.820
2022-10-27 08:59:02.333
These should be 3 events (because of 3 different days). And I would like to have an overall result like:
month | events-count |
---|---|
January | 15 |
February | 2 |
March | ... |
- There are indeed values for several different years and I need the values for each month for the past 3 years.
- Is it possible to have the results as COLUMNS? Like:
| December 2022 | January 2023 | February 2023 | ... |
| ------------ | ------------ | ------------ | ------------ |
| 13 | 7 | 18 | ... |
4条答案
按热度按时间8hhllhi21#
You can group dates by month and then count distinct days. This should work.
UPDATE: Updated version with different years:
As a result you get:
avkwfej42#
I would just partition it by Month/Day, do a row count for each combination and then count the first row of each group.
Something like this:
mwkjh3gx3#
A possible approach for the initial question is a statement, using
COUNT
withDISTINCT
and an appropriateGROUP BY
clause:A possible approach for your final question (... to get the count of the events for each month for the past 3 years and rotate them as columns ...) is to
PIVOT
the values usingCOUNT
as aggregation function to get theDISTINCT
days for each period (a combination of year and month). You have at least these options:PIVOT
, if you want to count the events for known periods (202312, 202311, 202310, ....).PIVOT
, if you want to count the events for relative periods (current period is 0, previous period is 1, ...).PIVOT
, if you want to include a variable number of periods, starting from current period. The approach uses a numbers table, build with recursive query in this specific case, but you may always choose a different technique:Sample data:
Static
PIVOT
for known periods:Static
PIVOT
for relative periods:Dymanic
PIVOT
for variable number of years:Results:
oprakyz74#
You have answers, but if you wanted to return the data in columns (by years) instead of rows either of these could work. Includes columns for 0 values too, so that may not be desired.
Note: For dynamic changing headers, I believe you would need to write some dynamic SQl.
Pivot Example
Cross Apply Example (could use outer too)
Sample Data
Results
![](https://i.stack.imgur.com/YC2F5.png)
=== Update ===
I wanted to give an example dynamic query, @Zhorov already beat me to this, so consider this an extension of what Zhorov already said.
| February 2022 | October 2022 | November 2022 | November 2023 |
| ------------ | ------------ | ------------ | ------------ |
| 1 | 3 | 1 | 1 |
Uses the monthname/year column naming and only lists results for data that you have. So, yes, it is possible to do. It adds complexity and maintenance weight.
I have commented the code a bit for better understanding.