I have table Buyer
| BuyId | QuantityOrdered | dateordered |
| ------------ | ------------ | ------------ |
| 1 | 10 | 2021-11-04 |
| 1 | 20 | 2022-01-22 |
| 2 | 50 | 2022-02-20 |
| 2 | 60 | 2022-05-02 |
| 3 | 10 | 2022-05-02 |
| 4 | 10 | 2022-05-02 |
I need to select all BuyId's who consecutively had higher QuantityOrder in each new record
- buyid=1 had first order with quantity=10, second order with quantity=20
- buyid=2 had first order with quantity=50, second order with quantity=60
So BuyId=1 and 2 would enter the results, while 3 and 4 would be filtered out because they had only one order or they did not have orders with consecutively higher quantities ordered
I tried with this, and I'm aware that this query gives me only buyid's who have more than one order, but I am missing the rule where I have to filter results out by quantity increased with each new order
select buyid, count(*) as ordered
from buyer
group by buyid
having count(*) >1
How would I write out that rule in a query, to select only BuyId's who had multiple orders, and in each new order they ordered higher quantities than in previous orders?
2条答案
按热度按时间zfciruhq1#
Taking advantage of the fact that count() doesn't takes into account NULL values:
carvr3hs2#
With this query, it will check if all purchases as bigger in quantity as the last and will not show buyers id. that have only one purchase or sold equal or less than the last time4
fiddle