App Inventor 2 - UPDATE SQL: Fusion table - sql

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.

Related

Insert a column from output of a query into already populated postgresql database without adding new rows

I have a table named comments_live which has 4 fields and some 24000 entries. I want to add another field which is the output of a select query. The query is like this:
INSERT INTO comments_live SELECT SUBSTR(record,1, POSITION(' ' IN record)) AS project_id FROM comments_live;
What this query is doing is it's appending the result of the SELECT query to the table. I want the 'project_id' field to be appended to the existing 24000 rows. i.e. The above query should not add those extra rows to the table.
Any help would be appreciated.
PS: I tried adding a empty column 'project_id' to the table first and then executing the query, still I'm getting the same result.
I conclude from your code example that you want to add a column whose content is the substring from the column RECORD. If you succeeded in adding the empty column, then you just need code like the following:
update comments_live
set project_id=substr(record,1,[...])

SQL query to SELECT and replace only one column value with out defining the other columns

I have a need to do a select, but I need to replace one column's value out. The table has 25 columns, and I wanted to make this readable with out listing all columns to do the replacement of one column from another table. Here is what i did that does work,
SELECT *
INTO #temp_grouping
FROM [ae_p_phs_e]
WHERE [template_id] = '1010'
AND [status_code] = 'OPEN'
AND [shop] = 'SP-STEAM'
-- select row from the temp table for inserting
UPDATE #temp_grouping
SET
[description] = [source_data].[description]
FROM
[ae_a_asset_e] AS [source_data]
WHERE
[source_data].[asset_tag] = [#temp_grouping].[asset_tag]
AND [source_data].[multitenant_id] = [#temp_grouping].[multitenant_id]
SELECT *
FROM #temp_grouping
--drop the temp table
DROP TABLE #temp_grouping
But what are other ways to do this same thing?
SAMPLE
TABLE A
-------------------------------------------------
|col1 | col2 | description | .... | nthColumn|
-------------------------------------------------
TABLE B
-------------------------------------------------
|col1 | col2 | description | .... | nthColumn|
-------------------------------------------------
EXAMPLE Data return on the first TABLE A
1,2017-026221,001,BAD description,..... VERY LAST
1,2017-026221,002,BAD description,..... VERY LAST
1,2017-026221,003,BAD description,..... VERY LAST
1,2017-026221,004,BAD description,..... VERY LAST
1,2017-026221,005,BAD description,..... VERY LAST
EXAMPLE Data return on the first TABLE B
1,null,XX1,GOOD description,..... VERY LAST
1,null,XX2,GOOD description,..... VERY LAST
1,null,XX3,GOOD description,..... VERY LAST
1,null,XX4,GOOD description,..... VERY LAST
1,null,XX5,GOOD description,..... VERY LAST
EXAMPLE RETURN Data return, basically first TABLE A with one value on TABLE B
1,2017-026221,001,GOOD description,..... VERY LAST
1,2017-026221,002,GOOD description,..... VERY LAST
1,2017-026221,003,GOOD description,..... VERY LAST
1,2017-026221,004,GOOD description,..... VERY LAST
1,2017-026221,005,GOOD description,..... VERY LAST
Assume
Script Table As is a solution to auto fill the column, but is not going to fit for the need. The solution of the
question should not include the opposite of the question's request to
not list the columns as stated in the title.
A solution that requires GRANTS that will let you read the system table are not allowed in many cases.
Solution as of yet
It is starting to sound like the answer is there may not be another way to do what I did with out listing all columns out. If it doesn't turn out to be the case then I'll remove this section.
It is the only way to perform it using dynamic queries.
declare #query nvarchar(max) = ''
declare #temp_grouping nvarchar(max) = 'col1, col2, col3'
set #query = 'SELECT '+#temp_grouping+' FROM [ae_p_phs_e] where [template_id] = ''1010'''
print #query
exec sp_executesql #query
I am afraid your comment didn't really clarify what I'm trying to figure out, but let me go ahead and suggest a solution based on what I'm guessing you are trying to do. Actually, I have two solutions, based on two guesses:
You want to write a SELECT from a table with a lot of columns, so you want to avoid typing out all the column names for no other reason than that it would be a lot of typing. But you can't do a simple SELECT * because there is one column that you want to replace in the output with a column from another table.
Solution: Right click on the table in the SSMS Object Explorer pane, and choose Script Table As > SELECT To > New Query Window.
You will get a new query window with the SELECT query written out, with all the column names written out for you. No typing required. Then just modify that query with the JOIN you want, and find the column you want to replace in the SELECT list and replace it with the column from the JOINed table. This produces the SELECT query you want on an adhoc basis.
You have a more fluid situation where you don't necessarily know, for whatever reason, what the columns are going to be, so therefore you can't hard-code a column list, but you know that if a certain column appears, you want to replace it with a different one.
Solution: use a dynamic sql query, like the one in Rainman's answer. Instead of typing out the column list, like you mention in your comment to his answer, you can dynamically generate that list by querying the system tables to get all the columns belonging to the table you are interested in.

How could retrieve the specific row from HIVE table?

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

SQL update set table if value in table A is equals to value in table B

this query is working fine.
UPDATE data
SET unit_id='a3110a89'
WHERE unit_id='7d18289f';
Now, I need to run this query over 30 times
so I made csv file import it to the DB with this command:
COPY mytable FROM 'D:/test.csv' WITH CSV HEADER DELIMITER AS ','
Now I have table called my table with 2 columns OLD and NEW
i want to search the table "data" in column unit_id anywhere there if the value equals to the value in table "mytable.old" replace it with the value "mytable.new" on the same row.
I tried to run this query but I get an error:
UPDATE data
SET unit_id=(SELECT mytable."old" FROM public.mytable)
WHERE unit_id=(SELECT mytable."new" FROM public.mytable)
error:
more than one row returned by a subquery used as an expression
I think i'm just trying to do it in the wrong way...
thx for the help!
by the way Im using PostgreSQL
Your subqueries need to be correlated to the outer update:
UPDATE data
SET unit_id = (SELECT mytable."new" FROM public.mytable where data.old = mytable.old)
WHERE unit_id in (SELECT mytable."old" FROM public.mytable);
That is, set the unit_id to the "new" value, when you find the "old" value in the table.
Can you try like this,
UPDATE data A
SET A.unit_id=B.old
FROM (SELECT mytable."old",mytable."new" FROM public.mytable) B
WHERE A.unit_id=B.new
UPDATE data A
SET unit_id = B."old"
FROM public.mytable B
WHERE A.unit_id = B."new"
;
BTW: it looks like you also have old and new swapped in your question. Do you really want A's value to be set to B's old field?

Only the latest data show on table

How to create table or maybe trigger that doing like this below?
Date Data
2/6/2013 2
2/6/2013 1
2/6/2013 3
2/6/2013 1
2/6/2013 0
The table at top will alternately as input for next table below, but only the last input will show at table below. The input will alternately from 2 until 0
Just say the input start from 2 - 1 - 3 - 1 - 0, and only 0 will show on table. How to make the trigger? 0 only data, not something unique. Only the latest input will show on table. Remember the table below triggered table at top to take the data.
I just want every time new input inserted, the data that already existed delete and only the new input will show
Data
0
I think you just want a VIEW that shows the latest value.
The following would be the query you'd use for your view...
SELECT TOP 1 Data FROM Table ORDER BY Date DESC
EDIT To do exactly what you want you'd just need a stored procedure for inserting.
-- Copy old value into history
INSERT INTO History SELECT * From Table WHERE yourCondition = True
-- Delete old value
DELETE FROM Table WHERE yourCondition = True
-- Insert new value
INSERT INTO Table VALUES(#Date, #Data)
Now Table will contain only 1 row with the latest value, while History will contain all the old values. This is completely non-standard and irrational, but should be exactly what you want. If you don't need history you can just remove the first INSERT statement.