title: Return sum of column where max from other column
body: <p>I have this query:</p>
  <pre class="lang-sql prettyprint-override"><code>select
      Costumer as Customer,
      siteId,
      pos,
      week,
      sum(Inventory) as totalInventory,
      sum(Amount) as totalAmount
  from Uploads
  WHERE Year = 2022
  group by Costumer, week, siteId, pos;
  </code></pre>
  <p>and returns something like this:</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>Customer</th>
  <th>siteId</th>
  <th>pos</th>
  <th>week</th>
  <th>totalInventory</th>
  <th>totalAmount</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>customer A</td>
  <td>id00A1</td>
  <td>posA1</td>
  <td>1</td>
  <td>67</td>
  <td>50</td>
  </tr>
  <tr>
  <td>customer A</td>
  <td>id00A2</td>
  <td>posA2</td>
  <td>1</td>
  <td>75</td>
  <td>45</td>
  </tr>
  <tr>
  <td>customer A</td>
  <td>id00A3</td>
  <td>posA3</td>
  <td>1</td>
  <td>98</td>
  <td>88</td>
  </tr>
  <tr>
  <td>customer B</td>
  <td>id00B1</td>
  <td>posB1</td>
  <td>1</td>
  <td>67</td>
  <td>50</td>
  </tr>
  <tr>
  <td>customer C</td>
  <td>id00C1</td>
  <td>posC1</td>
  <td>1</td>
  <td>67</td>
  <td>50</td>
  </tr>
  <tr>
  <td>customer C</td>
  <td>id00C2</td>
  <td>posC2</td>
  <td>1</td>
  <td>75</td>
  <td>45</td>
  </tr>
  <tr>
  <td>customer B</td>
  <td>id00B1</td>
  <td>posB1</td>
  <td>2</td>
  <td>67</td>
  <td>50</td>
  </tr>
  <tr>
  <td>customer C</td>
  <td>id00C1</td>
  <td>posC1</td>
  <td>2</td>
  <td>67</td>
  <td>50</td>
  </tr>
  <tr>
  <td>customer C</td>
  <td>id00C2</td>
  <td>posC2</td>
  <td>2</td>
  <td>75</td>
  <td>45</td>
  </tr>
  <tr>
  <td>customer B</td>
  <td>id00B1</td>
  <td>posB1</td>
  <td>3</td>
  <td>67</td>
  <td>50</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>But I want to know the data of the last week depending on the client, not every week. Something like this:</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>Customer</th>
  <th>siteId</th>
  <th>pos</th>
  <th>week</th>
  <th>totalInventory</th>
  <th>totalAmount</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>customer A</td>
  <td>id00A1</td>
  <td>posA1</td>
  <td>1</td>
  <td>67</td>
  <td>50</td>
  </tr>
  <tr>
  <td>customer A</td>
  <td>id00A2</td>
  <td>posA2</td>
  <td>1</td>
  <td>75</td>
  <td>45</td>
  </tr>
  <tr>
  <td>customer A</td>
  <td>id00A3</td>
  <td>posA3</td>
  <td>1</td>
  <td>98</td>
  <td>88</td>
  </tr>
  <tr>
  <td>customer C</td>
  <td>id00C1</td>
  <td>posC1</td>
  <td>2</td>
  <td>67</td>
  <td>50</td>
  </tr>
  <tr>
  <td>customer C</td>
  <td>id00C2</td>
  <td>posC2</td>
  <td>2</td>
  <td>75</td>
  <td>45</td>
  </tr>
  <tr>
  <td>customer B</td>
  <td>id00B1</td>
  <td>posB1</td>
  <td>3</td>
  <td>67</td>
  <td>50</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>What is missing from my query?</p>
  <p>I tried the following queries:</p>
  <pre class="lang-sql prettyprint-override"><code>select
      Costumer as Customer,
      siteId,
      pos,
      max(week),
      sum(Inventory) as totalInventory,
      sum(Amount) as totalAmount
  from Uploads
  WHERE Year = 2022
  group by Costumer, week, siteId, pos;
  </code></pre>
  <pre class="lang-sql prettyprint-override"><code>select
      Costumer as Customer,
      siteId,
      pos,
      max(week),
      sum(Inventory) as totalInventory,
      sum(Amount) as totalAmount
  from Uploads
  WHERE Year = 2022
  group by Costumer, week, siteId, pos
  having max(week);
  </code></pre>
  <p>But still having the same results.</p>
  <p>create table is:</p>
  <pre><code>CREATE TABLE Uploads ( 
     id int, 
     Year int, 
     Week int, 
     Division varchar(255), 
     ProductGroupID varchar(255), 
     Country varchar(255), 
     Costumer varchar(255), 
     POS varchar(255), 
     SiteID varchar(255), 
     Brand varchar(255), 
     Product varchar(255), 
     Inventory int NOT NULL, 
     SoldUnits int NOT NULL, 
     Amount float NOT NULL )
  </code></pre>