My Requirement is to display some of the columns in one table and some of the columns in another table in an html table. Though it has same coloumn id , value will be different. So, I cannot match this two. My query is as follows:
SELECT time_stamp,queryresultset FROM table1 d WHERE dID = 'CP009'
AND d.time_stamp >'2011-05-01 00:00:00' AND d.time_stamp < '2011-05-01 05:00:00'
order by time_stamp
UNION ALL
SELECT time_stamp,cpuutil FROM table2 h WHERE hID='HS002'
AND h.time_stamp >'2011-05-01 00:00:00' AND h.time_stamp < '2011-05-01 05:00:00'
order by time_stamp
So, the time_stampe here I'm getting will vary just in milliseconds for both the table. But, I want it in one resultset. Though the time value varies in millisecones between the table, the number of rows will be equal. So, I have to bring this in a single resultset. I don't know whether it is possible to handle in sql query. Or I may have to try in java coding? Please guide me. Following is my sample html table.
----------------------------------------------------
Time_stamp Cpuutil Queryresultset
----------------------------------------------------
2011-03-09 12:00:00 2.3 9.8
2011-03-09 12:15:00 5.3 4.5
2011-03-09 12:30:00 4.3 9.3
2011-03-09 12:45:00 2.3 9.2
I am afraid, I have difficulties with understanding your question, but it seems to me, you are looking for something as:
SELECT table1.time_stamp t1, table1.queryresultset, table2.time_stamp t2, table2.cpuutil
FROM table1 , table2
WHERE ABS(t1-t2)<100
AND t1 >'2011-05-01 00:00:00'
AND t1 < '2011-05-01 05:00:00'
ORDER by t1
Another posibility:
`SELECT column list
FROM table1
INNER JOIN table2
ON table1.col1=table2.col2
WHERE criteria
ORDER BY column list `
Have a nice day.
This sounds like a database conception mistake. If your two tables are linked logically, they should have a physical connector. If you can refactor your database model, the best solution is to add a table table0 holding the common factors between the two tables (or just an autoincrement id), and then to add an external key to table1 and table2. You need to insert a row into table0 first, then to insert a row into table1 and one into table2 using table0's key as an external key.
If you can't refactor the tables, that's too bad. Anyway, the easiest way to do what you want is to write java code:
Open resultset1 from table 1
Open resultset2 from table 2
While resultset1 is not empty
fetch row1 from resultset1
fetch row2 from resultset2
generate html
Close resultsets
Done!
But this way is wrong and you will get problems as you advance further in your project.
Thanks for your responses. So I found out the query. It's working now.
SELECT a.cpuutil,a.hostid,a.time_stamp, b.queryresultset, b.time_stamp AS tm
FROM table1 a, table2 b
WHERE a.hID = 'hs002' AND b.dID='cp011'
AND SUBSTR(a.time_Stamp,1,15) = SUBSTR(b.time_stamp,1,15)
AND a.time_stamp > '2011-05-10 00:00:00'
AND a.time_stamp < '2011-05-10 14:00:00'
Related
I'm using JDBI3 (and would like to use #SQLUpdate) and an Oracle DB.
I want to insert an item with 4 columns into the table if it does not exist, if it does exist I want to instead update 3 of the 4 values of the item. If it wasn't Oracle I would've used some ON DUPLICATE_KEY logic but that does not exist in Oracle. I read some things about using Merge but the Queries seemed really wonky for what I was trying to do. Any tips on what to look for?
Additional question: If it is Merge I should use (with some form of sub queries I assume), how does the query affect performance? I think this database is quite write heavy.
MERGE INTO device db USING (SELECT 'abc' AS col1, 'bcd' as col2, 'cde' as col3, 'def' as col4 FROM DUAL) input
on (db.col1 = input.col1 AND db.col2= input.col2)
WHEN MATCHED THEN UPDATE
SET db.col4 = input.col4
WHEN NOT MATCHED THEN INSERT
(db.col1, db.col2, db.col3, db.col4)
VALUES (input.col1, input.col2, input.col3, input.col4)
Merge it is. Performs well.
Dummy example based on your description:
merge into target_table a
using source_table b
on (a.id = b.id)
when matched then update set
a.name = b.name,
a.job = b.job,
a.sal = b.sal
when not matched then
insert (id, name, job, sal)
values (b.id, b.name, b.job, b.sal);
I am working on a personal project to help develop SQL skills. The current problem I am having is trying to have my SQL database automatically propagate into the desired table based on a certain column value from 5 other tables.
Is it more efficient to do this on the backend like this or just to query the information from the frontend GUI that is accessing the database and output into a table?
Just wondering if someone can point me in the right direction and not necessarily a solution, I want to figure this out on my own if possible.
This is basically an inventory reporting & tracking tool as of right now:
Database schema:
App
Source Tables for query:
Customer
Demo
Loaner
Training
Other
Target output Table from schema dbo:
Out
Table columns (all the same):
Serial
Model
Date
Category
Status
Skin
Fidelity
Responsibility
OutDate
The intended target value is any row within these source tables that contain the value "Out" within the Status column.
You want to select all rows from three tables with the same structure, where column Status has value 'Out'.
A UNION ALL query should do the trick :
SELECT * FROM Customer WHERE Status = 'Out'
UNION ALL
SELECT * FROM Demo WHERE Status = 'Out'
UNION ALL
SELECT * FROM Loaner WHERE Status = 'Out'
You could use something like:
insert into out (serial,
model,
date,
category,
status,
skin,
fidelity,
responsibility,
outdate)
select
c.serial,
c.model,
c.date,
c.category,
c.status,
c.skin,
c.fidelity,
c.responsibility,
c.outdate
from
customer c, demo d, loaner l
where
c.status = d.status and
d.status = l.status and
l.status = 'out';
I am trying to select one row from all 3 tables. The tables have the same structure, 4 columns, "code", "airportname", "latitude", "longitude".
The query I am trying to write is:
select
finnairroute.code,finnairroute.airportname,finnairroute.latitude,finnairroute.longitude,lhroute.airportname,lhroute.latitude,lhroute.longitude,sasroute.code,sasroute.airportname,sasroute.latitude,sasroute.longitude
FROM frs.sasroute, frs.finnairroute,frs.lhroute where
sasroute.code="BER" or finnairroute.code ="BER" or lhroute.code
="BER";
Tested with Mysql workbench and I got this result:
How should I write the query in order to get one single row that has all the info for "BER"?
Many thanks!
You can try something like this:
SELECT finnairroute.*,lhroute.*,sasroute.* FROM DUAL
LEFT OUTER JOIN finnairroute ON (finnairroute.code='BER')
LEFT OUTER JOIN lhroute ON (lhroute.code='BER')
LEFT OUTER JOIN sasroute ON (sasroute.code='BER')
The thing is that you will always get one row, even if the code is in none of the table. You need to test whether finnairroute.code, lhroute.code and sasroute.code are null or not.
Another way to approach the problem:
SELECT 'finnair' as company,code,airportname,latitude,longitude FROM finnairroute WHERE code='BER'
UNION SELECT 'lh' as company,code,airportname,latitude,longitude FROM lhroute WHERE code='BER'
UNION SELECT 'sas' as company,code,airportname,latitude,longitude FROM sasroute WHERE code='BER'
This gives you between zero and three rows, depending on what tables contain the code 'BER'. An additional column, 'company' tels you which table contains each row.
Using an Oracle DB, I need to select all the IDs from a table where a condition exists, then delete the rows from multiple tables where that ID exists. The pseudocode would be something like:
SELECT ID FROM TABLE1 WHERE AGE > ?
DELETE FROM TABLE1 WHERE ID = <all IDs received from SELECT>
DELETE FROM TABLE2 WHERE ID = <all IDs received from SELECT>
DELETE FROM TABLE3 WHERE ID = <all IDs received from SELECT>
What is the best and most efficient way to do this?
I was thinking something like the following, but wanted to know if there was a better way.
PreparedStatement selectStmt = conn.prepareStatment("SELECT ID FROM TABLE1 WHERE AGE > ?");
selectStmt.setInt(1, age);
ResultSet rs = selectStmt.executeQuery():
PreparedStatement delStmt1 = conn.prepareStatment("DELETE FROM TABLE1 WHERE ID = ?");
PreparedStatement delStmt2 = conn.prepareStatment("DELETE FROM TABLE2 WHERE ID = ?");
PreparedStatement delStmt3 = conn.prepareStatment("DELETE FROM TABLE3 WHERE ID = ?");
while(rs.next())
{
String id = rs.getString("ID");
delStmt1.setString(1, id);
delStmt1.addBatch();
delStmt2.setString(1, id);
delStmt2.addBatch();
delStmt3.setString(1, id);
delStmt3.addBatch();
}
delStmt1.executeBatch();
delStmt2.executeBatch();
delStmt3.executeBatch();
Is there a better/more efficient way?
You could do it with one DELETE statement if two of your 3 tables (for example "table2" and "table3") are child tables of the parent table (for example "table1") that have a "ON DELETE CASCADE" option.
This means that the two child tables have a column (example column "id" of "table2" and "table3") that has a foreign key constraint with "ON DELETE CASCADE" option that references the primary key column of the parent table (example column "id" of "table1"). This way only deleting from the parent table would automatically delete associated rows in the child tables.
Check out this in more detail : http://www.techonthenet.com/oracle/foreign_keys/foreign_delete.php
If you delete only few records of a large tables ensure that an index on the
column ID is defined.
To delete the records from the table TABLE2 and 3 the best strategy is to use the CASCADE DELETE as proposed by
#ivanzg - if this is not possible, see below.
To delete from TABLE1 a far superior option that a batch delete on a row basis, use signle delete using the age based predicate:
PreparedStatement stmt = con.prepareStatement("DELETE FROM TABLE1 WHERE age > ?")
stmt.setInt(1,60)
Integer rowCount = stmt.executeUpdate()
If you can't cascade delete, use for the table2 and 3 the same concept as above but with the following statment:
DELETE FROM TABLE2/*or 3*/ WHERE ID in (SELECT ID FROM TABLE1 WHERE age > ?)
General best practice - minimum logic in client, whole logic in the database server. The database should be able to do reasonable execution plan
- see the index note above.
DELETE statement operates a table per statement. However the main implementations support triggers or other mechanisms that perform subordinate modifications. For example Oracle's CREATE TRIGGER.
However developers might end up figuring out what is the database doing behind their backs. (When/Why to use Cascading in SQL Server?)
Alternatively, if you need to use an intermediate result in your delete statements. You might use a temporal table in your batch (as proposed here).
As a side note, I see not transaction control (setAutoCommit(false) ... commit() in your example code. I guess that might be for the sake of simplicity.
Also you are executing 3 different delete batches (one for each table) instead of one. That might negate the benefit of using PreparedStatement.
We have a use case to count no. of rows returned by actual query. I am not getting how can I get it using criteria in hibernate.
For example suppose I want to run query
select sum(TOTAL_EARNINGS) from table where column1 = 'value1' group by column2);
Along with this query result I also want to get total rows return by above query(above query may have rownum limit also but for total rows I have to ignore rownum limit).
select count(*) from (select sum(TOTAL_EARNINGS) from table
where column1 = 'value1' group by column2);
Is there any easy way of getting this data. I am fine with running separate query to get count data but I am not getting how can I write this in hibernate using criteria. I can't even see any option in SubQueries which I can use
In general with criteria you can use projections.
You can do so, if you have an entity "Your" mapped by Hibernate:
Edited:
Accordingly to your comment, you need two results, a total count and a group by results.
//the group by results...
List result1 = session.createCriteria("Your.class").setProjection(Projections.projectionList()
.add(Projections.groupProperty("column2"))
.add(Projections.sum("totalErnings"))
).list();
//the total count result
return (Integer) session.createCriteria("Your.class").add(Restrictions.eq("column1", val)).setProjection(Projections.rowCount()).uniqueResult();