【发布时间】:2022-12-27 00:41:43
【问题描述】:
So I have three columns: Time_id, accounts and code. See attached picture for a sn-p of the data.
I want to find all account that have changed the code from 7100 to 7000 for that account, with ordered by the most recent ones.
- Time_id: is the date generated once every month for each account with updated fields. In the format of yyyymmdd
- account: is an unique account id for this customer
- code: is a four letter digit describing
I have tried a LAG of 'code' over a partition by time_id. However this returned a LAG code of from a different account. Not sure how to transform the query to only return changed code from LAG based on the same accounts. This was the query I tried:
SELECT time_id, account, code
,LAG(code, 1) OVER (partition by time_id order by time_id) LAG_1
FROM my_table
group by time_id, account, code
I was hoping to get all the accounts and the rows where code went from 7100 to 7000 and when it happened. For instance I want to get account 12500 and 15500 returned from the table below with the row when it changed from 7100:
| time_id | account | code |
|---|---|---|
| 20220510 | 12500 | 7100 |
| 20221101 | 12500 | 7000 |
| 20221120 | 12500 | 7000 |
| 20221201 | 17500 | 7100 |
| 20221202 | 12500 | 7100 |
| 20221203 | 15500 | 7100 |
| 20221204 | 15500 | 7000 |
| 20221205 | 15500 | 7000 |
I appreciate any new suggestions. Or improvements to my own query
【问题讨论】:
-
Please provide proper sample data and the desired results and the table schema.
-
We don't know for sure what it changed FROM here but consider
WHERE code = 7000- for example your last three rows in the last block looks like it changed from 7000 to 7000 SO do you want the first row that is 7000 or the last one since it did not appear to change.. -
@stu just curious: how much more sample data do I need to provide? I added desired result in the text based on sample data in the table. How is table schema necessary to get help on this exact table? I want to be more clear on my questions in the future. If you feel this was insufficient for people to help me please point out exactly what and why I should make certain changes to it. Thank you!
标签: sql sql-server lag