How can I produce a table with column headers from org-babel shell output? - org-babel

I'm trying to add headers to this:
#+BEGIN_SRC sh :dir ~ :results table
for n in 1 2 3 4; do
echo $n $(($n * $n))
done
#+END_SRC
Which results in:
#+RESULTS:
| 1 | 1 |
| 2 | 4 |
| 3 | 9 |
| 4 | 16 |
The output I want is:
#+RESULTS:
| N | N*N |
|---+-----|
| 1 | 1 |
| 2 | 4 |
| 3 | 9 |
| 4 | 16 |
The difficulty I'm having is injecting the second line. This does not work:
#+BEGIN_SRC sh :dir ~ :results table
echo "N N**2"
echo "|-"
for n in 1 2 3 4; do
echo $n $(($n * $n))
done
#+END_SRC
This results in:
#+RESULTS:
| N | N**2 |
| | - |
| 1 | 1 |
| 2 | 4 |
| 3 | 9 |
| 4 | 16 |
Neither can I just use a blank line, as suggested here:
#+BEGIN_SRC sh :dir ~ :results table
echo "N N**2"
echo
for n in 1 2 3 4; do
echo $n $(($n * $n))
done
#+END_SRC
As this results in:
#+RESULTS:
| N | N**2 |
| | |
| 1 | 1 |
| 2 | 4 |
| 3 | 9 |
| 4 | 16 |
Any hints greatly appreciated!

I think :results org is what you're looking for, and then make your code output what you'd type yourself as a table with separators (quoted to protect from the shell)
#+BEGIN_SRC sh :dir ~ :results org
echo "|N|N**2"
echo "|-"
for n in 1 2 3 4; do
echo "|" $n "|" $(($n * $n))
done
#+END_SRC
That produces this (emacs 25.1.50.1, org 8.3.3):
#+RESULTS:
#+BEGIN_SRC org
| N | N**2 |
|---+------|
| 1 | 1 |
| 2 | 4 |
| 3 | 9 |
| 4 | 16 |
#+END_SRC

Related

dense_rank over boolean column

Good day. I have a permutated table with condition and I am running redshift DB. This is a table with events log and I splitted it into session start (bool = 1) and session continue (bool = 0) like this:
=======================
| ID | BOOL |
=======================
| 1 | 0 |
| 2 | 1 |
| 3 | 0 |
| 4 | 0 |
| 5 | 0 |
| 6 | 0 |
| 7 | 0 |
| 8 | 0 |
| 9 | 0 |
| 10 | 0 |
| 11 | 1 |
| 12 | 0 |
| 13 | 0 |
| 14 | 1 |
| 15 | 0 |
| 16 | 0 |
=======================
I need to create sesssion_id column with something like dense_rank:
================================
| ID | BOOL | D_RANK |
================================
| 1 | 0 | 1 |
| 2 | 1 | 2 |
| 3 | 0 | 2 |
| 4 | 0 | 2 |
| 5 | 0 | 2 |
| 6 | 0 | 2 |
| 7 | 0 | 2 |
| 8 | 0 | 2 |
| 9 | 0 | 2 |
| 10 | 0 | 2 |
| 11 | 1 | 3 |
| 12 | 0 | 3 |
| 13 | 0 | 3 |
| 14 | 1 | 4 |
| 15 | 0 | 4 |
| 16 | 0 | 4 |
================================
Is there any option to do this? Would appreciate any help.
Use a cumulative sum. Assuming that bool is the start of a new session:
select t.*,
sum(bool) over (order by id) as session_id
from t;
Note: This will start at 0. You can add 1 if you need.

Query with WITH clause and COUNT subquery

