3

First, I tried to create a table by normal standards:

\begin{table}[H]
    \centering
    \begin{tabular}{p{14cm}|c}
        \textit{Query} & Grupo \\
        \hline
        SELECT SUM(lo\_extendedprice*lo\_discount) AS revenue FROM lineorder, dwdate WHERE d\_yearmonthnum = 199401 AND lo\_orderdate = d\_datekey AND lo\_discount BETWEEN 4 AND 6 AND lo\_quantity BETWEEN 26 AND 35; & 1 \\
        SELECT SUM(lo\_extendedprice*lo\_discount) AS revenue FROM lineorder, dwdate WHERE d\_year = 1993 AND lo\_orderdate = d\_datekey AND lo\_discount BETWEEN 1 AND 3 AND lo\_quantity < 25; & 1 \\
        SELECT SUM(lo\_extendedprice*lo\_discount) AS revenue FROM lineorder, dwdate WHERE d\_weeknuminyear = 6 AND lo\_orderdate = d\_datekey AND d\_year = 1994 AND lo\_discount BETWEEN 5 AND 7 AND lo\_quantity BETWEEN 26 AND 35; & 1 \\
        SELECT SUM(lo\_revenue), d\_year, p\_brand FROM lineorder, dwdate, part, supplier WHERE p\_category = 'MFGR\#12' AND lo\_orderdate = d\_datekey AND lo\_partkey = p\_partkey AND lo\_suppkey = s\_suppkey AND s\_region = 'AMERICA' GROUP BY d\_year, p\_brand ORDER BY d\_year, p\_brand; & 1 \\
        SELECT SUM(lo\_revenue), d\_year, p\_brand FROM lineorder, dwdate, part, supplier WHERE p\_brand BETWEEN 'MFGR\#2221' AND lo\_orderdate = d\_datekey AND lo\_partkey = p\_partkey AND lo\_suppkey = s\_suppkey AND 'MFGR\#2228' AND s\_region = 'ASIA' GROUP BY d\_year, p\_brand ORDER BY d\_year, p\_brand; & 1 \\
        SELECT SUM(lo\_revenue), d\_year, p\_brand FROM lineorder, dwdate, part, supplier WHERE p\_brand = 'MFGR\#2221' AND lo\_orderdate = d\_datekey AND lo\_partkey = p\_partkey AND lo\_suppkey = s\_suppkey AND s\_region = 'EUROPE' GROUP BY d\_year, p\_brand ORDER BY d\_year, p\_brand; & 1 \\
        SELECT c\_nation, s\_nation, d\_year, SUM(lo\_revenue) AS revenue FROM customer, lineorder, supplier, dwdate WHERE lo\_custkey = c\_custkey AND lo\_suppkey = s\_suppkey AND lo\_orderdate = d\_datekey AND c\_region = 'ASIA' AND s\_region = 'ASIA' AND d\_year >= 1992 AND d\_year <= 1997 GROUP BY c\_nation, s\_nation, d\_year ORDER BY d\_year asc, revenue DESC; & 1 \\
        SELECT c\_city, s\_city, d\_year, SUM(lo\_revenue) AS revenue FROM customer, lineorder, supplier, dwdate WHERE c\_nation = 'UNITED STATES' AND lo\_custkey = c\_custkey AND lo\_suppkey = s\_suppkey AND lo\_orderdate = d\_datekey AND s\_nation = 'UNITED STATES' AND d\_year >= 1992 AND d\_year <= 1997 GROUP BY c\_city, s\_city, d\_year ORDER BY d\_year asc, revenue DESC; & 1 \\
        SELECT c\_city, s\_city, d\_year, SUM(lo\_revenue) AS revenue FROM customer, lineorder, supplier, dwdate WHERE d\_year >= 1992 AND lo\_custkey = c\_custkey AND lo\_suppkey = s\_suppkey AND lo\_orderdate = d\_datekey AND (c\_city='UNITED KI1' OR c\_city='UNITED KI5') AND (s\_city='UNITED KI1' OR s\_city='UNITED KI5') AND d\_year <= 1997 GROUP BY c\_city, s\_city, d\_year ORDER BY d\_year asc, revenue DESC; & 1 \\
        SELECT c\_city, s\_city, d\_year, SUM(lo\_revenue) AS revenue FROM customer, lineorder, supplier, dwdate WHERE d\_yearmonth = 'Dec1997' AND lo\_custkey = c\_custkey AND lo\_suppkey = s\_suppkey AND lo\_orderdate = d\_datekey AND (c\_city='UNITED KI1' OR c\_city='UNITED KI5') AND (s\_city='UNITED KI1' OR s\_city='UNITED KI5') GROUP BY c\_city, s\_city, d\_year ORDER BY d\_year asc, revenue DESC; & 1 \\
        SELECT d\_year, c\_nation, SUM(lo\_revenue - lo\_supplycost) AS profit FROM dwdate, customer, supplier, part, lineorder WHERE lo\_custkey = c\_custkey  AND lo\_suppkey = s\_suppkey  AND lo\_partkey = p\_partkey  AND lo\_orderdate = d\_datekey  AND c\_region = 'AMERICA'  AND s\_region = 'AMERICA'  AND (p\_mfgr = 'MFGR\#1' OR p\_mfgr = 'MFGR\#2') GROUP BY d\_year, c\_nation ORDER BY d\_year, c\_nation; & 1 \\
        SELECT d\_year, s\_nation, p\_category, SUM(lo\_revenue - lo\_supplycost) AS profit FROM dwdate, customer, supplier, part, lineorder WHERE lo\_custkey = c\_custkey AND lo\_suppkey = s\_suppkey AND lo\_partkey = p\_partkey AND lo\_orderdate = d\_datekey AND c\_region = 'AMERICA' AND s\_region = 'AMERICA' AND (d\_year = 1997 OR d\_year = 1998) AND (p\_mfgr = 'MFGR\#1' OR p\_mfgr = 'MFGR\#2') GROUP BY d\_year, s\_nation, p\_category ORDER BY d\_year, s\_nation, p\_category; & 1 \\
        SELECT d\_year, s\_city, p\_brand, SUM(lo\_revenue - lo\_supplycost) AS profit FROM dwdate, customer, supplier, part, lineorder WHERE lo\_custkey = c\_custkey AND lo\_suppkey = s\_suppkey AND lo\_partkey = p\_partkey AND lo\_orderdate = d\_datekey AND c\_region = 'AMERICA' AND s\_nation = 'UNITED STATES' AND (d\_year = 1997 OR d\_year = 1998) AND p\_category = 'MFGR\#14' GROUP BY d\_year, s\_city, p\_brand ORDER BY d\_year, s\_city, p\_brand; & 1 \\
    \end{tabular}
    \caption{Classificação das \textit{queries} do \gls{ssb}}
    \label{tab:ssb_queries}
