aspose file tools*
The moose likes JDBC and the fly likes [MySQL/Hibernate] joining 2 resultsets and adding a new column based on condition Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "[MySQL/Hibernate] joining 2 resultsets and adding a new column based on condition" Watch "[MySQL/Hibernate] joining 2 resultsets and adding a new column based on condition" New topic
Author

[MySQL/Hibernate] joining 2 resultsets and adding a new column based on condition

Saurabh Pillai
Ranch Hand

Joined: Sep 12, 2008
Posts: 498
Scenario : We have sensors that senses data on given interval and a java program that saves the data back on the server through webservices. At the end of each day I need to find out if there are any bad sensors (if I do not see sensor reading in database.)

Approach 1 :

- Retrieve total list of sensors from db.
- Retrieve list of sensors from which you heard back from db.
- In java, compare the list and it is done.

No of queries : 2

Approach 2 :

Write a single query that does the left outer join between 2 resultsets (mentioned in approach 1). but this will give me list about ONLY bad sensors. What I want is, list of all sensors with a non-existent column in resultset that tells me about good/bad sensor.

Result set:

Sensor Id          condition(non-existent column)

11111111          bad
22222222          good

I know it is possible to add a new column in resultset based on condition but I can not recall it now. Is it possible to write a single query (later I need to convert to HQL) to achieve this? Should I fall back to approach 1?

Thank you and have a happy new year :-)



Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3435
    
  47

Why should you get only a list of bad sensors with an outer join?

Left outer join contains all rows from the left table. Columns from the right table will contain the proper value from the right table's rows (for rows existing in the right table), or NULL when the row doesn't exist in the right table. If you use a non-null column from the right table, you can be sure that non-null values denote good sensors and NULL denotes bad sensors.
 
Consider Paul's rocket mass heater.
 
subject: [MySQL/Hibernate] joining 2 resultsets and adding a new column based on condition
 
Similar Threads
@Generated values
comparing two ResultSet
parameters in a servlet
ORA-01000: maximum open cursors exceeded
Error Numeric Overflow-oracle