Solved

# COUNT the number of enquiries for the past 6 MONTHS

Posted on 2008-09-30
165 Views
I want to return a table (to use as a dataview) which has the name of the past 6 months (each month with the count of no. of inquiries) from today's date and a count on the no. of inquiries in that period

so at the moment I have a table which has a date columns and an inquiry column
0
Question by:manivineet
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 6
• 6
• 2

LVL 60

Expert Comment

ID: 22610604
0

LVL 60

Expert Comment

ID: 22610632
Something like this should work:
``````SELECT Month(datecol) AS InquiryMonth
, Count(inquiry) AS InquiryCount
FROM inquiries
GROUP BY Month(datecol)
ORDER BY 1
``````
0

Author Comment

ID: 22610643
here is my table structure
clip-image002.jpg
0

Author Comment

ID: 22610665
for mwvisa1:

the current solution looks fine, but it returns data for past three months and not 6 months, how can we modify it?

pardon my ignorance, for i'am a complete beginner
0

LVL 60

Accepted Solution

Kevin Cross earned 250 total points
ID: 22610669
Or in the honor of chapmandew, you can use a common table expression like this:
``````;WITH inquiryCTE AS (
SELECT DateDiff(mm, datecol, getdate()) AS MonthsInThePast
, Month(datecol) AS InquiryMonth
, Count(inquiry) AS InquiryCount
FROM inquiries
GROUP BY DateDiff(mm, datecol, getdate()), Month(datecol)
)
SELECT *
FROM inquiryCTE
WHERE MonthsInThePast <= 6
``````
0

Author Comment

ID: 22610673
actually ignore that please... for my database doesnt have entry before july (7)
0

LVL 60

Expert Comment

ID: 22610679
>>the current solution looks fine, but it returns data for past three months and not 6 months, how can we modify it?

Do you have data in the inquiry table from 6 months ago?

This query will only show month if it has a count.  If it doesn't it will not show up.  For that, you will have to join to the report through UNION or something to that effect a query with all months.
0

Author Comment

ID: 22610685
can we also return the YEAR as another column as well like

year month inquires
2008   9      243
0

LVL 60

Expert Comment

ID: 22610699
In either query you just add this to the select and group by clauses Year(datecol) where datecol is the name of your actual date column.
0

LVL 60

Expert Comment

ID: 22610721
If I am reading the image correctly, this should be your query:
``````SELECT Year(dttSubmitted) AS EnquiryYear
, Month(dttSubmitted) AS EnquiryMonth
, Count(*) AS EnquiryCount
FROM tblEnquiry
GROUP BY Year(dttSubmitted), Month(dttSubmitted)
ORDER BY 1 DESC, 2 DESC
``````
0

Author Closing Comment

ID: 31501806
thank you so much mwvisa1
0

Author Comment

ID: 22610826
just one more addition to it... even if the ENQUIRY COUNT is 0 , still the table should show year and month
0

LVL 60

Expert Comment

ID: 22610896
Do I have you hooked on CTEs yet?  ;)
0

LVL 60

Expert Comment

ID: 22610937
I think so!

@manivineet, as suggested earlier, you will need to right join to a table holding all days OR months OR use a UNION to all days OR months.
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE sâ€¦
###### Suggested Courses
Course of the Month4 days, 16 hours left to enroll