dongpaocuan7498 2013-11-19 07:31
浏览 76
已采纳

在php中查询SQL数据库 - Datediff?

I am building a sales dashboard and have run into a small problem which I am reasonably sure has a simple solution, but I can't quite get there.

So I have a SQL database built in SQL Server 2008. In terms of this database, I have no issues drilling down into the information to get sales relevant data. There is one exception though; Revenue which reoccurs each month. The application which writes to the database (Connectwise) writes reoccurring revenue as a single entry controlled with a datetime field for start and end dates.

Currently I have the sales dashboard getting information from the other fields, but I haven't been able to get it to actually hook in with the datetime constraint correctly. I am using php to query the database with the two ? fields being a user defined start and end date.See below :

$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',

I was reading about the Datediff command but have tried to input it into this to no avail. If anyone can offer any assistance or advice, I really couldn't tell you how much I would appreciate it.

Sample data to demonstrate the idea of the dates as one entry. For example, this would constitute 52 reoccurring instances:

SO_Forecast_Dtl_RecID: 1 Opportunity_RecID: 1 Description: Opportunity1 Revenue: 1000 Cost: 500 SO_Opp_Status_RecID: 42 Include_Flag: 1 QuoteWerks_DocNO: 3938 QuoteWerks_DocName: 1223 Updated_By: User Last_Update: 18/11/2013 SO_Forecast_Type_ID: 1 Link_Flag:1 Recurring_Revenue: 100 Recurring_Cost: 50 Recurring_Date_Start: 01/11/2013 Recurring_Date_End 01/11/2014 Bill_Cycle_RecID:1 (Cross referenced with a weeks setup) Cycle_Basis_ID: 1 Recurring_Flag: 1 Product_Flag: 1 Mobile_Guid:

  • 写回答

1条回答 默认 最新

  • dongzecai0684 2013-11-19 08:01
    关注

    Something like this

    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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持