\end{table}

pdf_1

The code above gives me two problems, it's not fit on one or multiple pages and content of the second column is not vertically aligned. So, I tried to use longtable instead of table:

\begin{center}
    \begin{longtable}{p{14cm}|c}
    \textit{Query} & Group \\
    \hline
    \endfirsthead
    \textit{Query} & Group \\
    \hline
    \endhead
    \hline
    \multicolumn{2}{l}{Continue\ldots} \\
    \endfoot
    \hline
    \multicolumn{2}{l}{End} \\
    \endlastfoot
    SELECT SUM(lo\_extendedprice*lo\_discount) AS revenue FROM lineorder, dwdate WHERE d\_yearmonthnum = 199401 AND lo\_orderdate = d\_datekey AND lo\_discount BETWEEN 4 AND 6 AND lo\_quantity BETWEEN 26 AND 35; & 1 \\
    SELECT SUM(lo\_extendedprice*lo\_discount) AS revenue FROM lineorder, dwdate WHERE d\_year = 1993 AND lo\_orderdate = d\_datekey AND lo\_discount BETWEEN 1 AND 3 AND lo\_quantity < 25; & 1 \\
    SELECT SUM(lo\_extendedprice*lo\_discount) AS revenue FROM lineorder, dwdate WHERE d\_weeknuminyear = 6 AND lo\_orderdate = d\_datekey AND d\_year = 1994 AND lo\_discount BETWEEN 5 AND 7 AND lo\_quantity BETWEEN 26 AND 35; & 1 \\
    SELECT SUM(lo\_revenue), d\_year, p\_brand FROM lineorder, dwdate, part, supplier WHERE p\_category = 'MFGR\#12' AND lo\_orderdate = d\_datekey AND lo\_partkey = p\_partkey AND lo\_suppkey = s\_suppkey AND s\_region = 'AMERICA' GROUP BY d\_year, p\_brand ORDER BY d\_year, p\_brand; & 1 \\
    SELECT SUM(lo\_revenue), d\_year, p\_brand FROM lineorder, dwdate, part, supplier WHERE p\_brand BETWEEN 'MFGR\#2221' AND lo\_orderdate = d\_datekey AND lo\_partkey = p\_partkey AND lo\_suppkey = s\_suppkey AND 'MFGR\#2228' AND s\_region = 'ASIA' GROUP BY d\_year, p\_brand ORDER BY d\_year, p\_brand; & 1 \\
    SELECT SUM(lo\_revenue), d\_year, p\_brand FROM lineorder, dwdate, part, supplier WHERE p\_brand = 'MFGR\#2221' AND lo\_orderdate = d\_datekey AND lo\_partkey = p\_partkey AND lo\_suppkey = s\_suppkey AND s\_region = 'EUROPE' GROUP BY d\_year, p\_brand ORDER BY d\_year, p\_brand; & 1 \\
    SELECT c\_nation, s\_nation, d\_year, SUM(lo\_revenue) AS revenue FROM customer, lineorder, supplier, dwdate WHERE lo\_custkey = c\_custkey AND lo\_suppkey = s\_suppkey AND lo\_orderdate = d\_datekey AND c\_region = 'ASIA' AND s\_region = 'ASIA' AND d\_year >= 1992 AND d\_year <= 1997 GROUP BY c\_nation, s\_nation, d\_year ORDER BY d\_year asc, revenue DESC; & 1 \\
    SELECT c\_city, s\_city, d\_year, SUM(lo\_revenue) AS revenue FROM customer, lineorder, supplier, dwdate WHERE c\_nation = 'UNITED STATES' AND lo\_custkey = c\_custkey AND lo\_suppkey = s\_suppkey AND lo\_orderdate = d\_datekey AND s\_nation = 'UNITED STATES' AND d\_year >= 1992 AND d\_year <= 1997 GROUP BY c\_city, s\_city, d\_year ORDER BY d\_year asc, revenue DESC; & 1 \\
    SELECT c\_city, s\_city, d\_year, SUM(lo\_revenue) AS revenue FROM customer, lineorder, supplier, dwdate WHERE d\_year >= 1992 AND lo\_custkey = c\_custkey AND lo\_suppkey = s\_suppkey AND lo\_orderdate = d\_datekey AND (c\_city='UNITED KI1' OR c\_city='UNITED KI5') AND (s\_city='UNITED KI1' OR s\_city='UNITED KI5') AND d\_year <= 1997 GROUP BY c\_city, s\_city, d\_year ORDER BY d\_year asc, revenue DESC; & 1 \\
    SELECT c\_city, s\_city, d\_year, SUM(lo\_revenue) AS revenue FROM customer, lineorder, supplier, dwdate WHERE d\_yearmonth = 'Dec1997' AND lo\_custkey = c\_custkey AND lo\_suppkey = s\_suppkey AND lo\_orderdate = d\_datekey AND (c\_city='UNITED KI1' OR c\_city='UNITED KI5') AND (s\_city='UNITED KI1' OR s\_city='UNITED KI5') GROUP BY c\_city, s\_city, d\_year ORDER BY d\_year asc, revenue DESC; & 1 \\
    SELECT d\_year, c\_nation, SUM(lo\_revenue - lo\_supplycost) AS profit FROM dwdate, customer, supplier, part, lineorder WHERE lo\_custkey = c\_custkey  AND lo\_suppkey = s\_suppkey  AND lo\_partkey = p\_partkey  AND lo\_orderdate = d\_datekey  AND c\_region = 'AMERICA'  AND s\_region = 'AMERICA'  AND (p\_mfgr = 'MFGR\#1' OR p\_mfgr = 'MFGR\#2') GROUP BY d\_year, c\_nation ORDER BY d\_year, c\_nation; & 1 \\
    SELECT d\_year, s\_nation, p\_category, SUM(lo\_revenue - lo\_supplycost) AS profit FROM dwdate, customer, supplier, part, lineorder WHERE lo\_custkey = c\_custkey AND lo\_suppkey = s\_suppkey AND lo\_partkey = p\_partkey AND lo\_orderdate = d\_datekey AND c\_region = 'AMERICA' AND s\_region = 'AMERICA' AND (d\_year = 1997 OR d\_year = 1998) AND (p\_mfgr = 'MFGR\#1' OR p\_mfgr = 'MFGR\#2') GROUP BY d\_year, s\_nation, p\_category ORDER BY d\_year, s\_nation, p\_category; & 1 \\
    SELECT d\_year, s\_city, p\_brand, SUM(lo\_revenue - lo\_supplycost) AS profit FROM dwdate, customer, supplier, part, lineorder WHERE lo\_custkey = c\_custkey AND lo\_suppkey = s\_suppkey AND lo\_partkey = p\_partkey AND lo\_orderdate = d\_datekey AND c\_region = 'AMERICA' AND s\_nation = 'UNITED STATES' AND (d\_year = 1997 OR d\_year = 1998) AND p\_category = 'MFGR\#14' GROUP BY d\_year, s\_city, p\_brand ORDER BY d\_year, s\_city, p\_brand; & 1 \\
    \caption{Classificação das \textit{queries} do \gls{ssb}} \\
    \end{longtable}
