I want to apply this query on the database. There is problem in limit function. Please help me to solve this probem.
This is the database. https://drive.google.com/file/d/1-mwsQHaJHbVzIDXVsj9jw35ZO8TyV6qM/view?usp=drivesdk
SELECT Track.Track, Artist.Artist, Album.Album, Genre.Genre FROM Track JOIN Genre JOIN Album JOIN Artist ON Track.genre_id = Genre.ID and Track.album_id = Album.id AND Album.artist_id = Artist.id ORDER BY Artist.Artist, Track.Track LIMIT 3
Error
android.database.sqlite.SQLiteException: near "limit": syntax error (code 1 SQLITE_ERROR): , while compiling: SELECT Track.Track, Artist.Artist, Album.Album, Genre.Genre FROM Track JOIN Genre JOIN Album JOIN Artist ON Track.genre_id = Genre.ID and Track.album_id = Album.id AND Album.artist_id = Artist.id ORDER BY Artist.Artist, Track.Track LIMIT 3 limit 20 offset 0
Read the error carefully: the end of the message says the following
... ORDER BY Artist.Artist, Track.Track LIMIT 3 limit 20 offset 0
There is a second "limit" clause appended to the query and this is why sqlite can't process it.
I believe your code appends "limit 20 offset 0" to the query automatically. Check the way how you create the query. The error is definitely there.
Related
I have 3 tables: inventory as table 1, buy_transaction as table 2, and po_data as table 3.
These are the queries I tried:
update vnt_inventory_tab.quantity
set vnt_inventory_tab.quantity = quantity + (SELECT pdt.quantity
FROM vnt_po_data_tab pdt JOIN vnt_buy_trnsctn_tab bt ON pdt.po_no = bt.po_no
WHERE pdt.po_no = :p_po_no)
where VNT_INVENTORY_TAB.CODE = VNT_PO_DATA_TAB.CODE;
It return as table doesn't exist. I'm not sure where I put wrong because when I do the sub-query it returns exactly the number I want to update into the inventory.
Help me pls
Syntax UPDATE statement requires only table-Name (not a specific column like yours) after the keyword update. More details see Oracle docs
i have been trying to find an answer but the things i found and tried does not seem to work, or i am doing something wrong.
As an example we have 2 tables:
Table Messages:
Int: id
Date: create_timestamp
String: join_number
Table Map:
String: entity_name
String: join_number
I am trying to count create_timestamps and group them by Entity_name, so the query is:
select count(create_timestamp) as count , entity_name from Messages left join Map on Messages.join_number = Map.join_number group by entity_name
The query is working fine except of one problem it does not return entity with result 0 if no timestamps were found. I need it to return the entity_name and result 0 if it found nothing.
I tried query with COALESCE like this:
select COALESCE(count(create_timestamp), 0) as count , entity_name from Messages left join Map on Messages.join_number = Map.join_number group by entity_name
But it still does not return 0 values. Please keep in mind this is a DB2 Database, any comments, questions, or answers are really welcome here.
It turns out answer by Mark is correct. I did a mistake on query as i also included the condition "where timestamp > timestamp (xxxx)" which caused the query not to include those 0 results. In case someone can find it helpful, the correct query if you want to choose the timestamp is:
select count(timestamp) as count, entity_name from Map left join Messages on timestamp > timestamp('2020-01-01') and Messages.join_number = Map.join_number group by entity_name
Thanks a lot again!
My Aim
I would like to count the number of distinct values of FileName for Azure Cosmos DB documents like the following in a single partition, using the SQL API.
{
"id": "some uuid",
"FileName": "file-1.txt",
"PartitionKeyField": "some key",
... other fields ...
}
My Test
I have uploaded 533,956 documents with 500,000 different FileName values, i.e. 33,956 documents have duplicate FileName (other fields are different). These are all uploaded with the same PartitionKeyField.
(I can only reproduce the behaviour below for 100,000s of documents).
I would like to count the number of distinct FileName values - so hope to get back 500,000.
Attempt 0 - Sanity Check
If I run the following query:
SELECT DISTINCT c.FileName
FROM c
WHERE c.PartitionKeyField = 'some key'
This returns 500,000 documents as expected.
Attempt 1
However, I don't need all the documents, I just need the count, so I try to run the following query
SELECT VALUE COUNT(1)
FROM (
SELECT DISTINCT c.FileName
FROM c
WHERE c.PartitionKeyField = 'some key'
) c2
But this gives 533,956 - i.e. it's as if DISTINCT has not been applied.
Attempt 2
Next I tried the following, redundant GROUP BY in an attempt to force the count to work:
SELECT c2.PartitionKeyField, COUNT(1)
FROM (
SELECT DISTINCT c.FileName
FROM c
WHERE c.PartitionKeyField = 'some key'
) c2
GROUP BY c2.PartitionKeyField
The result returned by this depends on how many RUs is allocated to the collection, e.g.
Returns 500,007 at 9900 RUs
Returns 500,175 at 5000 RUs
Returns 500,441 at 3000 RUs
Returns 500,812 at 1000 RUs
Returns 501,406 at 400 RUs
Also, the above values are averages, e.g. for 9900 RUs results of 500,009 and 500,006 were also returned.
Questions
Is it possible to write the required "count" query in a deterministic way that doesn't depend on the number of RUs? (other than retrieving all documents as in Attempt 0?)
Why does increasing the number of RUs change the result of the query in Attempt 2?
Please try this SQL:
SELECT VALUE COUNT(c2)
FROM (
SELECT DISTINCT c.FileName
FROM c
WHERE c.PartitionKeyField = 'some key'
) c2
I tried to apply the solution in Google BigQuery - Updating a nested repeated field to the field hits.transaction.transactionRevenue, but I receive error message:
Scalar subquery produced more than one element
I have tried to run the following query:
UPDATE `project_id.dataset_id.table`
SET hits = ARRAY(
SELECT AS STRUCT * REPLACE (
(SELECT AS STRUCT transaction.* REPLACE (1 AS transactionRevenue)) AS transaction
)
FROM UNNEST(hits) as transactionRevenue
)
WHERE (select h.transaction.transactionId from unnest(hits) as h) LIKE 'ABC123XYZ'
Are there any obvious mistakes on my part? Would be great if anyone could share some tips or experiences that could help me with this.
What I basically want to do is to set the revenue of a specific transaction to 1.
Many thanks in advance,
David
This is the problem:
WHERE (select h.transaction.transactionId from unnest(hits) as h) LIKE 'ABC123XYZ'
If there is more than one hit in the array, this will cause the error that you are seeing. You probably want this instead:
WHERE EXISTS (select 1 from unnest(hits) as h WHERE h.transaction.transactionId LIKE 'ABC123XYZ')
But note that your UPDATE will now replace all elements of the array for any row where this condition is true. What you may want is to move the condition inside the ARRAY function call instead:
UPDATE `project_id.dataset_id.table`
SET hits = ARRAY(
SELECT AS STRUCT * REPLACE (
(SELECT AS STRUCT transaction.* REPLACE (1 AS transactionRevenue)) AS transaction
)
FROM UNNEST(hits) as h
WHERE h.transaction.transactionId LIKE 'ABC123XYZ'
)
WHERE true
Now the replacement will only apply to hits with a transaction ID matching the pattern.
I have the following columns in a SQLite DB.
id,ts,origin,product,bid,ask,nextts
1,2016-10-18 20:20:54.733,SourceA,Dow,1.09812,1.0982,
2,2016-10-18 20:20:55.093,SourceB,Oil,7010.5,7011.5,
3,2016-10-18 20:20:55.149,SourceA,Dow,18159.0,18161.0,
How can I populate the 'next timestamp' column (nextts) with the next timestamp for the same product (ts), from the same source? I've been trying the following, but I can't seem to put a subquery in an UPDATE statement.
UPDATE TEST a SET nextts = (select ts
from TEST b
where b.id> a.id and a.origin = b.origin and a.product = b.product
order by id asc limit 1);
If I call this, I can display it, but I haven't found a way of updating the value yet.
select a.*,
(select ts
from TEST b
where b.id> a.id and a.origin = b.origin and a.product = b.product
order by id asc limit 1) as nextts
from TEST a
order by origin, a.id;
The problem is that you're using table alias for table in UPDATE statement, which is not allowed. You can skip alias from there and use unaliased (but table-name prefixed) reference to its columns (while keeping aliased references for the SELECT), like this:
UPDATE TEST
SET nextts = (
SELECT b.ts
FROM TEST b
WHERE b.id > TEST.id AND
TEST.origin = b.origin AND
TEST.product = b.product
ORDER BY b.id ASC
LIMIT 1
);
Prefixing unaliased column references with the table name is necessary for SQLite to identify that you're referencing to unaliased table. Otherwise the id column whould be understood as the id from the closest[*] possible data source, in which case it's the aliased table (as b alias), while we're interested in the unaliased table, therefore we need to explicitly tell SQLite that.
[*] Closest data source is the one listed in the same query, or parent query, or parent's parent query, etc. SQLite is looking for the first data source (going from inner part to the outside) in the query hierarchy that defines this column.