In the query below, I don't get the results i would expect. Any insights why? How could i reformulate such query to get the desired results?
Schema (SQLite v3.30)
WITH RECURSIVE
cnt(x,y) AS (VALUES(0,ABS(Random()%3)) UNION ALL SELECT x+1, ABS(Random()%3) FROM cnt WHERE x<10),
i_rnd as (SELECT r1.x, r1.y, (SELECT COUNT(*) FROM cnt as r2 WHERE r2.y<=r1.y) as idx FROM cnt as r1)
SELECT * FROM i_rnd ORDER BY y;
result:
| x | y | idx |
| --- | --- | --- |
| 1 | 0 | 3 |
| 5 | 0 | 6 |
| 8 | 0 | 5 |
| 9 | 0 | 4 |
| 10 | 0 | 2 |
| 3 | 1 | 4 |
| 0 | 2 | 11 |
| 2 | 2 | 11 |
| 4 | 2 | 11 |
| 6 | 2 | 11 |
| 7 | 2 | 11 |
expected result:
| x | y | idx |
| --- | --- | --- |
| 1 | 0 | 5 |
| 5 | 0 | 5 |
| 8 | 0 | 5 |
| 9 | 0 | 5 |
| 10 | 0 | 5 |
| 3 | 1 | 6 |
| 0 | 2 | 11 |
| 2 | 2 | 11 |
| 4 | 2 | 11 |
| 6 | 2 | 11 |
| 7 | 2 | 11 |
In other words, idx should indicate how many rows have y less or equal than the y of row considered.
I would just use:
select cnt.*,
count(*) over (order by y)
from cnt;
Here is a db<>fiddle.
The issue with your code is probably that the CTE is re-evaluated each time it is called, so the values are not consistent -- a problem with volatile functions in CTEs.

The results of two queries into one table

I have a SQL table that looks something like this:
| FileName | Category | Value | Number |
|:---------:|:--------:|:-----:|:------:|
| TAG File1 | First | 10 | 1 |
| TAG File1 | Second | 8 | 1 |
| TAG File1 | Third | 4 | 1 |
| TAG File2 | First | 13 | 1 |
| TAG File2 | Second | 5 | 1 |
| TAG File2 | Third | 6 | 1 |
| TAG File1 | First | 11 | 2 |
| TAG File1 | Second | 7 | 2 |
| TAG File1 | Third | 5 | 2 |
| TAG File2 | First | 14 | 2 |
| TAG File2 | Second | 6 | 2 |
| TAG File2 | Third | 5 | 2 |
| TAG File1 | First | 10 | 3 |
| TAG File1 | Second | 6 | 3 |
| TAG File1 | Third | 5 | 3 |
| TAG File2 | First | 12 | 3 |
| TAG File2 | Second | 7 | 3 |
| TAG File2 | Third | 4 | 3 |
| TAG File1 | First | 11 | 4 |
| TAG File1 | Second | 8 | 4 |
| TAG File1 | Third | 5 | 4 |
| TAG File2 | First | 13 | 4 |
| TAG File2 | Second | 5 | 4 |
| TAG File2 | Third | 5 | 4 |
I wanted to write a query that will only show the results for the two "most recent" values in the Numbercolumn. The number column is a counting value. Everytime this table is updated with a new set of data, the value in the Number column for that set of data is +1 from the max value. Ultimately, I want a query that would accomplish what this query would.
select FileName, Category, Value, (select max(Number) from Table) as Number
from Table;
while also having these results in the table as well:
select FileName, Category, Value, (select max(Number)-1 from Table) as Number
from Table;
The results should look something like this:
| FileName | Category | Value | Number |
|:---------:|:--------:|:-----:|:------:|
| TAG File1 | First | 10 | 3 |
| TAG File1 | Second | 6 | 3 |
| TAG File1 | Third | 5 | 3 |
| TAG File2 | First | 12 | 3 |
| TAG File2 | Second | 7 | 3 |
| TAG File2 | Third | 4 | 3 |
| TAG File1 | First | 11 | 4 |
| TAG File1 | Second | 8 | 4 |
| TAG File1 | Third | 5 | 4 |
| TAG File2 | First | 13 | 4 |
| TAG File2 | Second | 5 | 4 |
| TAG File2 | Third | 5 | 4 |
Use a subquery to find the max number
SELECT * FROM table WHERE number >= (SELECT MAX(number) FROM table) - 1
You can use a subquery to get the 2 largest, distinct numbers:
select FileName, Category, Value, Number
from Table
where Number in (SELECT DISTINCT TOP 2 Number FROM Table ORDER BY Number desc);
Try this
SELECT
FileName,
Category,
Value,
Number
FROM
TABLE T
WHERE
T.Number IN
(
SELECT DISTINCT TOP 2 Number
FROM Table IT
WHERE
IT.FileName = T.FileName AND
IT.Category = T.Category
ORDER BY IT.Number DESC
)