\end{center}

pdf_2

pdf_3

The second code places the long table across multiple pages, but the caption of the table is inside the foot part. How can I vertically center the second column, put the caption at the bottom and fit the table into one or multiples pages ? Additionally, is it possible to add spacing between rows ?

EDIT:

The template I'm using can be found here.

3
  • Welcome to TeX.SE. Do please tell us which document class you employ, what the pointsize of the main document is, wnich main document font you employ, and how wide and tall the text block is.
    – Mico
    Mar 12, 2023 at 22:57
  • 1
    Never encase a longtable environment in a center environment.
    – Mico
    Mar 12, 2023 at 22:57
  • @Mico, I'm new to LaTeX and I'm using a template distributed by my college, so I'm not sure how to answer some of your questions. But I can say that I'm using: \documentclass[pt,oneside,onehalfspacing,bsc]{risethesis}, \usepackage[utf8]{inputenc}. You can find the template here Mar 13, 2023 at 4:10

4 Answers 4

4

Here's a code based on longtable with working headers and footers. I also added two packages: listings improves code formatting; and cellspace inserts extra vertical spacing between rows.

\documentclass{article}
\usepackage{array}
\usepackage{xcolor}
\usepackage[margin=1in]{geometry}
\usepackage{listings}
\usepackage{longtable}
\usepackage[column=E]{cellspace}

\lstset{
    basicstyle=\small\rmfamily,   % \ttfamily or \sffamily
    aboveskip=0pt,
    belowskip=0pt,
    boxpos=t,
    language=sql,
    keywordstyle=\color{black!30!blue}\bfseries,
    columns=flexible,
}

\setlength\cellspacetoplimit{4pt}
\setlength\cellspacebottomlimit{4pt}


\begin{document}
\begin{longtable}{E{w{l}{12cm}}|c}
    \caption{Classificação das \textit{queries} do SSB}\label{tab:ssb_queries} \\
    % \caption{Classificação das \textit{queries} do \gls{ssb}}
    \textit{Query} & Group \\
    \hline\endfirsthead
    \textit{Query} & Group \\
    \hline\endhead
    \hline
    \multicolumn{2}{@{}E{l}}{Continue ...}\endfoot
    \hline
    \multicolumn{2}{@{}E{l}}{End.}\endlastfoot
    %%%
\begin{lstlisting}
SELECT SUM(lo_extendedprice*lo_discount) AS revenue
    FROM lineorder, dwdate
WHERE d_yearmonthnum = 199401
    AND lo_orderdate = d_datekey
    AND lo_discount BETWEEN 4
    AND 6 AND lo_quantity
    BETWEEN 26 AND 35;
\end{lstlisting} & 1 \\
\begin{lstlisting}
SELECT SUM(lo\_extendedprice*lo\_discount) AS revenue
    FROM lineorder, dwdate
WHERE d_year = 1993
    AND lo_orderdate = d\_datekey
    AND lo_discount
    BETWEEN 1 AND 3
    AND lo_quantity < 25;
\end{lstlisting} & 1 \\
\begin{lstlisting}
SELECT SUM(lo\_extendedprice*lo\_discount) AS revenue
    FROM lineorder, dwdate
WHERE d_year = 1993
    AND lo_orderdate = d\_datekey
    AND lo_discount
    BETWEEN 1 AND 3
    AND lo_quantity < 25;
\end{lstlisting} & 1 \\
\begin{lstlisting}
SELECT SUM(lo\_extendedprice*lo\_discount) AS revenue
    FROM lineorder, dwdate
WHERE d_year = 1993
    AND lo_orderdate = d\_datekey
    AND lo_discount
    BETWEEN 1 AND 3
    AND lo_quantity < 25;
\end{lstlisting} & 1 \\
\begin{lstlisting}
SELECT SUM(lo\_extendedprice*lo\_discount) AS revenue
    FROM lineorder, dwdate
WHERE d_year = 1993
    AND lo_orderdate = d\_datekey
    AND lo_discount
    BETWEEN 1 AND 3
    AND lo_quantity < 25;
