Solved

JSP & MySql: display 1 resultset from multiple tables.

Posted on 2003-11-11
12
529 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
A look at what happened in the Verizon cloud breach.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

617 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