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 / 问题 / 329497
Accepted
RaGe
RaGe
Asked: 2023-07-20 22:04:58 +0800 CST2023-07-20 22:04:58 +0800 CST 2023-07-20 22:04:58 +0800 CST

TSQL使用数字表计算2个不同的列

  • 772

我尝试使用数字表按月间隔计算 2 个不同的日期列:

数字表:

编号
0
1
2
3

原始表(示例):

性别 订阅_日期 取消日期
男性 2023-01-01 2023-05-01
男性 2023-01-05 2023-01-08
男性 2023-01-05 2023-03-09
男性 2023-02-01 2023-04-08
女性 2023-01-05 2023-04-08
女性 2023-01-07 无效的

最终输出应该是这样的:

报告月 性别 订阅 取消
2023-01-01 男性 3 1
2023-01-01 女性 2 0
2023-02-01 男性 1 0
2023-02-01 女性 0 0
2023-03-01 男性 0 1
2023-03-01 女性 0 0
2023-04-01 男性 0 1
2023-04-01 女性 0 1
2023-05-01 男性 0 0
2023-05-01 女性 1 0

第一次尝试:

-- first day of the current month, one year ago
DECLARE @StartDate date = DATEADD(year,-1,DATEADD(month,datediff(month,0,getdate()),0)) 
-- Start from StartDate, showing results for Subscriptions for each month
SELECT
   @StartDate AS StartDate, Numbers.Num, DATEADD(month, Numbers.Num, @StartDate) AS ReportMonth
   ,S.Gender
   ,COUNT(S.SubscriptionDate) AS Subscriptions
FROM Numbers
JOIN
OriginatingTable AS S ON DATEADD(month, Numbers.Num, @StartDate) = DATEADD(MONTH, DATEDIFF(MONTH, 0, S.SubscriptionDate), 0)
WHERE (Numbers.Num <= DATEDIFF(month, @StartDate, GETDATE())) 
GROUP BY Numbers.Num, DATEADD(month, Numbers.Num, @StartDate), DATEADD(MONTH, DATEDIFF(MONTH, 0, S.Subscription_Date), 0), S.Gender
ORDER BY Numbers.Num

这可行,但我无法整合第二列“取消”。

感谢您的帮助,

拉尔夫.

t-sql
  • 2 2 个回答
  • 32 Views

2 个回答

  • Voted
  1. Best Answer
    Sergey
    2023-07-20T22:52:13+08:002023-07-20T22:52:13+08:00

    定义您的驱动表:Numbers Cartesian join Genders 并为每一行计算计数作为标量子查询类似

    select n.StartDate, n.EndDate,g.gender,
     DATEADD(month, Numbers.Num, @StartDate) AS ReportMonth,
     (select count(*) from OriginatingTable o 
       where o.gender=g.gender
         and o.Subscription_Date between n.StartDate and n.EndDate) as SubscriptionsCount,
     (select count(*) from OriginatingTable o 
       where o.gender=g.gender
         and o.Cancel_Date between n.StartDate and n.EndDate) as CancellationsCount
    from numbers n
    cross join Genders g
    
    • 0
  2. RaGe
    2023-07-21T18:36:27+08:002023-07-21T18:36:27+08:00

    谢尔盖,

    谢谢你!

    稍微修改一下就可以了:

    DECLARE @StartDate date = DATEADD(year,-1,DATEADD(month,datediff(month,0,getdate()),0)) --first day of current month, one year ago
    DECLARE @Genders table (Gender varchar(10));
    INSERT INTO @Genders values('male'),('female');
    
    SELECT DATEADD(month, N.Num, @StartDate) AS ReportMonth -- monthly Counter
            , (select count (Subscription_Date) from Originating_Table o
                where DATEADD(month,N.Num,@StartDate)= DATEADD(MONTH, DATEDIFF(MONTH,0,o.Subscription_Date),0)
                and o.Gender = g.Gender
              ) as Subscriptions
            , g.Gender
            ,
            (select count (Cancel_Date) from Originating_Table o
                where DATEADD(month,N.Num,@StartDate)= DATEADD(MONTH, DATEDIFF(MONTH,0,o.Cancel_Date),0)
                and o.Gender = g.Gender
              ) as Cancellations
        FROM Numbers N
        CROSS JOIN @Genders g
        WHERE N.Num < datediff(MONTH,@Startdate, GETDATE())+1 -- Report up to current month
        ORDER BY ReportMonth
    
    
    
    • 0

相关问题

  • 如何使用 TSQL 更改 SQL 服务器配置管理器设置?

  • 如何从结果集中获取列名和类型的列表?

  • MS SQL:使用计算值计算其他值

  • 如何判断 SQL Server 数据库是否仍在使用?

  • 实施 PIVOT 查询

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