SQL 如何根据月至今表计算单日 Gain/Loss
SQL How to Calculate single day Gain/Loss from month-to-date tables
我有一个 table,它有每月迄今为止的总收益损失,其中包含各种投资组合、证券标识符和业务部门。每个月 Period Start Date 都会刷新到新月份的开始,但 table 通常一次包含整个季度的数据。我如何编写一个查询来计算按投资组合、代码和业务单位分组的所有“结束日期”的单日收益损失?
Portfolio Fund Legal Entity SECURITY IDENTIFIER BUSINESS UNIT GAIN/LOSS Start Date End Date
Portfolio1 LegalEntity1 Ticker1 PUBLIC (4,760,588.0) 1-Feb 27-Feb
Portfolio1 LegalEntity1 Ticker1 PUBLIC 9,123,144.0 1-Feb 28-Feb
Portfolio1 LegalEntity1 Ticker1 PUBLIC 7,470,013.0 1-Mar 1-Mar
Portfolio1 LegalEntity1 Ticker1 PUBLIC 24,466,723.0 1-Mar 2-Mar
Portfolio1 LegalEntity1 Ticker1 PUBLIC 4,272,096.0 1-Mar 3-Mar
Portfolio1 LegalEntity1 Ticker1 PUBLIC 14,043,454.0 1-Mar 4-Mar
Portfolio1 LegalEntity1 Ticker1 PUBLIC 17,631,760.0 1-Mar 5-Mar
如果您的 dbms 支持,您可以使用延迟:
SELECT Portfolio, Fund_Legal_Entity, SECURITY_IDENTIFIER,
BUSINESS_UNIT, GAIN_LOSS, Start_Date, End_Date,
(gain_loss-lag(GAIN_LOSS) OVER (PARTITION BY Portfolio,
Fund_Legal_Entity, SECURITY_IDENTIFIER, BUSINESS_UNIT
ORDER BY end_date)) Daily_gain_loss
FROM tablename
我有一个 table,它有每月迄今为止的总收益损失,其中包含各种投资组合、证券标识符和业务部门。每个月 Period Start Date 都会刷新到新月份的开始,但 table 通常一次包含整个季度的数据。我如何编写一个查询来计算按投资组合、代码和业务单位分组的所有“结束日期”的单日收益损失?
Portfolio Fund Legal Entity SECURITY IDENTIFIER BUSINESS UNIT GAIN/LOSS Start Date End Date
Portfolio1 LegalEntity1 Ticker1 PUBLIC (4,760,588.0) 1-Feb 27-Feb
Portfolio1 LegalEntity1 Ticker1 PUBLIC 9,123,144.0 1-Feb 28-Feb
Portfolio1 LegalEntity1 Ticker1 PUBLIC 7,470,013.0 1-Mar 1-Mar
Portfolio1 LegalEntity1 Ticker1 PUBLIC 24,466,723.0 1-Mar 2-Mar
Portfolio1 LegalEntity1 Ticker1 PUBLIC 4,272,096.0 1-Mar 3-Mar
Portfolio1 LegalEntity1 Ticker1 PUBLIC 14,043,454.0 1-Mar 4-Mar
Portfolio1 LegalEntity1 Ticker1 PUBLIC 17,631,760.0 1-Mar 5-Mar
如果您的 dbms 支持,您可以使用延迟:
SELECT Portfolio, Fund_Legal_Entity, SECURITY_IDENTIFIER,
BUSINESS_UNIT, GAIN_LOSS, Start_Date, End_Date,
(gain_loss-lag(GAIN_LOSS) OVER (PARTITION BY Portfolio,
Fund_Legal_Entity, SECURITY_IDENTIFIER, BUSINESS_UNIT
ORDER BY end_date)) Daily_gain_loss
FROM tablename