universe U2.net SELECT wildcard - wildcard

When i use SQL to query MS access or MS SQL. I can have the following sql to retrieve data
SELECT ORDER_NO,NAME FROM ORDER WHERE LEFT(ORDER_NO,6)='123456'
to retrieve the record with order number start with '123456'
But LEFT is not working in the Universe U2.net.
What is do is below, but the query return result slow.
SELECT ORDER_NO,NAME FROM ORDER WHERE ORDER_NO LIKE '%123456%'
I am looking for alternative for , LEFT,MID and etc to optimize the code
Thank you

Is it any faster if you drop the first '%'? You are looking for a 123456 in any part of the field instead of just starting from the beginning of the field.
This requires the entire value of every that field in every record to be read and evaluated instead of just the first character. With a large set of data this could be a big difference, especially on a non-indexed field.

You can just use the selection operators instead. I think this would be faster, but didn't test:
SELECT ORDER_NO,NAME FROM ORDER WHERE ORDER_NO = "123456]'
For order with the keyword in the middle you would do:
SELECT ORDER_NO,NAME FROM ORDER WHERE ORDER_NO = "[123456]'
And for orders ending in a string:
SELECT ORDER_NO,NAME FROM ORDER WHERE ORDER_NO = "[123456'
[, ], [] are the Ends With, Starts With, Includes.
I believe those are faster than the LIKE lookups.
In addition, you could use LIKE with the ellipsis wildcard syntax:
SELECT ORDER_NO,NAME FROM ORDER WHERE ORDER_NO LIKE "...123456'
SELECT ORDER_NO,NAME FROM ORDER WHERE ORDER_NO LIKE "...123456...'
SELECT ORDER_NO,NAME FROM ORDER WHERE ORDER_NO LIKE "123456...'

Related

Cosmos db Order by on 'computed field'

I am trying to select data based on a status which is a string. What I want is that status 'draft' comes first, so I tried this:
SELECT *
FROM c
ORDER BY c.status = "draft" ? 0:1
I get an error:
Unsupported ORDER BY clause. ORDER BY item expression could not be mapped to a document path
I checked Microsoft site and I see this:
The ORDER BY clause requires that the indexing policy include an index for the fields being sorted. The Azure Cosmos DB query runtime supports sorting against a property name and not against computed properties.
Which I guess makes what I want to do impossible with queries... How could I achieve this? Using a stored procedure?
Edit:
About stored procedure: actually, I am just thinking about this, that would mean, I need to retrieve all data before ordering, that would be bad as I take max 100 value from my database... IS there any way I can do it so I don t have to retrieve all data first? Thanks
Thanks!
ORDER BY item expression could not be mapped to a document path.
Basically, we are told we can only sort with properties of document, not derived values. c.status = "draft" ? 0:1 is derived value.
My idea:
Two parts of query sql: The first one select c.* from c where c.status ='draft',second one select c.* from c where c.status <> 'draft' order by c.status. Finally, combine them.
Or you could try to use stored procedure you mentioned in your question to process the data from the result of select * from c order by c.status. Put draft data in front of others by if-else condition.

Can I loop over the results of an SQL query to use each value in another query all at once in SQL Developer?

What I want to achieve (if it is possible from SQL Developer) is that when I execute the script it do the following:
Run a SELECT statement that will return a list of IDs. Approx 270 records.
I need to use each of those IDs individually in another SELECT statement (in the WHERE clause) which will return some records. A few of this could result in over 17,000 records and some can be one.
Then each result from the second SELECT I want it to be exported to an excel or csv file into a folder at my pc.
I have both 'Select' ready but I don't know how to loop over the results of the first to grab each ID and use it in the second one. Also I don't know how to export automatically from the code.
You can use GROUP BY clause.
read more here:
http://docs.oracle.com/javadb/10.6.2.1/ref/rrefsqlj32654.html
If the first SELECT returns you IDs only, and that's all you need for your second SELECT, just use IN clause providing your first SELECT query for the second one.
Example:
-- Your second select
SELECT
col1
,col2
,col3
,col4
FROM
second_table
WHERE
some_foreign_id IN (
-- Your first select
SELECT
id
FROM
first_table
WHERE
some_conditions...
)
In my opinion don't use PLSQL for this. In PLSQL the only way you can get an output of this by using a REFCURSOR (unless you dont use UTIL File package to do it). A simple SELECT WITH JOIN condition will suffice your requirement.
Test illustration
SELECT A.* FROM TABLE_A A, TABLE_B
WHERE A.COMMON_COLUMN = B.COMMON_COLUMN;
Hope this helped you in same way

SQLite, Sorting a data base by a timestamp

