datetime - SQL: How to Group by Custom Year -


For simplicity sake, I will make a similar example to him:

We say order order field in DB And there is a table of order with a company field. Then there is a table of companies and each record has a year-round figure (which indicates that year ends year that year, like 6/6).

I need to add all the orders for each year

I think it will have to do something like this, but I can not understand it:

  SELECT SUM (orderValue), case when order date & lt; = YearEndingDate then date date (year, order date) in case of order date & gt; Year end date, date of date (year, order date) + year 1 year from year to order by company.companyID = order by company company, company, year  

Even thoughts?

Not sure which RDMS you are using, but this should do the trick. TCLL is special in Datapaptor and Dataid accessories but I believe that this kind of function will have access to all the platforms you are using. In the case where to determine which year value is determined.

Answer: Select

  c.companyid, Yeringdet + '/' + Convert (varchar, datepart (yy, Dateadd (yy, 1, o.orderdate) )) As as youirding, as sum (ordervalue) from @orderso joinoforders ccompanies c o.companyid = c.companyid where to order between cast (cast (yearendingdate + '/' (varchar, datepart (varchar, datepart Yy, o.orderdate) datetime) gt; = o.orderdate; change again + '/' + convert (varchar, datepart (yy, dateadd (yy, -1, o.orderdate))) the other day ('Varchar, datepart (yy, o.orderdate)) as' datetime' in the form of '' '' '' '' '' '' 'convert' (varchar, datepart (yy, o.orderdate)) & Gt; = O.orderdate) Replace + '/' + Convert (Behavior, Datepart (i.e., Order)  

  Declare the @orders table (order date datat, company id varchar (20), order value int) @rdndor (getdate ( ), 'MS', 2) Insert @Reders Value (date, add (year, -1, millennate ()), 'ms', 1) @order value (date, adwords value (date, 1, dissection) , 'MS', 4) Insert in @Redder's Values ​​(DayAd (Up Companion values ​​('id', 'ms', 'ms', 11) @companies tables (company id varna (20), earandingdate varchar (20) Enter in the values ​​of companies @ Enter ('Blizzard', '07 / 01 ') in c.companyid, o.orderdate, yearendingdate +' / '+ Convert the same year In the form of green, datestrate (i.e., ordered)), '' '' '+' convert (worker, datatrate (yoy, datayed (yay, 1, order.)) Plus-year, yering + '/' + Convert (varchar, da Tepart (yy, dateadd (yy, -1, o.orderdate))) @orders to be included as minusyear @companies c o.companyid = c.companyid select c.companyid, yearendingdate + '/' + Convert Join Numbersoforders @orders o as the hacking, ordervalue (varchar, datepart (yy, dateadd (yy, 1, o.orderdate))) @companies c o.companyid = c.companyid where the case The middle order when ('varchar, datepart (yy, o.orderdate)) & gt; (' dot ' = O.orderdate) Converts + '/' + Convert (varchar, datepart (yy, dateadd (yy, -1, o.orderdate))) else yerendingdate + '/' + convert (varchar, datepart (yy, O.orderdate)) End and C ASE () () () () () () ('(' '' '' ',' CRPNID, O. Order, Yerending Data,  

Comments

Popular posts from this blog

c++ - Linux and clipboard -

What is expire header and how to achive them in ASP.NET and PHP? -

sql server - How can I determine which of my SQL 2005 statistics are unused? -