CVS Health' topped Wall Street's expectations with its retail pharmacy business fueling growth despite fewer customers shopping in the front of its stores.

Revenue from CVS' retail pharmacy segment reached $20.7 billion, up 5.7 percent from the year-ago quarter, driven by an 8.3 percent increase in pharmacy revenue. Revenue from front-end sales, which includes goods like greeting cards and household items, grew just 0.2 percent.

Same-store prescription volume surged 9.5 percent, while front-end same-store sales dipped 1 percent.

Here's how the company did compared with what Wall Street analysts polled by Thomson Reuters expected:

Adjusted earnings: $1.69 per share vs. $1.61 per share

Revenue: $46.7 billion vs. $46.35 billion

In the quarter, CVS' revenue reached $46.7 billion, up 2.2 percent from the year-ago quarter and above analysts polled by Thomson Reuters' estimates of $46.35 billion.

In the quarter ended June 30, CVS reported a net loss of $2.56 billion, or $2.52 per share, compared with a profit of $1.10 billion, or $1.07 per share, a year ago.

After stripping out one-time expenses, including a $3.9 billion goodwill impairment charge related to its long-term care business, the company earned $1.72 billion, or $1.69 per share, topping analysts' estimates of $1.61 per share from Thomson Reuters.

The company narrowed its full-year adjusted earnings estimate to between $6.98 and $7.08 per share, up from $6.87 to $7.08 per share.

CVS expects its roughly $69 billion acquisition of health insurer Aetna to close in the late third quarter or early part of the fourth quarter. California's Insurance Commissioner last week urged the Department of Justice to block the deal.

Shares of CVS gained more then 3 percent Wednesday in premarket trading.

"The strong revenue, adjusted EPS, gross and operating margins, along with cash flow generated in the quarter were the direct result of our team's ability to increase prescription growth by expanding relationships with PBMs and health plans as well as our ongoing streamlining efforts and innovation," CVS CEO Larry Merlo said in a statement.

CVS and other drugstores, including Walgreens Boots Alliance and Rite Aid, have seen their so-called front-of-store sales threatened as shoppers increasingly buy household staples online or from convenience stores. E-commerce giant Amazon will enter the pharmacy business with its $1 billion acquisition of online start-up PillPack.

To protect its physical retail business, CVS has delved further into health-care services. Its walk-in retail clinics, known as MinuteClinics, are a key component of its acquisition of Aetna. The idea is they can add more services to the clinics and steer people into them and away from more expensive facilities like emergency rooms.

CVS is also trying to expand digitally. It announced Wednesday it's partnering with Teladoc Health to roll out virtual MinuteClinic visits nationwide this year.