this is my first time asking a question, so bear with me and thanks in advance for any response I get.
I am using sqlite3 on a Macbook pro.
Every record in my database has a time stamp in the form YYYY-MM-DD HH:MM:SS, and I need to sort the entire database by the time stamps. The closest answer I have found to letting me do this is SELECT * FROM Table ORDER BY date(dateColumn) DESC Limit 1 from SQLite Order By Date but this returns the most recent date. I would love to be able to apply this but I am just learning sqlite can't figure how to do so.
Change the limit to the number of rows you want:
SELECT * FROM Table ORDER BY dateColumn DESC Limit 10000000;
you can figure out how many rows you have using
SELECT count(*) FROM Table;
and give a limit greater than that number. Beware: If you want all rows you should really put a limit, because if you don't put a limit and simply do
SELECT * FROM Table ORDER BY dateColumn DESC;
it will limit the output to a certain number depending on your system configurations so you might not get all rows.
When you don't want a limit, omit it.
Please note that it is not necessary to call the date function:
SELECT * FROM MyTable ORDER BY dateColumn;
Just leave off the "Limit 1". The query means "SELECT *" (the star means return all the columns) "FROM Table" (kind of obvious, but from the table name you enter here) "ORDER BY date(dateColumn)" (again, somewhat obvious, but this is the sort order where you put your data column name) "DESC" (backwards sort, leave this off if you want ascending, aka forward, sort) and "Limit 1" (only return the first record in the record set).

Keep first and remove dupliciate rows only using sqlite

Maybe i should do this in C# but i have more then one row with linkId X. I would like to remove it but i am unsure how. In code i could just use a foreach from 0 to n and remove any found rows with a greater (or !=) id but thats in code. Is there a less difficult way of doing it using sqlite?
Assuming the table's name is tableName and there is a primary key field named id, the following sql would do it. I think the following SQL query is general enough and should be able to be executed under any database engine.
delete from tableName
where id not in (
select min(id) from tableName
group by linkId
)

How to get the number of rows of the selected result from sqlite3?

I want to get the number of selected rows as well as the selected data. At the present I have to use two sql statements:
one is
select * from XXX where XXX;
the other is
select count(*) from XXX where XXX;
Can it be realised with a single sql string?
I've checked the source code of sqlite3, and I found the function of sqlite3_changes(). But the function is only useful when the database is changed (after insert, delete or update).
Can anyone help me with this problem? Thank you very much!
SQL can't mix single-row (counting) and multi-row results (selecting data from your tables). This is a common problem with returning huge amounts of data. Here are some tips how to handle this:
Read the first N rows and tell the user "more than N rows available". Not very precise but often good enough. If you keep the cursor open, you can fetch more data when the user hits the bottom of the view (Google Reader does this)
Instead of selecting the data directly, first copy it into a temporary table. The INSERT statement will return the number of rows copied. Later, you can use the data in the temporary table to display the data. You can add a "row number" to this temporary table to make paging more simple.
Fetch the data in a background thread. This allows the user to use your application while the data grid or table fills with more data.
try this way
select (select count() from XXX) as count, *
from XXX;
select (select COUNT(0)
from xxx t1
where t1.b <= t2.b
) as 'Row Number', b from xxx t2 ORDER BY b;
just try this.
You could combine them into a single statement:
select count(*), * from XXX where XXX
or
select count(*) as MYCOUNT, * from XXX where XXX
To get the number of unique titles, you need to pass the DISTINCT clause to the COUNT function as the following statement:
SELECT
COUNT(DISTINCT column_name)
FROM
'table_name';
Source: http://www.sqlitetutorial.net/sqlite-count-function/
For those who are still looking for another method, the more elegant one I found to get the total of row was to use a CTE.
this ensure that the count is only calculated once :
WITH cnt(total) as (SELECT COUNT(*) from xxx) select * from xxx,cnt
the only drawback is if a WHERE clause is needed, it should be applied in both main query and CTE query.
In the first comment, Alttag said that there is no issue to run 2 queries. I don't agree with that unless both are part of a unique transaction. If not, the source table can be altered between the 2 queries by any INSERT or DELETE from another thread/process. In such case, the count value might be wrong.
Once you already have the select * from XXX results, you can just find the array length in your program right?
If you use sqlite3_get_table instead of prepare/step/finalize you will get all the results at once in an array ("result table"), including the numbers and names of columns, and the number of rows. Then you should free the result with sqlite3_free_table
int rows_count = 0;
while (sqlite3_step(stmt) == SQLITE_ROW)
{
rows_count++;
}
// The rows_count is available for use
sqlite3_reset(stmt); // reset the stmt for use it again
while (sqlite3_step(stmt) == SQLITE_ROW)
{
// your code in the query result
}

Resources