KDB+ / Q Syntax optimizations to oneliners - performance

I am definitly a q-mortal. I would even say a q-baby. Well I have some question how to put together my code from separate lines to a one-liner. I guess there is a way more elegant solution than mine.
How to write the following statements in one line:
q)t1:(3#3)?\:`8
q)t1[;0]:`abc
In this table creation, how can I add another column which I have as list (like with the command ([]id:id_list;data:data_list;.....)). Till now I am creating another table and doing an inner join on them. I guess that's not very efficient:
`id xkey update id:i from flip (`row1`row2!(1 2;3 4))

For the 1st one you may do:
q)`abc,/:(3#2)?\:`8
abc jognjhck cihanjhp
abc hkpblald aeajbddp
abc blmjhgah ooeiogdj
For 2nd one you may treat the table as a dictionary:
q)tb
row1 row2
---------
1 3
2 4
q)(tb`id): 5 6
q)tb
row1 row2 id
------------
1 3 5
2 4 6

Two options:-
Create a 3*2 matrix of random symbols and append `abc in front.
q)`abc,/:(3#2)?\:`8
Or create 3*3 matrix of random symbols and use functional form of amend to update the first column. This is a more generic approach.
q).[(3#3)?\:`8;(::;0);:;`abc]
I don't see any problem with using just update statement.
q)c:1 2
q)t
row1 row2
---------
1 3
2 4
q)update newCol:c from `t
`t
q)t
row1 row2 newCol
----------------
1 3 1
2 4 2

Related

Sort Column header based on row value, and show as columns

We have sheet with column names and values in the cells below.
We like to have a list of the names and the value next to it ordered.
example.
A
B
C
D
E
1
John
Mary
Tom
Grace
2
3
4
5
2
and we would like the same data below which looks like...
A
B
1
Tom
5
2
Mary
4
3
John
3
4
Grace
2
Any ideas? Thanks
use:
=SORT(TRANSPOSE(A1:D2), 2, )
or:
=SORT(TRANSPOSE({A1:D1; A4:D4}), 2, )
SUGGESTION
Perhaps you can try this way:
=QUERY(TRANSPOSE(A1:D2),"SELECT * order by Col2 DESC")
Sample Sheet
Reference
TRANSPOSE
QUERY

how to sort 2 different columns in google sheet individually?

I have a Google Sheet that has 2 columns with integer rows in it. Both these columns have no relation to each other. But when I apply a A->Z sort on the 1st column the 2nd column values also change and vice versa. My task is to SORT these 2 columns individually in ascending order and create a 3rd column which checks if the values of this 1st 2 column are equal or not.
Example:
Col1 Col2
4 5
1 8
2 9
5 1
Expected Output after sorting them individually:
col1 col2
1 1
2 5
4 8
5 9
Solution 1:
You can filter and sort each column separately:
Solution 2:
Another solution would be to create the desired output by sorting each column separately and concatenating the results:
={{"Col1";sort(A2:A)},{"Col2";sort(B2:B)}}

Distinct on two columns with same data type

In my game application I have a combats table:
id player_one_id player_two_id
---- --------------- ---------------
1 1 2
2 1 3
3 3 4
4 4 1
Now I need to know hoy many unique users played the game. How can I apply distinct, count on both columns player_one_id and player_two_id?
Many thanks.
By using union you can get unique distinct value.
$playerone = DB::table("combats")
->select("combats.player_one_id");
$playertwo = DB::table("combats")
->select("combats.player_two_id")
->union($playerone)
->count();

Oracle. Leave blank line on composite key level break

I want the output of my SQL to leave a blank line when any of three columns (a b or c below) change.
So if the table had columns a b c and d, you end up with a report like this:
a b c d #<- column names
-------
1 a a a #<- V - data itself
1 a a b
#<- level break here because 1 a b is different from 1 a a
1 a b c
From Googling, I have seen BREAK might solve this.
But from what I can make out this pertains to one column.
I then thought what if I have a computed column.
a is numeric, b & c are alphanumeric data types.. so I guess I could possibly use CONCAT plus TO_CHAR..
I am wondering if anyone can give me some pointers?
Cheers.
You can break on more than one column:
SQL> break on a skip 1 duplicates on b skip 1 duplicates on c skip 1 duplicates
SQL> select * from your_table
A B C D
---------- - - -
1 a a a
1 a a b
1 a b c
3 rows selected.
If you issue a plain break it shows what is set:
SQL > break
break on a skip 1 dup
on b skip 1 dup
on c skip 1 dup
Another option is to generate an extra column expression of your composite key, break on that, and set it not to display:
SQL> break on composite skip 1
SQ>> column composite noprint
SQL> select t.*, a||':'||b||':'||c as composite from your_table t;
A B C D
---------- - - -
1 a a a
1 a a b
1 a b c
3 rows selected.
which has the advantage of not showing multiple blank lines if more than one column changes at the same time.
I've separated the values with a colon; the idea of that is to use a character that doesn't appear in the values themselves, to avoid an accidental clash. If any of the columns could actually have a colon then pick something else more obscure.

What if the value of order field is the same for all the records [duplicate]

This question already has answers here:
Why does Oracle return specific sequence if 'orderby' values are identical?
(4 answers)
Closed 7 years ago.
All, Let's say the SQL looks like below.
Select a, b ,c from table1 order by c
If all the rows in table1 have the same field value in the field c. I want to know if the result has the same order for each time I executed the SQL.
Let's say data in the table1 looks like below.
a b c
-------------------------------------------
1 x1 2014-4-1
....
100 x100 2014-4-1
....
1000 x1000 2014-4-1
....
How Oracle determine the rows sequence for the same order by value?
Added
Will they be random sequence for each time?
One simple answer is NO. There is no guarantee that the ORDER BY on equal values will return the same sorted result every time. It might seem to you it is always stable, however, there are many reasons when it could change.
For example, the sorting on equal values might defer after:
Gathering statistics
Adding an index on the column
For example,
Let's say I have a table t:
SQL> SELECT * FROM t ORDER BY b;
A B
---------- ----------
1 1
2 1
3 2
4 2
5 3
6 3
6 rows selected.
The sorting on the column having similar values is just like:
SQL> CREATE TABLE t1 AS SELECT * FROM t ORDER BY b, DBMS_RANDOM.VALUE;
Table created.
SQL> SELECT * FROM t1 ORDER BY b;
A B
---------- ----------
1 1
2 1
4 2
3 2
5 3
6 3
6 rows selected.
So, similar data in bot the tables, however, ORDER BY on the column having equal values, dos not guarantee the same sorting.
They must not be random (change each time), but the order is not guaranteed (change sometimes).

Resources