I am attempting to subtotal all amounts with dates in the current year (year to date) and dates last year by this time. Crystal reports has a yeartodate function in which I can simply say invoicedate in yeartodate. Need to accomplish same goal in the view. Is there a similar function in TSQL?
Below is the code with the part sum part missing. I want a sum of all invoice amounts for each customer.
Thanks!
select -- invoice
ivc.batch_no,
ivc.invoice_no,
--Added by DLW 06/29/2011
coalesce(ivc.orig_invoice_no, ivc.invoice_no) as orig_ivc_no,
ivc.invoice_date,
ivc.branch_code,
ivc.customer_id,
current_sub_amt = (select sum(ivc2.sub_amt)
from invoice ivc2
where ivc.invoice_date in year
ivc.cust_name,
ivc.sub_amt,
-- invoice group
ig.doc_no,
doc_type = case
when ig.doc_type IN ('CBL', 'CO', 'COL') then 'CO'
when ig.doc_type IN ('WO', 'WOA', 'WOS', 'WOT') then 'WO'
when ig.doc_type IN ('SO', 'SOL', 'SOS', 'SSL') then 'SO'
when ig.doc_type IN ('IA', 'IAL') then 'IA'
when ig.doc_type IN ('CLH', 'CLA', 'CLT') then 'CLH'
else ig.doc_type
end,
ig.customer_po
from invoice ivc
join invoice_group ig on ig.invoice_no = ivc.invoice_no
join currency cu on cu.curr_code = ivc.curr
Below is the code with the part sum part missing. I want a sum of all invoice amounts for each customer.
Thanks!
select -- invoice
ivc.batch_no,
ivc.invoice_no,
--Added by DLW 06/29/2011
coalesce(ivc.orig_invoice_no, ivc.invoice_no) as orig_ivc_no,
ivc.invoice_date,
ivc.branch_code,
ivc.customer_id,
current_sub_amt = (select sum(ivc2.sub_amt)
from invoice ivc2
where ivc.invoice_date in year
ivc.cust_name,
ivc.sub_amt,
-- invoice group
ig.doc_no,
doc_type = case
when ig.doc_type IN ('CBL', 'CO', 'COL') then 'CO'
when ig.doc_type IN ('WO', 'WOA', 'WOS', 'WOT') then 'WO'
when ig.doc_type IN ('SO', 'SOL', 'SOS', 'SSL') then 'SO'
when ig.doc_type IN ('IA', 'IAL') then 'IA'
when ig.doc_type IN ('CLH', 'CLA', 'CLT') then 'CLH'
else ig.doc_type
end,
ig.customer_po
from invoice ivc
join invoice_group ig on ig.invoice_no = ivc.invoice_no
join currency cu on cu.curr_code = ivc.curr