sql - How to use GROUP with DATEPART? -


i using following script count number of shopid 1 year.

i need breakdown result month year.

so end result should be

month 1     shopid 5     100     shopid 4     90 month 2     shopid 1     150     shopid 4     80 

   select shopid, count(shopid) interest     dbo.analytics     datecreated >= '2014' , datecreated < '2015'     group shopid order interest desc 

table structure

create table analytics ( datecreated datetime2(2), shopid  int ); 

what should change in script? shall use datepart near group by

you can use datepart.

try this

    select datepart(mm,datecreated) 'month',shopid, count(shopid) interest     dbo.analytics     year(datecreated) = '2015'      group datepart(mm,datecreated),shopid      order interest desc 

datepart return month number only. if need result have month name should use datename

try this

    select select datename( month , dateadd( month , datepart(mm,datecreated) , -1 ) ) 'month',shopid, count(shopid) interest     dbo.analytics     year(datecreated) = '2015'      group datename( month , dateadd( month , datepart(mm,datecreated) , -1 ) ),shopid      order interest desc 

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -