SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Group by Month


Group by Month

Author
Message
davidhyder
davidhyder
SSC Journeyman
SSC Journeyman (96 reputation)SSC Journeyman (96 reputation)SSC Journeyman (96 reputation)SSC Journeyman (96 reputation)SSC Journeyman (96 reputation)SSC Journeyman (96 reputation)SSC Journeyman (96 reputation)SSC Journeyman (96 reputation)

Group: General Forum Members
Points: 96 Visits: 33
I used to be the go-to-guy for SQL and SSRS in another life. But alas, I'm now manager who rarely gets to write queries or build reports. So the answer to this simple question escapes me and I can't seem to find a solution posted anywhere.

I want to group dates by month.

Here is my query:


select l.location_name Center, convert (date, a.appt_date) Date, count(a.appt_id) [Kept Appointments]
from appointments a
join location_mstr l on a.location_id = l.location_id
where a.appt_date between '20170101' and getdate()
group by l.location_name, a.appt_id, a.appt_date
order by center, date

This is an example of what I get:
Location Date Count
Los Angeles 1/1/2017 1
Los Angeles 1/1/2017 1
Los Angeles 1/1/2017 1
Los Angeles 1/2/2017 1
Los Angeles 1/2/2017 1
Los Angeles 1/2/2017 1
Los Angeles 1/3/2017 1
Los Angeles 1/3/2017 1
Los Angeles 1/3/2017 1
New York 1/1/2017 1
New York 1/1/2017 1
New York 1/1/2017 1
New York 1/2/2017 1
New York 1/2/2017 1
New York 1/2/2017 1
New York 1/3/2017 1
New York 1/3/2017 1
New York 1/3/2017 1
Chicago 1/1/2017 1
Chicago 1/1/2017 1
Chicago 1/1/2017 1
Chicago 1/2/2017 1
Chicago 1/2/2017 1
Chicago 1/2/2017 1
Chicago 1/3/2017 1
Chicago 1/3/2017 1
Chicago 1/3/2017 1

This is what I'm looking for: (drill down report)


Thanks!

mister.magoo
mister.magoo
One Orange Chip
One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)

Group: General Forum Members
Points: 26724 Visits: 7939
It's your Group By - it's too specific.

Firstly you are grouping by a.appt_id, so your Count will always be 1.
Secondly you are grouping by a.appt_date, but displaying convert (date, a.appt_date) - which suggests a.appt_date is a datetime and therefore your Group By on a.appt_date is grouping by time and date.

Try changing your GROUP BY to this:

group by l.location_name, convert (date, a.appt_date)

MM


