CallumVass
CallumVass

Reputation: 11458

Select only months which have data for a specified Year

I would like a query which selects only those month's which have data for a specified year but I'm not quite sure how to achieve this. Here is what I have got so far:

select MONTH(DateRaised) as 'Month'
from Complaints
where
    (select COUNT(*)
     from Complaints
     where YEAR(DateRaised) = 2000) > 0
group by MONTH(dateraised)
order by MONTH(dateraised)

So if my data had a Complaint from May, August and December in 2000, then I would only like 5, 8 and 12 to appear in my query, is this possible?

Upvotes: 0

Views: 91

Answers (3)

silly
silly

Reputation: 7887

SELECT MONTH(DateRaised) as 'Month', COUNT(*) AS count
FROM Complaints
WHERE YEAR(DateRaised) = 2000
GROUP BY MONTH(dateraised)
ORDER BY MONTH(dateraised)

Upvotes: 1

Barmar
Barmar

Reputation: 781974

select DISTINCT MONTH(DateRaised) AS 'Month'
from Complaints
where YEAR(DateRaised) = 2000

Upvotes: 2

TechDo
TechDo

Reputation: 18659

select MONTH(DateRaised) as 'Month'
from Complaints
where YEAR(DateRaised) = 2000
ORDER BY MONTH(dateraised)

Upvotes: 2

Related Questions