How could retrieve the specific row from HIVE table? - hive

I have table of 200 rows and 50 columns in a HIVE table.
I could write one Java program to read the input file data by increment line number, when the line counter reached 10th row for top , i could print 10th row of table.
Instead of writing Java program , is there any way to retrieve the 10th row from table using HIVE query?

Do something like this :
select * from (select * from tableName limit 10) as tb1 limit 1;
It should give you the 10th row.
PS : Checked on non-partitioned managed table

Related

Get actual target table insert count

I'm inserting data into hive external table in append mode. Every time I insert some records in a table, I want to get the count of actual records which are inserted into the hive external table. Is there any way I could find this information in any hive log file?
There can be workaround for this. Not sure about any hive property for this.
Have an additional timestamp column in your table.
Do self join on table on timestamp column.
count the latest records inserted into table. You can check below sample query:-
SELECT count(1) from (
SELECT tbl_alias.* FROM test_table tbl_alias JOIN
( select max(timestamp_date) as max_timestamp_date FROM test_table) max_timestamp_date_table ON
tbl_alias.timestamp_date=max_timestamp_date_table.max_timestamp_date ) outer_table;

App Inventor 2 - UPDATE SQL: Fusion table

I'm trying to update a single field in an fusion table using AppInventor. I have successfully obtained the rowid using a select query and stored this value and displayed in a label.
I then want to update a field for this row using the rowid obtained but the rowid is being stored as 'rowid 1001' and not just '1001'
Any suggestions on how I can just have the value of the rowid and not the column heading as well will be greatly appreciated.
Snippet = Do It Result: UPDATE SET 'Name'='Tim' WHERE ROWID = 'rowid 1001'
Many Thanks
The result you get back from the fusiontable is always a table in csv format, in your case it is a 1 column csv table which looks like this
rowid
1001
the first row is the header row, the second row is the rowid you are looking for.
Now just split the result using the split block at \n (new line) to get a list with 2 items. The rowid you are looking for is the second item in that list.

SQL help extracting data from one table and based on a backup table, inserting that data

I have the following SQL (SSMS) statement that returns invalid records for endstrands not found in the fiberstrands tabl:
SELECT * FROM FIBERSPLICE fs
WHERE ENDSTRAND NOT IN (SELECT ID FROM FIBERSTRAND ft)
Every record returned here needs to be rectified and placed back into the fiberstrand table based off a backup of the fiberstrand table.
Now, I have a backup table, FiberStrandHAS, that has all of the fiberstrand records that are missing (plus more) as indicated by the above statement. My goal is to insert the specified records from the above statement using the FiberStranHAS backup table into the fiberstrand table. Any ideas on how I could accomplish this task?
Figured out how to get what I needed...
select *
from fiberstrandhas fsh
inner join (SELECT * FROM FIBERSPLICE fs WHERE ENDSTRAND NOT IN (SELECT ID FROM FIBERSTRAND ft)) es
on (fsh.id = es.endstrand)

Select statement in SQLite recognizing row number

I want to write SQLite statement something like this:
SELECT * FROM Table WHERE RowNumber BETWEEN 1 AND 10;
but i don't have such column RowNumber. I have primary key in my table. But is there row number by default that i could use ?
Also i am searching info about writing more complicated SQLite statement. So if you have some links in bookmarks please share.
Thanks.
You want to use LIMIT and OFFSET
SELECT * FROM Table LIMIT 10 OFFSET 0
Which can also be expressed with the following shorthand syntax
SELECT * FROM Table LIMIT X,Y
Where X represents the offset, which is exclusive, and Y represents the quantity, so for example
SELECT * FROM Table LIMIT 50,50
Would return rows 51-100
The automatically-created rowid for a table can be accessed by a few different names. From the SQLite documentation:
Every row of every SQLite table has a 64-bit signed integer key that uniquely identifies the row within its table. This integer is usually called the "rowid". The rowid value can be accessed using one of the special case-independent names "rowid", "oid", or "_rowid_" in place of a column name.
SELECT * FROM Table WHERE ROWID BETWEEN 1 AND 10;

How to return record count in PostgreSQL

I have a query with a limit and an offset. For example:
select * from tbl
limit 10 offset 100;
How to keep track of the count of the records, without running a second query like:
select count(*) from tbl;
I think this answers my question, but I need it for PostgreSQL. Any ideas?
I have found a solution and I want to share it. What I do is - I create a temp table from my real table with the filters applied, then I select from the temp table with a limit and offset (no limitations, so the performance is good), then select count(*) from the temp table (again no filters), then the other stuff I need and last - I drop the temp table.
select * into tmp_tbl from tbl where [limitations];
select * from tmp_tbl offset 10 limit 10;
select count(*) from tmp_tbl;
select other_stuff from tmp_tbl;
drop table tmp_tbl;
I haven't tried this, but from the section titled Obtaining the Result Status in the documentation you can use the GET DIAGNOSTICS command to determine the effect of a command.
GET DIAGNOSTICS number_of_rows = ROW_COUNT;
From the documentation:
This command allows retrieval of system status indicators. Each item
is a key word identifying a state value to be assigned to the
specified variable (which should be of the right data type to receive
it). The currently available status items are ROW_COUNT, the number of
rows processed by the last SQL command sent down to the SQL engine,
and RESULT_OID, the OID of the last row inserted by the most recent
SQL command. Note that RESULT_OID is only useful after an INSERT
command into a table containing OIDs.
Depends if you need it from the psql CLI or if you're accessing the database from something like an HTTP server. I am using postgres from my Node server with node-postgres. The result set is returned as an array called 'rows' on the result object so I can just do
console.log(results.rows.length)
To get the row count.