Solved

COUNT the number of enquiries for the past 6 MONTHS

Posted on 2008-09-30
14
163 Views
Last Modified: 2011-10-19
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
Comment
Question by:manivineet
  • 6
  • 6
  • 2
14 Comments
 
LVL 60

Expert Comment

by:chapmandew
ID: 22610604
post your table structure.
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22610632
Something like this should work:
SELECT Month(datecol) AS InquiryMonth
, Count(inquiry) AS InquiryCount
FROM inquiries
WHERE datecol >= DateAdd(mm, -6, DateAdd(dd, -day(getdate())+1, DateDiff(dd, 0, getdate())))
GROUP BY Month(datecol)
ORDER BY 1

Open in new window

0
 

Author Comment

by:manivineet
ID: 22610643
here is my table structure
clip-image002.jpg
0
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 

Author Comment

by:manivineet
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 59

Accepted Solution

by:
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

Open in new window

0
 

Author Comment

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

Expert Comment

by:Kevin Cross
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

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

year month inquires
2008   9      243
0
 
LVL 59

Expert Comment

by:Kevin Cross
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 59

Expert Comment

by:Kevin Cross
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
WHERE dttSubmitted >= DateAdd(mm, -6, DateAdd(dd, -day(getdate())+1, DateDiff(dd, 0, getdate())))
GROUP BY Year(dttSubmitted), Month(dttSubmitted)
ORDER BY 1 DESC, 2 DESC

Open in new window

0
 

Author Closing Comment

by:manivineet
ID: 31501806
thank you so much mwvisa1
0
 

Author Comment

by:manivineet
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

by:chapmandew
ID: 22610896
Do I have you hooked on CTEs yet?  ;)
0
 
LVL 59

Expert Comment

by:Kevin Cross
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SSIS On fail action 5 38
SQL Server How-To Show Notes In First Row of Results 4 31
SQL server 2014 replication error 35 45
What is this datetime? 1 20
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question