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.

 
Go Back  dBforums > Database Server Software > MySQL > Create view from two tables

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-16-10, 02:30
slamdunk slamdunk is offline
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;
Reply With Quote
  #2 (permalink)  
Old 08-16-10, 05:13
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
Quote:
Originally Posted by slamdunk View Post
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
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 08-18-10, 01:13
slamdunk slamdunk is offline
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!
Reply With Quote
  #4 (permalink)  
Old 08-18-10, 02:28
r937 r937 is offline
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
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 08-18-10, 23:39
slamdunk slamdunk is offline
Registered User
 
Join Date: Apr 2007
Posts: 9
Quote:
Originally Posted by r937 View Post
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?
Reply With Quote
  #6 (permalink)  
Old 08-19-10, 05:24
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
Quote:
Originally Posted by slamdunk View Post
Any workaround?
create a view for the UNION, then create a view for the outer query
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On