\end{lstlisting} & 1 \\
\begin{lstlisting}
SELECT SUM(lo\_extendedprice*lo\_discount) AS revenue
    FROM lineorder, dwdate
WHERE d_year = 1993
    AND lo_orderdate = d\_datekey
    AND lo_discount
    BETWEEN 1 AND 3
    AND lo_quantity < 25;
\end{lstlisting} & 1 \\
\begin{lstlisting}
SELECT SUM(lo\_extendedprice*lo\_discount) AS revenue
    FROM lineorder, dwdate
WHERE d_year = 1993
    AND lo_orderdate = d\_datekey
    AND lo_discount
    BETWEEN 1 AND 3
    AND lo_quantity < 25;
\end{lstlisting} & 1 \\
\begin{lstlisting}
SELECT SUM(lo_extendedprice*lo_discount) AS revenue
    FROM lineorder, dwdate
WHERE d_yearmonthnum = 199401
    AND lo_orderdate = d_datekey
    AND lo_discount BETWEEN 4
    AND 6 AND lo_quantity
    BETWEEN 26 AND 35;
\end{lstlisting} & 1 \\
\end{longtable}
\end{document}

enter image description here


EDIT. If you prefer a different vertical alignment, simply change the value of boxpos. For instance, I assumed m for the middle alignment:

\lstset{
    % ...
    boxpos=m,  % t for top or b for bottom alignment
    % ...
}

I also assumed you would expect the title on the last page at the bottom of your table. You have to move both \caption and \label to a block ending with \endlastfoot. Below is the snippet:

% ...
\begin{longtable}{E{w{l}{12cm}}|c}
    \textit{Query} & Group \\
    \hline\endfirsthead
    \textit{Query} & Group \\
    \hline\endhead
    \hline
    \multicolumn{2}{@{}E{l}}{Continue ...}\endfoot
    \hline
    \multicolumn{2}{@{}E{l}}{End.} \\[2ex]
    \caption{Classificação das \textit{queries} do SSB}
    \label{tab:ssb_queries}\endlastfoot
    %%%
% ...

The rest of the code remains the same.

enter image description here

