sql server - Cannot set the Order By to my SQL Statement, need a workaround -


i understand cannot have sql order paymentdate results come out not in payment date order. there easy way can make sure period column in actual date order?

the sql below works perfect if add "--order f.paymentdate" 'column "paymentitem.paymentdate" invalid in order clause because not contained in either aggregate function or group clause.' i'm trying think how around this

            select g.sitedescription,                 case a.surname when '' a.company else a.surname + ', ' + isnull(a.forename,'') end landowner,                 h.paymenttypedesc [rentalelection],                 d.relevantproportion,@in_showrelevantproportion showrelevantproportion,                 g.siteid,a.landownerid,e.paymenttypeid,e.paymentcategoryid,                      case @in_outputformat                  when 'monthly'                                      convert(char(3), f.paymentdate, 0) + '-' + ltrim(year(f.paymentdate))                 when 'annually'                                     ltrim(year(f.paymentdate))                  else                                      ltrim(year(f.paymentdate)) + ' qtr ' + ltrim(datepart(quarter,f.paymentdate))                 end period,                 sum(isnull(f.actualpayment,0)) total                              [site] g,                  landowner a,                 [site] c,                 siteagreement d,                  payment e,                  paymentitem f,                 paymenttype h                         a.landownerid = d.landownerid                  , g.siteid = d.siteid                 , e.siteagreementid = d.siteagreementid                 , f.paymentid = e.paymentid                  , e.paymenttypeid = h.paymenttypeid                 , f.paymentdate between @in_daysfrom , @in_daysto                 , isnull(f.actualpayment,0) != 0              group g.sitedescription,                 case a.surname when '' a.company else a.surname + ', ' + isnull(a.forename,'') end,                 h.paymenttypedesc,                 d.relevantproportion,                 g.siteid,a.landownerid,e.paymenttypeid,e.paymentcategoryid,                 case @in_outputformat                  when 'monthly'                          convert(char(3), f.paymentdate, 0) + '-' + ltrim(year(f.paymentdate))                 when 'annually'                         ltrim(year(f.paymentdate))                  else                         ltrim(year(f.paymentdate)) + ' qtr ' + ltrim(datepart(quarter,f.paymentdate))                 end             --order f.paymentdate 

order entire expression uses payment date column:

order case @in_outputformat              when 'monthly'                      convert(char(3), f.paymentdate, 0) + '-' + ltrim(year(f.paymentdate))             when 'annually'                     ltrim(year(f.paymentdate))              else                     ltrim(year(f.paymentdate)) + ' qtr ' + ltrim(datepart(quarter,f.paymentdate))             end 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -