Solved

How to get a count based upon a grouping

Posted on 2016-08-08
8
62 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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
LVL 2

Author Comment

by:brgdotnet
ID: 41749676
Thank you.
0
 
LVL 47

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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
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…
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…

803 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