Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
日志状态值的SQL查询_Sql_Sql Server - Fatal编程技术网

日志状态值的SQL查询

日志状态值的SQL查询,sql,sql-server,Sql,Sql Server,好的,基本上我有一个如下所述的StatusLogs表。我想计算任何一个月内处于“合同下”状态的报价金额,只要随后没有取消 条件:如果一个要约在一个月内两次根据合同生效,第一次被取消的仅算一次,如果一个要约根据合同生效,然后被取消,则不算 到目前为止,我只能够在一个月内得到合同下的报价,但不知道如何过滤掉他们,如果他们被取消后 OfferStatusLogs列 身份证件 日志状态 讨厌的 日志日期 日志状态值 创建 根据合同 关闭 取消 当前查询: SELECT max(os.LogDa

好的,基本上我有一个如下所述的StatusLogs表。我想计算任何一个月内处于“合同下”状态的报价金额,只要随后没有取消

条件:如果一个要约在一个月内两次根据合同生效,第一次被取消的仅算一次,如果一个要约根据合同生效,然后被取消,则不算

到目前为止,我只能够在一个月内得到合同下的报价,但不知道如何过滤掉他们,如果他们被取消后

OfferStatusLogs列

身份证件 日志状态 讨厌的 日志日期 日志状态值

创建 根据合同 关闭 取消 当前查询:

SELECT      max(os.LogDate) AS LastUCDate,
            os.OfferStatusLogType_Type AS LogType, 
            os.OfferStatusLogType_Code AS LogCode

FROM        dbo.OfferStatusLogs AS os
            
WHERE       os.LogStatus = 'Under Contract'

GROUP BY    os.OfferStatusLogType_Type, os.OfferStatusLogType_Code
样本数据:

ID     LogStatus     LogDate     OfferId
1      Created       2020-01-01  1
2      UnderContract 2020-01-03  1
3      Cancelled     2020-01-04  1
4      Created       2020-01-06  2
5      UnderContract 2020-01-07  2
6      Closed        2020-01-08  2
7      UnderContract 2020-01-10  1
8      Closed        2020-01-11  1
9      Created       2020-01-12  3
10     UnderContract 2020-01-13  3
11     Cancelled     2020-01-14  3
预期结果:

Month     UnderContract
Jan, 20   2

在这里,我做了以下几点

获得当月“欠合同”报价的数量 同一报价的“欠合同”和“取消”价值的最大值和最小值。 将它们放在CASE-WHEN子句中,并将数字作为条件,并将新列命名为FLAG。 然后将这些值相加。
在我的测试中,这起作用并产生预期的结果

SELECT FORMAT(os.LogDate, 'MMM') + ' ' + CAST(YEAR(os.LogDate) AS varchar) AS [Month], COUNT(*) AS UnderContract 
FROM dbo.OfferStatusLogs os
WHERE os.LogStatus = 'UnderContract' AND os.OfferID NOT IN 
(SELECT OfferID FROM dbo.OfferStatusLogs WHERE OfferID=os.OfferID AND LogDate>os.LogDate AND LogStatus='Cancelled')
GROUP BY FORMAT(os.LogDate, 'MMM') + ' ' + CAST(YEAR(os.LogDate) AS varchar)

我删除了MySQL标记,因为代码看起来像SQL Server。样本数据和期望的结果将是一个很大的帮助。我提供了所要求的详细信息。如果它在合同下,并在以后的一个月取消了,会怎么样?是否考虑签订合同后关闭的合同?不,则不再计算
SELECT FORMAT(os.LogDate, 'MMM') + ' ' + CAST(YEAR(os.LogDate) AS varchar) AS [Month], COUNT(*) AS UnderContract 
FROM dbo.OfferStatusLogs os
WHERE os.LogStatus = 'UnderContract' AND os.OfferID NOT IN 
(SELECT OfferID FROM dbo.OfferStatusLogs WHERE OfferID=os.OfferID AND LogDate>os.LogDate AND LogStatus='Cancelled')
GROUP BY FORMAT(os.LogDate, 'MMM') + ' ' + CAST(YEAR(os.LogDate) AS varchar)