Solved

How to get a count based upon a grouping

Posted on 2016-08-08
8
56 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:Scott Pletcher
Scott Pletcher 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
LVL 46

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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

863 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

23 Experts available now in Live!

Get 1:1 Help Now