Rama Dhani
Rama Dhani

Reputation: 43

Query SQL group by per next row if have same value?

How to do group by if the value of the next column is the same, later I will calculate how long the purchase will take, later added per group

enter image description here

Upvotes: 0

Views: 612

Answers (2)

charlesdk
charlesdk

Reputation: 176

User COUNT with GROUP BY to achieve it.

SELECT BUAH, COUNT(*) AS Total FROM your_table GROUP BY `WAKTU BELI`

Upvotes: -2

John Cappelletti
John Cappelletti

Reputation: 82020

This is a classic Gaps-and-Islands Problem.

In the future, please provide sample data as TEXT not as an image.

Example

with cte as (
    Select *
          ,Grp = row_number() over (order by ID) 
                -row_number() over (partition by BUAH order by ID)
    From  YourTable
)
Select BUAH
      ,Cnt = sum(1)
 From cte
 Group By BUAH,Grp
 Order By min(ID)   --<< Optional

Results

BUAH    Cnt
APEL    2
TOMAT   2
APEL    3
...

Upvotes: 6

Related Questions