select geometry::STGeomFromWKB(0x0106000000020000000103000000010000000B0000001000000000000840000000000000003DD8CCCCCCCCCC0840000000000000003DD8CCCCCCCCCC08408014AE47E17AFC3F040000000000104000CDCCCCCCCCEC3F9C999999999913408014AE47E17AFC3F9C99999999991340000000000000003D0000000000001440000000000000003D000000000000144000000000000000400400000000001040000000000000F03F100000000000084000000000000000401000000000000840000000000000003D0103000000010000000B000000000000000000143D000000000000003D009E99999999B93F000000000000003D009E99999999B93F8014AE47E17AFC3F400000000000F03F00CDCCCCCCCCEC3FA06666666666FE3F8014AE47E17AFC3FA06666666666FE3F000000000000003D1800000000000040000000000000003D18000000000000400000000000000040400000000000F03F000000000000F03F000000000000143D0000000000000040000000000000143D000000000000003D, 0);




  • Forum Etiquette: How to post Reporting Services problems
  • Forum Etiquette: How to post data/code on a forum to get the best help - by Jeff Moden
  • How to Post Performance Problems - by Gail Shaw

  • davidhyder
    davidhyder
    SSC Journeyman
    SSC Journeyman (96 reputation)SSC Journeyman (96 reputation)SSC Journeyman (96 reputation)SSC Journeyman (96 reputation)SSC Journeyman (96 reputation)SSC Journeyman (96 reputation)SSC Journeyman (96 reputation)SSC Journeyman (96 reputation)

    Group: General Forum Members
    Points: 96 Visits: 33
    Thank you for your suggestion.

    I'd tried that, and it does in fact count for each day, but what I'm looking for is a count by month not day.
    Sue_H
    Sue_H
    SSC-Dedicated
    SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)

    Group: General Forum Members
    Points: 33595 Visits: 9564
    davidhyder - Monday, September 18, 2017 10:48 AM
    Thank you for your suggestion.

    I'd tried that, and it does in fact count for each day, but what I'm looking for is a count by month not day.


    It looks to me like you may need to do the grouping by DATENAME(month(convert (date, a.appt_date))), l.location_name

    Sue



    mister.magoo
    mister.magoo
    One Orange Chip
    One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)

    Group: General Forum Members
    Points: 26724 Visits: 7939
    davidhyder - Monday, September 18, 2017 10:48 AM
    Thank you for your suggestion.

    I'd tried that, and it does in fact count for each day, but what I'm looking for is a count by month not day.

    Oops, yeah I missed the month thing, so as suggested, use either month () or datename(month,...)


    MM


    select geometry::STGeomFromWKB(0x0106000000020000000103000000010000000B0000001000000000000840000000000000003DD8CCCCCCCCCC0840000000000000003DD8CCCCCCCCCC08408014AE47E17AFC3F040000000000104000CDCCCCCCCCEC3F9C999999999913408014AE47E17AFC3F9C99999999991340000000000000003D0000000000001440000000000000003D000000000000144000000000000000400400000000001040000000000000F03F100000000000084000000000000000401000000000000840000000000000003D0103000000010000000B000000000000000000143D000000000000003D009E99999999B93F000000000000003D009E99999999B93F8014AE47E17AFC3F400000000000F03F00CDCCCCCCCCEC3FA06666666666FE3F8014AE47E17AFC3FA06666666666FE3F000000000000003D1800000000000040000000000000003D18000000000000400000000000000040400000000000F03F000000000000F03F000000000000143D0000000000000040000000000000143D000000000000003D, 0);




  • Forum Etiquette: How to post Reporting Services problems
  • Forum Etiquette: How to post data/code on a forum to get the best help - by Jeff Moden
  • How to Post Performance Problems - by Gail Shaw

  • davidhyder
    davidhyder
    SSC Journeyman
    SSC Journeyman (96 reputation)SSC Journeyman (96 reputation)SSC Journeyman (96 reputation)SSC Journeyman (96 reputation)SSC Journeyman (96 reputation)SSC Journeyman (96 reputation)SSC Journeyman (96 reputation)SSC Journeyman (96 reputation)

    Group: General Forum Members
    Points: 96 Visits: 33
    Thanks y'all... That got me there!

    Month() gave me the number of the month but Datename(m,...) gave me the name of the month so I went with that one.

    Greatly appreciated!!!
    sgmunson
    sgmunson
    SSC-Forever
    SSC-Forever (43K reputation)SSC-Forever (43K reputation)SSC-Forever (43K reputation)SSC-Forever (43K reputation)SSC-Forever (43K reputation)SSC-Forever (43K reputation)SSC-Forever (43K reputation)SSC-Forever (43K reputation)

    Group: General Forum Members
    Points: 43640 Visits: 5422
    davidhyder - Monday, September 18, 2017 4:24 PM
    Thanks y'all... That got me there!

    Month() gave me the number of the month but Datename(m,...) gave me the name of the month so I went with that one.

    Greatly appreciated!!!

    Just keep in mind that you won't be able to have this report span years that way. Only a combination of month and year can do that.
    For example:
    GROUP BY CONVERT(char(4), YEAR(CONVERT(date, a.appt_date))) + '_' + DATENAME(MONTH(CONVERT(date, a.appt_date))), l.location_name



    Steve
    ‌(aka sgmunson)
    Smile Smile Smile
    Health & Nutrition
    Lynn Pettis
    Lynn Pettis
    SSC Guru
    SSC Guru (226K reputation)SSC Guru (226K reputation)SSC Guru (226K reputation)SSC Guru (226K reputation)SSC Guru (226K reputation)SSC Guru (226K reputation)SSC Guru (226K reputation)SSC Guru (226K reputation)

    Group: General Forum Members
    Points: 226034 Visits: 40423
    I would convert all the dates to the first of each month and group on that value. No parsing and concatenating of values required.

    dateadd(month,datediff(month,0,Datecol),0)

    Cool
    Lynn Pettis

    For better assistance in answering your questions, click here
    For tips to get better help with Performance Problems, click here
    For Running Totals and its variations, click here or when working with partitioned tables
    For more about Tally Tables, click here
    For more about Cross Tabs and Pivots, click here and here
    Managing Transaction Logs

    SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
    Go


    Permissions

    You can't post new topics.
    You can't post topic replies.
    You can't post new polls.
    You can't post replies to polls.
    You can't edit your own topics.
    You can't delete your own topics.
    You can't edit other topics.
    You can't delete other topics.
    You can't edit your own posts.
    You can't edit other posts.
    You can't delete your own posts.
    You can't delete other posts.
    You can't post events.
    You can't edit your own events.
    You can't edit other events.
    You can't delete your own events.
    You can't delete other events.
    You can't send private messages.
    You can't send emails.
    You can read topics.
    You can't vote in polls.
    You can't upload attachments.
    You can download attachments.
    You can't post HTML code.
    You can't edit HTML code.
    You can't post IFCode.
    You can't post JavaScript.
    You can post emoticons.
    You can't post or upload images.

    Select a forum








































































































































































    SQLServerCentral


    Search