How to access data from many tables in src blocks - org-mode

In my org document, I have several tables named (with #+name:) t1, t2, etc. I want to pass all of the tables to some lisp code. This is what I have so far:
#+name: process-tables
#+header: :var t1=t1 t2=t2 t3=t3 t4=t4 t5=t5 t6=t6 t7=t7 t8=t8 t9=t9 t10=t10 t11=t11 t12=t12 t13=t13 t14=t14
#+BEGIN_SRC emacs-lisp
(process (append t1 t2 t3 t4 t5 t6 t7 t8 t9 t10 t11 t12 t13 t14))
#+END_SRC
This seems very clumsy. Is there a better way? I do not want to merge the tables in the org document.

You can try the org-table-map-tables and org-table-to-lisp functions to create a list of all tables in the buffer. This avoids having to invoke table names individually.
(defun org-tables-to-list ()
(let (tbls)
(org-table-map-tables
(lambda ()
(push (org-table-to-lisp) tbls))
t)
(apply #'append (nreverse tbls))))
For example:
#+name: t1
| 0 | 1 |
#+name: t2
| 2 | 3 |
#+name: process-tables
#+BEGIN_SRC emacs-lisp :results value verbatim
(org-tables-to-list)
#+END_SRC
#+RESULTS: process-tables
: (("0" "1") ("2" "3"))

Related

Is there an equivalent PostgresSQL window function (or alternate procedure) for the aggregate function bool_or()?

Given the following data:
select a,b from newtable;
a | b
---+---
a | f
a | f
a | f
b | f
b | f
b | t
(6 rows)
The statement
select a, bool_or(b) from newtable group by a;
a | bool_or
---+---------
a | f
b | t
will produce a single row per distinct value (as expected from an aggregate function).
I was looking for an equivalent window function but seems that there is no such function in PostgreSQL. Is there any way to get the same result? Just to be clear I was looking for this result:
a | bool_or
---+---------
a | f
a | f
a | f
b | t
b | t
b | t
Although the bool_or() is not explicitly listed in the PostgreSQL documentation page for window functions you can still use aggregate functions like bool_or() or any built-in function over windows.
It says so in the window function documentation:
any built-in or user-defined general-purpose or statistical aggregate
can be used as a window function
So to get the desired result use:
select a, bool_or(b) over w from newtable window w as (partition by a) ;
a | bool_or
---+---------
a | f
a | f
a | f
b | t
b | t
b | t
(6 rows)

SPARQL same indi

Can SPARQL be used to find subjects having identical objects for a given predicate. Considering a class Variable with data property as
Variable--hasvalue-->Integer, if there are five instances such as
a ----hasvalue------> 2
b ----hasvalue------> 1
c ----hasvalue------> 2
d ----hasvalue------> 0
e ----hasvalue------> 1
How to extract a and c has same value whereas b and e has same value. Group option works in grouping as above, but is it possible to extract subjects corresponding to each grouped objects.
It's always easier to write example code if you provide sample data to work with. Please provide sample data in the future. Your suggested data looks like this:
#prefix : <urn:ex:>
:a :hasValue 2 .
:b :hasValue 1 .
:c :hasValue 2 .
:d :hasValue 0 .
:e :hasValue 1 .
You can use a query with group by and group_concat to concatenate the variables together for each distinct value:
prefix : <urn:ex:>
select ?value (group_concat(?variable) as ?variables) {
?variable :hasValue ?value
}
group by ?value
-------------------------------
| value | variables |
===============================
| 2 | "urn:ex:c urn:ex:a" |
| 1 | "urn:ex:e urn:ex:b" |
| 0 | "urn:ex:d" |
-------------------------------

Compute the duration of appointment in an org-mode table

I have a list of appointments in the style of:
| Appointment | Dur. |
|------------------------------+-------|
| <2014-02-20 Thu 09:30-18:30> | ??? |
| <2014-02-22 Sat 09:00-10:00> | ??? |
| | |
How can I make orgmode calculate the durations of my appointments?
Org mode allows you to specify formulas for calculating values for a given column. You are allowed to execute elisp code in those formulas. We can use feature to achieve what you want first, we will need to define some functions which find difference between times. You can add the following to your org document
#+begin_src emacs-lisp
(defun my-get-number-of-minutes (time)
(+ (* (nth 2 time) 60) (nth 1 time)))
(defun my-get-time-diff (t1 t2)
(- (my-get-number-of-minutes t1) (my-get-number-of-minutes t2)))
(defun my-get-duration (time-string)
(let* ((times (split-string (substring time-string 16 -1) "-"))
(minute-diff (my-get-time-diff (parse-time-string (nth 1 times))
(parse-time-string (nth 0 times)))))
(format "%dhrs %dmins" (/ minute-diff 60) (% minute-diff 60))))
#+end_src
Then do C-cC-c anywhere between the #+begin-src emacs-lisp and #+end-src tags, this will evaluate the elisp.
OR
Simply copy the elisp code (between #+begin_src emacs-lisp #+end-src) to *scratch* buffer and do M-xeval-bufferRET.
Now that we have the functions defined we can tell org-mode the formula to calculate the second column from first column, to do this simply paste the following line below the org-table
#+TBLFM: $2='(my-get-duration $1)
This tells org that value of column 2 ($2) is the function my-get-duration applied 'value of column 1' ($2). Then with point on the line do C-cC-c, if you have done everything well, the second column should be filled with the duration.
NOTE: The code assumes that the datetime will be of the same format as given in your example.
I recommend you read this short article about org-mode's capabilities as a spreadsheet.
UPDATE
If you can reformat your table as follows, you will be able to simplify the formula
| Appointment | Start | End | Duration |
|------------------+-------+-------+----------|
| <2014-02-20 Thu> | 09:30 | 18:30 | ??? |
| <2014-02-22 Sat> | 09:00 | 10:00 | ??? |
The formula for calculating the duration now would simply be
#+TBLFM: $4=$3-$2;T

Org mode spreadsheet programmatic remote references

I keep my budget in org-mode and have been pleased with how simple it is. The simplicity fails, however, as I am performing formulas on many cells; for instance, my year summary table that performs the same grab-and-calculate formulas for each month. I end up with a massive line in my +TBLFM. This would be dramatically shorter if I could programmatically pass arguments to the formula. I'm looking for something like this, but working:
| SEPT |
| #ERROR |
#+TBLFM: #2$1=remote(#1,$tf)
Elsewhere I have a table named SEPT and it has field named "tf". This function works if I replace "#1" with "SEPT" but this would cause me to need a new entry in the formula for every column.
Is there a way to get this working, where the table itself can specify what remote table to call (such as the SEPT in my example)?
Yes, you can't do this with built-in remote and you need to use org-table-get-remote-range. Hopefully this better suits your needs than the answer given by artscan (I used his/her example):
| testname1 | testname2 |
|-----------+-----------|
| 1 | 2 |
#+TBLFM: #2='(org-table-get-remote-range #<$0 (string ?# ?1 ?$ ?1))
#+TBLNAME: testname1
| 1 |
#+TBLNAME: testname2
| 2 |
Note the (string ?# ?1 ?$ ?1): this is necessary because before evaluating table formulae, all substitutions will be done first. If you use "#1$1" directly, it would have triggered the substitution mechanism and be substituted by the contents of the first cell in this table.
There is some ugly hack for same effect without using remote:
1) it needs named variable for remote address
(setq eab/test-remote "#1$1")
2) it uses elisp expression (from org-table.el) instead remote(tablename,#1$1)
(defun eab/test-remote (x)
`(car (read
(org-table-make-reference
(org-table-get-remote-range ,x eab/test-remote)
't 't nil))))
3) worked example
| testname1 | testname2 |
|-----------+-----------|
| | |
#+TBLFM: #2='(eval (eab/test-remote #1))
#+TBLNAME: testname1
| 1 |
#+TBLNAME: testname2
| 2 |
4) result
| testname1 | testname2 |
|-----------+-----------|
| 1 | 2 |

Emacs org-mode totals table

I have an org file, describing a project:
* task1
** task1-1
:PROPERTIES:
:price: 10
:given: <2012-11-08 Thu>
:END:
** task1-2
:PROPERTIES:
:price: 11
:given: <2012-11-08 Thu>
:END:
* task2
** task2-1
:PROPERTIES:
:price: 20
:given: <2012-11-08 Thu>
:END:
** task2-2
:PROPERTIES:
:price: 21
:given: <2012-11-08 Thu>
:END:
I used org-collector to produce a totals table:
#+BEGIN: propview :id global :conds ((not (= price 0))) :cols (ITEM price)
| ITEM | price |
|-----------+-------|
| "task1-1" | 10 |
| "task1-2" | 11 |
| "task2-1" | 20 |
| "task2-2" | 21 |
|-----------+-------|
| | 62 |
#+TBLFM: #6$2=vsum(#2$2..#5$2)
#+END:
But I want to have something like this:
| ITEM | price |
|-----------+-------|
| "task1-1" | 10 |
| "task1-2" | 11 |
| "task2-1" | 20 |
| "task2-2" | 21 |
|-----------+-------|
| Total | 62 |
How to do it?
For the “Total” line, you can add a line |Total| |, press C-u C-c = in the empty cell (to define a formula for it), and enter the formula vsum(#1$2..#4$2). (If you wanna recalc, that's C-u C-c C-c for all.)
I don't know about org-collector, so can't help you with this part. Run it on the entire document (is :id global working?), or shift everything by one level to the right for it to be inside a single tree, maybe.
I just figured this one out this morning. The answer you're looking for with Org-Collector is straightforward. Just put the table formulae two blank lines beneath the beginning of the property view and it'll be calculated automatically when the property view is evaluated (with C-c C-c).
#+BEGIN: propview :scope tree :cols (ITEM CLOCKSUM EFFORT) :match "TODO=\"TODO\"" :noquote ALL
#+TBLFM: #>$1=Totals::#>$2=vsum(#I..#II);t::#>$3=vsum(#I..#II);t
#+END:
If you want to turn this into a generic yasnippet, you need to escape the $ and the \:
#+BEGIN: propview :scope tree :cols (ITEM CLOCKSUM EFFORT) :match "TODO=\\"TODO\\"" :noquote ALL
#+TBLFM: #>\$1=Totals::#>\$2=vsum(#I..#II);t::#>\$3=vsum(#I..#II);t
#+END:
Now, you can add a generic property view that calculates the time so far and effort remaining for each step of a project, as well as the total time overall.