MySQL 工作台,如何为日期创建过滤器以安排组

Posted

技术标签:

【中文标题】MySQL 工作台,如何为日期创建过滤器以安排组【英文标题】:MySQL workbench, How to create a Filter for a Date to arrange a group 【发布时间】:2018-07-17 18:58:03 【问题描述】:

我正在处理日期组。目前它不会返回任何结果。 我尝试过不同的过滤器或没有日期的组,效果很好,但我需要根据日期或时间戳创建一个组。

这是一个代码

SELECT 
    settlement_id,
    settlement_start_date,
    sku AS 'Settlement_sku',
        SUM(IF(transaction_type = 'order'
            AND amount_description = 'Principal'
            AND amount_type = 'ItemPrice',
        quantity_purchased,
        0)) AS 'total_QTY_Order'

FROM settlements
Where settlement_start_date ='2017-11-12'
GROUP BY sku 
HAVING Settlement_sku IS NOT NULL
    AND LENGTH(Settlement_sku) > 0
ORDER BY Settlement_sku ASC

当我得到结果时

如果我删除where settlement_start_date ='2017-11-12' 并将其更改为Where settlement_id ='7135956892'

结果正常,请看截图

问题是结算开始日期是 0000-00-00 那么如何在不依赖 0000-00-00 的情况下获得一个组。

这是表格 如您所见,我想要黄色高亮组与蓝色高亮并忽略所有红色高亮

如果你不确定我在说什么,那么也许我可以解释更多

【问题讨论】:

我完全不清楚,如果您还没有存储开始日期,您希望从哪里获得它?如果您将来自定居点的示例原始数据作为文本添加到您的问题中,这可能有助于澄清 也许这可以帮助你更清楚db-fiddle.com/f/h4dXWZgsHebqGyeMLSutPC/0 【参考方案1】:

您可以使用自联接根据结算的最小 ID 填写开始和结束日期 例如

select #s.minid,s.maxstdate,s.maxenddate, s2.*
        id                          ,                
        s2.settlement_id        ,    
        s.maxstdate             ,    
        s.maxenddate               ,    
        deposit_date            ,    
        total_amount            ,    
        currency                ,    
        transaction_type        ,    
        order_id                ,    
        merchant_order_id       ,    
        adjustment_id           ,    
        shipment_id             ,    
        marketplace_name        ,    
        amount_type             ,    
        amount_description      ,    
        amount                  ,    
        fulfillment_id          ,    
        posted_date             ,    
        posted_date_time        ,    
        order_item_code         ,    
        merchant_order_item_id  ,    
        merchant_adjustment_item_id ,
        sku                     ,    
        quantity_purchased      ,    
        promotion_id
from
(
select s1.settlement_id , min(id) minid,max(settlement_start_date) maxstdate,max(settlement_end_date) maxenddate 
from settlements_2_test s1
group by s1.settlement_id
) s
join settlements_2_test s2 on s2.id > s.minid and s2.settlement_id = s.settlement_id
order by id,settlement_id limit 5

结果

+----+---------------+------------+------------+--------------+--------------+----------+------------------+---------------------+---------------------+---------------+-------------+------------------+-------------+--------------------------+--------+----------------+-------------+-------------------------+-----------------+------------------------+-----------------------------+------------------+--------------------+--------------+
| id | settlement_id | maxstdate  | maxenddate | deposit_date | total_amount | currency | transaction_type | order_id            | merchant_order_id   | adjustment_id | shipment_id | marketplace_name | amount_type | amount_description       | amount | fulfillment_id | posted_date | posted_date_time        | order_item_code | merchant_order_item_id | merchant_adjustment_item_id | sku              | quantity_purchased | promotion_id |
+----+---------------+------------+------------+--------------+--------------+----------+------------------+---------------------+---------------------+---------------+-------------+------------------+-------------+--------------------------+--------+----------------+-------------+-------------------------+-----------------+------------------------+-----------------------------+------------------+--------------------+--------------+
|  2 |    7174103342 | 2017-12-24 | 2018-01-07 |              |         0.00 |          | Order            | 204-3505716-0081143 | 204-3505716-0081143 |               | DMX5Sr4rN   | Amazon.co.uk     | ItemPrice   | Principal                |  39.99 | AFN            | 26.12.2017  | 26.12.2017 15:08:49 UTC | 59733226356523  |                        |                             | UK-KC3816-0006   |                  1 |              |
|  3 |    7174103342 | 2017-12-24 | 2018-01-07 |              |         0.00 |          | Order            | 204-3505716-0081143 | 204-3505716-0081143 |               | DMX5Sr4rN   | Amazon.co.uk     | ItemFees    | FBAPerUnitFulfillmentFee |  -4.24 | AFN            | 26.12.2017  | 26.12.2017 15:08:49 UTC | 59733226356523  |                        |                             | UK-KC3816-0006   |                  1 |              |
|  4 |    7174103342 | 2017-12-24 | 2018-01-07 |              |         0.00 |          | Order            | 204-3505716-0081143 | 204-3505716-0081143 |               | DMX5Sr4rN   | Amazon.co.uk     | ItemFees    | Commission               |  -6.00 | AFN            | 26.12.2017  | 26.12.2017 15:08:49 UTC | 59733226356523  |                        |                             | UK-KC3816-0006   |                  1 |              |
|  5 |    7174103342 | 2017-12-24 | 2018-01-07 |              |         0.00 |          | Order            | 205-1808036-3091505 | 205-1808036-3091505 |               | D2RMyvqF4   | Amazon.co.uk     | ItemPrice   | Principal                |  29.99 | AFN            | 07.01.2018  | 07.01.2018 11:49:18 UTC | 25477500530227  |                        |                             | GB-KS8015-Grey-R |                  1 |              |
|  6 |    7174103342 | 2017-12-24 | 2018-01-07 |              |         0.00 |          | Order            | 205-1808036-3091505 | 205-1808036-3091505 |               | D2RMyvqF4   | Amazon.co.uk     | ItemFees    | FBAPerUnitFulfillmentFee |  -2.48 | AFN            | 07.01.2018  | 07.01.2018 11:49:18 UTC | 25477500530227  |                        |                             | GB-KS8015-Grey-R |                  1 |              |
+----+---------------+------------+------------+--------------+--------------+----------+------------------+---------------------+---------------------+---------------+-------------+------------------+-------------+--------------------------+--------+----------------+-------------+-------------------------+-----------------+------------------------+-----------------------------+------------------+--------------------+--------------+
5 rows in set (0.00 sec)

【讨论】:

以上是关于MySQL 工作台,如何为日期创建过滤器以安排组的主要内容,如果未能解决你的问题,请参考以下文章

Python UNO(libreoffice):如何为工作表启用自动过滤器

如何为以下场景安排本地通知?

iPhone 'Event Kit' - 如何为获取指定额外的搜索过滤器(超过开始/结束日期)?

如何为crontab日志创建文件夹(如果不存在),并根据日期将日志写入特定文件夹?

mysql - 如何为列创建 id 列

如何为 Kendo UI 日期列 _filter_ 的 DatePicker 设置日期格式