max - SQL Server Query syntax to count maximum occurrences of a variable and output same -


i using sql server 2014 , have simple query gives me following output if run against relevant table. here's query:

use mydatabase  select reservationstayid        , nonroombundleid        ,staydate  resstaynonroombundle  reservationstayid = 11676 

output follows:

reservationstayid   nonroombundleid     staydate  11676                   2              2014-07-23 00:00:00.000  11676                   2              2014-07-24 00:00:00.000  11676                   2              2014-07-25 00:00:00.000  11676                   2              2014-07-26 00:00:00.000  11676                   2              2014-07-27 00:00:00.000  11676                   2              2014-07-28 00:00:00.000  11676                   4              2014-07-29 00:00:00.000  11676                   4              2014-07-30 00:00:00.000  11676                   4              2014-07-31 00:00:00.000  11676                   4              2014-08-01 00:00:00.000  11676                   4              2014-08-02 00:00:00.000  11676                   4              2014-08-03 00:00:00.000  11676                   4              2014-08-04 00:00:00.000  11676                   1              2014-08-05 00:00:00.000 

now, need modify query following output:

reservationstayid   nonroombundle id    mth         11676            2               july 2014        11676            4               august 2014 

and did:

use mydatabase select reservationstayid,max(nonroombundleid) [nonroombundle  id],datename(m,staydate) + ' ' + cast(datepart(yyyy,staydate) varchar)  [mth]   resstaynonroombundle  reservationstayid = 11676  group datename(m,staydate) + ' ' + cast(datepart(yyyy,staydate) varchar), reservationstayid 

and giving me following output:

reservationstayid   nonroombundle id    mth        11676            4               july 2014        11676            4               august 2014 

i need change max(nonroombundleid) logic outputs 'nonroombundleid' maximum occurences rather maximum value.

in other words, want query count maximum occurrences of nonroombundleid each month , output one. in case of july 2014, nonroombundleid has occurences 2. so, want query output 2 result july 2014.

may using count function? if yes, how implement existing query?

try this:

select  reservationstayid, nonroombundleid, tot,     convert(varchar, datepart(yyyy, dateadd(month,  m,'1970-1-1')))+'-'+convert(varchar,datepart(month, dateadd(month,  m,'1970-1-1'))) [month]      (     select reservationstayid, nonroombundleid, count(*) tot,         datediff(month, '1970-1-1', staydate) m,       row_number() on (              partition reservationstayid,               datediff(month, '1970-1-1', staydate)                order count(*) desc              )  rownum      resstaynonroombundle      group reservationstayid, nonroombundleid,              datediff(month, '1970-1-1', staydate)      )       e   rownum =1 

to use own month format:

select  reservationstayid, nonroombundleid, tot,     datename(m,dateadd(month, m, '1970-1-1')) + ' ' + cast(datepart(yyyy,dateadd(month, m,'1970-1-1')) varchar)     (     select reservationstayid, nonroombundleid, count(*) tot,         datediff(month, '1970-1-1', staydate) m,       row_number() on (partition reservationstayid, datediff(month, '1970-1-1', staydate)  order count(*) desc)  rownum      resstaynonroombundle      group reservationstayid, nonroombundleid, datediff(month, '1970-1-1', staydate)      )      e   rownum =1 

if want list top spots in each month, try this:

select  reservationstayid, nonroombundleid, tot,     datename(m,dateadd(month, m, '1970-1-1')) + ' ' + cast(datepart(yyyy,dateadd(month, m,'1970-1-1')) varchar) [month]     (     select reservationstayid, nonroombundleid, count(*) tot,         datediff(month, '1970-1-1', staydate) m      resstaynonroombundle l      group reservationstayid, nonroombundleid, datediff(month, '1970-1-1', staydate)       having count(*) = (         select max(tot)  (            select count(*) tot             resstaynonroombundle t            datediff(month, '1970-1-1', t.staydate)  = datediff(month, '1970-1-1', l.staydate)             group reservationstayid, nonroombundleid            ) c         )      )     e  order m  

Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -