Solved

need help to write SQL

Posted on 2011-09-28
4
380 Views
Last Modified: 2013-11-16
How to write a SQL for the following?  Thanks for the help!

INQREG      Wtd avg pay      Factor
<=0      <= 22                       0.905
<=0      > 22                       0.905
>0      <= 22                       1.000
>0      > 22                       1.257
0
Comment
Question by:jjxia2001
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 23

Expert Comment

by:nemws1
ID: 36812872
You didn't provide very much information (please provide as much information as possible when posting questions!)  What is your table structure?  What does "Factor" mean?

The conditions you list will display all results (you have all cases enumerated).  Are you looking for separate queries for each?
0
 
LVL 2

Expert Comment

by:sanofi-aventis
ID: 36815076
I would assume your trying to get the appropriate factor.

This versin matches exacly what you have above.
Case When INQREG <= 0
     THEN ( CASE WHEN Wtd_avg_pay <= 22
                 THEN 0.905
                 ELSE 0.905 END )
     ELSE ( CASE WHEN Wtd_avg_pay <= 22
                 THEN 1.000
                 ELSE 1.257 END )
     END AS Factor

But could be simplified since the rate is identical if INQREG <= 0 no mater what the week-to-date average pay is
Case When INQREG <= 0
     THEN 0.905
     WHEN Wtd_avg_pay <= 22
     THEN 1.000
     ELSE 1.257 END AS Factor

Test SQL;
Proc SQL;

      SELECT Case When INQREG <= 0
                      THEN 0.905
                      WHEN Wtd_avg_pay <= 22
                      THEN 1.000
                      ELSE 1.257 END AS Factor1,
               Case When INQREG <= 0
                      THEN ( CASE WHEN Wtd_avg_pay <= 22
                                  THEN 0.905
                                  ELSE 0.905 END )
                      ELSE ( CASE WHEN Wtd_avg_pay <= 22
                                  THEN 1.000
                                  ELSE 1.257 END )
                      END AS Factor2
        FROM WORK.TEST;

QUIT;


Best of luck;
T-Bone

     
0
 
LVL 2

Accepted Solution

by:
sanofi-aventis earned 500 total points
ID: 36815129
This could also be used in the WHERE clause like this

Proc SQL;

      SELECT T2.*, T1.*
      FROM TEST    AS T1,
           FACTORS AS T2
      WHERE Case When T1.INQREG <= 0
                       THEN 0.905
                       WHEN T1.Wtd_avg_pay <= 22
                      THEN 1.000
                      ELSE 1.257 END = T2.Factor;

QUIT;

T-Bone
0
 

Author Closing Comment

by:jjxia2001
ID: 36818157
thanks!
0

Featured Post

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

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

Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

624 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