cpuphantom
IS-IT--Management
Hello all, I'm trying to do something with T-SQL in a Stored Procedure, that I imagine is possible but so far have not been able to do. Hopefully someone here can offer some insight.
I have a program, that reports on user's sales totals for my office. The basic output of the query is a sorted list of each user and that user's billing sum for any time period fed into it.
Now, I am trying to make the application work with a Currency Conversion. An exchange rate is entered into a table monthly (yeah, I know it should be more often, but the bosses said no). Offices enter their sales in their local currency (say Euros for example), and I store the values in dollars by dividing it by my exchange rate. That part works great too.
The problem comes when trying to generate a one query report like I used to but display it in their local currency for a time period over a month. Say you ran it for 3 months... essentially I need the sum of month 1 * month 1's exchange + sum of month 2 * month 2's exchange and so on to give me one total of everything that office entered in their own currency.
The closest I've been able to get is this:
select
user_id,
sum(billings) * (select dollar from tbCurrencyExchange where symbol = @symbol and month = month(datestamp) and year = year(datestamp)) as sum_billings
from
tbNumbers
where
user_id = @user_id and
datestamp between @startdate and @enddate
group by
user_id,
datestamp
It gives me almost what I want. Something like this:
user_id sum_fee
1 0
1 0
1 10000
1 15000
1 0
1 5000
But what I really want to be able to do is just get this:
user_id sum_fee
1 30000
One sum for the entire time period. Entering "datestamp" in the group by is what's breaking it up by datestamp, but if I don't put it there it tells me I have to since I used it up before the "From"
Is it possible at all to do a sum of the sum??
Thanks for your help in advanced... sorry this is so long, but its a complicated problem and hard to explain in brief.
I have a program, that reports on user's sales totals for my office. The basic output of the query is a sorted list of each user and that user's billing sum for any time period fed into it.
Now, I am trying to make the application work with a Currency Conversion. An exchange rate is entered into a table monthly (yeah, I know it should be more often, but the bosses said no). Offices enter their sales in their local currency (say Euros for example), and I store the values in dollars by dividing it by my exchange rate. That part works great too.
The problem comes when trying to generate a one query report like I used to but display it in their local currency for a time period over a month. Say you ran it for 3 months... essentially I need the sum of month 1 * month 1's exchange + sum of month 2 * month 2's exchange and so on to give me one total of everything that office entered in their own currency.
The closest I've been able to get is this:
select
user_id,
sum(billings) * (select dollar from tbCurrencyExchange where symbol = @symbol and month = month(datestamp) and year = year(datestamp)) as sum_billings
from
tbNumbers
where
user_id = @user_id and
datestamp between @startdate and @enddate
group by
user_id,
datestamp
It gives me almost what I want. Something like this:
user_id sum_fee
1 0
1 0
1 10000
1 15000
1 0
1 5000
But what I really want to be able to do is just get this:
user_id sum_fee
1 30000
One sum for the entire time period. Entering "datestamp" in the group by is what's breaking it up by datestamp, but if I don't put it there it tells me I have to since I used it up before the "From"
Is it possible at all to do a sum of the sum??
Thanks for your help in advanced... sorry this is so long, but its a complicated problem and hard to explain in brief.