对应月的分叉记录

Posted

技术标签:

【中文标题】对应月的分叉记录【英文标题】:Bifurcate Records respective to months 【发布时间】:2013-04-15 09:16:40 【问题描述】:

在这里,我将解释我的场景:

我有以下表格:

Table : Service_Type

Id  | Service_Name | Service_Table_Name|
----------------------------------------
1   |   FA         |    FA     |
2   |   TPA        |        TPA        |


Table: Service_Mapping

Id  | Service_Type_Id |
---------------------------
1   |   1     |
2   |   2     |
3   |   2     |
4   |   1     |

Table: FA

Id  | Created_date| Qty |
-----------------------------
1   | 3/20/2012   | 20  |
2   | 4/22/2012   | 10  |
3   | 5/12/2012   | 15  |
4   | 6/3/2012    | 5   |

Table: TPA

Id  | Created_date| Qty |
-----------------------------
1   | 5/20/2012   | 2   |
2   | 7/22/2012   | 10  |
3   | 1/12/2012   | 1   |
4   | 9/3/2012    | 5   |

我想要这样的输出:

Month | FA| TPA|
----------------
Jan     0   1
Mar     1   0
Apr     1   0
Jul     1   0
Sep     0   1

在输出中,我希望月份对应于 FA 和 TPA 表的 Created_date 字段。并且相对于月份,我想要一个月内发生多少 FA 和 TPA 的总和。

我得到了像

这样的输出
FA   |  TPA  |
--------------
3    |  2    |

SELECT 
SUM(CASE WHEN Service_Type_Id = 1 THEN 1 ELSE 0 END) AS FA, 
SUM(CASE WHEN Service_Type_Id = 2 THEN 1 ELSE 0 END) AS TPA 
FROM Service_Mapping

但现在我想根据它们发生的月份来划分它们。自 TPA 和 FA 提交之日起。

【问题讨论】:

【参考方案1】:

试试这个:

SELECT Mon.monthNAME, ISNULL(TPAcount,0) TPAcount, ISNULL(FAcount,0) FAcount FROM
(
    SELECT DATENAME(MONTH,created_date) AS [monthNAME], MONTH(created_date) [month], YEAR(created_date) as [year] FROM FA
    UNION
    SELECT DATENAME(MONTH,created_date) AS [monthNAME], MONTH(created_date) [month], YEAR(created_date) as [year]  FROM TPA
) Mon LEFT JOIN
(
    SELECT DATENAME(MONTH,created_date) tpaMonth, YEAR(created_date) as [year] , COUNT(1) TPAcount 
    FROM TPA GROUP BY DATENAME(MONTH,created_date), YEAR(created_date)
) TPA ON TPA.tpaMonth = Mon.monthNAME and tpa.[year] = mon.[year] LEFT JOIN
(
    SELECT DATENAME(MONTH,created_date) faMonth, YEAR(created_date) as [year] , COUNT(1) FAcount 
    FROM FA GROUP BY DATENAME(MONTH,created_date), YEAR(created_date)
) FA ON FA.faMonth = MON.monthNAME and FA.[year] = mon.[year]

ORDER BY 
 Mon.[year], Mon.[month]

【讨论】:

我不知道为什么它会显示这个错误消息.......错误消息:'Mon.month' 不是可识别的日期名称选项。 我不知道为什么它在您的网站上不起作用。我创建了与您相同的表,对我来说效果很好。你试过我的两个查询吗?【参考方案2】:

您还可以检查另一种方法:

SELECT Mon.monthNAME, SUM(tpCount) tpCount, SUM(faCount) faCount FROM
(
    SELECT DATENAME(MONTH,created_date) AS [monthNAME], MONTH(created_date) [month], YEAR(created_date) as [year], 0 AS tpCount, 1 as faCount FROM FA
    UNION
    SELECT DATENAME(MONTH,created_date) AS [monthNAME], MONTH(created_date) [month], YEAR(created_date) as [year], 1 as tpCount, 0 AS faCount FROM TPA
) Mon 
GROUP BY 
   [monthNAME],[month],[year]
ORDER BY 
    Mon.[year], Mon.[month]

【讨论】:

这个查询工作正常。但是现在如果我添加更多服务,例如。 FA,TPA,Gift,AA 等等。那么我需要做些什么改变呢? 您必须添加下一个联合并向当前选择添加新列。例如,如果要添加 GIFT 表,则必须在当前选择中添加 0 作为 GiftCount,然后在下一个联合中添加如下:SELECT DATENAME(MONTH,created_date) AS [monthNAME], MONTH(created_date) [month], YEAR (created_date) as [year], 0 as tpCount, 0 AS faCount, 1 as GiftCount FROM Gift

以上是关于对应月的分叉记录的主要内容,如果未能解决你的问题,请参考以下文章

如何在python中杀死一个分叉的孩子及其jackd子进程

第159篇 笔记-区块链的升级与分叉

区块链中的软分叉与硬分叉详解

区块链中的软分叉与硬分叉详解

APFS 是不是真的支持命名分叉或仅支持资源分叉和扩展属性?

这种分叉箭头用PS怎么做