Summing columns in a row in all rows with Emacs org-mode - emacs

I'm using Emacs org-mode to track times worked on various tasks. The last column in the table is the weekly sum for each task:
|------+-----+-----+-----+-----+-----+-------|
| Task | Mon | Tue | Wed | Thu | Fri | Total |
|------+-----+-----+-----+-----+-----+-------|
| Foo | 2 | 3 | 4 | 5 | 6 | 20 |
| Bar | 2 | 3 | 4 | 5 | 7 | 21 |
#+TBLFM: #2$7=vsum($2..$6)::#3$7=vsum($2..$6)
Currently, I have to add a formula for each new row. Is there any way to customise the formula so that it calculates the sums regardless of how many rows there are?

Column formula did the trick as suggested by fniessen. Here's what I ended up with:
|------+-----+-----+-----+-----+-----+-------|
| Task | Mon | Tue | Wed | Thu | Fri | Total |
|------+-----+-----+-----+-----+-----+-------|
| Foo | 2 | 3 | 4 | 5 | 6 | 20 |
| Bar | 2 | 3 | 4 | 5 | 7 | 21 |
#+TBLFM: $7=vsum($2..$6)
More info in the Column formulas and field formulas section from the docs.

You really should take a closer look at the documentation, and read about "column formulas" (and, even, "row formulas"). A colum formula is $7=...' and is editable viaC-c ='.

Related

How to display results for each year dynamic column in Crystal report

How to display each years cost in dynamic column (Max 3 years) in Crystal report.
Parameters : Date From and Date To
Crystal report Version : 2013
Table : Jobs
+-------+------------+------------+
| EQ_no | Job_Date | Total_Cost |
+-------+------------+------------+
| 1006 | 01/30/2017 | 250 |
| 1006 | 01/31/2018 | 350 |
| 1006 | 01/01/2019 | 150 |
| 1006 | 02/01/2019 | 322 |
| 1006 | 05/05/2019 | 450 |
| 1006 | 02/02/2020 | 500 |
| 1006 | 02/03/2021 | 1212 |
| 29198 | 02/04/2017 | 3000 |
| 29198 | 02/05/2018 | 250 |
+-------+------------+------------+
Table : Equipment
+-------+-----------+
| EQ_no | Serial no |
+-------+-----------+
| 1006 | MDRSC12 |
| 29198 | FDRSC13 |
| 6218 | REAFC14 |
+-------+-----------+
Result:
+-------+-----------+------+------+------+
| EQ_no | Serial no | 2018 | 2019 | 2020 |
+-------+-----------+------+------+------+
| 1006 | MDRSC12 | 350 | 922 | 500 |
| 29198 | FDRSC13 | 250 | 0 | 0 |
| 6218 | REAFC14 | 0 | 0 | 0 |
+-------+-----------+------+------+------+
If date from 1-jan-2018 to 1-June-2020 then show each years total cost 2018,2019 & 2020.
If date from 1-jan-2020 to 1-June-2021 then show each years total cost of 2020 & 2021 only.
Create a Formula Field that uses the Year() function to extract only the 4 digit numerical year from your Job_Date field. Name this field whatever you like, but I will call it "JobYear" going forward in this answer.
The formula will be Year(Job_Date);.
Now create a second Formula Field that uses the same function to extract the 4 digit numerical year from today's date. I will call this formula field "CurrentYear" going forward.
This formula will be Year(CurrentDate);.
Now create 3 Running Total Fields. Name them something like ThisYear, LastYear, and TwoYearsAgo. Set all three of these fields to summarize the Total_Cost field. Set the reset conditions to whatever is most appropriate for your report, and then set the evaluate conditions to use a formula and use the following formulas for each one.
For ThisYear the formula should be CurrentYear = JobYear;.
For LastYear the formula should be CurrentYear - 1 = JobYear;.
For TwoYearsAgo the formula should be CurrentYear - 2 = JobYear;.
This will allow the running total fields to summarize the total cost for any job into the correct buckets based upon the year the job was completed.

