• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 538
  • Last Modified:

JSP & MySql: display 1 resultset from multiple tables.

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
yapsing
Asked:
yapsing
  • 6
  • 6
1 Solution
 
vk33Commented:
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
 
vk33Commented:
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
 
yapsingAuthor Commented:
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
yapsingAuthor Commented:
TYPE is a MySQL keyword. gotta change the structures abit
0
 
yapsingAuthor Commented:
i tried keying all test data but it return empty set any idea y?
0
 
yapsingAuthor Commented:
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
 
vk33Commented:
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
 
yapsingAuthor Commented:
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
 
vk33Commented:
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
 
vk33Commented:
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
 
yapsingAuthor Commented:
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
 
vk33Commented:
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.

  • 6
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now