| |
|
If this is your first visit, be sure to check out the FAQ by clicking the link above.
You may have to register before you can post: click the register link above to proceed.
To start viewing messages, select the forum that you want to visit from the selection below.
|
 |

08-16-10, 02:30
|
|
Registered User
|
|
Join Date: Apr 2007
Posts: 9
|
|
|
Create view from two tables
|
|
Hi,
Guys how to create view from two tables to display daily, monthly and yearly summary. I want to create view with following columns but can't work out the join query. Branchcode is the common in two tables. Please help.
select id, branchcode, monthyear, sum(total) as Received, sum(totalpaid) as Paid, sum(total-totalpaid) as Balance from deposit,expense where branchcode = 'x' and creationdate >= 'commondate' and invoicedate >= 'commondate' group by monthyear;
table deposit:
id
branchcode
creationdate
monthyear
total
select id, branchcode, creationdate, sum(total) from deposit where branchcode = 'x' and creationdate >= 'somedate' group by monthyear;
table expense:
id
branchcode
invoicedate
monthyear
totalpaid
select id, branchcode, invoicedate, sum(totalpaid) from expense where branchcode = 'x' and creationdate >= 'somedate' group by monthyear;
|
|

08-16-10, 05:13
|
|
SQL Consultant
|
|
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
|
|
Quote:
Originally Posted by slamdunk
Hi I want to create view with following columns but can't work out the join query.
|
please repeat the exact columns that you wish to see in the result set
|
|

08-18-10, 01:13
|
|
Registered User
|
|
Join Date: Apr 2007
Posts: 9
|
|
|
|
r937,
I need branchcode, monthyear, sum(deposit.total) as Received, sum(expense.totalpaid) as Paid, sum(deposit.total-expense.totalpaid) as Banance group by monthyear
select branchcode, monthyear, sum(total) as Received, sum(totalpaid) as Paid, sum(total-totalpaid) as Balance from deposit,expense where branchcode = 'x' and creationdate >= 'commondate' and invoicedate >= 'commondate' group by monthyear;
Thanks!
|
|

08-18-10, 02:28
|
|
SQL Consultant
|
|
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
|
|
Code:
SELECT branchcode
, monthyear
, SUM(Received) as Sum_Received
, SUM(Paid) as Sum_Paid
, SUM(Received - Paid) as Banance
FROM ( SELECT branchcode
, monthyear
, SUM(total) AS Received
, 0.00 AS Paid
FROM deposit
WHERE creationdate >= 'commondate'
GROUP
BY branchcode
, monthyear
UNION all
SELECT branchcode
, monthyear
, 0.00 AS Received
, SUM(totalpaid) AS Paid
FROM expense
WHERE invoicedate >= 'commondate'
GROUP
BY branchcode
, monthyear
) AS d
GROUP
BY branchcode
, monthyear

|
|

08-18-10, 23:39
|
|
Registered User
|
|
Join Date: Apr 2007
Posts: 9
|
|
Quote:
Originally Posted by r937
Code:
SELECT branchcode
, monthyear
, SUM(Received) as Sum_Received
, SUM(Paid) as Sum_Paid
, SUM(Received - Paid) as Banance
FROM ( SELECT branchcode
, monthyear
, SUM(total) AS Received
, 0.00 AS Paid
FROM deposit
WHERE creationdate >= 'commondate'
GROUP
BY branchcode
, monthyear
UNION all
SELECT branchcode
, monthyear
, 0.00 AS Received
, SUM(totalpaid) AS Paid
FROM expense
WHERE invoicedate >= 'commondate'
GROUP
BY branchcode
, monthyear
) AS d
GROUP
BY branchcode
, monthyear

|
R937,
Thanks for the code. I tried to create a view with this query but it returns an error "View's SELECT contains a subquery in the FROM clause". Any workaround?
|
|

08-19-10, 05:24
|
|
SQL Consultant
|
|
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
|
|
Quote:
Originally Posted by slamdunk
Any workaround?
|
create a view for the UNION, then create a view for the outer query
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|