Nicer visual formatting for sqlite3 tables? - sqlite

How do I format table output in sqlite? For example, if the default behavior for showing entries is hard to read when one of the columns has variable length text. If I do select a,b,c from table I get
a1|b1|c1
a2|b2|c2
while I want some nicer and easily readible formatting like..
++++++++++++++++
| a1 | b1 | c1 |
| a2 | b2 | c2 |
++++++++++++++++

you can use the dot.commands ".mode columns / .width"
sqlite3 test.db <<SQL
.mode columns
.width 3 10 10 4
select 'ABCDEFGHIJKLM','ABCDEFGHIJKLM','ABC','ABCDEFGHIJKLM';
SQL
will receive
ABC ABCDEFGHIJ ABC ABCD
but with no other formatings like borders etc, so very rudimentary
ant

For sqlite3 you just need a command
.mode table
and your tables should look like this:
+----------+-----------+
| per_id | per2_id |
+----------+-----------+
| 13449958 | 5135490 |
| 21085892 | 5135490 |
| 3291872 | 5135490 |
+----------+-----------+
In the case you want this to be permanent change, you need to write the same command .mode table in the file called .sqliterc (if it doesn't exist just make one) in your home directory.
And that should do it.

Related

Split column string with delimiters into separate columns in azure kusto

I have a column 'Apples' in azure table that has this string: "Colour:red,Size:small".
Current situation:
|-----------------------|
| Apples |
|-----------------------|
| Colour:red,Size:small |
|-----------------------|
Desired Situation:
|----------------|
| Colour | Size |
|----------------|
| Red | small |
|----------------|
Please help
I'll answer the title as I noticed many people searched for a solution.
The key here is mv-expand operator (expands multi-value dynamic arrays or property bags into multiple records):
datatable (str:string)["aaa,bbb,ccc", "ddd,eee,fff"]
| project splitted=split(str, ',')
| mv-expand col1=splitted[0], col2=splitted[1], col3=splitted[2]
| project-away splitted
project-away operator allows us to select what columns from the input exclude from the output.
Result:
+--------------------+
| col1 | col2 | col3 |
+--------------------+
| aaa | bbb | ccc |
| ddd | eee | fff |
+--------------------+
This query gave me the desired results:
| parse Apples with "Colour:" AppColour ", Size:" AppSize
Remember to include all the different delimiters preceding each word you want to extract, e.g ", Size". Mind the space between.
This helped me then i used my intuition to customize the query according to my needs:
https://learn.microsoft.com/en-us/azure/data-explorer/kusto/query/parseoperator

Ingest different csv with different fields to an ADX table

Wondering if its possible to ingest multiple CSV with different number/ type of fields to a single ADX table? (Refer to the csv sample below)
Any way for me to use the header of the csv as the fields?
CSV type sample:
Type A
+--------+-----+--------+
| Name | Age | Uni |
+--------+-----+--------+
| Hazriq | 27 | UNITEN |
+--------+-----+--------+
Type B
+------+------+-----+
| Name | Uni | Age |
+------+------+-----+
| John | UNIx | 31 |
+------+------+-----+
Type C
+------+------+--------------+-----+
| Name | Uni | Hometown | Age |
+------+------+--------------+-----+
| Jane | UNIt | Kuala Lumpur | 31 |
+------+------+--------------+-----+
Yes, you can create multiple CSV mappings and provide the applicable mapping for a given file. You cannot use the CSV headers. Here is an example:
.create table demo(name:string, age:int, uni:string, hometown:string)
//define the mappings
.create table demo ingestion csv mapping "typeA" '[{"Name":"name", "Ordinal":0}, {"Name":"age", "Ordinal":1}, {"Name":"uni", "Ordinal":2}]'
.create table demo ingestion csv mapping "typeB" '[{"Name":"name", "Ordinal":0}, {"Name":"uni", "Ordinal":1}, {"Name":"age", "Ordinal":2}]'
.create table demo ingestion csv mapping "typeC" '[{"Name":"name", "Ordinal":0}, {"Name":"uni", "Ordinal":1}, {"Name":"age", "Ordinal":3}, {"Name":"hometown", "Ordinal":2} ]'
//Ingest some test date
.ingest inline into table demo with (csvMappingReference="typeA", pushbypull=true) <| Hazriq,27,UNITEN
.ingest inline into table demo with (csvMappingReference="typeB", pushbypull=true) <| John,UNIx,31
.ingest inline into table demo with (csvMappingReference="typeC", pushbypull=true) <| Jane,UNIt,Kuala Lumpur,31
//test
demo

SQLite3: dynamic between query

I have this sqlite3 table (simplified):
+--------+----------+-------+
| ROUTE | WPNumber | WPID |
+--------+----------+-------+
| A123 | 1 | WP001 |
| A123 | 2 | WP002 |
| A123 | 3 | WP003 |
| [...] | [...] | [...] |
| A123 | 20 | WP020 |
+--------+----------+-------+
Lets say I want to travel this route in the reverse direction (020 to 001).
How do I get all the WPID's in between? I know it is possible to build a query using BETWEEN and DESC, but then I'd have to build two seperate queries and have Python check when to use which query. Is it possible to have sqlite3 do the work, independent of the direction (reverse or not).
You can reverse the sorting order by reversing the number used in the ORDER BY clause.
Set the parameter ? to either 1 or -1:
SELECT WPID
FROM ThisTable
WHERE ROUTE = 'A123'
ORDER BY WPNumber * ?
If you would just use a similar query with DESC, the database would have a better opportunity to optimize the sorting with an index.

referencing tables in restructuredtext

Is there anyway to refer a table in RestructuredText? something like see table `referencetable`_
(I saw some workarounds for referencing figures. couldn't find a way to refer a table though .. )
Thanks!
You can simply define a hyperlink target.
Here is table-1_.
.. _table-1:
+------------+------------+-----------+
| Header 1 | Header 2 | Header 3 |
+============+============+===========+
| body row 1 | column 2 | column 3 |
+------------+------------+-----------+
| body row 2 | Cells may span columns.|
+------------+------------+-----------+
| body row 3 | Cells may | - Cells |
+------------+ span rows. | - contain |
| body row 4 | | - blocks. |
+------------+------------+-----------+

Last matching date in spreadsheet function

I have a spreadsheet where dates are being recorded in regards to individuals, with additional data, as such:
Tom | xyz | 5/2/2012
Dick | foo | 5/2/2012
Tom | bar | 6/1/2012
On another sheet there is a line in which I want to be able to put in the name, such as Tom, and retrieve on the following cell through a formula the data for the LAST (most recent by date) entry in the first sheet. So the first sheet is a log, and the second sheet displays the most recent one. In the following example, the first cell is entered and the remaining are formulas displaying data from the first sheet:
Tom | bar | 6/1/2012
and so on, showing the latest dated entry in the log.
I'm stumped, any ideas?
If you only need to do a single lookup, you can do that by adding two new columns in your log sheet:
Sheet1
| A | B | C | D | E | F
1 | Tom | xyz | 6/2/2012 | | * | *
2 | Dick | foo | 5/2/2012 | | * | *
3 | Tom | bar | 6/1/2012 | | * | *
Sheet2
| A | B | C
1 | Tom | =Sheet1.E1 | =Sheet1.F1
*(E1) = =IF(AND($A1=Sheet2.$A$1;E2=0);B1;E2)
(i.e. paste the formula above in E1, then copy/paste it in the other cells with *)
Explanation: if A is not what you're looking for, go for the next; if it is, but there is a non-empty next, go for the next; otherwise, get it. This way you're selecting the last one corresponding to your search. I'm assuming you want the last entry, not "the one with the most recent date", since that's what you asked in your example. If I interpreted your question wrong, please update it and I can try to provide a better answer.
Update: If the log dates can be out of order, here's how you get the last entry:
*(F1) = =IF(AND($A1=Sheet2.$A$1;C1>=F2);C1;F2)
*(E1) = =IF(C1=F1;B1;E2)
Here I just replaced the test F2=0 (select next if non-empty) for C1>=F2 (select next if more recent) and, for the other column, select next if the first test also did so.
Disclaimer: I'm very inexperienced with spreadsheets, the solution above is ugly but gets the job done. For instance, if you wanted a 2nd row in Sheet2 to do another lookup, you'd need to add two more columns to Sheet1, etc.

Resources