sql server 2008 - How to run sql n times increasing variable and after joining results -


i've transact table (historical) createddate, transact related employee transact table. (inner join in transact_id)

this being said, comes problem: need query these table , state month , because during year, createddate can change. e.g. employee update in july create new line, shouldn't affect march total.

the solution looks forech, how can join lines @ end? result should like:

  • january - $123
  • february - $234
  • march - $123
  • ...

i last state of each employee this:

select alltransact.id_employee, alltransact.id_department (  select id_employee, id_department, rank() on (partition id_employee order created_date desc) desc_rank transact_employee transemployee inner join transact     on transemployee.id_transact = transact.id_transact                          , transact.status = 8                          , transact.created_date < @currentmonth) alltransact  desc_rank = 1 

*i don't want copy , past code 12 times. :)

you can partition on many columns. rank() on (partition [id_employee],datepart(month,[created_date]) order [created_date] desc) give have each month (and doesn't care year month in either need partition year or add limit on created_date).


Comments

Popular posts from this blog

android - getbluetoothservice() called with no bluetoothmanagercallback -

sql - ASP.NET SqlDataSource, like on SelectCommand -

ios - Undefined symbols for architecture armv7: "_OBJC_CLASS_$_SSZipArchive" -