SQLite select query. Get row with array - ios

I coder for iOS and I use SQLite for my data storage.
For now i have two tables in my database. This is table_1 and table_2 (it's easier for understanding)
About struct:
table_1 contain id field (int type) and some_value field (text type)
table_2 contain id field (int type) and table_1_id field (int type)
My question is next: how to select all row from table_1 via array table_1_id field table_2, and this is must SELECT with ORDER BY. I mean order by array.
For example:
if table_2 -> table_1_id contain value 5, 1, 10, 3, 15, 2
the result of output must be in this order.
For now my query is simple:
SELECT * FROM table_1 WHERE id IN (5, 1, 10, 3, 15, 2)
but the select not return order for this query it return data order by 1, 2, 3, 5, 10, 15.
I think I make relation between my table and make SELECT from table_2 uses ORDER BY, but I don't know how to do this correct.
Thanks in advance!
(Very important for me return this order - 5, 1, 10, 3, 15, 2)

I find some result in stackoverflow
link
This is work for me:
SELECT * FROM "comments" WHERE ("comments"."id" IN (1,3,2,4)) ORDER
BY id=1 DESC, ID=3 DESC, id=2 DESC, ID=4 DESC
This is hard code but I create this query string programmatically in cycle and this work great.

Related

typeorm group by using column order number

I'm trying to get typeorm to generate a select query with group by clause, in which the parameters of the group by clause references the column in the select clause using its numeric order number:
SELECT SUBSTRING(A, 1, LENGTH(A)), some_expression(B), DISTINCT C
FROM table
GROUP BY 1, 2
Instead of
SELECT SUBSTRING(A, 1, LENGTH(A)), some_expression(B), DISTINCT C
FROM table
GROUP BY SUBSTRING(A, 1, LENGTH(A)), some_expression(B)
Is there an elegant way to do this? I'm using the QueryBuilder API to generate a sql query which is then used for some other purpose.

PostgreSQL sort by oldest of two dates?

I need to sort a postgres table by the "more recent of column A, fallback to column B"
If my table looks like this: id, reminder_at, updated_at
1, 01-11-2019, 12-01-2018
2, null, 01-04-2019
3, null, 01-02-2019
4, 01-01-2019, 01-04-2019
expected sorting output would be
4, 01-01-2019, 01-04-2019 # 01-01-2019 is soonest
3, null, 01-02-2019 # then 01-02-2019
2, null, 01-04-2019 # then 01-04-2019
1, 01-11-2019, 12-01-2018 # then 01-11-2019
I'm currently doing this with application code, and i'd prefer to do in SQL
For example if the reminder_at went to NULL for record 1, then it would immediately go to the top because the updated_at date is the oldest
Currently:
SELECT *
FROM "tasks"
WHERE completed_at IS NULL
ORDER by reminder_at, updated_at
EDIT with Correct Answer:
SELECT *
FROM "tasks"
WHERE completed_at IS NULL
ORDER by COALESCE(reminder_at, updated_at)
use coalesce. It chooses the first non null value.
select * from tab
order by coalesce(col1, col2)
if instead, you wanted to use the greater of the 2 dates.. then use greatest()
select * from tab
order by greatest(col1, col2)
I am interpreting your data format to be mm-dd. Should 3 and 2 be flipped in your output, the second coming after the fourth?
Does the following work?
select id,reminder_at,updated_at,greatest(coalesce(reminder_at,'1/1/1900'),coalesce(updated_at,'1/1/1900')) as testcolumn from test order by greatest(coalesce(reminder_at,'1/1/1900'),coalesce(updated_at,'1/1/1900'));

SQL Join based on three keys

Database is Teradata
I have two table which I am trying to join. Following are the table structures. When I join these table I expect to get two rows as output but getting 4 rows.what is reason for this behavior. Join based on three keys should uniquely identify a row but still getting 4 rows as output. Any help is appreciated.
TableA
Weekkey|segment|type|users
201501|1|A|100
201501|1|B|100
TableB
Weekkey|segment|type|revenue
201501|1|A|200
201501|1|B|200
when I join these two table using the following query i get the following result
select a.* ,b.user
from tablea a left join tableb b on a.weekkey=b.weekkey
and a.segment=b.segment
and a.type=b.type
Weekkey|segment|type|revenue|users
201501|1|A|200|100
201501|1|B|200|100
201501|1|A|200|100
201501|1|B|200|100
Using sql server, here is ddl and sample data along with the query you posted. The output you state you are getting doesn't happen here.
create table #tablea
(
Weekkey int
, segment int
, type char(1)
, users int
)
insert #tablea
select 201501, 1, 'A', 100 union all
select 201501, 1, 'B', 100
create table #TableB
(
Weekkey int
, segment int
, type char(1)
, revenue int
)
insert #TableB
select 201501, 1, 'A', 200 union all
select 201501, 1, 'B', 200
select a.*
, b.revenue
from #tablea a
left join #tableb b on a.weekkey = b.weekkey
and a.segment = b.segment
and a.type = b.type
drop table #tablea
drop table #TableB

Sqlite where clause with multiple values in select statement

I am trying a sqlite select query statement as below:
SELECT IndicatorText
FROM Table
where IndicatorID in('13','25','64','52','13','25','328')
AND RubricID in('1','1','1','1','1','1','6')
This gives an output but the duplicate values are not displayed. I want to display all the values of IndicatorText even though it is duplicate.
Please help me with this query.
The two IN conditions are evaluated individually.
To check both values at once, you could concatenate them so that you have a single string to compare:
SELECT IndicatorText
FROM MyTable
WHERE IndicatorID || ',' || RubricID IN (
'13,1', '25,1', '64,1', '52,1', '13,1', '25,1', '328,6')
However, doing this operation on the column values prevents the query optimizer from using indexes, so this query will be slow if the table is big.
To allow optimizations, create a temporary table with the desired values, and join that with the original table:
SELECT IndicatorText
FROM MyTable
NATURAL JOIN (SELECT 13 AS IndicatorID, 1 AS RubricID UNION ALL
SELECT 25, 1 UNION ALL
SELECT 64, 1 UNION ALL
SELECT 52, 1 UNION ALL
SELECT 13, 1 UNION ALL
SELECT 25, 1 UNION ALL
SELECT 328, 6)

Finding an array of ids while keeping the order with Rails and PostGreSQL

I have an array of ids of objects that I want to get from the database, but PostGreSQL returns them sorted by ids:
Users.find([4, 1, 3])
=> [User 1, User 3, User 4]
I know I could sort them back like this:
ids = [4, 3, 1]
r = Users.find(ids)
users = ids.map{|id| r.detect{|each| each.id == id}}
But wouldn't it be better if I could do this with the database? I know MySQL has a "field" option. Does PostGreSQL has something equivalent?
Thank you,
Kevin
This function will simulate the one from MySQL
CREATE OR REPLACE FUNCTION field(anyelement, anyarray) RETURNS integer AS
$body$
SELECT COALESCE((
SELECT i
FROM generate_series(array_lower($2, 1), array_upper($2, 1)) s(i)
WHERE $2[i] = $1
),0);
$body$ LANGUAGE SQL STABLE;
Example of usage:
CREATE TABLE x (id integer);
INSERT INTO x (1),(2),(3);
SELECT * FROM x ORDER BY field(id, ARRAY[3,1,2]);
id
----
3
1
2
(3 rows)

Resources