有条件地在MySQL查询中添加Where子句

时间:2017-03-15 14:22:11

标签: mysql where-clause

目的

要获得ratings.idratings.ticker,其中prices.price_date > ratings.date_issued在日期范围内增加5%:
在哪里prices.price_date < new_rating.date_issued
如果(为该股票代理/经纪人组合发布了另一个评级),则| id | ticker | broker | issue_date | close_price | |------|--------|--------|------------|-------------| | 22 | ABF | HSBC | 2017-01-23 | 2523 | | 775 | ABF | HSBC | 2017-02-15 | 2527 | | 1111 | ABF | HSBC | 2017-02-28 | 2619 | 否则不设置小于日期的where子句

当前设置

表:评分

| id    | ticker | price_date | price   |
|-------|--------|------------|---------|
| 42    | ABF    | 2017-01-24 | 2533.52 |
| 80    | ABF    | 2017-01-25 | 2531.00 |
| 145   | ABF    | 2017-01-26 | 2500.00 |
| 263   | ABF    | 2017-01-27 | 2420.00 |
| 416   | ABF    | 2017-01-28 | 2440.00 |
| 585   | ABF    | 2017-01-29 | 2440.00 |
| 754   | ABF    | 2017-01-30 | 2440.00 |
| 923   | ABF    | 2017-01-31 | 2449.00 |
| 1112  | ABF    | 2017-02-01 | 2440.00 |
| 1315  | ABF    | 2017-02-02 | 2405.00 |
| 1535  | ABF    | 2017-02-03 | 2388.00 |
| 1778  | ABF    | 2017-02-04 | 2427.00 |
| 2044  | ABF    | 2017-02-05 | 2427.00 |
| 2309  | ABF    | 2017-02-06 | 2427.00 |
| 2575  | ABF    | 2017-02-07 | 2412.00 |
| 2859  | ABF    | 2017-02-08 | 2451.00 |
| 3159  | ABF    | 2017-02-09 | 2503.00 |
| 3475  | ABF    | 2017-02-10 | 2507.00 |
| 3797  | ABF    | 2017-02-11 | 2500.00 |
| 4125  | ABF    | 2017-02-12 | 2500.00 |
| 4453  | ABF    | 2017-02-13 | 2500.00 |
| 4779  | ABF    | 2017-02-14 | 2575.00 |
| 5111  | ABF    | 2017-02-15 | 2550.00 |
| 5450  | ABF    | 2017-02-16 | 2561.00 |
| 5798  | ABF    | 2017-02-17 | 2579.00 |
| 6151  | ABF    | 2017-02-18 | 2609.02 |
| 6507  | ABF    | 2017-02-19 | 2609.02 |
| 6863  | ABF    | 2017-02-20 | 2609.02 |
| 7219  | ABF    | 2017-02-21 | 2616.00 |
| 7580  | ABF    | 2017-02-22 | 2586.00 |
| 7951  | ABF    | 2017-02-23 | 2620.36 |
| 8635  | ABF    | 2017-02-24 | 2620.00 |
| 8963  | ABF    | 2017-02-25 | 2632.72 |
| 9291  | ABF    | 2017-02-28 | 2668.00 |
| 11551 | ABF    | 2017-03-08 | 2618.00 |
| 11842 | ABF    | 2017-03-09 | 2639.00 |
| 12190 | ABF    | 2017-03-10 | 2645.00 |
| 12538 | ABF    | 2017-03-13 | 2657.00 |
| 12894 | ABF    | 2017-03-14 | 2657.50 |

表:价格

SELECT 
ratings.id AS rating_id
MIN(price_date) AS first_correct,
DATEDIFF( MIN(price_date), issue_date ) AS days_lapsed
FROM
(
    SELECT
    ratings.id,
    ratings.ticker,
    broker,
    issue_date,
    close_price,
    price_date,
    ( ( ( price - close_price) / close_price ) * 100 ) AS diff_percent,
    FROM ratings
    JOIN prices
    ON ratings.ticker = prices.ticker
    WHERE price_date > issue_date
    HAVING diff_percent >= 5
) correct_ratings
GROUP BY ratings.id

当前查询

| rating_id | ticker | broker | issue_date | close_price | price_date | diff_percent | first_correct | days_lapsed |
|-----------|--------|--------|------------|-------------|------------|--------------|---------------|-------------|
| 22        | ABF    | HSBC   | 2017-01-23 | 2523        | 2017-02-28 | 5.747126     | 2017-02-28    | 36          |
| 775       | ABF    | HSBC   | 2017-02-15 | 2527        | 2017-02-28 | 5.579739     | 2017-02-28    | 13          |

结果

first_correct

问题

正如您所看到的,rating_id=22的{​​{1}}日期位于issue_date rating_id=775之后。所以不应该出现在表格中。结果中只应包含rating_id=775

1 个答案:

答案 0 :(得分:0)

正如我在问题中所提到的,问题是我只有一个开始日期来检查百分比的增加,实际上我需要一个start_date 一个end_date

可以推导出的唯一方法是检查该经纪人/股票代码组合的下一个评级,并将其添加到名为end_date的新列。

我可以使用此处的查询创建新列:https://stackoverflow.com/a/15247545/1281101

有时评分为未被取代end_date将为NULL,这可由IFNULL(column, return) MySQL函数处理。

结束查询如下所示:

SELECT * 
FROM ( 
    SELECT rating_id, wp_broker_ratings.ticker, broker, date_issued AS start_date, issue_date_close_price, ( 
         SELECT MIN(st.date_issued) 
         FROM wp_broker_ratings st 
         WHERE st.broker = wp_broker_ratings.broker 
           AND st.ticker = wp_broker_ratings.ticker 
           AND st.date_issued > wp_broker_ratings.date_issued ) AS end_date, price_id, price_date, day_high AS absolute_correct_price, ( ( ( day_high - issue_date_close_price) / issue_date_close_price ) * 100 ) AS pct_diff 
FROM wp_broker_ratings 
INNER JOIN wp_share_prices 
ON wp_broker_ratings.ticker = wp_share_prices.ticker 
WHERE buy_sell = 'buy' 
HAVING price_date > start_date 
AND price_date <= IFNULL(end_date,'2050-01-01') 
AND pct_diff >= 5 ) correct 
GROUP BY rating_id

希望这可能有助于其他人。