Ordering SQLite table by sum of two columns - sqlite

I have a database that has two integer columns, and I'm trying to find a way to select the top 'x' amount of rows with the highest sums of these two columns. I'm trying to eliminate the need of creating a third column that stores the sum of the two, unless there's a way to to automatically update this column every time one of the other two are altered. I'm using SQLite by the way, as I know there are some slight differences here and there between SQL/SQLite syntax.
Any help is appreciated.

Something like
SELECT a, b
FROM yourtable
ORDER BY a + b DESC
LIMIT :x
should do it.

Related

Count distinct elements across multiple columns

I have a scenario where I got multiple columns with similar content. I want to count how many distinct values are there in all the columns. Slightly different to the below linked case where content of two columns are looked at as a single attribute/element.
Counting DISTINCT over multiple columns
My table is as above. I need to go thru all club columns and count how many distinct clubs there are.
The below code I managed only counts distinct rows. Not individual distinct elements in each column.
select count(*) from( select distinct Club1 Club2 from StudentClubs) as ClubCount
The above returns 6
I need it to output 12 as there are 12 clubs in total.
Thanks in advance.
Actually I found the below solution as a interim. First stacking the columns and then using a count distinct.
It seem to work at least in this example.
select count(distinct A.Club1)
from (
select Club1 from StudentClubs as A
union all
select Club2 from StudentClubs as A
union all
select Club3 from StudentClubs as A
union all
select Club4 from StudentClubs as A
) as A
The above outputs 12.
Please do share if you have better ways to handle this.
Thanks

SQLite: Add up integer tables based on shared indicators

I am aggregating numbers from different sqlite databases into a single output database table.
I need to add up integer columns i1,i2,i3 in the output table based on three indicating columns a,b,c that tell me which rows to update:
ATTACH DATABASE "out.db" AS output;
INSERT INTO output.rows(a,b,c,i1,i2,i3)
SELECT DISTINCT "some_value", b, c, 0, 0, 0 FROM main.rows
ON CONFLICT IGNORE;
#THE FOLLOWING LINES MIGHT SHOW WHAT I MEAN...
UPDATE output.rows SET i1=i1+i1_,i2=i2+i2_, i3=i3+i3_
WHERE a="some_value" AND b=b_ and c=c_
SELECT i1_, i2_, i3_, b_, c_ FROM main.rows;
I do not want to type in all the combinations of a,b,c. As you can see, a does not come from main but from external information (the filename).
In newer versions of SQLite that support UPSERT, the following seems to work:
ATTACH DATABASE "$out.db" AS output;
INSERT INTO output.rows(a,b,c,i1,i2,i3)
SELECT "some_value", b, c, i1, i2, i3 FROM main.rows WHERE true
ON CONFLICT (a,b,c) DO UPDATE SET i1=i1+i1, i2=i2+i2, i3=i3+i3;
In my case, the columns i1,i2,i3 coming from main actually had a different name (say I1,I2,I3) than their counterpart in output, therefore the UPDATE was clearer (i1=i1+I1). I failed to reference as main.rows.i1 inside the UPDATE statement. If you know how to solve that ambiguity, please comment.

sqlite3: Intersect two tables where one value BETWEEN two others

I have two tables, one has single entries like this:
'rs47' 1027
The other has ranges:
'gene1' 1000 1500
These tables are huge, so I am trying to figure out the most efficient way to get all entries from table 1 where the entries are within any range in table 2.
I don't think that INTERSECT can be used like this. I know how to use SELECT to do this for a single entry:
SELECT name FROM 'table2' INDEXED BY 'start_end' WHERE 1027 BETWEEN start AND end
But I am not sure how to do that for every record in a table. Any ideas?
To check whether corresponding rows exist in the other table, you can use a correlated subquery:
SELECT *
FROM Table1
WHERE EXISTS (SELECT 1
FROM Table2
WHERE Table1.Value BETWEEN Table2.StartValue AND Table2.EndValue);

SQLite - Update with random unique value

