Solved

JSP & MySql: display 1 resultset from multiple tables.

Posted on 2003-11-11
12
523 Views
Last Modified: 2010-04-01
hi guys,
i'm preparing a few reports for my manager in food business.
i have a few tables:
suppliers-(code,name)
purchase-(id, code,date,doNo,amount)<--related to supplier
foodtrans-(id,date,ttno,type,location,description,amount)
papertrans-(id,date,ttno,type,location,description,amount)

 suppliers & purchase
1) i need to display the list of suppliers and the amount of purchase we made for certain month.
i can group them by code but how can i display them accordingly(sorted by name)?
i.e
<tr>
<td><%=supplier_name%></td>
<td><%=amt_that_belongs_to_it%></td>
</tr>

 foodtrans & papertrans
2) there's 2 types of transfer, In and Out. i need to get the total amount of food and paper transfer according to their types, also for a certain month.
<tr>
<td>Transfer In</td>
<td><%=transIn_amt%></td>
</tr>
<tr>
<td>Transfer Out</td>
<td><%=transOut_amt%></td>
</tr>


all this into 1 dynamic table.
Can i use only 1 query statement?
0
Comment
Question by:yapsing
  • 6
  • 6
12 Comments
 
LVL 4

Expert Comment

by:vk33
ID: 9728622
Hi, yapsing!

1. Assuming that you have a supplier_id field in your purchases table (as a foreign key to maintain reference):

// create database connection before...
String query = "SELECT name,SUM(amount) as total FROM suppliers INNER JOIN purchases ON suppliers.id=purchases.supplier_id " +
      "GROUP BY name ORDER BY name";
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery(query);
while (rs.next()) {
   out.println ("<tr>");
   String name = rs.getString("name");
   int amount = rs.getInt("total");
   out.println ("<td>" + name + "</td>");
   out.println ("<td>" + amount + "</td>");
   out.println ("</tr>");
}

Regards!
0
 
LVL 4

Expert Comment

by:vk33
ID: 9728776
For the secont part, I guess it will look similar.

String query = "SELECT t1.type AS transtype,famount,pamount FROM " +
   "(SELECT type,sum(amount) AS famount FROM foodtrans GROUP BY type) AS t1 FULL OUTER JOIN " +
   "(SELECT type,sum(amount) AS pamount FROM papertrans GROUP BY type) AS t2 ON t1.type=t2.type";
...
while (rs.next()) {
   String type = rs.getString("transtype");
   int famount = rs.getInt(famount);
   int pamount = rs.getInt(pamount);
   ...
}

Good luck!
0
 
LVL 1

Author Comment

by:yapsing
ID: 9728797
oh its u again, glad to u see u here.
Give me some time to try it out, say, 1/2 hr. u using MSN messenger or Yahoo Messenger?
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 1

Author Comment

by:yapsing
ID: 9728807
TYPE is a MySQL keyword. gotta change the structures abit
0
 
LVL 1

Author Comment

by:yapsing
ID: 9728876
i tried keying all test data but it return empty set any idea y?
0
 
LVL 1

Author Comment

by:yapsing
ID: 9728940
You have an error in your SQL syntax near 'SELECT trans,sum(amount) AS famount FROM foodtrans GROUP BY trans) AS t1 FULL OU' at line 1
0
 
LVL 4

Expert Comment

by:vk33
ID: 9729058
Hi!

1. Which of the two queries returned empty set?
2. I guess the syntax in the second query is not good specifically for MySQL (tested on PostgreSQL). MySQL doesn't support FULL OUTER JOIN (amazing!). So you'll need to use some workaroung like this:

SELECT t1.type AS transtype,famount,pamount FROM
(SELECT type,sum(amount) AS famount FROM foodtrans GROUP BY type) AS t1 LEFT OUTER JOIN
(SELECT type,sum(amount) AS pamount FROM papertrans GROUP BY type) AS t2 ON t1.type=t2.type
UNION
(SELECT type,sum(amount) AS famount FROM foodtrans GROUP BY type) AS t3 RIGHT OUTER JOIN
(SELECT type,sum(amount) AS pamount FROM papertrans GROUP BY type) AS t4 ON t3.type=t4.type
0
 
LVL 1

Author Comment

by:yapsing
ID: 9729354
1) ignore it, my mistake
2) papertrans and foodtrans are independent of each other maybe we can solve this by using another approach i.e
<%
select sum(foodtrans.amount) as famountIn,sum(papertrans.amount) as pamountIn from foodtrans,papertrans where foodtrans.type=papertrans.type and foodtrans.type='in' and papertrans.type='in'

float transIn_amt = famountIn + pamountIn;

select sum(foodtrans.amount) as famountOut,sum(papertrans.amount) as pamountOut from foodtrans,papertrans where foodtrans.type=papertrans.type and foodtrans.type='out' and papertrans.type='out'

float transOut_amt = famountOut + pamountOut;
%>

i know this is very amatuer but what else can i do?????
0
 
LVL 4

Expert Comment

by:vk33
ID: 9729882
no, you'll get wrong data:

papertrans:
'in'   10
'in'    20

foodtrans:
'in' 100

select papertrans.type,papertrans.amount,foodtrans.amount from papertrans,foodtrans where papertrans.type='in' and foodtrans.type='in':

type papertrans.amount foodtrans.amount
'in'                     10                      100
'in'                     20                      100

And your sums will be 30 and 200 (not 100).

If you need foodtrans sum and papertrans sum in the same result set, you'll have to process subquery. Isn't it working? If so, I suggest getting the sums separately:

SELECT type,sum(amount) FROM papertrans GROUP BY type
SELECT type,sum(amount) FROM foodtrans GROUP BY type

... and processing two resultsets in your JSP. It's not that difficult.
0
 
LVL 4

Accepted Solution

by:
vk33 earned 50 total points
ID: 9729954
yeah, I just re-readyour code... If you do

float transIn_amt = famountIn + pamountIn;

why not getting famountIn and pamountIn separately?

ResultSet rs1 = st.executeQuery("SELECT type,sum(amount) as pamount FROM papertrans GROUP BY type");
double pamountIn, pamountOut;
while (rs.next()) {
   if (rs.getString("type").equals("in"))
      pamountIn = rs.getFloat(pamount);
   else
      pamountOut = rs.getFloat(pamount);
}
...
ResultSet rs2 = st.executeQuery("SELECT type,sum(amount) as famount FROM foodtrans GROUP BY type");
double famountIn, famountOut;
while (rs.next()) {
   if (rs.getString("type").equals("in"))
      famountIn = rs.getFloat(famount);
   else
      famountOut = rs.getFloat(famount);
}
...

Is it ok?
0
 
LVL 1

Author Comment

by:yapsing
ID: 9730047
yeah i think i'll work like this, less complicated that way. i was thinking i could only use 1 resultset but 2 is better than none. easier to manage
0
 
LVL 4

Expert Comment

by:vk33
ID: 9730375
You can do it with one resultset, no doubt of it, but you'll have to use subqueries. I've tested the solution on Postgres, it works fine...

Good luck in everything!
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
java.util.concurrent.Callable way of creating threads 2 110
some methods private some are not 8 217
own marker interface 1 57
maven set up 2 170
Learn how to PXE Boot both BIOS & UEFI machines with DHCP Policies and Custom Vendor Classes
When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question