Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to get a count based upon a grouping

Posted on 2016-08-08
8
Medium Priority
?
79 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 11

Assisted Solution

by:HuaMinChen
HuaMinChen earned 660 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 30

Accepted Solution

by:
Olaf Doschke earned 680 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 70

Assisted Solution

by:Scott Pletcher
Scott Pletcher earned 660 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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
LVL 2

Author Comment

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

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 11

Expert Comment

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

Expert Comment

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

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
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.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

722 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