PostgreSQL Query Using Crosstab

Now, I am leaning postgreSQL. In the study, I found crosstab in postgreSQL. I tried to apply this function to my customized table, but it dose not work. please help!!
This is my Table
year | type | count
------+----------+----
2015 | AS | 6
2015 | HY | 6
2015 | KR | 6
2015 | SE | 6
2016 | AS | 2
2016 | HY | 2
2016 | KR | 2
2016 | SE | 2
2017 | AS | 1
2017 | HY | 1
2017 | KR | 1
2017 | SE | 1
2018 | AS | 2
2018 | HY | 2
2018 | KR | 2
2018 | SE | 2
I want to change this table like this
year | AS | HY | KR | SE |
----------------------------------
2015 | 6 | 6 | 6 | 6 |
2016 | 2 | 2 | 2 | 2 |
2017 | 1 | 1 | 1 | 1 |
2018 | 2 | 2 | 2 | 2 |
To make that table, I designed query using crosstab, but dose not work!
Please Let me know the query of this problem.
You could achieve this without Crosstab, you can use Aggregate function.
Query :
select
year,
max(counts) filter (where type = 'AS') as "AS",
max(counts) filter (where type = 'HY') as "HY",
max(counts) filter (where type = 'KR') as "KR",
max(counts) filter (where type = 'SE') as "SE"
from
tbl
group by
year
order by
year asc
Demo <> DB Fiddle
And if you are try learning Crosstab this answer are really great and explain really well about to do pivot use Crosstab.
PostgreSQL Crosstab Query

Architecture Design for Bus Routing with Time

