【问题标题】:PostgreSQL query help: How to check if multiple columns are increasing/decreasing in value at the same timePostgreSQL 查询帮助:如何检查多个列的值是否同时增加/减少
【发布时间】:2020-05-24 07:22:54
【问题描述】:

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

如何查询随着时间的推移在 latest_price 和sentiment_change 中都在增加的代码。例如,您可以看到 AMSC 在 latest_price 和 mood_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 sql postgresql window-functions


    【解决方案1】:

    我在这里为此创建了一个 db-fiddle: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                               |
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多