I am trying to populate everyrow in a column with random ranging from 0 to row count.
So far I have this
UPDATE table
SET column = ABS (RANDOM() % (SELECT COUNT(id) FROM table))
This does the job but produces duplicate values, which turned out to be bad. I added a Unique constraint but that just causes it to crash.
Is there a way to update a column with random unique values from certain range?
Thanks!
If you want to later read the records in a random order, you can just do the ordering at that time:
SELECT * FROM MyTable ORDER BY random()
(This will not work if you need the same order in multiple queries.)
Otherwise, you can use a temporary table to store the random mapping between the rowids of your table and the numbers 1..N.
(Those numbers are automatically generated by the rowids of the temporary table.)
CREATE TEMP TABLE MyOrder AS
SELECT rowid AS original_rowid
FROM MyTable
ORDER BY random();
UPDATE MyTable
SET MyColumn = (SELECT rowid
FROM MyOrder
WHERE original_rowid = MyTable.rowid) - 1;
DROP TABLE MyOrder;
What you seem to be seeking is not simply a set of random numbers, but rather a random permutation of the numbers 1..N. This is harder to do. If you look in Knuth (The Art of Computer Programming), or in Bentley (Programming Pearls or More Programming Pearls), one suggested way is to create an array with the values 1..N, and then for each position, swap the current value with a randomly selected other value from the array. (I'd need to dig out the books to check whether it is any arbitrary position in the array, or only with a value following it in the array.) In your context, then you apply this permutation to the rows in the table under some ordering, so row 1 under the ordering gets the value in the array at position 1 (using 1-based indexing), etc.
In the 1st Edition of Programming Pearls, Column 11 Searching, Bentley says:
Knuth's Algorithm P in Section 3.4.2 shuffles the array X[1..N].
for I := 1 to N do
Swap(X[I], X[RandInt(I,N)])
where the RandInt(n,m) function returns a random integer in the range [n..m] (inclusive). That's nothing if not succinct.
The alternative is to have your code thrashing around when there is one value left to update, waiting until the random number generator picks the one value that hasn't been used yet. As a hit and miss process, that can take a while, especially if the number of rows in total is large.
Actually translating that into SQLite is a separate exercise. How big is your table? Is there a convenient unique key on it (other than the one you're randomizing)?
Given that you have a primary key, you can easily generate an array of structures such that each primary key is allocated a number in the range 1..N. You then use Algorithm P to permute the numbers. Then you can update the table from the primary keys with the appropriate randomized number. You might be able to do it all with a second (temporary) table in SQL, especially if SQLite supports UPDATE statements with a join between two tables. But it is probably nearly as simple to use the array to drive singleton updates. You'd probably not want a unique constraint on the random number column while this update is in progress.

sqlite subqueries with group_concat as columns in select statements

I have two tables, one contains a list of items which is called watch_list with some important attributes and the other is just a list of prices which is called price_history. What I would like to do is group together 10 of the lowest prices into a single column with a group_concat operation and then create a row with item attributes from watch_list along with the 10 lowest prices for each item in watch_list. First I tried joins but then I realized that the operations where happening in the wrong order so there was no way I could get the desired result with a join operation. Then I tried the obvious thing and just queried the price_history for every row in the watch_list and just glued everything together in the host environment which worked but seemed very inefficient. Now I have the following query which looks like it should work but it's not giving me the results that I want. I would like to know what is wrong with the following statement:
select w.asin,w.title,
(select group_concat(lowest_used_price) from price_history as p
where p.asin=w.asin limit 10)
as lowest_used
from watch_list as w
Basically I want the limit operation to happen before group_concat does anything but I can't think of a sql statement that will do that.
Figured it out, as somebody once said "All problems in computer science can be solved by another level of indirection." and in this case an extra select subquery did the trick:
select w.asin,w.title,
(select group_concat(lowest_used_price)
from (select lowest_used_price from price_history as p
where p.asin=w.asin limit 10)) as lowest_used
from watch_list as w

Resources