Customers often moved their branches and changed their products for sale.
From the historical records of table, CustomerLocation
:
ID | EffectiveDate | CustomerCode | CustomersBranchCode | Location | Product |
---|---|---|---|---|---|
1 | 01/01/22 | A | 1 | Auckland | Carrot |
2 | 02/01/22 | A | 1 | Christchurch | Kale |
3 | 03/01/22 | A | 1 | Dunedin | Carrot |
4 | 04/01/22 | A | 2 | Queenstown | Kale |
5 | 05/01/22 | A | 2 | Wellington | |
6 | 06/01/22 | B | 1 | Dunedin | Kale |
7 | 07/01/22 | B | 1 | Christchurch | |
8 | 08/01/22 | B | 2 | Dunedin | Dunedin |
9 | 09/01/22 | C | 1 | Queenstown | Kale |
10 | 10/01/22 | C | 1 | Auckland |
How can I get result of latest updates like this ?
CustomerCode | CustomersBranchCode | Location | Product |
---|---|---|---|
A | 1 | Dunedin | Carrot |
A | 2 | Wellington | Kale |
B | 1 | Christchurch | Kale |
B | 2 | Dunedin | Carrot |
C | 1 | Auckland | Kale |
I could do only 1 step as follows and it was not correct.
SELECT DISTINCT "CL"."CustomersBranchCode",
FIRST_VALUE ( "CL"."Location" )
OVER ( PARTITION BY "CL"."CustomersBranchCode"
ORDER BY CASE WHEN "CL"."Location" IS NOT NULL
THEN "EffectiveDate" END DESC ) "CL01",
FIRST_VALUE ( "CL"."Product" )
OVER ( PARTITION BY "CL"."CustomersBranchCode"
ORDER BY CASE WHEN "CL"."Product" IS NOT NULL
THEN "EffectiveDate" END DESC ) "CL02"
FROM "CustomerLocation" "CL"
Can you please help edit my SQL statement with great thanks ?
0040FirstValueOf2Columns.odb (3.4 KB)
LibreOffice:
Version: 7.3.7.2 / LibreOffice Community
Build ID: 30(Build:2)
CPU threads: 4; OS: Linux 5.15; UI render: default; VCL: gtk3
Locale: en-US (en_US.UTF-8); UI: en-US
Ubuntu package version: 1:7.3.7-0ubuntu0.22.04.1
Calc: threaded
Base: Firebird
OS: Ubuntu 22.04LTS