Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Sum a col for MTD and another for YTD in same query?
Message
General information
Forum:
Microsoft SQL Server
Category:
SQL syntax
Miscellaneous
Thread ID:
01197144
Message ID:
01197224
Views:
20
>Try
>
>
>DECLARE @YEAR int
>DECLARE @PrevMonth int
>
>SET @Year = YEAR(getdate())
>SET @PrevMonth = MONTH(DATEADD(mm, -1, getdate()))
>
>select site, impactedMedia, incidentCategory,
>		SUM( CASE WHEN Year(EventDate) = @Year
>					AND month(EventDate) = @PrevMonth
>				THEN 1 ELSE 0 END) AS MTD,
>		SUM( CASE WHEN Year(EventDate) = @Year
>					AND month(EventDate) <= @PrevMonth
>				THEN 1 ELSE 0 END) AS YTD
>	from tb_EnvIncident
>	left join
>		tb_sc_data_codes
>			on impactedMedia = code
>				and codetype = 'iris_EnvImpactedMedia'
>				AND Status = 'Approved'
>	group by site, impactedMedia, incidentCategory
>
>

Thanks.
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform