For a one-off adjustment you could add
\let\oldsection\section% Store \section
\renewcommand{\section}{% Update \section
\renewcommand{\theequation}{\thesection.\arabic{equation}}% Update equation number
\oldsection}% Regular \section
\let\oldsubsection\subsection% Store \subsection
\renewcommand{\subsection}{% Update \subsection
\renewcommand{\theequation}{\thesubsection.\arabic{equation}}% Update equation number
\oldsubsection}% Regular \subsection
to your preamble. The above commands update \section
and \subsection
to adjust \theequation
- the equation number printing mechanism - to insert either \thesection
or \thesubsection
as part of the equation number.

\documentclass{article}
\usepackage{amsmath}
\numberwithin{equation}{subsection}
\let\oldsection\section% Store \section
\renewcommand{\section}{% Update \section
\renewcommand{\theequation}{\thesection.\arabic{equation}}% Update equation number
\oldsection}% Regular \section
\let\oldsubsection\subsection% Store \subsection
\renewcommand{\subsection}{% Update \subsection
\renewcommand{\theequation}{\thesubsection.\arabic{equation}}% Update equation number
\oldsubsection}% Regular \subsection
\begin{document}
\section{Section}
\begin{equation}
E=mc^2.
\end{equation}
\subsection{Sub-section}
\begin{equation}
E=\hbar \omega .
\end{equation}
\end{document}
For a more general approach (per-section type equation numbers), you could patch \@startsection
directly using etoolbox
:
\usepackage{etoolbox}
\makeatletter
\patchcmd{\@startsection}% <cmd>
{\if@noskipsec}% <search>
{\csgdef{theequation}{\csname the#1\endcsname.\arabic{equation}}\if@noskipsec}% <replace>
{}{}% <success><failure>
\makeatother
\@startsection
is used by every sectional command (when not using adjustments caused by certain packages) to set the sectional heading. Additionally, the first argument #1
contains the sectional unit name (see Where can I find help files or documentation for commands like \@startsection
for LaTeX?).
We patch this macro to use #1
as part of the counter updating, adding the prefix \csname the#1\endcsname.
to the equation number.