I'm still new at mssqlserver, i need to get the first order for the same customer made order in certain period based on the brand name and customer_id I used first_value() as sub query with top 1 however i got only the first value based on the brand regardless the customer id
what i got
| Data | Customer_ID | Brand | first_order |
|---|---|---|---|
| 2022-01-01 | 1649104 | PH | 2020-09-26 |
| 2022-01-01 | 1306498 | PH | 2020-09-26 |
| 2022-01-01 | 1290371 | PH | 2020-09-26 |
| 2022-01-01 | 456756 | TB | 2020-11-09 |
| 2022-01-01 | 1823713 | TB | 2020-11-09 |
| 2022-01-01 | 2178025 | BK | 2020-08-12 |
| 2022-01-01 | 216435 | BK | 2020-08-12 |
| 2022-01-01 | 19031 | BK | 2020-08-12 |
| 2022-01-01 | 438095 | BK | 2020-08-12 |
the code I used
SELECT Data , Brand , Customer_ID ,
(select top 1 first_value(H.Data) over (partition by Customer_ID , Brand order by H.Data ASC)
from ORDER H
where h.Customer_ID = Customer_ID and H.Brand = Brand
) as firts_order
from ORDER
LEFT OUTER JOIN ORDER_SOURCE SRC ON SRC.SRC_ID = ORDR_SOURCE
WHERE SRC_NAME IN ('SR 1' , 'SR 2') and DATE = '2022-01-01'