5
  • +1 for introducing of `listing. Using it table get some sense...
    – Zarko
    Mar 12, 2023 at 23:40
  • @Zarko Thanks. Since the first column contains pure code, I thought OP might find it useful.
    – Celdor
    Mar 12, 2023 at 23:55
  • Your code does not allow to place the caption below the "End" of \endlastfoot, also the second column is not aligned vertically, as I wish. Mar 13, 2023 at 4:46
  • 1
    @NicholasHenrique By aligned vertically, I assume the middle alignment. There is an update in my answer with suggested changes. I also included screenshot so you can see what my output is after applying changes.
    – Celdor
    Mar 13, 2023 at 9:43
  • 1
    @NicholasHenrique, tables usual have captions on the top of table. For example, all packages for long table anticipate this.
    – Zarko
    Mar 13, 2023 at 19:30
3

Here's a longtable based solution that strives to give the table a more open "look" by using the line-drawing macros of the booktabs package -- specifically, \midrule, \addlinespace, and \bottomrule -- instead of \hline. It also dispenses with full justification.

enter image description here

\documentclass{article}
\usepackage[a4paper,margin=2.5cm]{geometry} % set page parameters as needed
\usepackage[portuguese]{babel}
\usepackage[T1]{fontenc}
\providecommand\gls[1]{#1} % ??
% new:
\usepackage{longtable,array,booktabs}

\begin{document}

\begin{longtable}{@{} >{\raggedright\arraybackslash}p{14cm} r @{}}

%% headers and footers
\caption{Classificação das \textit{queries} do \gls{ssb}} \\
\textit{Query} & Group \\
\midrule
\endfirsthead

\tablename\ \thetable, continued\\[1ex]
\textit{Query} & Group \\
\midrule
\endhead

\midrule
\multicolumn{2}{r@{}}{Continue\dots} \\
\endfoot

\bottomrule
%\multicolumn{2}{l}{End} \\
\endlastfoot

%% body of longtable
SELECT SUM(lo\_extendedprice*lo\_discount) AS revenue FROM lineorder, dwdate WHERE d\_yearmonthnum = 199401 AND lo\_orderdate = d\_datekey AND lo\_discount BETWEEN 4 AND 6 AND lo\_quantity BETWEEN 26 AND 35;
& 1 \\ 
\addlinespace
SELECT SUM(lo\_extendedprice*lo\_discount) AS revenue FROM lineorder, dwdate WHERE d\_year = 1993 AND lo\_orderdate = d\_datekey AND lo\_discount BETWEEN 1 AND 3 AND lo\_quantity < 25;
& 1 \\
\addlinespace
SELECT SUM(lo\_extendedprice*lo\_discount) AS revenue FROM lineorder, dwdate WHERE d\_weeknuminyear = 6 AND lo\_orderdate = d\_datekey AND d\_year = 1994 AND lo\_discount BETWEEN 5 AND 7 AND lo\_quantity BETWEEN 26 AND 35;
& 1 \\
\addlinespace
SELECT SUM(lo\_revenue), d\_year, p\_brand FROM lineorder, dwdate, part, supplier WHERE p\_category = 'MFGR\#12' AND lo\_orderdate = d\_datekey AND lo\_partkey = p\_partkey AND lo\_suppkey = s\_suppkey AND s\_region = 'AMERICA' GROUP BY d\_year, p\_brand ORDER BY d\_year, p\_brand;
& 1 \\
\addlinespace
SELECT SUM(lo\_revenue), d\_year, p\_brand FROM lineorder, dwdate, part, supplier WHERE p\_brand BETWEEN 'MFGR\#2221' AND lo\_orderdate = d\_datekey AND lo\_partkey = p\_partkey AND lo\_suppkey = s\_suppkey AND 'MFGR\#2228' AND s\_region = 'ASIA' GROUP BY d\_year, p\_brand ORDER BY d\_year, p\_brand;
& 1 \\
\addlinespace
SELECT SUM(lo\_revenue), d\_year, p\_brand FROM lineorder, dwdate, part, supplier WHERE p\_brand = 'MFGR\#2221' AND lo\_orderdate = d\_datekey AND lo\_partkey = p\_partkey AND lo\_suppkey = s\_suppkey AND s\_region = 'EUROPE' GROUP BY d\_year, p\_brand ORDER BY d\_year, p\_brand;
& 1 \\
\addlinespace
SELECT c\_nation, s\_nation, d\_year, SUM(lo\_revenue) AS revenue FROM customer, lineorder, supplier, dwdate WHERE lo\_custkey = c\_custkey AND lo\_suppkey = s\_suppkey AND lo\_orderdate = d\_datekey AND c\_region = 'ASIA' AND s\_region = 'ASIA' AND d\_year >= 1992 AND d\_year <= 1997 GROUP BY c\_nation, s\_nation, d\_year ORDER BY d\_year asc, revenue DESC;
& 1 \\
\addlinespace
SELECT c\_city, s\_city, d\_year, SUM(lo\_revenue) AS revenue FROM customer, lineorder, supplier, dwdate WHERE c\_nation = 'UNITED STATES' AND lo\_custkey = c\_custkey AND lo\_suppkey = s\_suppkey AND lo\_orderdate = d\_datekey AND s\_nation = 'UNITED STATES' AND d\_year >= 1992 AND d\_year <= 1997 GROUP BY c\_city, s\_city, d\_year ORDER BY d\_year asc, revenue DESC;
& 1 \\
\addlinespace
SELECT c\_city, s\_city, d\_year, SUM(lo\_revenue) AS revenue FROM customer, lineorder, supplier, dwdate WHERE d\_year >= 1992 AND lo\_custkey = c\_custkey AND lo\_suppkey = s\_suppkey AND lo\_orderdate = d\_datekey AND (c\_city='UNITED KI1' OR c\_city='UNITED KI5') AND (s\_city='UNITED KI1' OR s\_city='UNITED KI5') AND d\_year <= 1997 GROUP BY c\_city, s\_city, d\_year ORDER BY d\_year asc, revenue DESC;
& 1 \\
\addlinespace
SELECT c\_city, s\_city, d\_year, SUM(lo\_revenue) AS revenue FROM customer, lineorder, supplier, dwdate WHERE d\_yearmonth = 'Dec1997' AND lo\_custkey = c\_custkey AND lo\_suppkey = s\_suppkey AND lo\_orderdate = d\_datekey AND (c\_city='UNITED KI1' OR c\_city='UNITED KI5') AND (s\_city='UNITED KI1' OR s\_city='UNITED KI5') GROUP BY c\_city, s\_city, d\_year ORDER BY d\_year asc, revenue DESC;
& 1 \\
\addlinespace
SELECT d\_year, c\_nation, SUM(lo\_revenue - lo\_supplycost) AS profit FROM dwdate, customer, supplier, part, lineorder WHERE lo\_custkey = c\_custkey  AND lo\_suppkey = s\_suppkey  AND lo\_partkey = p\_partkey  AND lo\_orderdate = d\_datekey  AND c\_region = 'AMERICA'  AND s\_region = 'AMERICA'  AND (p\_mfgr = 'MFGR\#1' OR p\_mfgr = 'MFGR\#2') GROUP BY d\_year, c\_nation ORDER BY d\_year, c\_nation;
& 1 \\
\addlinespace
SELECT d\_year, s\_nation, p\_category, SUM(lo\_revenue - lo\_supplycost) AS profit FROM dwdate, customer, supplier, part, lineorder WHERE lo\_custkey = c\_custkey AND lo\_suppkey = s\_suppkey AND lo\_partkey = p\_partkey AND lo\_orderdate = d\_datekey AND c\_region = 'AMERICA' AND s\_region = 'AMERICA' AND (d\_year = 1997 OR d\_year = 1998) AND (p\_mfgr = 'MFGR\#1' OR p\_mfgr = 'MFGR\#2') GROUP BY d\_year, s\_nation, p\_category ORDER BY d\_year, s\_nation, p\_category;
& 1 \\
\addlinespace
SELECT d\_year, s\_city, p\_brand, SUM(lo\_revenue - lo\_supplycost) AS profit FROM dwdate, customer, supplier, part, lineorder WHERE lo\_custkey = c\_custkey AND lo\_suppkey = s\_suppkey AND lo\_partkey = p\_partkey AND lo\_orderdate = d\_datekey AND c\_region = 'AMERICA' AND s\_nation = 'UNITED STATES' AND (d\_year = 1997 OR d\_year = 1998) AND p\_category = 'MFGR\#14' GROUP BY d\_year, s\_city, p\_brand ORDER BY d\_year, s\_city, p\_brand;
& 1 \\
\end{longtable}

\end{document}
6
  • Is it possible to place the caption at the bottom of the table and vertically align the second column? Mar 13, 2023 at 5:10
  • 1
    @NicholasHenrique - Please clarify what you mean by "vertically align". E.g. do you require vertical centering? To place the caption at the very end -- a rather questionable practice -- move the \caption statement from the \endfirsthead to the \endlastfoot area.
    – Mico
    Mar 13, 2023 at 5:14
  • 1
    @NicholasHenrique - If you mean by "vertically align" vertical centering, then all you need to do is to change p{14cm} to m{14cm}.
    – Mico
    Mar 13, 2023 at 12:46
  • 1
    Yes, I meant vertical centering, m{14cm} does the trick, thanks. I think just for your answer to be complete, the table should contain a vertical line between the columns, is that possible? I tried to use \begin{longtable}{@{} >{\raggedright\arraybackslash}m{14.5cm} | r @{}}, but the vertical line doens't appear where \addlinespace is. Mar 13, 2023 at 23:41
  • 1
    I understand your point, but I just wanted to follow the pattern of the given thesis template. Mar 14, 2023 at 2:59
2

Try with longtblr of tabularray package. MWE using it is much shorter and clear:

\documentclass{article}
\usepackage{geometry}
\usepackage{tabularray}

\begin{document}

    \begin{longtblr}[
caption = {Classificação das \textit{queries} do},% \gls{ssb}
  label = {longtab:?}
                    ]{hline{2,Z} = {solid}, vline{2} = solid,
                      colspec = {X[j] c},
                      rowsep=3pt,
                      rowhead = 1
                      }
\textit{Query} 
    & Group \\
%%%% table body
SELECT SUM(lo\_extendedprice*lo\_discount) AS revenue FROM lineorder, dwdate WHERE d\_yearmonthnum = 199401 AND lo\_orderdate = d\_datekey AND lo\_discount BETWEEN 4 AND 6 AND lo\_quantity BETWEEN 26 AND 35; 
    & 1 \\
SELECT SUM(lo\_extendedprice*lo\_discount) AS revenue FROM lineorder, dwdate WHERE d\_year = 1993 AND lo\_orderdate = d\_datekey AND lo\_discount BETWEEN 1 AND 3 AND lo\_quantity < 25;
    & 1 \\
SELECT SUM(lo\_extendedprice*lo\_discount) AS revenue FROM lineorder, dwdate WHERE d\_weeknuminyear = 6 AND lo\_orderdate = d\_datekey AND d\_year = 1994 AND lo\_discount BETWEEN 5 AND 7 AND lo\_quantity BETWEEN 26 AND 35;
    & 1 \\
SELECT SUM(lo\_revenue), d\_year, p\_brand FROM lineorder, dwdate, part, supplier WHERE p\_category = 'MFGR\#12' AND lo\_orderdate = d\_datekey AND lo\_partkey = p\_partkey AND lo\_suppkey = s\_suppkey AND s\_region = 'AMERICA' GROUP BY d\_year, p\_brand ORDER BY d\_year, p\_brand; 
    & 1 \\
SELECT SUM(lo\_revenue), d\_year, p\_brand FROM lineorder, dwdate, part, supplier WHERE p\_brand BETWEEN 'MFGR\#2221' AND lo\_orderdate = d\_datekey AND lo\_partkey = p\_partkey AND lo\_suppkey = s\_suppkey AND 'MFGR\#2228' AND s\_region = 'ASIA' GROUP BY d\_year, p\_brand ORDER BY d\_year, p\_brand;
    & 1 \\
SELECT SUM(lo\_revenue), d\_year, p\_brand FROM lineorder, dwdate, part, supplier WHERE p\_brand = 'MFGR\#2221' AND lo\_orderdate = d\_datekey AND lo\_partkey = p\_partkey AND lo\_suppkey = s\_suppkey AND s\_region = 'EUROPE' GROUP BY d\_year, p\_brand ORDER BY d\_year, p\_brand; 
    & 1 \\
SELECT c\_nation, s\_nation, d\_year, SUM(lo\_revenue) AS revenue FROM customer, lineorder, supplier, dwdate WHERE lo\_custkey = c\_custkey AND lo\_suppkey = s\_suppkey AND lo\_orderdate = d\_datekey AND c\_region = 'ASIA' AND s\_region = 'ASIA' AND d\_year >= 1992 AND d\_year <= 1997 GROUP BY c\_nation, s\_nation, d\_year ORDER BY d\_year asc, revenue DESC;
    & 1 \\
SELECT c\_city, s\_city, d\_year, SUM(lo\_revenue) AS revenue FROM customer, lineorder, supplier, dwdate WHERE c\_nation = 'UNITED STATES' AND lo\_custkey = c\_custkey AND lo\_suppkey = s\_suppkey AND lo\_orderdate = d\_datekey AND s\_nation = 'UNITED STATES' AND d\_year >= 1992 AND d\_year <= 1997 GROUP BY c\_city, s\_city, d\_year ORDER BY d\_year asc, revenue DESC;
    & 1 \\
SELECT c\_city, s\_city, d\_year, SUM(lo\_revenue) AS revenue FROM customer, lineorder, supplier, dwdate WHERE d\_year >= 1992 AND lo\_custkey = c\_custkey AND lo\_suppkey = s\_suppkey AND lo\_orderdate = d\_datekey AND (c\_city='UNITED KI1' OR c\_city='UNITED KI5') AND (s\_city='UNITED KI1' OR s\_city='UNITED KI5') AND d\_year <= 1997 GROUP BY c\_city, s\_city, d\_year ORDER BY d\_year asc, revenue DESC;
    & 1 \\
SELECT c\_city, s\_city, d\_year, SUM(lo\_revenue) AS revenue FROM customer, lineorder, supplier, dwdate WHERE d\_yearmonth = 'Dec1997' AND lo\_custkey = c\_custkey AND lo\_suppkey = s\_suppkey AND lo\_orderdate = d\_datekey AND (c\_city='UNITED KI1' OR c\_city='UNITED KI5') AND (s\_city='UNITED KI1' OR s\_city='UNITED KI5') GROUP BY c\_city, s\_city, d\_year ORDER BY d\_year asc, revenue DESC;
    & 1 \\
SELECT d\_year, c\_nation, SUM(lo\_revenue - lo\_supplycost) AS profit FROM dwdate, customer, supplier, part, lineorder WHERE lo\_custkey = c\_custkey  AND lo\_suppkey = s\_suppkey  AND lo\_partkey = p\_partkey  AND lo\_orderdate = d\_datekey  AND c\_region = 'AMERICA'  AND s\_region = 'AMERICA'  AND (p\_mfgr = 'MFGR\#1' OR p\_mfgr = 'MFGR\#2') GROUP BY d\_year, c\_nation ORDER BY d\_year, c\_nation;
    & 1 \\
SELECT d\_year, s\_nation, p\_category, SUM(lo\_revenue - lo\_supplycost) AS profit FROM dwdate, customer, supplier, part, lineorder WHERE lo\_custkey = c\_custkey AND lo\_suppkey = s\_suppkey AND lo\_partkey = p\_partkey AND lo\_orderdate = d\_datekey AND c\_region = 'AMERICA' AND s\_region = 'AMERICA' AND (d\_year = 1997 OR d\_year = 1998) AND (p\_mfgr = 'MFGR\#1' OR p\_mfgr = 'MFGR\#2') GROUP BY d\_year, s\_nation, p\_category ORDER BY d\_year, s\_nation, p\_category;
    & 1 \\
SELECT d\_year, s\_city, p\_brand, SUM(lo\_revenue - lo\_supplycost) AS profit FROM dwdate, customer, supplier, part, lineorder WHERE lo\_custkey = c\_custkey AND lo\_suppkey = s\_suppkey AND lo\_partkey = p\_partkey AND lo\_orderdate = d\_datekey AND c\_region = 'AMERICA' AND s\_nation = 'UNITED STATES' AND (d\_year = 1997 OR d\_year = 1998) AND p\_category = 'MFGR\#14' GROUP BY d\_year, s\_city, p\_brand ORDER BY d\_year, s\_city, p\_brand;
    & 1 \\
    \end{longtblr}
\end{document}    

enter image description here

4
  • Is it possible to vertically center the second column ? Mar 14, 2023 at 3:04
  • 1
    @NicholasHenrique, yes, it is possible. Change colspec = {X[j] c} to colspec = {X[j,m] c} (observe added m).
    – Zarko
    Mar 14, 2023 at 3:14
  • I couldn't validate your answer, when I tried to use it with this template, I got Missing number, treated as zero. and Package xcolor Error: Undefined color `j'. errors. Mar 14, 2023 at 23:57
  • Zill now we haven't any information about used \documentclass and which packages you load in preamble. So please, first edit your question, extend provided code fragment to complete small document (MWE: Minimal working example) and provide used \documentclass˛. It seems, that you need to add packages used in answer(s) in your document preamble.
    – Zarko
    Mar 15, 2023 at 2:19
