PostgreSQL查询帮助:如何检查多个列是否同时在增加/减少值

问题描述 投票:0回答:1

我有一个类似于以下的结果表:

我如何查询随着时间变化,最新价格和情感变化都在增加的股票行情。例如,您可以看到AMSC在latest_price和sentiment_change中都增加了。我正在使用postgresql,谢谢您的帮助

"created_at"    "ticker"    "title" "latest_price"  "sentiment_change"
"2020-05-24 06:41:40.948862"    "AMSC"  "American Superconductor Corporation"   "6.66"  "5.56"
"2020-05-24 06:42:02.987018"    "AMSC"  "American Superconductor Corporation"   "7.66"  "6.56"
"2020-05-24 07:59:08.014871"    "AMSC"  "American Superconductor Corporation"   "8.66"  "7.27"
"2020-05-24 08:00:23.406509"    "AMSC"  "American Superconductor Corporation"   "9.66"  "8.27"
"2020-05-24 08:04:02.881144"    "AMSC"  "American Superconductor Corporation"   "10.66" "9.27"
"2020-05-24 06:41:51.797114"    "AUPH"  "Aurinia Pharmaceuticals Inc."  "16.24" "-0.37"
"2020-05-24 06:41:52.768141"    "CGC"   "Canopy Growth Corporation "    "19.42" "-0.89"
"2020-05-24 06:42:06.947722"    "COF"   "Capital One Financial Corp."   "60.21" "9.19"
"2020-05-24 07:58:49.696729"    "CNC"   "Centene Corp." "64.84" "0.0"
"2020-05-24 08:00:05.909404"    "CNC"   "Centene Corp." "64.84" "0.0"
python postgresql
1个回答
0
投票

我在这里为此创建了一个数据库提琴:https://www.db-fiddle.com/f/37qeZWoFpVLkJLqN16TirX/0

如果您的PostgreSQL版本支持LAG,则可以使用下面的查询开始。我已经完成了增加的部分,您可以完成减少的部分。

然后,您可以计算latest_pricesentiment_change相对于您拥有的观察次数的增加/减少的次数,如果超过某个阈值,则确定您的行动方针。

SELECT
    created_at, 
    ticker,
    latest_price, 
    sentiment_change, 
    LAG(latest_price,1) OVER (
        PARTITION BY ticker 
        ORDER BY created_at) AS previous_price,
    CASE WHEN latest_price >= LAG(latest_price, 1) OVER (
        PARTITION BY ticker 
        ORDER BY created_at)
         THEN 1 ELSE 0 END AS increased_price_flag,
    LAG(sentiment_change,1) OVER (
        PARTITION BY ticker 
        ORDER BY created_at) AS previous_sentiment_change,
    CASE WHEN sentiment_change >= LAG(sentiment_change, 1) OVER (
        PARTITION BY ticker 
        ORDER BY created_at)
         THEN 1 ELSE 0 END AS increased_sentiment_change_flag
FROM results
ORDER BY ticker, created_at;

结果:

| created_at               | ticker | latest_price | sentiment_change | previous_price | increased_price_flag | previous_sentiment_change | increased_sentiment_change_flag |
| ------------------------ | ------ | ------------ | ---------------- | -------------- | -------------------- | ------------------------- | ------------------------------- |
| 2020-05-24T06:41:40.948Z | AMSC   | 6.66         | 5.56             |                | 0                    |                           | 0                               |
| 2020-05-24T06:42:02.987Z | AMSC   | 7.66         | 6.56             | 6.66           | 1                    | 5.56                      | 1                               |
| 2020-05-24T07:59:08.014Z | AMSC   | 8.66         | 7.27             | 7.66           | 1                    | 6.56                      | 1                               |
| 2020-05-24T08:00:23.406Z | AMSC   | 9.66         | 8.27             | 8.66           | 1                    | 7.27                      | 1                               |
| 2020-05-24T08:04:02.881Z | AMSC   | 10.66        | 9.27             | 9.66           | 1                    | 8.27                      | 1                               |
| 2020-05-24T06:41:51.797Z | AUPH   | 16.24        | -0.37            |                | 0                    |                           | 0                               |
| 2020-05-24T06:41:52.768Z | CGC    | 19.42        | -0.89            |                | 0                    |                           | 0                               |
| 2020-05-24T07:58:49.696Z | CNC    | 64.84        | 0.00             |                | 0                    |                           | 0                               |
| 2020-05-24T08:00:05.909Z | CNC    | 64.84        | 0.00             | 64.84          | 1                    | 0.00                      | 1                               |
| 2020-05-24T06:42:06.947Z | COF    | 60.21        | 9.19             |                | 0                    |                           | 0                               |
© www.soinside.com 2019 - 2024. All rights reserved.