I am trying to create a view which joins 2 tables but the results are dependent on the query. Here is a example of what I want to achieve.
I have a table called sessionaccounting and a table called sessionaccountingdailysplit.
sessionaccounting contains all our accounting data and sessionaccountingdailysplit is the sessionaccounting data split by date. They are joined by the foreign key sessionaccountingid
How the two tables work in unison is as follows:
for the row in sessionaccounting :
starttime - '2012-01-01', endtime - '2012-01-03', usage - 10000
for the rows in sessionaccountingdailysplit :
date - '2012-01-01', usage - 3000
date - '2012-01-02', usage - 5000
date - '2012-01-03', usage - 2000
Now what I want to do is if I run a view called vw_sessionaccounting as
SELECT *
FROM vw_sessionaccounting
WHERE starttime >= '2011-01-01' AND starttime <= '2011-01-02';
it must only sum the first two dates from sessionaccountingdailysplit and replace the usage in sessionaccounting accordingly for each effected row. (most cases sessionaccountingdailysplit wont have a row as there was no split)
So as above if I run
SELECT *
FROM sessionaccounting
WHERE starttime >= '2011-01-01' AND starttime <= '2011-01-02';
I will get the result of
starttime - '2012-01-01', endtime - '2012-01-03', usage - 10000
but if I run the query
SELECT *
FROM vw_sessionaccounting
WHERE starttime >= '2011-01-01'
AND starttime <= '2011-01-02';
I will get the result of
starttime - '2012-01-01', endtime - '2012-01-03', usage - 8000