% This file is embedded in datatool-user.pdf version 3.4.1 2025-04-25
% Example 78 Display Data in a Table with an Extra Column
% Label: "ex:displaydbpostrow"
% arara: pdflatex
% arara: pdfcrop
\documentclass[12pt]{article}
\pagestyle{empty}
\usepackage{booktabs}
\usepackage{datatool}
\DTLsetup{store-datum,default-name=products}
% define database:
\DTLaction{new}
% add columns in desired order:
\DTLaction[key=Title]{add column}
\DTLaction[key=Author]{add column}
\DTLaction[key=Format]{add column}
\DTLaction[key=Quantity]{add column}
\DTLaction[key=Price,value={Price (\$)}]{add column}
% 1st row:
\DTLaction[
 assign={
  Title = {The Adventures of Duck and Goose},
  Author = {Sir Quackalot},
  Format = paperback,
  Quantity = 3, Price = {10.99}
 }
]{new row}
% 2nd row:
\DTLaction[
 assign={
  Title = {The Return of Duck and Goose},
  Author = {Sir Quackalot},
  Format = paperback,
  Quantity = 5, Price = {19.99}
 }
]{new row}
% 3rd row:
\DTLaction[
 assign={
  Title = {More Fun with Duck and Goose},
  Author = {Sir Quackalot},
  Format = paperback,
  Quantity = 1, Price = {12.99}
 }
]{new row}
% 4th row:
\DTLaction[
 assign={
  Title = {Duck and Goose on Holiday},
  Author = {Sir Quackalot},
  Format = paperback,
  Quantity = 3, Price = {11.99}
 }
]{new row}
% 5th row:
\DTLaction[
 assign={
  Title = {The Return of Duck and Goose},
  Author = {Sir Quackalot},
  Format = hardback,
  Quantity = 3, Price = {19.99}
 }
]{new row}
% 6th row:
\DTLaction[
 assign={
  Title = {The Adventures of Duck and Goose},
  Author = {Sir Quackalot},
  Format = hardback,
  Quantity = 9, Price = {18.99}
 }
]{new row}
% 7th row:
\DTLaction[
 assign={
  Title = {My Friend is a Duck},
  Author = {A. Parrot},
  Format = paperback,
  Quantity = 20, Price = {14.99}
 }
]{new row}
% 8th row:
\DTLaction[
 assign={
  Title = {Annotated Notes on the ‘Duck and Goose’ chronicles},
  Author = {Prof Macaw},
  Format = ebook,
  Quantity = 10, Price = {8.99}
 }
]{new row}
% 9th row:
\DTLaction[
 assign={
  Title = {‘Duck and Goose’ Cheat Sheet for Students},
  Author = {Polly Parrot},
  Format = ebook,
  Quantity = 50, Price = {5.99}
 }
]{new row}
% 10th row:
\DTLaction[
 assign={
  Title = {‘Duck and Goose’: an allegory for modern times?},
  Author = {Bor Ing},
  Format = hardback,
  Quantity = 0, Price = {59.99}
 }
]{new row}
% 11th row:
\DTLaction[
 assign={
  Title = {Oh No! The Chickens have Escaped!},
  Author = {Dickie Duck},
  Format = ebook,
  Quantity = 11, Price = {2.0}
 }
]{new row} 

\newcommand{\productappendtotal}[2]{% 
 \DTLaction[
 return={ \theQuantity=Quantity, \thePrice=Price }
 ]{current row values}% 
 \DTLmul{\theTotal}{\theQuantity}{\thePrice}% 
 \DTLround{\theTotal}{\theTotal}{2}% 
 \appto#1{&}% 
 \eappto#1{\expandonce\theTotal}% 
 \DTLadd{\runningtotal}{\runningtotal}{\theTotal}% 
} 
\begin{document}
\DTLaction[
 options={
  only-keys={Title,Quantity,Price},
  pre-head={\toprule},
  after-head={\midrule},
  align-specs={lrrr},
  post-head={& \dtlcolumnheader{c}{Total}},
  init={\def\runningtotal{0}},
  post-row-function=\productappendtotal,
  foot={\midrule & & & \runningtotal}
 }
]{display} 
\end{document}
