sql server 2008 - ms sql grouping results query -



sql server 2008 - ms sql grouping results query -

i have transaction table have created records invoices in , out. have flag sales (=1) , purchases (=2).

i want simple sql calculates sales , purchases month. problem is, if remove transaction type (mybooks_trans_class_id_fk) grouping doesnt work, if leave in results not want.

sql:

select datepart(year, mybooks_trans_transaction_datetime) 'sales_year', datepart(month, mybooks_trans_transaction_datetime) 'sales_month', case when cast( mybooks_trans_class_id_fk nvarchar(100))!='1' cast(sum(mybooks_trans_gross_amount) nvarchar(100)) else '0' end total_sales, case when cast( mybooks_trans_class_id_fk nvarchar(100))!='2' cast(sum(mybooks_trans_gross_amount) nvarchar(100)) else '0' end total_purchases view_mybooks_transactions_all_details grouping datepart(year, mybooks_trans_transaction_datetime), datepart(mm, mybooks_trans_transaction_datetime),mybooks_trans_class_id_fk

results are:

2012 11 0 15300.00 2012 12 0 2500.00 2013 1 0 1300.00 2013 1 600.00 0 2013 2 0 2750.00 2013 2 1500.00 0

but want month 1 illustration in 1 row results want are

2012 11 0 15300.00 2012 12 0 2500.00 2013 1 600.00 1300.00 2013 2 1500.00 2750.00

any suggestions? sql ability moderate @ best!

this should work: (tried not alter much)

select datepart(year, mybooks_trans_transaction_datetime) 'sales_year', datepart(month, mybooks_trans_transaction_datetime) 'sales_month', sum(case when mybooks_trans_class_id_fk != 1 mybooks_trans_gross_amount else 0 end) total_sales, sum(case when mybooks_trans_class_id_fk != 2 mybooks_trans_gross_amount else 0 end) total_purchases view_mybooks_transactions_all_details grouping datepart(year, mybooks_trans_transaction_datetime), datepart(mm, mybooks_trans_transaction_datetime)

sql-server-2008

Comments

Popular posts from this blog

web services - java.lang.NoClassDefFoundError: Could not initialize class net.sf.cglib.proxy.Enhancer -

Accessing MATLAB's unicode strings from C -

javascript - mongodb won't find my schema method in nested container -