Solved

How to get a count based upon a grouping

Posted on 2016-08-08
8
49 Views
Last Modified: 2016-09-16
I need to query a table similar to the one below. Basically I need to group the records below by the number of records
in each month. So the query would produce this result from the CustomerPurchases table. I can have the query display the month/year however how do I get the counts, of the number of records per month?
Purchase Per Month
5/2016     3
6/2016     1
7/2016     4

CustomerPurchases
1 | 2016-05-01 12:33:44:187  |  Bartermore
2 | 2016-05-13 12:30:00:000  |  Drake
3 | 2016-05-31 01:22:11:187  |  Sami
4 | 2016-06-01 12:33:44:166  |  Gabak
5 | 2016-07-01 11:28:44:144  |  Laster
6 | 2016-07-11 12:34:45:186  |  Gobo
7 | 2016-07-15 12:33:44:187  |  Darmore
8 | 2016-07-15 12:33:44:175  |  Grabson
0
Comment
Question by:brgdotnet
8 Comments
 
LVL 10

Assisted Solution

by:HuaMinChen
HuaMinChen earned 165 total points (awarded by participants)
ID: 41748226
Try
select substring(convert(varchar,[PurchaseDate],112),1,6) [Month],count(*) [Total]
from Tab1
group by substring(convert(varchar,[PurchaseDate],112),1,6);

Open in new window

1
 
LVL 29

Accepted Solution

by:
Olaf Doschke earned 170 total points (awarded by participants)
ID: 41748274
Well, in SQL terminology you don't say " I need to group the records below by the number of records
in each month", you say "I need to group the records by month and get the count.

I'd give this a try - depends on your SQL Server version:
Select Year(Purchasedate), Month(Purchasedate), Count(*) FROM CustomerPurchases
Group By Year(Purchasedate), Month(Purchasedate)

Open in new window


Bye, Olaf.

PS: YEAR() and MONTH() are available since 2008, if your SQL Server installation is older then it's time to think of an upgrade, but you can then also use datepart(year, Purchasedate) and datepart(month, Purchasedate).
1
 
LVL 69

Assisted Solution

by:ScottPletcher
ScottPletcher earned 165 total points (awarded by participants)
ID: 41749053
The "standard, best-practice" way of adjusting a date to a given time period -- day, week, month, year, whatever -- is this:
DATEADD(<time_period>, DATEDIFF(<time_period>, 0, Column_Date), 0)
This very efficiently strips the time and adjusts the value to the start of the relevant period.

Thus, for this case specifically, you would do something like below.  The conversion of date to char is done last because SQL is slower at handling chars than dates, so it's more efficient to delay the conversion until then.

SELECT STUFF(CONVERT(varchar(10), [Month], 101), 3, 3, '') AS Month,
    Purchase_Count
FROM (
    SELECT
        CAST(DATEADD(MONTH, DATEDIFF(MONTH, 0, PurchaseDate), 0) AS date) AS Month,
        COUNT(*) AS Purchase_Count
    FROM CustomerPurchases
    /* WHERE PurchaseDate >= '20160101' /*if you need to limit the time range considered*/ */    
    GROUP BY CAST(DATEADD(MONTH, DATEDIFF(MONTH, 0, PurchaseDate), 0) AS date)
) AS derived
ORDER BY [Month]
1
 
LVL 2

Author Comment

by:brgdotnet
ID: 41749676
Thank you.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 45

Expert Comment

by:Vitor Montalvão
ID: 41753853
brgdotnet, if your problem has been solved please choose the comment or comments that helped you out.
Cheers
1
 
LVL 2

Author Comment

by:brgdotnet
ID: 41755829
:)
0
 
LVL 10

Expert Comment

by:HuaMinChen
ID: 41774251
Hi Brgdotnet,
Please confirm that you have no more questions to this thread. Thanks
1
 
LVL 10

Expert Comment

by:HuaMinChen
ID: 41801189
The previous given advice should have resolved the author's original question.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
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.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

708 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

15 Experts available now in Live!

Get 1:1 Help Now