简体   繁体   中英

SQL LAST plus WHERE not giving needed result

I would like to make SQL query but it is not working. Here is how my table looks like

Client|Product|Amount|
  A   | P1    |  5   | 
  A   | P1    |  7   | 
  A   | P1    |  3   | 
  A   | P2    |  8   | 
  A   | P2    |  0   | 
  A   | P2    |  5   | 
  A   | P3    |  9   | 
  A   | P3    |  0   | 

I would like to set a query that would provide me a list last entries for each Client and each Product that is greater than 0. The output should look like:

Client|Product|Amount|
  A   | P1    |  3   |  
  A   | P2    |  5   | 

I tried using this code

SELECT Client,Product, LAST(Amount) 
FROM Table 
WHERE Amount>0 
GROUP BY Client,Product

But I get this output:

Client|Product|Amount|
  A   | P1    |  3   |  
  A   | P2    |  5   | 
  A   | P3    |  9   | 

Any ideas how to solve this?

Use a having clause:

SELECT Client, Product, LAST(Amount)
FROM Table
GROUP BY Client, Product
HAVING LAST(Amount) > 0;
SELECT Client,Product, LAST(Amount) 
FROM Table
WHERE product IN (SELECT product
                FROM table)   and LAST>0
GROUP BY Client, Product

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM