Topic : S02
Summary
- How to sum up a number or a list of numbers (non-groupBy)(or groupBy) with skipping the null value? PS.To sum up one field
Test
Summary
- dataview: v0.5.46
2. Overview
Summary
2.1. Main DQL or DVJS
- The fowllowng table presents the main DQL, DVJS or inline DVJS in the Solutions.
Code Name | Data type | Group By | Purposes | Remark |
---|---|---|---|---|
DQL10_sum_number_or_list | exercise :1.a number 2.a list of numbers |
no | 1.To sum up one field such as exercise | |
DQL20_sum_number_or_list_GROUP_BY | exercise :1.a number 2.a list of numbers |
yes | 1.To sum up one field such as exercise 2.group.key to fullname_of_month |
|
DVJS10_sum_number_or_list _and_TABLE_last_row_Total_seperately |
exercise :1.a number 2.a list of numbers |
no | 1.To sum up one field such as exercise 2.To display it as a table 3.To get i_Total_exercise 4.To display it as a table and hide the file count in the dataview table |
The code =DQL10_sum_number_or_list + DVJS30_sum_number_or_list _and_TABLE_last_row_ONLY |
DVJS15_sum_number_or_list _and_TABLE_last_row_Total_merged |
exercise :1.a number 2.a list of numbers |
no | 1.To sum up one field such as exercise 2.To get i_Total_exercise 3.To display it as a table |
The code =DVJS10_sum_number_or_list _and_TABLE_last_row_Total_seperately |
DVJS20_sum_number_or_list_groupBy _and_TABLE_last_row_Total_seperately |
exercise :1.a number 2.a list of numbers |
yes | 1.To sum up one field such as exercise 2.group.key to fullname_of_month 3.To display it as a table 4.To get i_Total_exercise 5.To display it as a table and hide the file count in the dataview table |
The code =DQL20_sum_number_or_list_GROUP_BY + DVJS30_sum_number_or_list _and_TABLE_last_row_ONLY |
DVJS30_sum_number_or_list _and_TABLE_last_row_ONLY |
exercise :1.a number 2.a list of numbers |
no | 1.To sum up one field such as exercise in each file 2.To display it as a table and hide the file count in the dataview table |
|
inDVJS10_sum_number_or_list_Total | exercise :1.a number 2.a list of numbers |
no | 1.To sum up one field such as exercise in each file 2.To display it as a value 3.Inline DVJS |
The code is fromDVJS30_sum_number_or_list _and_TABLE_last_row_ONLY |
Summary_Examples
2.2. SUM
2.2.1. SUM = (reduce(list, (a, b) => a + b))
2.2.1.1. a list of non-groupBy data = 1D array
Total_list=`=reduce([1,2,3], (a, b) => a + b)`//=>6
2.2.1.2. a list of groupBy data = 2D array = Array of arrays = AoA
a_List=`=reduce([[1],[2],[3]], (a, b) => a + b)`//=>1, 2, 3
Total_AoA=`=reduce(reduce([[1],[2],[3]], (a, b) => a + b),(a, b) => a + b)`//=>6
2.2.2. SUM
2.2.2.1. a list of non-groupBy data = 1D array
Total_list=`=sum([1,2,3])`//=>6
2.2.2.2. a list of groupBy data = 2D array = Array of arrays = AoA
a_List=`=sum([[1],[2],[3]])`//=>1, 2, 3
Total_AoA=`=sum(sum([[1],[2],[3]]))`//=>6
2.3. Examples
2.3.1. javascript
```javascript
let nResult = null + 10; //=>10
```
2.3.2. Inline DQL
ng_Result=`=null + 10` //=>No implementation found for 'null + number'
2.3.3. Use default
to transform null
into zero before using sum
nResult= `=default(null, 0) + 10` //=>10
2.3.4. Use number
to transform string
into number
before using sum
nResult= `=sum(number("5000"))` //=>5000
2.3.5. Use split
to get /[0-9]+/ from string
and use number
to transform it before using sum
// #### meta data ####
Price:: Note01 - 5000
Price:: Note02 - 7000
// #### meta data ####
2.3.5.1. Test
Price=`=split("Note01 - 5000", " - ")[1]`//=>5000
Price=`=split("Note01 - 7000", " - ")[1]`//=>7000
2.3.5.2. OK result
total_Price:: **TOTAL** - `=sum(map(this.Price, (e) => number(split(e, " - ")[1])))`//=>TOTAL - 12000
2.3.5.3. NG result
total_Price:: **TOTAL** = `=sum(number(this.Price))`//=>TOTAL = 3
2.3.6. Summary
- Make sure the input data type is
number
beforesum
.
3. Input
Summary
3.1. dictionary files:
- Location: “100_Project/02_dataview/Q01_Sum/20220725_Sum_02/Q01_test_data02”
3.1.1. F02_number_or_list
- filename :
dic_20220202_exercise
---
exercise: 60
---
- filename :
dic_20220207_exercise
---
exercise: [60, 60]
---
3.1.2. A03__list
- filename :
dic_20220302_exercise
---
exercise: [60, 35]
---
- filename :
dic_20220307_exercise
---
exercise: [56, 22]
---
4. DQL10_sum_number_or_list
Summary
4.1. Main DQL
Code Name | Data type | Group By | Purposes | Remark |
---|---|---|---|---|
DQL10_sum_number_or_list | exercise :1.a number 2.a list of numbers |
no | 1.To sum up one field such as exercise |
4.2. Code DQL10_sum_number_or_list
Summary_code
title: DQL10_sum_number_or_list => 1.To sum up one field such as exercise(type: a number or a list of numbers) 2.non-groupBy data
collapse: open
icon:
color:
```dataview
TABLE WITHOUT ID
file.link AS "File",
file.name AS "file.name",
sum(exercise) as "sum_exercise",
exercise AS "exercise"
FROM "100_Project/02_dataview/Q01_Sum/20220725_Sum_02/Q01_test_data02"
WHERE contains(file.name, "dic_")
WHERE file.name != this.file.name
WHERE exercise != null
SORT file.name ASC
```
4.2.1. Screenshots(DQL10)
5. DQL20_sum_number_or_list_GROUP_BY
Summary
5.1. Main DQL
Code Name | Data type | Group By | Purposes | Remark |
---|---|---|---|---|
DQL20_sum_number_or_list_GROUP_BY | exercise :1.a number 2.a list of numbers |
yes | 1.To sum up one field such as exercise 2.group.key to fullname_of_month |
5.2. Code DQL20_sum_number_or_list_GROUP_BY
Summary_code
title: DQL20_sum_number_or_list_GROUP_BY =>1.To sum up one field such as exercise(type: a number or a list of numbers) 2.groupBy data 3.group.key to fullname_of_month
collapse: open
icon:
color:
```dataview
TABLE WITHOUT ID
FG_month AS "G_month",
rows.file.link AS "File",
sum(map(rows.exercise, (e) => sum(e))) AS "sum_exercise",
rows.exercise AS "exercise"
FROM "100_Project/02_dataview/Q01_Sum/20220725_Sum_02/Q01_test_data02"
WHERE contains(file.name, "dic_")
WHERE file.name != this.file.name
WHERE exercise != null
WHERE file.day.year = 2022
SORT file.name ASC
GROUP BY file.day.month AS G_m
FLATTEN {
"1": "January",
"2": "February",
"3": "March",
"4": "April",
"5": "May",
"6": "June",
"7": "July",
"8": "August",
"9": "September",
"10": "October",
"11": "November",
"12": "December"
}[string(G_m)] AS "FG_month"
```
5.2.1. Screenshots(DQL20)
6. DVJS10_sum_number_or_list_and_TABLE_last_row_Total_seperately
Summary
6.1. Main DVJS
Code Name | Data type | Group By | Purposes | Remark |
---|---|---|---|---|
DVJS10_sum_number_or_list _and_TABLE_last_row_Total_seperately |
exercise :1.a number 2.a list of numbers |
no | 1.To sum up one field such as exercise 2.To display it as a table 3.To get i_Total_exercise 4.To display it as a table and hide the file count in the dataview table |
The code =DQL10_sum_number_or_list + DVJS30_sum_number_or_list _and_TABLE_last_row_ONLY |
6.2. Code DVJS10_sum_number_or_list_and_TABLE_last_row_Total_seperately
Summary_code
title: DVJS10_sum_number_or_list_and_TABLE_last_row_Total_seperately => 1.To sum up one field such as exercise(type: a number or a list of numbers) 2.non-groupBy data 3.To display it as a table 4.To get i_Total_exercise 5.To display it as a table and hide the file count in the dataview table
collapse: open
icon:
color:
```dataviewjs
// M11. define pages: gather all relevant pages
// #####################################################################
let pages = dv
.pages('"100_Project/02_dataview/Q01_Sum/20220725_Sum_02/Q01_test_data02"')
.where((page) => dv.func.contains(page.file.name, "dic_"))
.where((page) => page.file.name !== dv.current().file.name)
.where((page) => page.exercise !== null)
.sort((page) => page.file.name, "asc");
// ### M21.Output : pages
// #####################################################################
dv.header(2, "M21.Exercise Report in 2022");
dv.table(
["N", "File", "file.name", "sum_exercise", "exercise"],
pages.map((page, index) => [
index + 1,
page.file.link ,
page.file.name,
dv.func.sum(page.exercise),
page.exercise,
])
);
// M41. get i_Total_exercise: dv.func.sum(dv.func.default(pages.cash, 0))
// #####################################################################
let i_Total_exercise = dv.func.default(dv.func.sum(pages.exercise), 0);
// M51. Output : i_Total_exercise
// #####################################################################
//dv.span("i_Total_exercise=" + i_Total_exercise);//=>i_Total_exercise=353
// M53. TABLE :i_Total_exercise, i_Total_exercise
// #####################################################################
// dv.table(
// ["Name", "Price", "Caffeine Content"],
// [
// ["Black Coffee", 120, 300],
// ["Green Tea", 100, 200],
// ["Apple Juice", 110, 0],
// ["Iced Chocolate", 130, 0],
// ["Hot Chocolate", 105, 6],
// ]
// );
dv.table(
["", "exercise", "Total"],
[
["**Total**", i_Total_exercise, i_Total_exercise],
]
);
// M61. hide the file count in the `the_nth_of_table` dataview table :
// #####################################################################
let the_nth_of_table = 2;
this.container.querySelectorAll(".table-view-table tr:first-of-type th:first-of-type > span.small-text")[the_nth_of_table-1].style.visibility = "hidden";
```
6.2.1. Screenshots(DVJS10)
7. DVJS15_sum_number_or_list_and_TABLE_last_row_Total_merged
Summary
7.1. Main DVJS
Code Name | Data type | Group By | Purposes | Remark |
---|---|---|---|---|
DVJS15_sum_number_or_list _and_TABLE_last_row_Total_merged |
exercise :1.a number 2.a list of numbers |
no | 1.To sum up one field such as exercise 2.To get i_Total_exercise 3.To display it as a table |
The code =DVJS10_sum_number_or_list _and_TABLE_last_row_Total_seperately |
7.2. Code DVJS15_sum_number_or_list_and_TABLE_last_row_Total_merged
Summary_code
title: DVJS15_sum_number_or_list_and_TABLE_last_row_Total_merged => 1.To sum up one field such as exercise(type: a number or a list of numbers) 2.non-groupBy data 3.To get i_Total_exercise 4.To display it as a table
collapse: open
icon:
color:
```dataviewjs
// M11. define pages: gather all relevant pages
// #####################################################################
let pages = dv
.pages('"100_Project/02_dataview/Q01_Sum/20220725_Sum_02/Q01_test_data02"')
.where((page) => dv.func.contains(page.file.name, "dic_"))
.where((page) => page.file.name !== dv.current().file.name)
.where((page) => page.exercise !== null)
.sort((page) => page.file.name, "asc");
// M21. get i_Total_exercise: dv.func.sum(dv.func.default(pages.cash, 0))
// #####################################################################
let i_Total_exercise = dv.func.default(dv.func.sum(pages.exercise), 0);
// ### M31.define : aoa_drinks
// #####################################################################
// dv.table(
// ["Name", "Price", "Caffeine Content"],
// [
// ["Black Coffee", 120, 300],
// ["Green Tea", 100, 200],
// ["Apple Juice", 110, 0],
// ["Iced Chocolate", 130, 0],
// ["Hot Chocolate", 105, 6],
// ]
// );
// ["N", "File", "file.name", "sum_exercise", "exercise"]
let aoa_drinks = pages.map((page, index) => [
index + 1,
page.file.link ,
page.file.name,
dv.func.sum(page.exercise),
page.exercise,
]);
// ### M33.update aoa_drinks:push the_lat_row_Total
// #####################################################################
// ["N", "File", "file.name", "sum_exercise", "exercise"]
// aoa_drinks.push([
// "**Total**",
// "",
// "",
// i_Total_exercise,
// i_Total_exercise,
// ]);
aoa_drinks["values"][aoa_drinks.length] = [
"**Total**",
"",
"",
i_Total_exercise,
i_Total_exercise,
];
// M51. TABLE :aoa_drinks
// #####################################################################
// dv.table(
// ["Name", "Price", "Caffeine Content"],
// [
// ["Black Coffee", 120, 300],
// ["Green Tea", 100, 200],
// ["Apple Juice", 110, 0],
// ["Iced Chocolate", 130, 0],
// ["Hot Chocolate", 105, 6],
// ]
// );
dv.header(2, "M51.Exercise Report in 2022");
dv.table(["N", "File", "file.name", "sum_exercise", "exercise"], aoa_drinks);
```
7.2.1. Screenshots(DVJS15)
8. DVJS20_sum_number_or_list_groupBy_and_TABLE_last_row_Total_seperately
Summary
8.1. Main DVJS
Code Name | Data type | Group By | Purposes | Remark |
---|---|---|---|---|
DVJS20_sum_number_or_list_groupBy _and_TABLE_last_row_Total_seperately |
exercise :1.a number 2.a list of numbers |
yes | 1.To sum up one field such as exercise 2.group.key to fullname_of_month 3.To display it as a table 4.To get i_Total_exercise 5.To display it as a table and hide the file count in the dataview table |
The code =DQL20_sum_number_or_list_GROUP_BY + DVJS30_sum_number_or_list _and_TABLE_last_row_ONLY |
8.2. Code DVJS20_sum_number_or_list_groupBy_and_TABLE_last_row_Total_seperately
Summary_code
title: DVJS20_sum_number_or_list_groupBy_and_TABLE_last_row_Total_seperately => 1.To sum up one field such as exercise(type: a number or a list of numbers) 2.groupBy data 3.group.key to fullname_of_month 4.To display it as a table 5.To get i_Total_exercise 6.To display it as a table and hide the file count in the dataview table
collapse: open
icon:
color:
```dataviewjs
// M11. define pages: gather all relevant pages
// #####################################################################
let pages = dv
.pages('"100_Project/02_dataview/Q01_Sum/20220725_Sum_02/Q01_test_data02"')
.where((page) => dv.func.contains(page.file.name, "dic_"))
.where((page) => page.file.name !== dv.current().file.name)
.where((page) => page.exercise !== null)
.where((page) => page.file.day.year === 2022)
.sort((page) => page.file.name, "asc");
// M13. GROUP BY file.day.month:
// #####################################################################
dv.header(2, "M13.Exercise Report groupBy Month in 2022");
for (let group of pages.groupBy((page) => page.file.day.month)) {
// M13.FR10 transform the group.key into the fullname_of_month:
// #####################################################################
//dv.header(3, group.key);
dv.header(3,
{
"1": "January",
"2": "February",
"3": "March",
"4": "April",
"5": "May",
"6": "June",
"7": "July",
"8": "August",
"9": "September",
"10": "October",
"11": "November",
"12": "December"
}[group.key]
);
// M13.FR12 TABLE:
// #####################################################################
dv.table(
["N", "File", "sum_exercise", "exercise"],
group.rows
.sort((k) => k.file.name, "desc")
.map((k, index) => [
index + 1,
k.file.link,
dv.func.sum(k.exercise),
k["exercise"],
])
);
}
// M41. get i_Total_exercise: dv.func.sum(dv.func.default(pages.cash, 0))
// #####################################################################
let i_Total_exercise = dv.func.default(dv.func.sum(pages.exercise), 0);
// M51. Output : i_Total_exercise
// #####################################################################
//dv.span("i_Total_exercise=" + i_Total_exercise);//=>i_Total_exercise=353
// M53. TABLE :i_Total_exercise, i_Total_exercise
// #####################################################################
// dv.table(
// ["Name", "Price", "Caffeine Content"],
// [
// ["Black Coffee", 120, 300],
// ["Green Tea", 100, 200],
// ["Apple Juice", 110, 0],
// ["Iced Chocolate", 130, 0],
// ["Hot Chocolate", 105, 6],
// ]
// );
dv.table(
["", "exercise", "Total"],
[
["**Total**", i_Total_exercise, i_Total_exercise],
]
);
// M61. get te counts of unique file.day.month : page.file.day.month
// #####################################################################
let a_unique_months = pages.file.day.month.where((e) => !(this[e] = e in this));
//dv.span(a_unique_months);//=>2,3
// M63. get te counts of unique file.day.month : page.file.day.month
// #####################################################################
let i_counts_of_month = a_unique_months.length;
//dv.span(i_counts_of_month);//=>2
// M65. hide the file count in the `the_nth_of_table` dataview table :
// #####################################################################
let the_nth_of_table = i_counts_of_month + 1 ;
this.container.querySelectorAll(".table-view-table tr:first-of-type th:first-of-type > span.small-text")[the_nth_of_table-1].style.visibility = "hidden";
```
8.2.1. Screenshots(DVJS20)
9. DVJS30_sum_number_or_list_and_TABLE_last_row_ONLY
Summary
9.1. Main DQL
Code Name | Data type | Group By | Purposes | Remark |
---|---|---|---|---|
DVJS30_sum_number_or_list _and_TABLE_last_row_ONLY |
exercise :1.a number 2.a list of numbers |
no | 1.To sum up one field such as exercise in each file 2.To display it as a table and hide the file count in the dataview table |
9.2. Code DVJS30_sum_number_or_list_and_TABLE_last_row_ONLY
Summary_code
title: DVJS30_sum_number_or_list_and_TABLE_last_row_ONLY => 1.To sum up one field such as exercise(type: a number or a list of numbers) in each file 2.non-groupBy data 3.To display it as a table and hide the file count in the dataview table
collapse: open
icon:
color:
```dataviewjs
// M11. define pages: gather all relevant pages
// #####################################################################
let pages = dv
.pages('"100_Project/02_dataview/Q01_Sum/20220725_Sum_02/Q01_test_data02"')
.where((page) => dv.func.contains(page.file.name, "dic_"))
.where((page) => page.file.name !== dv.current().file.name)
.where((page) => page.exercise !== null)
.where((page) => page.file.day.year === 2022);
// M21. get i_Total_exercise: dv.func.sum(dv.func.default(pages.cash, 0))
// #####################################################################
let i_Total_exercise = dv.func.default(dv.func.sum(pages.exercise), 0);
// M31. Output : i_Total_exercise
// #####################################################################
//dv.span("i_Total_exercise=" + i_Total_exercise);//=>i_Total_exercise=353
// M41. TABLE :i_Total_exercise, i_Total_exercise
// #####################################################################
// dv.table(
// ["Name", "Price", "Caffeine Content"],
// [
// ["Black Coffee", 120, 300],
// ["Green Tea", 100, 200],
// ["Apple Juice", 110, 0],
// ["Iced Chocolate", 130, 0],
// ["Hot Chocolate", 105, 6],
// ]
// );
dv.header(2, "M41.Exercise Report in 2022");
dv.table(
["", "exercise", "Total"],
[
["**Total**", i_Total_exercise, i_Total_exercise],
]
);
// M51. hide the file count in the `the_nth_of_table` dataview table :
// #####################################################################
let the_nth_of_table = 1;
this.container.querySelectorAll(".table-view-table tr:first-of-type th:first-of-type > span.small-text")[the_nth_of_table-1].style.visibility = "hidden";
```
9.2.1. Screenshots(DVJS30)
10. inDVJS10_sum_number_or_list_Total
Summary
10.1. Main Inline DVJS
Code Name | Data type | Group By | Purposes | Remark |
---|---|---|---|---|
inDVJS10_sum_number_or_list_Total | exercise :1.a number 2.a list of numbers |
no | 1.To sum up one field such as exercise in each file 2.To display it as a value 3.Inline DVJS |
The code is fromDVJS30_sum_number_or_list _and_TABLE_last_row_ONLY |
10.2. Notes
Notes:
1.copy step M11 and M21 from DVJS30_sum_number_or_list_and_TABLE_last_row_ONLY into an Inline DVJS expression
2.Remove the comments .
3.Adddv.span(i_Total_exercise);
in the last row in the Inline DVJS expression.
4.Remove the blank lines.
5.An Inline DVJS is finished!
10.3. Code inDVJS10_sum_number_or_list_Total
Summary_code
Total=`$=
let pages = dv .pages('"100_Project/02_dataview/Q01_Sum/20220725_Sum_02/Q01_test_data02"')
.where((page) => dv.func.contains(page.file.name, "dic_"))
.where((page) => page.file.name !== dv.current().file.name)
.where((page) => page.exercise !== null)
.where((page) => page.file.day.year === 2022);
let i_Total_exercise = dv.func.default(dv.func.sum(pages.exercise), 0);
dv.span(i_Total_exercise);
`
10.3.1. Screenshots(inDVJS10)
//=>353
13. Conclusion
Summary
13.1. Sum
- According to the codes above, there is a summary as follows.
Field Name | Data type | Group By | Purpose | DQL Expression | DVJS Expression (Table vs. last_row_Total) | Reamrk |
---|---|---|---|---|---|---|
exercise | 1.a number 2.a list of numbers |
no | To sum up one field | sum(exercise) AS “sum_exercise” |
1.M21: (not using GROUP BY )dv.func.sum(page.exercise), 2.M41: (not using GROUP BY )let i_Total_exercise = dv.func.default(dv.func.sum(pages.exercise), 0); |
1.S02 > DQL10 2.S2 > DVJS10 |
exercise | 1.a number 2.a list of numbers |
yes (once) |
To sum up one field | sum(map(rows.exercise, (e) => sum(e))) AS “sum_list_G1_11” |
1.M13.FR12: (using GROUP BY once)dv.func.sum(k.exercise), 2.M41: (not using GROUP BY )let i_Total_exercise = dv.func.default(dv.func.sum(pages.exercise), 0); |
1.S02 > DQL20 2.S02 > DVJS20 |
13.2. To group by the data (once)
Field Name | Data type | Group By | Purpose | DQL Expression |
---|---|---|---|---|
exercise | 1.a number 2.a list of numbers |
yes | To groupBy year | GROUP BY file.day.year AS G_year |
exercise | 1.a number 2.a list of numbers |
yes | To groupBy month (For same year only) |
GROUP BY file.day.month AS G_month |
exercise | 1.a number 2.a list of numbers |
yes | To groupBy calendarweek | FLATTEN dateformat(file.day, “yyyy-WW”) AS F_calendarweek GROUP BY F_calendarweek AS GF_calendarweek |