about sqlite offset - What i do not understand? - select

If you check SELECT sqlite clause: https://www.sqlite.org/lang_select.html
You will see OFFSET demands EXPR. to get correct result from your database query.
And when I went checking what is EXPR. (Please see this: https://www.sqlite.org/syntax/expr.html) i see theoretically there should be a way to express a function after offset. For an example:
select * from my_table limit 50 offset count(id);
Count function would give you numeric value, however we know this is not possible. So my question is: Is there any way to add functions to offset or am I reading things in wrong way from links?

It is possible to use functions in the LIMIT/OFFSET expressions:
SELECT 42 LIMIT length('x') OFFSET round(0.123);
The count() function does not work here because it is an aggregate function, and inside the OFFSET clause, there is no table or group over which it could be applied.

It does not work in general. You have to select your count(id) in an extra query.
For more help look here:
Sqlite LIMIT / OFFSET query

Related

where column in (single value) performance

I am writing dynamic sql code and it would be easier to use a generic where column in (<comma-seperated values>) clause, even when the clause might have 1 term (it will never have 0).
So, does this query:
select * from table where column in (value1)
have any different performance than
select * from table where column=value1
?
All my test result in the same execution plans, but if there is some knowledge/documentation that sets it to stone, it would be helpful.
This might not hold true for each and any RDBMS as well as for each an any query with its specific circumstances.
The engine will translate WHERE id IN(1,2,3) to WHERE id=1 OR id=2 OR id=3.
So your two ways to articulate the predicate will (probably) lead to exactly the same interpretation.
As always: We should not really bother about the way the engine "thinks". This was done pretty well by the developers :-) We tell - through a statement - what we want to get and not how we want to get this.
Some more details here, especially the first part.
I Think this will depend on platform you are using (optimizer of the given SQL engine).
I did a little test using MySQL Server and:
When I query select * from table where id = 1; i get 1 total, Query took 0.0043 seconds
When I query select * from table where id IN (1); i get 1 total, Query took 0.0039 seconds
I know this depends on Server and PC and what.. But The results are very close.
But you have to remember that IN is non-sargable (non search argument able), it will not use the index to resolve the query, = is sargable and support the index..
If you want the best one to use, You should test them in your environment because they both work so good!!

Querying timestamp column In q

I want to count the number of records inserted in a kdb+ database using a q query.
Currently, using below query:
count select from executionTable where ingestTimeStamp within 2019.09.07D00:00:00.000000000 2019.09.08D00:00:00.000000000
It works but not highly performant. Any recommendations to make it efficient is highly appreciated.
Thank you for your help.
If you only want count then use 'count i' inside select like below:
q) select count i from executionTable where ingestTimeStamp within 2019.09.07D00:00:00.000000000 2019.09.08D00:00:00.000000000
This will only get the count instead of fetching full data which is what your query is doing and that's one of the reasons for taking more time.
And if it is a partitioned database, then add 'date' in the filter as #Callum Biggs mentioned.
Given the information you have provided I'm assuming you're querying on-disk data, likely saved in a standard date partitioned structure. In this case, you should be specifying a date clause before you specify a time clause, this will prevent searching all the date directories.
select from executionTable where date=2019.09.07, ingestTimeStamp within 2019.09.07D00:00:00.000000000 2019.09.08D00:00:00.000000000
I'd suggest reading through the whitepaper on query optimization, it will give some guidance in good query structure, and how to take advantage of map reduction in kdb.

How to run a query with multiple condition in mysql?

code:
SELECT * FROM `detail` WHERE country='Malaysia' or state='' or region='' ORDER BY rand() LIMIT 4
In this query I want to find record which is related to malaysia. Only one record that I have in my table which is related to country='malaysia' but it show other 4 records. I don't have any idea why its happening?. So, How can I solve this issue? Please help me.
Thank You
You are also including records which have empty string for the state or region. Maybe you should just be checking the country field:
SELECT *
FROM detail
WHERE country = 'Malaysia'
ORDER BY rand()
LIMIT 4;
You can also remove
ORDER BY rand()
because sql doesn't sort the result by default, so this statement is superfluous.

