Grouping Records By Year Query

Posted on 2011-10-02
Last Modified: 2012-05-12
The query below gives me the total accounts with the same submission date.  But  I need the total accounts with the same year only.

What needs to be changed in the query?

SubmissionDate is a DateTime datatype

SELECT count(account)as NumOfAccounts,SubmissionDate
FROM Accounts
Group By SubmissionDate HAVING count(account)> 0
Question by:PhilChapmanJr
    LVL 9

    Accepted Solution

    Looks you will  get multiple years data , unless you want to parametrize the year part in the where clause.

    try using this:
    select count(account) as NumOfAccount, datepart(yy,SubmissionDate) nYear
    from Accounts
    Group by datepart(yy,SubmissionDate)
    having count(account)>0

    or you can add a where clause
    where datepart(yy,SubmissionDate) = 2011
    LVL 2

    Author Closing Comment



    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
    International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
    Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
    Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

    779 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

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now