Here you go
10.17 Winning And Losing Pitcher
(a) The official scorer shall credit as the winning pitcher that pitcher whose team assumes a lead while such pitcher is in the game, or during the inning on offense in which such pitcher is removed from the game, and does not relinquish such lead, unless
(1) such pitcher is a starting pitcher and Rule 10.17(b) applies; or
(2) Rule 10.17(c) applies.
Rule 10.17(a) Comment: Whenever the score is tied, the game becomes a new contest insofar as the winning pitcher is concerned. Once the opposing team assumes the lead, all pitchers who have pitched up to that point and have been replaced are excluded from being credited with the victory. If the pitcher against whose pitching the opposing team gained the lead continues to pitch until his team regains the lead, which it holds to the finish of the game, that pitcher shall be the winning pitcher.
(b) If the pitcher whose team assumes a lead while such pitcher is in the game, or during the inning on offense in which such pitcher is removed from the game, and does not relinquish such lead, is a starting pitcher who has not completed
(1) five innings of a game that lasts six or more innings on defense, or
(2) four innings of a game that lasts five innings on defense, then the official scorer shall credit as the winning pitcher the relief pitcher, if there is only one relief pitcher, or the relief pitcher who, in the official scorer’s judgment was the most effective, if there is more than one relief pitcher.
Rule 10.17(b) Comment: It is the intent of Rule 10.17(b) that a relief pitcher pitch at least one complete inning or pitch when a crucial out is made, within the context of the game (including the score), in order to be credited as the winning pitcher. If the first relief pitcher pitches effectively, the official scorer should not presumptively credit that pitcher with the win, because the rule requires that the win be credited to the pitcher who was the most effective, and a subsequent relief pitcher may have been most effective. The official scorer, in determining which relief pitcher was the most effective, should consider the number of runs, earned runs and base runners given up by each relief pitcher and the context of the game at the time of each relief pitcher’s appearance. If two or more relief pitchers were similarly effective, the official scorer should give the presumption to the earlier pitcher as the winning pitcher.
(c) The official scorer shall not credit as the winning pitcher a relief pitcher who is ineffective in a brief appearance, when at least one succeeding relief pitcher pitches effectively in helping his team maintain its lead. In such a case, the official scorer shall credit as the winning pitcher the succeeding relief pitcher who was most effective, in the judgment of the official scorer.
Rule 10.17(c) Comment: The official scorer generally should, but is not required to, consider the appearance of a relief pitcher to be ineffective and brief if such relief pitcher pitches less than one inning and allows two or more earned runs to score (even if such runs are charged to a previous pitcher). Rule 10.17(b) Comment provides guidance on choosing the winning pitcher from among several succeeding relief pitchers.
(d) A losing pitcher is a pitcher who is responsible for the run that gives the winning team a lead that the winning team does not relinquish.
Rule 10.17(d) Comment: Whenever the score is tied, the game becomes a new contest insofar as the losing pitcher is concerned.
(e) A league may designate a non-championship game (for example, the Major League All-Star Game) for which Rules 10.17(a)(1) and 10.17(b) do not apply. In such games, the official scorer shall credit as the winning pitcher that pitcher whose team assumes a lead while such pitcher is in the game, or during the inning on offense in which such pitcher is removed from the game, and does not relinquish such lead, unless such pitcher is knocked out after the winning team has attained a commanding lead and the official scorer concludes that a subsequent pitcher is entitled to credit as the winning pitcher.
And here is ER and Runs Allowed:
Earned Runs And Runs Allowed
An earned run is a run for which a pitcher is held accountable. In determining earned runs, the official scorer shall reconstruct the inning without the errors (which exclude catcher's interference) and passed balls, giving the benefit of the doubt always to the pitcher in determining which bases would have been reached by runners had there been errorless play. For the purpose of determining earned runs, an intentional base on balls, regardless of the circumstances, shall be construed in exactly the same manner as any other base on balls.
(a) The official scorer shall charge an earned run against a pitcher every time a runner reaches home base by the aid of safe hits, sacrifice bunts, a sacrifice fly, stolen bases, putouts, fielder's choices, bases on balls, hit batters, balks or wild pitches (including a wild pitch on third strike that permits a batter to reach first base) before fielding chances have been offered to put out the offensive team. For the purpose of this rule, a defensive interference penalty shall be construed as a fielding chance. A wild pitch is solely the pitcher's fault and shall contribute to an earned run just as a base on balls or a balk.
Rule 10.16(a) Comment: The following are examples of earned runs charged to a pitcher:
(1) Peter pitches and retires Abel and Baker, the first two batters of an inning. Charlie reaches first base on an error charged to a fielder. Daniel hits a home run. Edward hits a home run. Peter retires Frank to end the inning. Three runs have scored, but no earned runs are charged to Peter, because Charlie should have been the third out of the inning, as reconstructed without the error.
(2) Peter pitches and retires Abel. Baker hits a triple. While pitching to Charlie, Peter throws a wild pitch, allowing Baker to score. Peter retires Daniel and Edward. One run has scored, charged as an earned run to Peter, because the wild pitch contributes to an earned run.
In an inning in which a batter-runner reaches first base on a catcher’s interference, such batter-runner shall not count as an earned run should he subsequently score. The official scorer shall not assume, however, that such batter would have made an out absent the catcher’s interference (unlike, for example, situations in which a batter-runner reaches first base safely because of a fielder’s misplay of a ball for an error). Because such batter never had a chance to complete his time at bat, it is unknown how such batter would have fared absent the catcher’s interference. Compare the following examples:
(3) With two out, Abel reaches first on an error by the shortstop in misplaying a ground ball. Baker hits a home run. Charlie strikes out. Two runs have scored, but none is earned, because Abel’s at-bat should have been the third out of the inning, as reconstructed without the error.
(4) With two out, Abel reaches first on a catcher’s interference. Baker hits a home run. Charlie strikes out. Two runs have scored, but one (Baker’s) is earned, because the official scorer cannot assume that Abel would have made an out to end the inning, absent the catcher’s interference.
(b) No run shall be earned when scored by a runner who reaches first base
(1) on a hit or otherwise after his time at bat is prolonged by a muffed foul fly;
(2) because of interference or obstruction; or
(3) because of any fielding error.
(c) No run shall be earned when scored by a runner whose presence on the bases is prolonged by an error, if such runner would have been put out by errorless play.
(d) No run shall be earned when the scoring runner's advance has been aided by an error, a passed ball or defensive interference or obstruction, if in the official scorer’s judgment the run would not have scored without the aid of such misplay.
(e) An error by a pitcher is treated exactly the same as an error by any other fielder in computing earned runs.
(f) Whenever a fielding error occurs, the pitcher shall be given the benefit of the doubt in determining to which bases any runners would have advanced had the fielding of the defensive team been errorless.
(g) When pitchers are changed during an inning, the official scorer shall not charge the relief pitcher with any run (earned or unearned) scored by a runner who was on base at the time such relief pitcher entered the game, nor for runs scored by any runner who reaches base on a fielder's choice that puts out a runner left on base by any preceding pitcher.
Rule 10.16(g) Comment: It is the intent of Rule 10.16(g) to charge each pitcher with the number of runners he put on base, rather than with the individual runners. When a pitcher puts runners on base and is relieved, such pitcher shall be charged with all runs subsequently scored up to and including the number of runners such pitcher left on base when such pitcher left the game, unless such runners are put out without action by the batter (i.e., caught stealing, picked off base or called out for interference when a batter-runner does not reach first base on the play). For example:
(1) Peter is pitching. Abel reaches first base on a base on balls. Roger relieves Peter. Baker grounds out, advancing Abel to second base. Charlie flies out. Daniel singles, scoring Abel. Abel’s run is charged to Peter
(2) Peter is pitching. Abel reaches first base on a base on balls. Roger relieves Peter. Baker forces Abel at second bases. Charlie grounds out, advancing Baker to second base. Daniel singles, scoring Baker. Baker’s run is charged to Peter.
(3) Peter is pitching. Abel reaches first base on a base on balls. Roger relieves Peter. Baker singles, advancing Abel to third base. Charlie grounds to short, with Abel out at home plate and Baker advancing to second base. Daniel flies out. Edward singles, scoring Baker. Baker’s run is charged to Peter.
(4) Peter is pitching. Abel reaches first base on a base on balls. Roger relieves Peter. Baker reaches on a base on balls. Charlie flies out. Abel is picked off second base. Daniel doubles, scoring Baker from first base. Baker’s run is charged to Roger.
(5) Peter is pitching. Abel reaches first base on a base on balls. Roger relieves Peter. Baker reaches first base on a base on balls. Sierra relieves Roger. Charlie forces Abel at third base. Daniel forces Baker at third base. Edward hits a home run, scoring three runs. The official scorer shall charge one run to Peter, one run to Roger and one run to Sierra.
(6) Peter is pitching. Abel reaches first base on a base on balls. Roger relieves Peter. Baker reaches first base on a base on balls. Charlie singles, filling the bases. Daniel forces Abel at home plate. Edward singles, scoring Baker and Charlie. The official scorer shall charge one run to Peter and one run to Roger.
(7) Peter is pitching. Abel reaches first base on a base on balls. Roger relieves Peter. Baker singles, but Abel is out trying to reach third base and Baker advances to second base on the throw. Charlie singles, scoring Baker. Baker’s run is charged to Roger.
(h) A relief pitcher shall not be held accountable when the first batter to whom he pitches reaches first base on four called balls if such batter has a decided advantage in the ball and strike count when pitchers are changed.
(1) If, when pitchers are changed, the count is
2 balls, no strike,
2 balls, 1 strike,
3 balls, no strike,
3 balls, 1 strike,
3 balls, 2 strikes,
and the batter gets a base on balls, the official scorer shall charge that batter and the base on balls to the preceding pitcher, not to the relief pitcher.
(2) Any other action by such batter, such as reaching base on a hit, an error, a fielder's choice, a force-out, or being touched by a pitched ball, shall cause such a batter to be charged to the relief pitcher.
Rule 10.16(h) Comment: The provisions of Rule 10.16(h)(2) shall not be construed as affecting or conflicting with the provisions of Rule 10.16(g).
(3) If, when pitchers are changed, the count is
2 balls, 2 strikes,
1 ball, 2 strikes,
1 ball, 1 strike,
1 ball, no strike,
no ball, 2 strikes,
no ball, 1 strike,
the official scorer shall charge that batter and the actions of that batter to the relief pitcher.
(i) When pitchers are changed during an inning, the relief pitcher shall not have the benefit of previous chances for outs not accepted in determining earned runs.
Rule 10.16(i) Comment: It is the intent of Rule 10.16(i) to charge a relief pitcher with earned runs for which such relief pitcher is solely responsible. In some instances, runs charged as earned against the relief pitcher can be charged as unearned against the team. For example:
(1) With two out and Peter pitching, Abel reaches first base on a base on balls. Baker reaches first base on an error. Roger relieves Peter. Charlie hits a home run, scoring three runs. The official scorer shall charge two unearned runs to Peter, one earned run to Roger and three unearned runs to the team (because the inning should have ended with the third out when Baker batted and an error was committed).
(2) With two out, and Peter pitching, Abel and Baker each reach first base on a base on balls. Roger relieves Peter. Charlie reaches first base on an error. Daniel hits a home run, scoring four runs. The official scorer shall charge two unearned runs to Peter and two unearned runs to Roger (because the inning should have ended with the third out when Charlie batted and an error was committed).
(3) With none out and Peter pitching, Abel reaches first base on a base on balls. Baker reaches first base on an error. Roger relieves Peter. Charlie hits a home run, scoring three runs. Daniel and Edward strike out. Frank reaches first base on an error. George hits a home run, scoring two runs. The official scorer shall charge two runs, one of them earned, to Peter, three runs, one of them earned, to Roger and five runs, two of them earned, to the team (because only Abel and Charlie would have scored in an inning reconstructed without the errors).