在Datediff中查询SQL数据库


Querying SQL Database in Datediff?

我正在构建一个销售仪表板,遇到了一个小问题,我有理由相信这个问题有一个简单的解决方案,但我不能完全解决。

因此,我在SQLServer2008中构建了一个SQL数据库。就这个数据库而言,我没有问题深入到信息中以获得与销售相关的数据。不过,有一个例外;每月重复出现的收入。写入数据库的应用程序(Connectwise)将重复出现的收入写入一个单独的条目,该条目由开始日期和结束日期的日期时间字段控制。

目前,我让销售仪表板从其他字段获取信息,但我还没能让它正确地与日期时间约束挂钩。我用php查询数据库有两个?字段是用户定义的开始和结束日期。见下文:

$sql = "select Owner_Level.Description as 'Location', v_rpt_Opportunity_SalesDash.Sales_Rep1 as 'Member', (select sum(v_Quota.GM) from v_Quota where v_Quota.Forecast_Year = ? and v_Quota.Forecast_Month between ? and ? and v_Quota.Member_RecID = v_rpt_Opportunity_SalesDash.MemberRec) as 'Quota',
sum(case when v_rpt_Opportunity_SalesDash.Sales_Stage = 11 then (ISNULL(SO_Forecast_Dtl.Revenue,0) -  ISNULL(SO_Forecast_Dtl.Cost,0) + (ISNULL(SO_Forecast_Dtl.Recurring_Revenue*(Nbr_Cycles/12),0)) -  ISNULL(SO_Forecast_Dtl.Recurring_Cost,0)) else 0 end) as 'Invoiced',

我读到了关于Datediff命令的文章,但试图将其输入其中,但没有成功。如果有人能提供任何帮助或建议,我真的无法告诉你我会多么感激

示例数据展示了将日期作为一个条目的想法。例如,这将构成52个重复出现的实例:

SO_Forecast_Dtl_RecID:1Opportunity_RecID:1说明:机会1收入:1000成本:500SO_Opp_Status_RecID:42Include_Flag:1报价Werks_DocNO:3938报价Werks_DocName:1223更新者:用户上次更新时间:2013年11月18日SO_Forecast_Type_ID:1链接滞后:1复发_复发:100周期成本:50周期日期起始日期:2013年11月1日周期日期截止日期2014年11月1日Bill_Cycle_RecID:1(与周设置交叉引用)Cycle_Basis_ID:1周期滞后:1产品滞后:1Mobile_Guid:

类似的东西

select Owner_Level.Description as 'Location', v_rpt_Opportunity_SalesDash.Sales_Rep1 as 'Member',
 (select sum(v_Quota.GM) from v_Quota where v_Quota.Forecast_Year = ? and
 v_Quota.Forecast_Month between ? and ? and v_Quota.Member_RecID = v_rpt_Opportunity_SalesDash.MemberRec) as 'Quota',
sum(case when v_rpt_Opportunity_SalesDash.Sales_Stage = 11 then (ISNULL(SO_Forecast_Dtl.Revenue,0) -  ISNULL(SO_Forecast_Dtl.Cost,0) +
 (ISNULL(SO_Forecast_Dtl.Recurring_Revenue*(Nbr_Cycles/12),0)) -
 ISNULL(SO_Forecast_Dtl.Recurring_Cost,0)) else 0 end) as 'Invoiced',
DATEDIFF(day,Recurring_Date_End ,Recurring_Date_Start)as datedifference