This is to confirm if my design is good enough or get the better ideas to solve the bus routing problem with time. Here is my solution with the primary steps given below:
Have one edges table which represents all the edges (the source and target represent vertices (bus stops):
postgres=# select id, source, target, cost from busedges;
id | source | target | cost
----+--------+--------+------
1 | 1 | 2 | 1
2 | 2 | 3 | 1
3 | 3 | 4 | 1
4 | 4 | 5 | 1
5 | 1 | 7 | 1
6 | 7 | 8 | 1
7 | 1 | 6 | 1
8 | 6 | 8 | 1
9 | 9 | 10 | 1
10 | 10 | 11 | 1
11 | 11 | 12 | 1
12 | 12 | 13 | 1
13 | 9 | 15 | 1
14 | 15 | 16 | 1
15 | 9 | 14 | 1
16 | 14 | 16 | 1
Have a table which represents bus details like from time, to time, edge etc.
NOTE: I have used integer format for "from" and "to" column for faster results as I can do an integer query, but I can replace it with any better format if available.
postgres=# select id, "busedgeId", "busId", "from", "to" from busedgetimes;
id | busedgeId | busId | from | to
----+-----------+-------+-------+-------
18 | 1 | 1 | 33000 | 33300
19 | 2 | 1 | 33300 | 33600
20 | 3 | 2 | 33900 | 34200
21 | 4 | 2 | 34200 | 34800
22 | 1 | 3 | 36000 | 36300
23 | 2 | 3 | 36600 | 37200
24 | 3 | 4 | 38400 | 38700
25 | 4 | 4 | 38700 | 39540
Use dijkstra algorithm to find the nearest path.
Get the upcoming buses from the busedgetimes table in the earliest first order for the nearest path detected by dijkstra algorithm. => This leads to a bit complex query though.
Can I do any kind of improvements to this, or are there any better designs?
Links to docs, articles related to this would be really helpful.
This is totally normal and the regular way to do it. See also,
PgRouting Example

Calculate LAG variable after filtering in Tableau

I have a dataset with 4 columns: ID (unique identifier of user), Year, Country and Level in this format:
+----+------+---------+-------+
| ID | Year | Country | Level |
+----+------+---------+-------+
| 1 | 2015 | USA | 1 |
| 1 | 2016 | China | 2 |
| 2 | 2015 | China | 2 |
| 2 | 2016 | Russia | 2 |
| 3 | 2015 | Russia | 1 |
| 3 | 2016 | China | 2 |
| 4 | 2015 | USA | 2 |
| 4 | 2016 | USA | 3 |
| 5 | 2014 | China | 1 |
| 5 | 2016 | USA | 2 |
| 6 | 2015 | USA | 1 |
| 6 | 2016 | USA | 2 |
| 7 | 2015 | Russia | 2 |
| 7 | 2016 | China | 3 |
+----+------+---------+-------+
The user will be able to filter the dataset by country.
I want to create a table using the country filter that shows in a column if a user was the previous year in any of the countries selected aggregated by the level variable, apart from other variables only affected by the current country filter.
For example E.g., if I select China and USA:
+----+------+---------+-------+-----------------+
| ID | Year | Country | Level | In selection PY |
+----+------+---------+-------+-----------------+
| 1 | 2015 | USA | 1 | No |
| 1 | 2016 | China | 2 | Yes |
| 2 | 2015 | China | 2 | No |
| 3 | 2016 | China | 2 | No |
| 4 | 2015 | USA | 2 | No |
| 4 | 2016 | USA | 3 | Yes |
| 5 | 2014 | China | 1 | No |
| 5 | 2016 | USA | 2 | No |
| 6 | 2015 | USA | 1 | No |
| 6 | 2016 | USA | 2 | Yes |
| 7 | 2016 | China | 3 | No |
+----+------+---------+-------+-----------------+
The aggregated result will be:
+-------+-------------------+-----------------+
| Level | Number of records | In selection PY |
+-------+-------------------+-----------------+
| 1 | 3 | 0 |
| 2 | 6 | 2 |
| 3 | 2 | 1 |
+-------+-------------------+-----------------+
Do you know any way to calculate this aggregated table efficiently? (this would be done in a dataset with millions of rows, with a variable set of countries to be selected)
I found a solution, will post in case it is helpful for someone else:
I change the Country filter to "Add to Context" and created this variable:
In Selection PY: if Year = 2016 then
{fixed [ID]:min(if Year = 2015 then 1 END)}
elseif Year = 2015 then
{fixed [ID]:min(if Year = 2014 then 1 END)}
elseif Year = 2014 then
{fixed [ID]:min(if Year = 2013 then 1 END)}
In this way the variable Selection PY is dynamically calculated according to the country filter.
It is only necessary to know in advance which years are stored in the dataset (or add more years to be safe).

Create daily report using crosstabs

I know that crosstabs are just for summaries. But is it possible to use a crosstab for daily reports given two dates? It's more like a details summary.
For example:
Date: 10 August 2012
Start Date: 10/8/2012
End Date: 11/8/2012
Date: 10 August 2012
_________| Center 1 | Center 2 | Total |
Person 1 | 1 | 2 | 3 |
Person 2 | 2 | 5 | 7 |
TOTAL | 3 | 7 | 10 |
Date: 11 August 2012
_________| Center 1 | Center 2 | Total |
Person 1 | 5 | 2 | 7 |
Person 2 | 8 | 5 | 13 |
TOTAL | 13 | 7 | 20 |
Unfortunately, you'd have to have each date as a row in the crosstab, and I think this is the best you'll be able to get:
10 August 2012|_________| Center 1 | Center 2 | Total |
|Person 1 | 1 | 2 | 3 |
|Person 2 | 2 | 5 | 7 |
|TOTAL | 3 | 7 | 10 |
11 August 2012|_________| Center 1 | Center 2 | Total |
|Person 1 | 5 | 2 | 7 |
|Person 2 | 8 | 5 | 13 |
|TOTAL | 13 | 7 | 20 |
|GRAND | 16 | 14 | 30 |
|TOTAL | | | |
So your setup would be:
Center Total
----+--------+-------+------+
Date| | | |
|Person | | |