1

The answer of @Celdor is very interesting, so I try to implement his approach in my answer. I quickly realized that this is not so easy, since listings can not be inserted directly in tabularray cells, even if you use verb option (it support only short \verb command).

By help of @Paul Gaborit answer on my query for help, below is possible solution of yours problem:

\documentclass{article}
\usepackage{tabularray}
\usepackage{xcolor}
\usepackage{listings}
\lstset{
    basicstyle=\small\sffamily, 
    aboveskip=-7pt,
    belowskip=-2pt,
    language=sql,
    keywordstyle=\color{black!30!blue}\bfseries,
    columns=flexible,
        }

\begin{document}

\begin{filecontents*}[force]{\jobname-sql-1.tex}
SELECT SUM(lo\_extendedprice*lo\_discount) AS revenue
    FROM lineorder, dwdate
WHERE d\_yearmonthnum = 199401
    AND lo\_orderdate = d\_datekey AND lo\_discount BETWEEN 4
    AND 6
    AND lo\_quantity BETWEEN 26 AND 35;
\end{filecontents*}\\
\begin{filecontents*}[force]{\jobname-sql-2.tex}
SELECT SUM(lo\_extendedprice*lo\_discount) AS revenue F
    ROM lineorder, dwdate
WHERE d\_year = 1993
    AND lo\_orderdate = d\_datekey
    AND lo\_discount BETWEEN 1
    AND 3
    AND lo\_quantity < 25;
