Solved

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

Posted on 2012-03-14
3
186 Views
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
0
Comment
Question by:thomas-sherrouse
  • 2
3 Comments
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 500 total points
ID: 37720960
Replace:

, [informix_styhstwe Query].reg_prate

with:

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

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
0
 

Author Closing Comment

by:thomas-sherrouse
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!

Thanks!!
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

825 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