?
Solved

Show Calculated Result on FormShow of Delphi cxGrid

Posted on 2011-04-29
11
Medium Priority
?
991 Views
Last Modified: 2012-05-11
I have 2 fields in my grid, FinalizedSalesAmount (currency) and TotalTime (float). I want a new field called "Finalized/Time" which is a calculation of FinalizedSalesAmount/TotalTime. I cannot run this calculation in SQL because not ALL records have a TotalTime thus giving me a 'cannot divide by zero error'. I tried using the columns 'OnDisplayText' event but can't seem to get that working... Any ideas on the best way to accomplish this?
Thanks!
0
Comment
Question by:Bianca
  • 6
  • 5
11 Comments
 
LVL 24

Expert Comment

by:jimyX
ID: 35492555
SQL has a way to handle that, you can check whether the division of FinalizedSalesAmount over TotalTime will result in error by checking the TotalTime if it's greater than zero:
select (case when TotalTime > 0 then TotalTime else 1 end) as Col1 from Table1
0
 

Author Comment

by:Bianca
ID: 35492585
Is there a way for me to enter this formula into the actual field itself to ONLY run the calculation if the TotalTime field is not 0?
0
 
LVL 24

Expert Comment

by:jimyX
ID: 35492593
Can you show your original SQL statement please?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:Bianca
ID: 35492639
SELECT JobNum, Contractor, Sales_Class, Job_Name, Buyer, Owner, Part, Approved_Sales, Sales_Amount, Sales_Initial, LF_SIZE, Job_Location, ' +
        'Finalized_Sales, Finalized_Sales_Amount, LF_Size_Final, TotalTimeOnJob, TotalTimeOnJobEng, Department, Street_Name, Street_Num, LFbid, ' +
        'bidsubmitted, bidamount, bidresult, Number_Of_Units, bidresultdate, awardedto, awardreason, Lost_Sales_Date, Lead_Category, Lead_Source, ' +
        'Lead_Provider, Close_Detail_ID, team, bidtype, bidlogtype, ctjobid, nextfollowupdate, JobStage, loggedin, Site_Ready_Date, Lead_Type, Region, ' +
        'County, City_Region, Sub_Region, JobMargin, Factor, StaticStatus, StaticStatusDetail, Deposit_Received, Deposit_Invoiced, Cut_Job, Shipped, ' +
        'CabinetCost, ComputerCost, TotalCost,  ' +
//        '(Finalized_Sales_Amount)/(TotalTimeOnJob) FROM SalesReports_View ' +
//        'WHERE ((TotalTimeOnJob > 0) AND (Finalized_Sales_Amount > 0)) AS AmountPerTime ' +
        'FROM SalesReports_View ';

I had to comment out the calculation because as soon as it hits a record that has TotalTimeOnJob = NULL then I get the divide by zero error.
0
 
LVL 24

Expert Comment

by:jimyX
ID: 35492678
make it:
SELECT JobNum, Contractor, Sales_Class, Job_Name, Buyer, Owner, Part, Approved_Sales, Sales_Amount, Sales_Initial, LF_SIZE, Job_Location, ' +
        'Finalized_Sales, Finalized_Sales_Amount, LF_Size_Final, TotalTimeOnJob, TotalTimeOnJobEng, Department, Street_Name, Street_Num, LFbid, ' +
        'bidsubmitted, bidamount, bidresult, Number_Of_Units, bidresultdate, awardedto, awardreason, Lost_Sales_Date, Lead_Category, Lead_Source, ' +
        'Lead_Provider, Close_Detail_ID, team, bidtype, bidlogtype, ctjobid, nextfollowupdate, JobStage, loggedin, Site_Ready_Date, Lead_Type, Region, ' +
        'County, City_Region, Sub_Region, JobMargin, Factor, StaticStatus, StaticStatusDetail, Deposit_Received, Deposit_Invoiced, Cut_Job, Shipped, ' +
        'CabinetCost, ComputerCost, TotalCost,  ' +
        '(Finalized_Sales_Amount)/(case when TotalTimeOnJob > 0 then TotalTimeOnJob else 1 end) as Col1' +
//        '(Finalized_Sales_Amount)/(TotalTimeOnJob) FROM SalesReports_View ' +
//        'WHERE ((TotalTimeOnJob > 0) AND (Finalized_Sales_Amount > 0)) AS AmountPerTime ' +
        'FROM SalesReports_View ';

Open in new window

0
 
LVL 24

Accepted Solution

by:
jimyX earned 2000 total points
ID: 35492699
Or you can try this way:
SELECT JobNum, Contractor, Sales_Class, Job_Name, Buyer, Owner, Part, Approved_Sales, Sales_Amount, Sales_Initial, LF_SIZE, Job_Location, ' +
        'Finalized_Sales, Finalized_Sales_Amount, LF_Size_Final, TotalTimeOnJob, TotalTimeOnJobEng, Department, Street_Name, Street_Num, LFbid, ' +
        'bidsubmitted, bidamount, bidresult, Number_Of_Units, bidresultdate, awardedto, awardreason, Lost_Sales_Date, Lead_Category, Lead_Source, ' +
        'Lead_Provider, Close_Detail_ID, team, bidtype, bidlogtype, ctjobid, nextfollowupdate, JobStage, loggedin, Site_Ready_Date, Lead_Type, Region, ' +
        'County, City_Region, Sub_Region, JobMargin, Factor, StaticStatus, StaticStatusDetail, Deposit_Received, Deposit_Invoiced, Cut_Job, Shipped, ' +
        'CabinetCost, ComputerCost, TotalCost,  ' +
        '(case when TotalTimeOnJob > 0 then (Finalized_Sales_Amount) / (TotalTimeOnJob) else 0 end) as Col1' +
//        '(Finalized_Sales_Amount)/(TotalTimeOnJob) FROM SalesReports_View ' +
//        'WHERE ((TotalTimeOnJob > 0) AND (Finalized_Sales_Amount > 0)) AS AmountPerTime ' +
        'FROM SalesReports_View ';

Open in new window

0
 

Author Comment

by:Bianca
ID: 35492732
The 1st query worked but records that did not have TotalTimeOnJob should show $0
0
 

Author Comment

by:Bianca
ID: 35492754
The 2nd query works beautifully, thanks!!!
0
 
LVL 24

Expert Comment

by:jimyX
ID: 35492766
Because we told it to divide Finalized_Sales_Amount by one if TotalTimeOnJob is zero:
(Finalized_Sales_Amount)/(case when TotalTimeOnJob > 0 then TotalTimeOnJob else 1 end) as Col1' +

What you want it to show if TotalTimeOnJob is zero?
0
 
LVL 24

Expert Comment

by:jimyX
ID: 35492785
Cheers :)
0
 

Author Comment

by:Bianca
ID: 35492793
Thanks JimyX, you have helped me plenty of times before :)
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Integration Management Part 2
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question