How to extract the column data/values with latest timestamp in db2 - java

From below table I want to extract the column values (C_Number) with the latest TimeStamp buy comparing with current system timestamp from db2 table? Please help.
Example: In Table "Computer" there are 3 columns i.e
C_Number | C_Data | TimeStamp
------------------------------------------------------------------------------
12-DFHK | Yes | 2013-08-14 07:33:05.29
13-DFCC | Yes | 2013-08-18 07:45:05.29
Form the above table how can i extract the Column "C_Number" values with latest Timestamp(in this above table latest timestamp is "2013-08-18 07:45:05.29" ) by comparing with current system time.

SELECT C_Number FROM Computer
WHERE TimeStamp = (SELECT MAX(TimeStamp) FROM Computer);

one more efficient way to achive your purpose is the following:
SELECT C_Number
FROM Computer
ORDER BY TimeStamp DESC
FETCH FIRST ROW ONLY ;

Related

how to store a column value as sql operations in mysql automatically

i need a suggestion to store Mysql operations(insert/update)in a particular column of the table automatically like timestamp.
ex: table name: emptable
it contains 4 columns as empid, empname, password, dboperation
while inserting/updating i have to store the operation of the sql query automatically at the last column.
like below
|-----|-------|--------|-----------|
|empid|empname|password|dboperation|
|-----|-------|--------|-----------|
|1 |sai |asdf |insert |
|-----|-------|--------|-----------|
|1 |sairaj |asdf |update |
please suggest me how to do.
thanks in advance

Generic Format DateTime for MySQL and Oracle

I want to cast a string to UTC date. But with environments, database varies and code needs to be changed accordingly as below.
if env1
//mysql
insert into table values (STR_TO_DATE('datetime','%%m/%%d/%%Y %%H:%%i:%%s'))
else
//oracle
insert into table values (to_date('%s', 'MM/DD/YYYY HH24:MI:SS'))
So, Is there a way to handle this generic? by just generating UTC date in code itself and then inserting in database accordingly with any date exception in database?
PreparedStatement has three methods to use to set dates: setDate, setTime and setTimestamp.
You can use either of them that suits you best.
To get the PreparedStatement object, call .prepareStatement("your sql query") on your connection ojbect.
In your case, your query will be "insert into table values (?)"
Within the databases, you can use the same INSERT statement if you use a TIMESTAMP literal (MySQL documentation, Oracle documentation):
SQL Fiddle
Oracle 11g R2 Schema Setup:
CREATE TABLE table_name ( value DATE );
INSERT INTO table_name ( value ) VALUES ( TIMESTAMP '2018-03-23 11:12:00' );
Query 1:
SELECT * FROM table_name
Results:
| VALUE |
|----------------------|
| 2018-03-23T11:12:00Z |
SQL Fiddle
MySQL 5.6 Schema Setup:
CREATE TABLE table_name ( value TIMESTAMP );
INSERT INTO table_name ( value ) VALUES ( TIMESTAMP '2018-03-23 11:12:00' );
Query 1:
SELECT * FROM table_name
Results:
| value |
|----------------------|
| 2018-03-23T11:12:00Z |

Converting Latitude and Longitude(double) data to byte array in order to store in mySql as Geometry or GeometryCollection value in java

My requirement is to store latitude and longitude coordinates(Double dataType values) fetched from a mobile webservice on java server and store in mySQL db as Geometry or Geometry collection datatype. I've created the table using one of the below queries:
CREATE TABLE `mydb`.`location`(
`coords` GEOMETRYCOLLECTION);
or
CREATE TABLE `mydb`.`location`(
`coords` GEOMETRY);
And in NetBeans inside my project, I've created an entity to store values from webservice to the db. And when I've created the entity for the table in my db, the coords column from db is reverse engineered as byte[] in the entity(since the geometry/geometrycollection is a blob in db).
From the client device(lets say a mobile), I get the latitude and longitude as Double values from webService to my java server something like:
lati: 21.0826801 lng: 80.2707184
Now I need to convert those coordinate values which are Double to byte[ ], such that it should look like a spatial data that is derived from one of the 3 queries below:
Query Type 1:
INSERT INTO location VALUES (geomfromwkb(point(9.197915773, 45.476819539)));
Query Type 2:
INSERT INTO location VALUES (geometrycollection(point(9.197915773, 45.476819539)));
Query Type 3:
INSERT INTO location VALUES (geomcollfromwkb(point(9.197915773, 45.476819539)));
How could I achieve it?
Sorry for the long post, I've searched all around the web including stack overflow and found nothing relevant to my current scenario.
The first and third give you a "point", as seen via
mysql> select ASTEXT(geomfromwkb(point(9.197915773, 45.476819539)));
+-------------------------------------------------------+
| ASTEXT(geomfromwkb(point(9.197915773, 45.476819539))) |
+-------------------------------------------------------+
| POINT(9.197915773 45.476819539) |
+-------------------------------------------------------+
1 row in set (0.00 sec)
mysql> select ASTEXT(geometrycollection(point(9.197915773, 45.476819539)));
+--------------------------------------------------------------+
| ASTEXT(geometrycollection(point(9.197915773, 45.476819539))) |
+--------------------------------------------------------------+
| GEOMETRYCOLLECTION(POINT(9.197915773 45.476819539)) |
+--------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> select ASTEXT(geomcollfromwkb(point(9.197915773, 45.476819539)));
+-----------------------------------------------------------+
| ASTEXT(geomcollfromwkb(point(9.197915773, 45.476819539))) |
+-----------------------------------------------------------+
| POINT(9.197915773 45.476819539) |
+-----------------------------------------------------------+
1 row in set (0.01 sec)
I see no need for byte[].

Count Number of Users in Cassandra column family?

I have a table like this in Cassandra-
CREATE TABLE DATA_HOLDER (USER_ID TEXT, RECORD_NAME TEXT, RECORD_VALUE BLOB, PRIMARY KEY (USER_ID, RECORD_NAME));
I want to count distinct USER_ID in my above table? Is there any way I can do that?
My Cassandra version is:
[cqlsh 4.1.1 | Cassandra 2.0.10.71 | DSE 4.5.2 | CQL spec 3.1.1 | Thrift protocol 19.39.0]
The select expression is defined as:
selection_list
| DISTINCT selection_list
so you can:
SELECT DISTINCT USER_ID FROM DATA_HOLDER;

How to handle a secondary INSERT related to a primary INSERT

I have 2 tables: user and photos (in a mysql db).
Here you can see the relation between the 2 tables
User Photos
----------------- -------------------------------
| id | user | | id_user | photo_url |
----------------- -------------------------------
|| /\
||______________________||
|________________________|
When I save a new user into the table "user", if the insert is done successfully, I have to take the value of the "id" field of the user just saved, and save it inside the "id_user" field of the table "photos", in order to make a relation between a user and its photos.
Making the first insert (table "user"), then getting, with a SELECT, the "id" of the last record and then again saving the user's photos it's not the right way to do it.
How should i do it?
To get the auto-incremented ID for the previous insert, you'll want to use mysql_insert_id().
use this toobtain user id:
http://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_last-insert-id
30chars

Categories