File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes can you do that in one query? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Databases » JDBC
Reply Bookmark "can you do that in one query?" Watch "can you do that in one query?" New topic
Author

can you do that in one query?

Peter Primrose
Ranch Hand

Joined: Sep 10, 2004
Posts: 755
Say I gave 2 tables A and B

A�s fields: Date_A and Amount_A
B�s files: Date_B and Amount_B

The end-user asks this question:
select amount (A+B) between 2006-01-01 to 2006-12-31

the return result should be as follow:
Total_A is 1500 (until 2006-05-01)
And Total_B is 1000 (from 2006-06-01)

(table A has info until 2006-05-01 � this is the max date) once you get to the max � continue from table B and get the result from 2006-06-01 to 2006-12-31)

* Table A has priority on table B.
* When the range exceeds Table A continue with table B

Is there anyway to do that in one query?

Thanks for any idea or any combination.
stu derby
Ranch Hand

Joined: Dec 15, 2005
Posts: 333
Originally posted by Peter Primrose:
Say I gave 2 tables A and B

A�s fields: Date_A and Amount_A
B�s files: Date_B and Amount_B

The end-user asks this question:
select amount (A+B) between 2006-01-01 to 2006-12-31

the return result should be as follow:
Total_A is 1500 (until 2006-05-01)
And Total_B is 1000 (from 2006-06-01)

(table A has info until 2006-05-01 � this is the max date) once you get to the max � continue from table B and get the result from 2006-06-01 to 2006-12-31)

* Table A has priority on table B.
* When the range exceeds Table A continue with table B

Is there anyway to do that in one query?

Thanks for any idea or any combination.


I don't really understand what you mean, but I'll take a few guesses anyway:



Peter Primrose
Ranch Hand

Joined: Sep 10, 2004
Posts: 755
Stu, thank you for your prompt reply.
I will try to put some light into my question.
here's an example that might help. Thanks again.


if the user requests for the amount between 2006-01-01 to 2006-01-08
the return result shoule be:

1+2+3+4+13+15+16+17+18+19+20

because table_a has all value up to 2006-04-01 and the rest comes from table_b from 2006-05-01

I'm looking for somthing like this:
2006-01-01 to 2006-04-01: 10
2006-01-01 to 2006-05-01: 118
Peter Primrose
Ranch Hand

Joined: Sep 10, 2004
Posts: 755
type:

if the user requests for the amount between 2006-01-01 to 2006-12-01
the return result shoule be:

1+2+3+4+13+15+16+17+18+19+20

because table_a has all value up to 2006-04-01 and the rest comes from table_b from 2006-05-01

I'm looking for somthing like this:
2006-01-01 to 2006-04-01: 10
2006-05-01 to 2006-12-01: 118
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 26192
    
  66

Peter,
Take a look at minus/except. You can use this operator in a way similar to union. It will give you all the rows in table b that are not in table a.

Then you can union that result with table a to get what you are looking for and do a query off of the union'd table.

The only catch is that the tables must have the same column names. You can use a view to do this to simplify things. You could also do it by defining the anonymous tables in your query, but this query is going to be quite involved as is. The views will make things more readable.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
stu derby
Ranch Hand

Joined: Dec 15, 2005
Posts: 333
Peter Primrose
Ranch Hand

Joined: Sep 10, 2004
Posts: 755
stu - you are the best!
thanks

by the way, do you know how to add a month in a query?

as in:

select max(date_a)+ONE_MONTH from....
Peter Primrose
Ranch Hand

Joined: Sep 10, 2004
Posts: 755
got it: INTERVAL(10) day
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: can you do that in one query?
 
Similar Threads
problem framing subquery...
Using two column functions to return a time span
retrieve data from huge result set
when i try run: weka.jar i get these error: "Error: Could not find Java 2 Runtime "
Query problem with MySQL 5