Return a Different Value of a SQL Query if the Result is 0.

Posted on 2012-03-14
Medium Priority
Last Modified: 2012-03-14
I'm wading into the world of writing SQL Queries, Functions, and Report Writing. I've got a query setup that returns all the Data I need with the exception of 2 variables that I need to return a different result.

The Query is pulling Employees Pay and Bill information. There are 2 employees that have a Pay Rate of $0 and I want to be able to substitute that $0 value with their Correct Value $22.50. If I can do that I can then create the functions to calculate the total Pay and Bill and Margin amounts.

The Employee ID Field is: [informix_styhstwe Query].empl_code
The Employee ID's of the Rates Needing Changed is: 334383 and 159154
The Pay Rate Field is:   [informix_styhstwe Query].reg_prate
The Rate Needs to be Changed from 0 to 22.50

So in summary - I need to replace the Pay Rates for Employee ID's 334383 and 159154 from 0 to 22.50.

I don't want to update the Database. Just change value of what is returned.

My Query is pasted below:

SELECT     [informix_styhstwe Query].week_end
                  , [informix_styhstwe Query].pay_month
                  , [informix_styhstwe Query].reference
                  , [informix_styhstwe Query].empl_code
                  , [informix_styhstwe Query].reg_phrs
                  , [informix_styhstwe Query].reg_prate
                  , [informix_styhstwe Query].ovt_phrs
                  , [informix_styhstwe Query].ovt_prate
                  , [informix_styhstwe Query].tot_phrs
                  , [informix_styhstwe Query].tot_bhrs
                  , [informix_styhstwe Query].tot_bamt
                  , [informix_styhstwe Query].tot_mtax_bamt
                  , [informix_styhstwe Query].tot_mded_bamt
                  , [informix_styhstwe Query].tot_mntx_bamt
                  , [informix_styhstwe Query].tot_pamt
                  , [informix_styhstwe Query].tot_mtax_pamt
                  , [informix_styhstwe Query].tot_mded_pamt
                  , [informix_styhstwe Query].tot_mntx_pamt
                  , [informix_styhstre Query].obl_total
                  , [informix_styhstwe Query].cust_code
                  , [informix_styhstwe Query].pay_year
FROM         [informix_styhstwe Query] INNER JOIN
                      [informix_styhstre Query] ON [informix_styhstwe Query].merge_no = [informix_styhstre Query].merge_no
WHERE     ([informix_styhstwe Query].cust_code = 1101) AND ([informix_styhstwe Query].pay_year = 2011) AND ([informix_styhstwe Query].reference = N'12690173')
ORDER BY [informix_styhstwe Query].reg_prate, [informix_styhstwe Query].week_end, [informix_styhstwe Query].empl_code
Question by:thomas-sherrouse
  • 2
LVL 70

Accepted Solution

Scott Pletcher earned 2000 total points
ID: 37720960

, [informix_styhstwe Query].reg_prate


, CASE WHEN [informix_styhstwe Query].empl_code IN ('334383', '159154') THEN 22.50 ELSE
[informix_styhstwe Query].reg_prate END
LVL 70

Expert Comment

by:Scott Pletcher
ID: 37720989
Technically it's better to create a lookup table and join to that, just in case you have to add extra overrides later, but the above will work for a "quick-and-dirty" approach.

Btw, it's considered best practice to use "aliases" to reference tables in queries.

SQL allows you to specify an "alias" name after the table name.  This makes it easier to code and change queries.  For example:

SELECT     we.week_end
                  , we.pay_month
                  , we.reference
                  , ...
                  , we.cust_code
                  , we.pay_year
FROM         [informix_styhstwe Query] AS we INNER JOIN
                      [informix_styhstre Query] AS re ON we.merge_no = re.merge_no
WHERE     (we.cust_code = 1101) AND (we.pay_year = 2011) AND (we.reference = N'12690173')
ORDER BY we.reg_prate, we.week_end, we.empl_code

The "AS" is optional, so you will often see it written this way:

FROM         [informix_styhstwe Query] we INNER JOIN

Author Closing Comment

ID: 37721069
Awesome Thanks! - I added a Column Name:

, CASE WHEN [informix_styhstwe Query].empl_code IN ('334383', '159154') THEN 22.50 ELSE
[informix_styhstwe Query].reg_prate END AS Reg_PayRate

 Thanks for the input on the other Best Practices. I'm still working back and forth between writing the Queries and Creating them in the wizard and then editing the out of the Wizard. I'm learning. I'll be sure to be back when I run into another question!


Featured Post

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

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

In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?

850 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