\end{filecontents*}\\
\begin{filecontents*}[force]{\jobname-sql-3.tex}
SELECT SUM(lo\_extendedprice*lo\_discount) AS revenue
    FROM lineorder, dwdate
WHERE d\_weeknuminyear = 6
    AND lo\_orderdate = d\_datekey
    AND d\_year = 1994
    AND lo\_discount BETWEEN 5
    AND 7
    AND lo\_quantity BETWEEN 26 AND 35;
\end{filecontents*}\\
\begin{filecontents*}[force]{\jobname-sql-4.tex}
SELECT SUM(lo\_revenue), d\_year, p\_brand
    FROM lineorder, dwdate, part, supplier
WHERE p\_category = 'MFGR\#12'
    AND lo\_orderdate = d\_datekey
    AND lo\_partkey = p\_partkey
    AND lo\_suppkey = s\_suppkey
    AND s\_region = 'AMERICA'
    GROUP BY d\_year, p\_brand
    ORDER BY d\_year, p\_brand;
\end{filecontents*}\\
\begin{filecontents*}[force]{\jobname-sql-5.tex}
SELECT SUM(lo\_revenue), d\_year, p\_brand
    FROM lineorder, dwdate, part, supplier
WHERE p\_brand BETWEEN 'MFGR\#2221'
    AND lo\_orderdate = d\_datekey
    AND lo\_partkey = p\_partkey
    AND lo\_suppkey = s\_suppkey
    AND 'MFGR\#2228' AND s\_region = 'ASIA'
    GROUP BY d\_year, p\_brand
    ORDER BY d\_year, p\_brand;
\end{filecontents*}\\
\begin{filecontents*}[force]{\jobname-sql-6.tex}
SELECT SUM(lo\_revenue), d\_year, p\_brand
    FROM lineorder, dwdate, part, supplier
WHERE p\_brand = 'MFGR\#2221'
    AND lo\_orderdate = d\_datekey
    AND lo\_partkey = p\_partkey
    AND lo\_suppkey = s\_suppkey
    AND s\_region = 'EUROPE'
    GROUP BY d\_year, p\_brand
    ORDER BY d\_year, p\_brand;
\end{filecontents*}\\
\begin{filecontents*}[force]{\jobname-sql-7.tex}
SELECT c\_nation, s\_nation, d\_year,
    SUM(lo\_revenue) AS revenue
    FROM customer, lineorder, supplier, dwdate
WHERE lo\_custkey = c\_custkey
    AND lo\_suppkey = s\_suppkey
    AND lo\_orderdate = d\_datekey
    AND c\_region = 'ASIA'
    AND s\_region = 'ASIA'
    AND d\_year >= 1992
    AND d\_year <= 1997
    GROUP BY c\_nation, s\_nation, d\_year
    ORDER BY d\_year asc, revenue DESC;
