Group on DateTimeColumn

  • SELECT

    cv.DischargeDtm as Discharged,

    count(cd.IsIncomplete)#incomplete

    FROM CV3ClientVisit cv (nolock)

    INNER JOIN CV3ClientDocument cd ON cv.GUID = cd.ClientVisitGUID

    WHERE cv.TypeCode = 'inpatient'

    and cd.documentname like 'kbc%profile'

    and cd.isincomplete = 1

    and cv.visitstatus = 'dsc'

    and cv.admitdtm <= getdate()-1 /*used to eliminate patients admitted on current date. Nurses have 24 hours

    post admission to complete the profiles */

    and cv.currentlocation not like 'loa%' -- excludes the test unit

    Group BY cv.DischargeDtm

  • And the question is ?



    Clear Sky SQL
    My Blog[/url]

  • 4:1 odds it is on the group predicate.

    Anyone want to take that bet on 😀

    --------------------------------------------------------------------------------------
    [highlight]Recommended Articles on How to help us help you and[/highlight]
    [highlight]solve commonly asked questions[/highlight]

    Forum Etiquette: How to post data/code on a forum to get the best help by Jeff Moden[/url]
    Managing Transaction Logs by Gail Shaw[/url]
    How to post Performance problems by Gail Shaw[/url]
    Help, my database is corrupt. Now what? by Gail Shaw[/url]

  • GROUP BY convert(varchar(6),getdate(),112)

    __________________________________________________

    Against stupidity the gods themselves contend in vain. -- Friedrich Schiller
    Stop, children, what's that sound? Everybody look what's going down. -- Stephen Stills

  • Silverfox (10/12/2009)


    4:1 odds it is on the group predicate.

    Anyone want to take that bet on 😀

    I'll go for "whats then velocity of an unladen swallow" 😀



    Clear Sky SQL
    My Blog[/url]

  • Thank you Bob and everyone else problem solved.

  • European or African swallow?

    __________________________________________________

    Against stupidity the gods themselves contend in vain. -- Friedrich Schiller
    Stop, children, what's that sound? Everybody look what's going down. -- Stephen Stills

  • Bob Hovious 24601 (10/12/2009)


    GROUP BY convert(varchar(6),getdate(),112)

    I'd have gone

    GROUP BY DATEADD(dd,datediff(dd,0,cv.Dischargedtm),0)

    but that's because I hate to convert pretty things like datetimes into varchars...



    --Mark Tassin
    MCITP - SQL Server DBA
    Proud member of the Anti-RBAR alliance.
    For help with Performance click this link[/url]
    For tips on how to post your problems[/url]

  • Your way would definitely save a few CPU cycles, but shouldn't it be

    GROUP BY DATEADD(month,datediff(month,0,cv.Dischargedtm),0)

    ??

    __________________________________________________

    Against stupidity the gods themselves contend in vain. -- Friedrich Schiller
    Stop, children, what's that sound? Everybody look what's going down. -- Stephen Stills

  • Bob Hovious 24601 (10/12/2009)


    Your way would definitely save a few CPU cycles, but shouldn't it be

    GROUP BY DATEADD(month,datediff(month,0,cv.Dischargedtm),0)

    ??

    That would group them into monthly buckets... since it seems to be about daily discharge dates, I figured grouping them on the day of discharge was good enough.



    --Mark Tassin
    MCITP - SQL Server DBA
    Proud member of the Anti-RBAR alliance.
    For help with Performance click this link[/url]
    For tips on how to post your problems[/url]

  • I have the following code. I would like to group on Month and year. I have tried converting datetime to varchar to change the format.That did not work. Running the script now returns one row for every day. Any assistance would be appreciated.

    This is the subtext appended when you look at the list of topics.... unfortunately it doesn't display when you look at the topic itself.

    __________________________________________________

    Against stupidity the gods themselves contend in vain. -- Friedrich Schiller
    Stop, children, what's that sound? Everybody look what's going down. -- Stephen Stills

  • Without a question from the OP , we shall never know 🙂



    Clear Sky SQL
    My Blog[/url]

Viewing 12 posts - 1 through 11 (of 11 total)

You must be logged in to reply to this topic. Login to reply