Selecting the maximum value only for another maximum value

If I have two int data type columns in SQL Server, how can I write a query so that I get the maximum number, at the maximum number of the other column?
Let me give an example. Lets say I have this table:
| Name | Version | Category | Value | Number | Replication |
|:-----:|:-------:|:--------:|:-----:|:------:|:-----------:|
| File1 | 1.0 | Time | 123 | 1 | 1 |
| File1 | 1.0 | Size | 456 | 1 | 1 |
| File2 | 1.0 | Time | 312 | 1 | 1 |
| File2 | 1.0 | Size | 645 | 1 | 1 |
| File1 | 1.0 | Time | 369 | 1 | 2 |
| File1 | 1.0 | Size | 258 | 1 | 2 |
| File2 | 1.0 | Time | 741 | 1 | 2 |
| File2 | 1.0 | Size | 734 | 1 | 2 |
| File1 | 1.1 | Time | 997 | 2 | 1 |
| File1 | 1.1 | Size | 997 | 2 | 1 |
| File2 | 1.1 | Time | 438 | 2 | 1 |
| File2 | 1.1 | Size | 735 | 2 | 1 |
| File1 | 1.1 | Time | 786 | 2 | 2 |
| File1 | 1.1 | Size | 486 | 2 | 2 |
| File2 | 1.1 | Time | 379 | 2 | 2 |
| File2 | 1.1 | Size | 943 | 2 | 2 |
| File1 | 1.2 | Time | 123 | 3 | 1 |
| File1 | 1.2 | Size | 456 | 3 | 1 |
| File2 | 1.2 | Time | 312 | 3 | 1 |
| File2 | 1.2 | Size | 645 | 3 | 1 |
| File1 | 1.2 | Time | 369 | 3 | 2 |
| File1 | 1.2 | Size | 258 | 3 | 2 |
| File2 | 1.2 | Time | 741 | 3 | 2 |
| File2 | 1.2 | Size | 734 | 3 | 2 |
| File1 | 1.3 | Time | 997 | 4 | 1 |
| File1 | 1.3 | Size | 997 | 4 | 1 |
| File2 | 1.3 | Time | 438 | 4 | 1 |
| File2 | 1.3 | Size | 735 | 4 | 1 |
How could I write a query so that I selected the maximum Replication value at the maximum Number value? As you can see, in this table, the maximum value in Number is 4 but the maximum number in Replication where Number = 4 is 1
All I can think to do is this:
SELECT MAX(Replication) FROM Table
WHERE Number IS MAX;
which is obviously wrong and doesn't work.
You can try Group By and Having
select max(Replication) from Table_Name group by [Number] having
[Number]=(select max([Number]) from Table_Name)
Just use a subquery to find the max number in the where clause. If you just want one single number as the result there is no need to use group by and having (which would make the query a lot more expensive):
select max([replication]) from tab
where number = (select max(number) from tab)

Ask about query in sql server

i have table like this:
| ID | id_number | a | b |
| 1 | 1 | 0 | 215 |
| 2 | 2 | 28 | 8952 |
| 3 | 3 | 10 | 2000 |
| 4 | 1 | 0 | 215 |
| 5 | 1 | 0 |10000 |
| 6 | 3 | 10 | 5000 |
| 7 | 2 | 3 |90933 |
I want to sum a*b where id_number is same, what the query to get all value for every id_number? for example the result is like this :
| ID | id_number | result |
| 1 | 1 | 0 |
| 2 | 2 | 523455 |
| 3 | 3 | 70000 |
This is a simple aggregation query:
select id_number, sum(a*b)
from t
group by id_number
I'm not sure what the first column is for.