\end{filecontents*}\\
\begin{filecontents*}[force]{\jobname-sql-8.tex}
SELECT c\_city, s\_city, d\_year,
    SUM(lo\_revenue) AS revenue
    FROM customer, lineorder, supplier, dwdate
WHERE c\_nation = 'UNITED STATES'
    AND lo\_custkey = c\_custkey
    AND lo\_suppkey = s\_suppkey
    AND lo\_orderdate = d\_datekey
    AND s\_nation = 'UNITED STATES'
    AND d\_year >= 1992
    AND d\_year <= 1997
    GROUP BY c\_city, s\_city, d\_year
    ORDER BY d\_year asc, revenue DESC;
\end{filecontents*}\\
\begin{filecontents*}[force]{\jobname-sql-9.tex}
SELECT c\_city, s\_city, d\_year,
    SUM(lo\_revenue) AS revenue
    FROM customer, lineorder, supplier, dwdate
WHERE d\_year >= 1992
    AND lo\_custkey = c\_custkey
    AND lo\_suppkey = s\_suppkey
    AND lo\_orderdate = d\_datekey
    AND (c\_city='UNITED KI1' OR c\_city='UNITED KI5')
    AND (s\_city='UNITED KI1' OR s\_city='UNITED KI5')
    AND d\_year <= 1997
    GROUP BY c\_city, s\_city, d\_year
    ORDER BY d\_year asc, revenue DESC;
\end{filecontents*}\\
\begin{filecontents*}[force]{\jobname-sql-10.tex}
SELECT c\_city, s\_city, d\_year,
    SUM(lo\_revenue) AS revenue
    FROM customer, lineorder, supplier, dwdate
WHERE d\_yearmonth = 'Dec1997'
    AND lo\_custkey = c\_custkey
    AND lo\_suppkey = s\_suppkey
    AND lo\_orderdate = d\_datekey
    AND (c\_city='UNITED KI1' OR c\_city='UNITED KI5')
    AND (s\_city='UNITED KI1' OR s\_city='UNITED KI5')
    GROUP BY c\_city, s\_city, d\_year
    ORDER BY d\_year asc, revenue DESC;
\end{filecontents*}\\
\begin{filecontents*}[force]{\jobname-sql-11.tex}
SELECT d\_year, c\_nation,
    SUM(lo\_revenue - lo\_supplycost) AS profit
    FROM dwdate, customer, supplier, part, lineorder
WHERE lo\_custkey = c\_custkey
    AND lo\_suppkey = s\_suppkey
    AND lo\_partkey = p\_partkey
    AND lo\_orderdate = d\_datekey
    AND c\_region = 'AMERICA'
    AND s\_region = 'AMERICA'
    AND (p\_mfgr = 'MFGR\#1' OR p\_mfgr = 'MFGR\#2')
    GROUP BY d\_year, c\_nation
    ORDER BY d\_year, c\_nation;
\end{filecontents*}\\
\begin{filecontents*}[force]{\jobname-sql-12.tex}
SELECT d\_year, s\_nation, p\_category,
    SUM(lo\_revenue - lo\_supplycost) AS profit
    FROM dwdate, customer, supplier, part, lineorder
WHERE lo\_custkey = c\_custkey
    AND lo\_suppkey = s\_suppkey
    AND lo\_partkey = p\_partkey
    AND lo\_orderdate = d\_datekey
    AND c\_region = 'AMERICA'
    AND s\_region = 'AMERICA'
    AND (d\_year = 1997 OR d\_year = 1998)
    AND (p\_mfgr = 'MFGR\#1' OR p\_mfgr = 'MFGR\#2')
    GROUP BY d\_year, s\_nation, p\_category
    ORDER BY d\_year, s\_nation, p\_category;
\end{filecontents*}\\
\begin{filecontents*}[force]{\jobname-sql-13.tex}
SELECT d\_year, s\_city, p\_brand,
    SUM(lo\_revenue - lo\_supplycost) AS profit
    FROM dwdate, customer, supplier, part, lineorder
WHERE lo\_custkey = c\_custkey
    AND lo\_suppkey = s\_suppkey
    AND lo\_partkey = p\_partkey
    AND lo\_orderdate = d\_datekey
    AND c\_region = 'AMERICA'
    AND s\_nation = 'UNITED STATES'
    AND (d\_year = 1997 OR d\_year = 1998)
    AND p\_category = 'MFGR\#14'
    GROUP BY d\_year, s\_city, p\_brand
    ORDER BY d\_year, s\_city, p\_brand;
\end{filecontents*}\\

\noindent%
    \begin{longtblr}[
caption = {Classificação das \textit{queries} do},% \gls{ssb}
  label = {longtab:?}
                    ]{hline{2,Z} = {solid}, 
                      colspec = {X[j,m] | c},
                      row{1} = {font=\itshape}, 
                      rowsep=0pt,
                      rowhead=1}
                      rowhead=1}
  \lstinputlisting{\jobname-sql-1.tex}  & 1 \\
  \lstinputlisting{\jobname-sql-2.tex}  & 1 \\
  \lstinputlisting{\jobname-sql-3.tex}  & 1 \\
  \lstinputlisting{\jobname-sql-4.tex}  & 1 \\
  \lstinputlisting{\jobname-sql-5.tex}  & 1 \\
  \lstinputlisting{\jobname-sql-6.tex}  & 1 \\
  \lstinputlisting{\jobname-sql-7.tex}  & 1 \\
  \lstinputlisting{\jobname-sql-8.tex}  & 1 \\
  \lstinputlisting{\jobname-sql-9.tex}  & 1 \\
  \lstinputlisting{\jobname-sql-10.tex} & 1 \\
  \lstinputlisting{\jobname-sql-11.tex} & 1 \\
  \lstinputlisting{\jobname-sql-12.tex} & 1 \\
  \lstinputlisting{\jobname-sql-13.tex} & 1 \\
\end{longtblr}
\end{document}

enter image description here

2
  • I voted up for the effort of chasing the issue and working out the solution.
    – Celdor
    Mar 15, 2023 at 1:13
  • @Celdor, thank you very much!
    – Zarko
    Mar 15, 2023 at 2:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .