SQL query error

Hello, 

I am trying to create this query to calculate opportunity age:

SELECT opportunities.id opportunity_id, opportunities.name opportunity_name, datediff(date(now()), date(opportunities.date_created)) opportunity_age_in_days, date(opportunities.date_created) created_at, date(opportunities.date_modified) updated_at, date(opportunities.date_closed) as closed_at
FROM opportunities

however I keep getting this error: 

Not Found

The requested URL was not found on this server.

Could you please let me know why?

Parents
  • Hello

    Can you clarify where are you running this query? 

    When trying it to run against a MySQL Database the error that I have is related to the opportunities.date_created field that doesn't exist. 
    Replacing it with opportunities.date_entered the query runs fine.



  • Hi ,

    Yes I just realized that I was using incorrect field name. However, I also realized that the field date_closed is showing me the signature date of the opportunity, which is not the date I want to use. 

    I want to use the date from activity stream - the date a user changed the opportunity to closed won. 

    Is that possible? 

  • You are right, the  date_closed stores the Expected Close date that can be set by the user before the Opportunity is closed.
    The information that you are looking for is not stored by default in Sugar. If you want to store it at an Opportunity level you'll need to create a field and populate it when the Opportunity is closed. This can be done via SugarBPM, Logic Hook or workflow.   

    Retrieving that info from the activity stream will not be easy, the data column in the activities table will store the record name, id and the before and after value of the field that is audited. 

    You can have a look at what is stored in the activities table for a given Opportunity querying for that row like this: 

    SELECT * FROM activities where parent_id={opportunity_id};    

  • OK here we are.

    1. Create a datetime field on Opportunities

    2. Create a SugarBPM set sets the current time once the status changes to Closed Won or Closed Lost


    3. Create another field "Opp Age"

    and add the following formula which gives you the number of days between both timestamps (date entered and closing date)

    abs(subtract(daysUntil($date_entered),daysUntil($closing_date_c)))

    4. An opp that is calculated looks like this

    5. create a summation report with details with criterias as you like that shows the average age of your opps  :-)


    Bests
    Björn

Reply
  • OK here we are.

    1. Create a datetime field on Opportunities

    2. Create a SugarBPM set sets the current time once the status changes to Closed Won or Closed Lost


    3. Create another field "Opp Age"

    and add the following formula which gives you the number of days between both timestamps (date entered and closing date)

    abs(subtract(daysUntil($date_entered),daysUntil($closing_date_c)))

    4. An opp that is calculated looks like this

    5. create a summation report with details with criterias as you like that shows the average age of your opps  :-)


    Bests
    Björn

Children