Big Query Query the Table and Store Result - google-bigquery

I want to query on of the big query datasets and store the result in on of the table so my code :
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
TableId destinationTable = TableId.of(datasetName, TableName);
QueryJobConfiguration queryConfig = QueryJobConfiguration.newBuilder(my_query)
.setDestinationTable(destinationTable).setWriteDisposition(JobInfo.WriteDisposition.WRITE_APPEND).
build();
TableResult results = bigquery.query(queryConfig);
It's working fine but the problem is with the results it's giving me all the rows present in the TableName , result should only give me the result of my current query not all the rows present in the table. How to make sure results give me only current query result ?

Related

BigQuery SQL for JSON field returns no data

Yet again I find myself flumoxed with my SQL of JSON field in bigquery.
This is the contents of a field called json_data - https://storage.googleapis.com/greyrock_storage/misc/freepik.json
The record has an id of 1675816490
This is my SQL:
SELECT
##JSON_EXTRACT(json_data, '$data.resources.boost.url_source') AS url_source,
JSON_VALUE(boost, "$.url_source") AS url_source,
FROM `my database` ,
UNNEST(JSON_QUERY_ARRAY(json_data.data)) AS data,
UNNEST(JSON_QUERY_ARRAY(data.resources)) AS resources,
UNNEST(JSON_QUERY_ARRAY(resources.boost)) AS boost
WHERE
id = 1675816490
I expected to see a list of all the values in the record for data.resources.boost.url_source BUT it returns 'There is no data to display.'
try like this
SELECT JSON_VALUE(boosts.url_source) AS url_source
FROM `my database` AS a
CROSS JOIN UNNEST(JSON_QUERY_ARRAY(a.json_data.data.resources.boost)) AS boosts

Impala where clouse with bigInt

we have a hive table that has a column in a bigInt type, so I tried to use a simple select query with use where clouse to that column.
Impala
select * form table where id = 1081145387194379590;
but it returns 0 results.
then I tried as below way
select * from dmi.ti_fact_cem_detail_ufdr_other_d
where id = CAST(1081145387194379590 AS BIGINT);
so still I'm getting 0 results.
also my table contain few rows with id = 0;
when I tried below query
select * from dmi.ti_fact_cem_detail_ufdr_other_d
where id = 0;
it returns correct results.
finally I tried the all above queries with Hive
and it returns correct results.
is there anything I missed here

How to combine these two Postgresql queries?

I want to insert into a first table, then update a second table with the returned (DB generated) UUID of the inserted row before finally returning the result of the insert.
The insert query looks like this:
INSERT INTO public.organisations ("name")
VALUES('StackOverflow')
RETURNING *
This will return a row with name, data and id. Data is an empty JSON and can be ignored, id is the UUID used below.
The update query looks like this, with <orgId> indicating the UUID generated and returned by the above insert and <userId> indicating a value passed in from code:
UPDATE public.users
SET id_organisation = <orgID>, last_modified_by = <userID>
WHERE id = <userID>;
Both of these queries work but I do not know how to string them together and return the output of the first query.
Is it possible to do this or would I be better just running two queries?
Use CTEs:
with i as (
INSERT INTO public.organisations ("name")
VALUES('StackOverflow')
RETURNING *
)
UPDATE public.users
SET id_organisation = (SELECT i.id FROM i LIMIT 1), last_modified_by = <userID>
WHERE id=<userID>;
Note that this uses a subquery with LIMIT -- this guarantees that at most one row is returned.
Here is a db<>fiddle illustrating that the syntax works.

SQL query of a string (nvarcahr(256)) doesn't give the data from table

[Table design]:
I inserted to the table using:
INSERT INTO Orders
VALUES (N'Test', 10, 20)
[Data in the table]:
Now when I try to set a query to get row in the table by user name.
So I used the query:
SELECT *
FROM Orders
WHERE User = N'Test'
Trying to get the row by User:
But I get zero results from the query although the table have a row with the User "Test".
For testing I tried to do the same thing but search by the ItemId or ItemAmount and it returns results.
Searching by int returns result
I am new about using SQL Server .mdf database files. But not with .db and SQL query.
So how can I fix the problem or get the unique User row, without adding another col to the table?
You need to escape column name with []:
SELECT * FROM Orders WHERE [User] = N'Test';
DBFiddle Demo
In SQL Server User <> [User]:
SELECT User, [User]
FROM Orders;
DBFiddle Demo2

How to select data from table A but if some data is present in table B show its too into the result

i have some tables like this:
i want to select data from tbl_absensi but some data is present in tbl_setting_pmk
can you help me, how to do this?
this is my Query :
but the data from sp.* isn't present in the result.
Desired output: