
903 S.W.2d 398 (1995)
Cecil R. WILLIAMS and Sandra A. Williams, Appellants,
v.
NATIONAL MORTGAGE COMPANY, Appellee.
No. 05-94-00685-CV.
Court of Appeals of Texas, Dallas.
June 12, 1995.
Rehearing Overruled July 25, 1995.
*400 Robert D. Lybrand, Lybrand & Schutza, P.C., Dallas, for appellants.
Gregory P. Standerfer, Tom C. Clark, Moseley & Standerfer, P.C., Dallas, for appellee.
Before THOMAS[1], C.J., and OVARD and MORRIS, JJ.

OPINION
MORRIS, Justice.
This is a summary judgment case. We decide whether the trial court correctly granted a take-nothing judgment in favor of National Mortgage Company against Cecil R. Williams and Sandra A. Williams on their wrongful foreclosure and other causes of action. Because National proved the elements of its affirmative defense of res judicata, we conclude it is entitled to judgment as a matter of law. We accordingly affirm the trial court's judgment.
Although this appeal involves a summary judgment signed in this case, the appeal necessarily involves a prior lawsuit. We discuss the prior lawsuit only for the purpose of resolving the issues in this case. We do not attempt to resolve issues in the prior lawsuit that possibly could have been raised if that case had been appealed.
The summary judgment evidence in this case established the following undisputed facts. In July 1984 the Williamses purchased a home in DeSoto, Texas. They signed a note payable to a bank for part of the purchase price and a deed of trust in favor of the same bank as security on the note. National later acquired the note and deed of trust from the bank through a series of transfers.
The Williamses made no payments on the note after March 1991. National sent several demand letters to the Williamses and eventually sought a nonjudicial foreclosure sale of the property. A foreclosure sale took place in January 1992. A substitute trustee deeded the property to National. The Williamses quickly filed a lawsuit for wrongful foreclosure and requested a temporary restraining order.[2] National on the other hand filed a forcible detainer complaint in a justice of the peace court seeking possession of the property. The justice of the peace court issued a writ of possession, and the Williamses' personal property was removed from the house.
National later discovered that Sandra Williams previously had sought bankruptcy protection. National feared that the January foreclosure may have violated the bankruptcy court's automatic stay prohibiting a creditor from attempting to collect from a debtor. In March 1992 National canceled its deed to the property from the January 1992 foreclosure sale and petitioned the bankruptcy court for relief from the automatic stay. A month later the bankruptcy court lifted the automatic stay. The lawsuit the Williamses initially filed after the January foreclosure remained pending.
*401 In May 1992 National filed a counterclaim to the Williamses' wrongful foreclosure action. National sought to recover a deficiency due and owing on the note as a result of the January foreclosure. But National had effectively rescinded that foreclosure; therefore, there was no deficiency resulting from the January foreclosure. Apparently realizing this, National filed in June 1992 a notice of nonsuit of its counterclaim against the Williamses.
At this point, the Williamses' first lawsuit took an unexpected turn. The trial court attributed National's notice of nonsuit to the Williamses. The trial court signed an order of nonsuit mistakenly dismissing all of the Williamses' claims against National rather than National's claim against the Williamses. Neither the Williamses nor National realized the mistake.
National sought a second nonjudicial foreclosure sale in July 1992, after which the substitute trustee again deeded the property to National. Within days National filed a second counterclaim in the first lawsuit. National sought to recover the deficiency determined after the second foreclosure. National then moved for summary judgment for the deficiency and on all of the Williamses' affirmative claims. The Williamses responded. The trial court signed an order in August 1993, granting summary judgment to National only for the deficiency claimed in the second counterclaim based on the second foreclosure. The trial court did not sign an order granting National's motion for summary judgment on the Williamses' affirmative claims. Eventually, the trial court recognized the confused state of the case and, at a docket call, informed the parties there was nothing left in the case to try.
In December 1993 the Williamses filed a new lawsuit against National. They asserted causes of action for wrongful foreclosure, trespass to try title, violations of the Deceptive Trade PracticesConsumer Protection Act,[3] and breach of implied duty of good faith and fair dealing. They also requested a determination of the fair market value of the property and sought a declaratory judgment. Except for the addition of the request to determine the fair market value of the property, their claims were essentially identical to those in the first lawsuit, but with additional information about the second foreclosure sale. National answered by asserting affirmative defenses and quickly moved for summary judgment on the Williamses' causes of action. National asserted that the doctrines of res judicata and collateral estoppel barred the Williamses' claims. The trial court granted National's motion for summary judgment in January 1994. This appeal ensued.
In their appeal, the Williamses argued that the trial court erred when it granted National's motion for summary judgment in January 1994. In response, National contended, as it did in its motion for summary judgment, that the litigation in the second lawsuit arose out of the same transaction at issue in the first lawsuit. National argued that the record from the first lawsuit conclusively showed the summary judgment in the first lawsuit disposed of all issues relating to the Williamses' claims. National asserted that, because the Williamses' claims against National were dismissed and the trial court signed summary judgment against them for the deficiency in the first lawsuit, res judicata barred the Williamses' second lawsuit. In raising the doctrine of res judicata, National specifically urged that the Williamses' causes of action in the second lawsuit were compulsory counterclaims in the first lawsuit. National also argued that collateral estoppel barred the Williamses' attempt to relitigate the issues resolved in the identical first lawsuit. To be entitled to summary judgment in the second lawsuit, National had to prove conclusively the elements of res judicata or collateral estoppel.
Res judicata and collateral estoppel are affirmative defenses. Tex.R.Civ.P. 94. When a defendant moves for summary judgment based on an affirmative defense, it must conclusively prove all essential elements of the affirmative defense as a matter of law. Swilley v. Hughes, 488 S.W.2d 64, 67 (Tex.1972). In reviewing the summary judgment proof, we indulge every reasonable inference in favor of the nonmovant. Nixon v. *402 Mr. Property Management Co., 690 S.W.2d 546, 548-49 (Tex.1985). We resolve any doubts in the nonmovant's favor. Id. at 549. Because the trial court's summary judgment order in this case does not state the specific grounds for summary judgment, we affirm the trial court's judgment if we conclude any ground advanced by the movant is meritorious. See Bellair, Inc. v. Aviall of Tex., Inc., 819 S.W.2d 895, 899 (Tex.App.Dallas 1991, writ denied).
We examine National's claim of res judicata first; then, if necessary, we examine its claim of collateral estoppel. See Jeanes v. Henderson, 688 S.W.2d 100, 103 (Tex.1985). The two doctrines are not the same and may not be interchanged. See Bonniwell v. Beech Aircraft Corp., 663 S.W.2d 816, 818 (Tex. 1984). We analyze the collateral estoppel defense only if we determine that National failed to show res judicata bars the Williamses' causes of action. Jeanes, 688 S.W.2d at 103.
The doctrine of res judicata addresses the conclusive effects of judgments. Puga v. Donna Fruit Co., 634 S.W.2d 677, 679 (Tex.1982). The general principle of res judicata is simple: a party may not dispute a right, question, or fact distinctly put in issue and directly determined by a court of competent jurisdiction in a prior suit as a ground of recovery or defense in a later suit between the same parties. Marange v. Marshall, 402 S.W.2d 236, 239-40 (Tex.Civ.App.Corpus Christi 1966, writ ref'd n.r.e.). In effect, res judicata applies in a later lawsuit between identical parties who appeared in a prior lawsuit if the cause of action asserted in the later lawsuit has been decided previously with finality by a court of competent jurisdiction. See Texas Water Rights Comm'n v. Crow Iron Works, 582 S.W.2d 768, 771-72 (Tex.1979). Res judicata also bars a party from litigating matters in a later lawsuit that it could have raised, but did not raise, in a previous lawsuit. Jeanes, 688 S.W.2d at 103; Abbott Labs. v. Gravis, 470 S.W.2d 639, 642 (Tex.1971); cf. Franklin v. Rainey, 556 S.W.2d 583, 585 (Tex.Civ.App.Dallas 1977, no writ) (res judicata will not apply where different facts or events exist which lead to a second lawsuit).
An essential, underlying element of res judicata that National must prove is the jurisdiction of the court rendering the judgment that is claimed to bar relitigation of the causes of action asserted here. See Employers' Indem. Corp. v. Woods, 230 S.W. 461, 465 (Tex.Civ.App.Beaumont 1921), aff'd, 243 S.W. 1085 (Tex.1922); cf. Great S. Life Ins. Co. v. Johnson, 13 S.W.2d 424, 428 (Tex. Civ.App.Amarillo 1928), rev'd on other grounds, 25 S.W.2d 1093 (Tex.Comm'n App. 1930, holding approved); Ruby v. Davis, 277 S.W. 430, 433 (Tex.Civ.App.Amarillo 1925, no writ). Without jurisdiction, the judgment rendered in the first lawsuit cannot bar relitigation of the Williamses' causes of action. Only if we conclude that the trial court had jurisdiction do we address whether the Williamses' claims are barred by the res judicata effect of the summary judgment rendered in the first lawsuit. Our initial analysis focuses, consequently, on whether National has shown that the trial court in the first lawsuit had jurisdiction to render judgment after the nonsuits in that case.
A party has an absolute right to take a nonsuit of a pending claim if it is taken in a timely manner. See Tex.R.Civ.P. 162. Moreover, the granting of a nonsuit is merely a ministerial act by the trial court. Greenberg v. Brookshire, 640 S.W.2d 870, 872 (Tex.1982) (orig. proceeding) (per curiam). A trial court may not deny a party's right to take a nonsuit. See BHP Petroleum Co. v. Millard, 800 S.W.2d 838, 840-41 (Tex.1990) (orig. proceeding). If an opposing party has no claim for affirmative relief pending, a trial court's jurisdiction over a cause ends when a notice of nonsuit is given for the only pending claim for affirmative relief. In effect, in such a situation, the filing of a nonsuit divests a trial court of its subject matter jurisdiction. See State ex rel. Dishman v. Gary, 163 Tex. 565, 569-70, 359 S.W.2d 456, 458-59 (1962) (orig. proceeding) (citing Ex parte Norton, 118 Tex. 581, 585, 17 S.W.2d 1041, 1043 (1929) (orig. proceeding)); see also Strawder v. Thomas, 846 S.W.2d 51, 59 (Tex. App.Corpus Christi 1992, no writ); Ex parte Helle, 477 S.W.2d 379, 384-85 (Tex.Civ. App.Corpus Christi 1972, orig. proceeding). *403 A party's right to take a nonsuit, however, does not affect an opposing party's pending claim for affirmative relief because the trial court retains jurisdiction over that claim if the trial court initially had subject matter jurisdiction. Although a nonsuit of a party's claims does not end an adverse party's right to be heard on its pending claims for affirmative relief, a claim for affirmative relief filed after a notice of nonsuit is usually ineffective to prevent termination of the lawsuit. See Progressive Ins. Cos. v. Hartman, 788 S.W.2d 424, 426 (Tex.App.Dallas 1990, orig. proceeding); Strawder, 846 S.W.2d at 59; see also Short v. Hepburn, 89 Tex. 622, 625, 35 S.W. 1056, 1057 (1896); PHB, Inc. v. Goldsmith, 534 S.W.2d 196, 198 (Tex.Civ. App.-Houston [14th Dist.]), writ ref'd n.r.e., 539 S.W.2d 60 (Tex.1976).
Here, the Williamses filed their first lawsuit against National early in 1992. National filed its first counterclaim for deficiency in the first lawsuit in May 1992. Less than a month later, National filed its notice of nonsuit with respect to its first counterclaim. This notice of nonsuit had the effect of immediately dismissing National's counterclaim. The trial court did not have to sign an order of nonsuit to effect the voluntary dismissal of National's counterclaim. Nonetheless, the trial court did sign an order of nonsuit. But the order mistakenly dismissed all of the Williamses' causes of action. At that point, no claim for affirmative relief remained pending in the first lawsuit. National's claim had been dismissed voluntarily; the Williamses' claims had been dismissed by the trial court's order. The trial court appeared to lose all subject matter jurisdiction. Our analysis, however, does not end here.
National later filed a second counterclaim for deficiency in July 1992. The second counterclaim was based on the second foreclosure, a claim not before presented to the trial court. The Williamses' various claims were no longer before the trial court because the trial court had dismissed them. Because of this, there was no lawsuit to which National's counterclaim could attach. National's second counterclaim, however, contained all the essentials of a separate claim. It could, therefore, be maintained as a separate suit. See Loomis Land & Cattle Co. v. Wood, 699 S.W.2d 594, 596 (Tex.App.-Texarkana 1985, writ ref'd n.r.e.). Though it was filed and docketed as part of a suit no longer in existence, this fact did not defeat its validity as an independent lawsuit. See Leach v. Brown, 156 Tex. 66, 69, 292 S.W.2d 329, 331 (1956). In effect, there were two distinct cases under the identical cause number, one no longer pending and the other newly filed. See Cockrell v. Central Sav. & Loan Ass'n, 788 S.W.2d 221, 224 (Tex.App. Dallas 1990, no writ). It is of no consequence that there was no citation or service of process for the second counterclaim because the Williamses voluntarily appeared by responding to National's motion for summary judgment. See Loomis, 699 S.W.2d at 596. Based on the summary judgment evidence presented here, the trial court in the first lawsuit had subject matter jurisdiction to consider National's second counterclaim as an independent suit and to render judgment for the deficiency.
Because the trial court had jurisdiction to render judgment on National's second counterclaim, we now consider whether that judgment precludes the Williamses' second lawsuit, the one on appeal here. We begin by noting that the described sequence of events required the Williamses to refile their various causes of action as compulsory counterclaims to National's newly filed deficiency action. See Tex.R.Civ.P. 97(g). National's deficiency cause of action and all of the Williamses' causes of action arose out of a single occurrence, the second foreclosure, and should have been litigated in the same lawsuit. See id. 97(a); Texas Water Rights Comm'n, 582 S.W.2d at 771-72. But the Williamses did not refile their claims after their initial claims were dismissed in the first lawsuit or after National filed its second counterclaim for deficiency.
A party's failure to assert compulsory counterclaims precludes that party from asserting them in later lawsuits. Gray v. Kirkland, 550 S.W.2d 410, 411 (Tex.Civ. App.-Corpus Christi 1977, writ ref'd n.r.e.); see also Barr v. Resolution Trust Corp., ex rel. Sunbelt Fed. Sav., 837 S.W.2d 627, 629-31 (Tex.1992). The test for determining *404 whether a claim is a compulsory counterclaim is the "logical relationship" test. Jack H. Brown & Co. v. Northwest Sign Co., 718 S.W.2d 397, 399-400 (Tex.App.Dallas 1986, writ ref'd n.r.e.); see also Tindle v. Jackson Nat'l Life Ins. Co., 837 S.W.2d 795, 798-800 (Tex.App.Dallas 1992, no writ). Application of this test requires that at least some of the facts surrounding the causes of action arise from the same transaction or occurrence. Jack H. Brown & Co., 718 S.W.2d at 400. When the same facts, whether controverted or uncontroverted, are significant and logically relevant to the various causes of action, the "logical relationship" test is satisfied. Id. When none of the facts are relevant to the various causes of action, there is no "logical relationship." Id.
The Williamses asserted causes of action in their second lawsuit for wrongful foreclosure, trespass to try title, violation of the Deceptive Trade PracticesConsumer Protection Act, and breach of implied duty of good faith and fair dealing. They also sought a determination of the fair market value for their property and a declaratory judgment. All of the causes of action arose out of National's foreclosure and efforts to collect the deficiency on the note the Williamses signed. In similar factual situations, Texas courts have concluded that such causes of action are compulsory counterclaims to a suit on a note or a foreclosure action. See, e.g., Jones v. First Nat'l Bank of Anson, 846 S.W.2d 107, 109 (Tex.App.Eastland 1992, no writ) (causes of action for breach of duty of good faith and fair dealing, breach of fiduciary duty, negligent misrepresentation, conversion, estoppel, and violations of the Deceptive Trade PracticesConsumer Protection Act are compulsory counterclaims to a suit on a note); Lamar Sav. Ass'n v. White, 731 S.W.2d 715, 717-18 (Tex.App.Houston [1st Dist.] 1987, orig. proceeding) (causes of action for breach of contract, breach of fiduciary duty, usury, duress, estoppel, and tortious interference are compulsory counterclaims to a lender's foreclosure action). We likewise conclude that all of the Williamses' causes of actions were logically related to National's second foreclosure and, therefore, were compulsory counterclaims to National's suit for deficiency.
Where a counterclaim is compulsory, a defendant is barred by the doctrine of res judicata from asserting the claim in a later lawsuit. See Lesbrookton, Inc. v. Jackson, 796 S.W.2d 276, 281 (Tex.App.Amarillo 1990, writ denied). When the summary judgment for National on the deficiency in the first lawsuit became final, the Williamses lost the right to file a later lawsuit on their claims that were compulsory counterclaims in the first lawsuit. See Western Inn Corp. v. Heyl, 452 S.W.2d 752, 761 (Tex.Civ.App. Fort Worth 1970, writ ref'd n.r.e.); see also Lamar Sav. Ass'n, 731 S.W.2d at 717-18. We conclude National proved it was entitled to summary judgment based on its res judicata affirmative defense. Because we conclude National showed res judicata bars the relitigation of the Williamses' causes of action, it is not necessary for us to address National's collateral estoppel defense. See Jeanes, 688 S.W.2d at 103.
We hold National proved the essential elements of its res judicata affirmative defense. Because National proved the essential elements of an affirmative defense, it established its entitlement to judgment as a matter of law. Gibbs v. General Motors Corp., 450 S.W.2d 827, 828 (Tex.1970). The trial court did not err in granting summary judgment. We overrule appellants' point of error.
We affirm the trial court's judgment.
NOTES
[1]  The Honorable Linda Thomas was on the original panel at the time this cause was submitted for decision. Justice Thomas was sworn in as Chief Justice on January 1, 1995.
[2]  The Williamses' petition sought damages for wrongful foreclosure and an injunction prohibiting National from evicting them. The petition was amended on June 2, 1992 to allege causes of action for wrongful foreclosure, deceptive trade practices, and breach of implied duty of good faith and fair dealing. The Williamses sought damages in excess of $31 million.
[3]  See TEX.BUS. & COM.CODE ANN. §§ 17.41-.61 (Vernon 1987 & Supp.1995).