Sort data within a subquery with another subquery?

I am trying to sort the OUN.note column by using the OUN.outcomeKey, since
the way it it is working right now is putting the notes in the wrong order (sorting alphabetically). Any idea on how to go about this? I've been trying to sort the data using another sub-query within, but I haven't had much luck (I don't have a plethora of experience).
Here's my current query:
SELECT DISTINCT OC.outcomeKey [Outcome Key], OC.outcome [Result],
STUFF((SELECT ','+' '+ OUN.note
FROM
Outcome AS OUT
JOIN OutcomeNote AS OUN
ON OUT.outcomeKey = OUN.outcomeKey
WHERE OUN.outcomeKey = OC.outcomeKey
GROUP BY OUN.note
FOR XML PATH ('')), 1, 1, '') [Outcome Note]
FROM Outcome AS OC
Any help or tips would be greatly appreciated! Also, please let me know if any more info is needed.
You may replace the line
GROUP BY OUN.note
with the line
ORDER BY OUN.outcomeKey
Also, because the concatenation starts with ', ', you may want to use 1, 2, '' as the additional arguments of the STUFF function. Otherwise, the values in your [Outcome note] column always start with a space.
Edit:
By the way, sorting the notes by outcomeKey in the subquery that generates the values for the [Outcome note] column has no effect... since all the notes in each subquery result will have the same outcomeKey value...
But you may sort on any column you want, of course. Perhaps there are other columns in your OutcomeNotes table that can serve as a useful sorting column of your outcome notes.
If I misunderstood your question, please provide definitions of the Outcome and OutcomeNote tables, together with a demo population of those tables and the desired/expected query result, please.
Edit 2:
Starting with SQL Server 2017, Transact-SQL contains a function called STRING_AGG, which seems to be functionally equivalent (more or less) to MySQL's GROUP_CONCAT function. Using this function, your query would become something like this:
SELECT
OUN.outcomeKey [Outcome Key],
OC.outcome [Result],
STRING_AGG(OUN.[Note], ', ') WITHIN GROUP (ORDER BY OUN.outcomeKey) [Outcome Note]
FROM
Outcome AS OC
JOIN OutcomeNote AS OUN ON OUN.outcomeKey = OC.outcomeKey
GROUP BY
OUN.outcomeKey,
OC.outcome;
When using SQL Server 2017 or SQL Azure, this might be a more fitting choice, since it does not only make the query more readable, but it also eliminates the use of (way less efficient) XML-functions in your query.
I too have used the XML-functionality for field concatenation (the way you use it) intensively in the past, but I noticed a considerable drop in performance of my queries (which sometimes contained up to 10 columns with concatenated data). Since then, I tend to go for recursive common table expressions or scalar UDF with recursion approaches in pre SQL Server 2017 environments.

Duplicating PostgreSQL's window functions like lag, lead, over

How do I change a PostgreSQL query into a mongodb bson call? I have the same use case listed at http://archives.postgresql.org/pgsql-general/2011-10/msg00157.php I would like to calculate the delta time between two log entries by using something like lag or lead. Is there anything similar in mongodb to Postgres' lag / lead syntax?
select
index,
starttime,
endtime,
starttime - lag(endtime) over(order by starttime asc) as delta
from test
http://www.postgresql.org/docs/8.4/static/functions-window.html
I was looking at http://www.mongovue.com/2010/11/03/yet-another-mongodb-map-reduce-tutorial/ and it seems that map / reduce / finalize should do it. Map the id, start and end time, reduce does nothing, then do a inner join on its self (the double fors) during the finalize. I can almost, kind of, sort of, see it...
This is something you'll have to do in your application. Right now, mongoDB doesn't support anything like this.
You can rewrite some of the window functions as subqueries. See if that's possible in the aggregation framework. This subquery should after the filtering and grouping are done.
Couchbase is going to have the standard window functions. https://blog.couchbase.com/on-par-with-window-functions-in-n1ql/