AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / dba / 问题 / 301332
Accepted
Jay Modi
Jay Modi
Asked: 2021-10-20 02:41:26 +0800 CST2021-10-20 02:41:26 +0800 CST 2021-10-20 02:41:26 +0800 CST

以按日期分组的按日期顺序显示所有数据

  • 772

目前我有 2 个表,一个列表表和一个日志表。通过以下查询,我试图获取特定日期的产品列表,并返回正确的输出。

with X as (
  select 
    l.*,
    (select status_from from logs
         where logs.refno = l.refno
           and logs.logtime >= '2021-10-01'
         order by logs.logtime limit 1) logstat
  from listings l
  where l.added_date < '2021-10-01'
)
, Y as (select X.*, ifnull(X.logstat, X.status) stat20211001 from X)
SELECT 
  status.text,
  COUNT(Y.id) AS c 
from status
left join Y on Y.stat20211001 = status.code
group by status.code, status.text;

这给出了这样的输出:

在此处输入图像描述

在这里,我按 1 个日期过滤了查询,在本例中为 2021-10-01。现在我有 2 个输入表单,用户可以在其中选择一个从日期和一个到日期。所以我希望能够获取提供的日期范围之间的所有数据。所以基本上,如果我选择 2021-10-01 和 2021-10-02 之间的日期,它应该显示该日期及其之间的所有内容。输出应如下所示:

日期 发布 行动 让 卖 草稿
2021-10-01 0 3 0 1 1
2021-10-02 0 2 0 1 2

dbfiddle:https ://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=5e0b8d484a41ac9104d0fb002e7f9a3c

我已格式化表格以使用以下查询以逐行方式显示条目:

with X as (
select l.*,
(select status_from from logs where logs.refno = l.refno and logs.logtime >= '2021-10-01' order by logs.logtime limit 1) logstat
from listings l
where l.added_date < '2021-10-01'
)
, Y as (select X.*, ifnull(X.logstat, X.status) stat20211001 from X)
SELECT
sum(case when status.text= 'Action' and Y.id is not null then 1 else 0 end) as `Action`,
sum(case when status.text= 'Draft' and Y.id is not null then 1 else 0 end) as `Draft`,
sum(case when status.text= 'Let' and Y.id is not null then 1 else 0 end) as `Let`,
sum(case when status.text= 'Sold' and Y.id is not null then 1 else 0 end) as `Sold`,
sum(case when status.text= 'Publish' and Y.id is not null then 1 else 0 end) as `Publish`
from status
left join Y on Y.stat20211001 = status.code

现在我只需要它来显示一个新的日期列,该列显示该特定日期的所有数据,并为每个日期都有一个新条目。

mysql
  • 2 2 个回答
  • 135 Views

2 个回答

  • Voted
  1. Best Answer
    Gerard H. Pille
    2021-10-22T09:34:28+08:002021-10-22T09:34:28+08:00

    看看下面和dbfiddle.uk

    with Y as (
      with recursive D (n, day) as (
        select 1 as n, '2021-09-25' my_date
        union
        select n+1, day + interval 1 day from D
          where day + interval 1 day < '2021-10-15'
      ) select * from D
    ), X as (
      select Y.day,
             l.*,
             (select status_from from logs
                where logs.refno = l.refno
                  and logs.logtime >= Y.day
                order by logs.logtime
                limit 1) logstat
        from listings l, Y
        where l.added_date <= Y.day
    ), Z as (
      select X.day, ifnull(X.logstat,X.status) stat_day, count(*) cnt
        from X
        group by X.day, stat_day
    )
    select Z.day,
      sum(case when Z.stat_day = 'D' then Z.cnt else 0 end ) Draft,
      sum(case when Z.stat_day = 'A' then Z.cnt else 0 end ) Action,
      sum(case when Z.stat_day = 'Y' then Z.cnt else 0 end ) Publish,
      sum(case when Z.stat_day = 'S' then Z.cnt else 0 end ) Sold,
      sum(case when Z.stat_day = 'L' then Z.cnt else 0 end ) Let
      from Z
      group by Z.day
      order by Z.day;
    
    • 1
  2. Rick James
    2021-10-20T10:03:52+08:002021-10-20T10:03:52+08:00

    从 3 列表开始

    SELECT date, status, count(*)
        FROM ...
        GROUP BY date, status
    

    然后“pivot”,使用上面的作为派生表(即FROM (SELECT...))。

    不要打扰WITH,它只会混淆问题。

    • 0

相关问题

  • 是否有任何 MySQL 基准测试工具?[关闭]

  • 我在哪里可以找到mysql慢日志?

  • 如何优化大型数据库的 mysqldump?

  • 什么时候是使用 MariaDB 而不是 MySQL 的合适时机,为什么?

  • 组如何跟踪数据库架构更改?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    连接到 PostgreSQL 服务器:致命:主机没有 pg_hba.conf 条目

    • 12 个回答
  • Marko Smith

    如何让sqlplus的输出出现在一行中?

    • 3 个回答
  • Marko Smith

    选择具有最大日期或最晚日期的日期

    • 3 个回答
  • Marko Smith

    如何列出 PostgreSQL 中的所有模式?

    • 4 个回答
  • Marko Smith

    列出指定表的所有列

    • 5 个回答
  • Marko Smith

    如何在不修改我自己的 tnsnames.ora 的情况下使用 sqlplus 连接到位于另一台主机上的 Oracle 数据库

    • 4 个回答
  • Marko Smith

    你如何mysqldump特定的表?

    • 4 个回答
  • Marko Smith

    使用 psql 列出数据库权限

    • 10 个回答
  • Marko Smith

    如何从 PostgreSQL 中的选择查询中将值插入表中?

    • 4 个回答
  • Marko Smith

    如何使用 psql 列出所有数据库和表?

    • 7 个回答
  • Martin Hope
    Jin 连接到 PostgreSQL 服务器:致命:主机没有 pg_hba.conf 条目 2014-12-02 02:54:58 +0800 CST
  • Martin Hope
    Stéphane 如何列出 PostgreSQL 中的所有模式? 2013-04-16 11:19:16 +0800 CST
  • Martin Hope
    Mike Walsh 为什么事务日志不断增长或空间不足? 2012-12-05 18:11:22 +0800 CST
  • Martin Hope
    Stephane Rolland 列出指定表的所有列 2012-08-14 04:44:44 +0800 CST
  • Martin Hope
    haxney MySQL 能否合理地对数十亿行执行查询? 2012-07-03 11:36:13 +0800 CST
  • Martin Hope
    qazwsx 如何监控大型 .sql 文件的导入进度? 2012-05-03 08:54:41 +0800 CST
  • Martin Hope
    markdorison 你如何mysqldump特定的表? 2011-12-17 12:39:37 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 对 SQL 查询进行计时? 2011-06-04 02:22:54 +0800 CST
  • Martin Hope
    Jonas 如何从 PostgreSQL 中的选择查询中将值插入表中? 2011-05-28 00:33:05 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 列出所有数据库和表? 2011-02-18 00:45:49 +0800 CST

热门标签

sql-server mysql postgresql sql-server-2014 sql-server-2016 oracle sql-server-2008 database-design query-performance sql-server-2017

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve