?
Solved

Using SELECT in CASE STATEMENT

Posted on 2010-11-22
3
Medium Priority
?
829 Views
Last Modified: 2013-12-18
I am having trouble getting a query to work. When I remove the SELECT subquery it runs. Can I do this in a CASE statement or is there a better way do achieve this?

An abbreviated  version of the query is:

Select Member_ID, Date_of_Sale,
 (CASE
          WHEN REV_CD IS NOT NULL AND POS_CD IN ('02', '06', '07', '08', '09', '13', '14', '23')       THEN '2'
          WHEN PLAN_CD = 'HPHC' AND POS_CD IN ('MS', 'SC', 'AS', '11') THEN '2'
          WHEN REV_CD IS NOT NULL AND PLAN_CD = 'TAHP' AND  PROV_ID IN
                       (SELECT PROV_ID FROM HOSP) THEN '2'
                  ELSE '3'
     END) TYPE_OF_SALE, SUM(ALL_AMT) ALL_AMT
From MySalesTable
Where Date_of_Sale between '01-JAN-07' and '31-DEC-09'
GROUP BY
Member_ID, Date_of_Sale,
 (CASE
          WHEN REV_CD IS NOT NULL AND POS_CD IN ('02', '06', '07', '08', '09', '13', '14', '23')       THEN '2'
          WHEN PLAN_CD = 'HPHC' AND POS_CD IN ('MS', 'SC', 'AS', '11') THEN '2'
          WHEN REV_CD IS NOT NULL AND PLAN_CD = 'TAHP' AND  PROV_ID IN
                       (SELECT PROV_ID FROM HOSP) THEN '2'
                  ELSE '3'
     END)

I get a "not a group by expression" error. When I remove the sub-select part of the query, it runs ok. Any help is appreciated. Maybe not to difficult question for the Experts, but it's time sensitive.  Thanks in advance
0
Comment
Question by:jvoconnell
[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
3 Comments
 
LVL 58

Accepted Solution

by:
cyberkiwi earned 2000 total points
ID: 34192991
You could subquery it and defer the group by...

select memberid, dateofsale, typeofsale, sum(all_amt) all_amt
from
(
select memberid, dateofsale, case....., all_amt
from mysalestable
where...
) SQ
group by memberid, dateofsale, typeofsale

The other thing that comes to mind is to use type_of_sale in the group by clause as the 3rd item, instead of the case
0
 
LVL 28

Expert Comment

by:Naveen Kumar
ID: 34193723
try this to see if it works. I think it is the same idea which is even given by cyberwiki.

select member_id, date_of_sale, type_of_sale, SUM(ALL_AMT) ALL_AMT
from
(
Select Member_ID, Date_of_Sale,
 (CASE
          WHEN REV_CD IS NOT NULL AND POS_CD IN ('02', '06', '07', '08', '09', '13', '14', '23')       THEN '2'
          WHEN PLAN_CD = 'HPHC' AND POS_CD IN ('MS', 'SC', 'AS', '11') THEN '2'
          WHEN REV_CD IS NOT NULL AND PLAN_CD = 'TAHP' AND  PROV_ID IN
                       (SELECT PROV_ID FROM HOSP) THEN '2'
                  ELSE '3'
     END) TYPE_OF_SALE,
all_amt
From MySalesTable
Where Date_of_Sale between '01-JAN-07' and '31-DEC-09' ) x
group by member_id, date_of_sale, type_of_sale
0
 
LVL 1

Author Closing Comment

by:jvoconnell
ID: 34198786
Thank you for the responses. As I mentioned, this was just an abbreviated portion ofthe query. The entire process was run overnight. I tried cyberkiwi's suggestion and let the process run. After some QC, it was successful. I did not have to try the second suggestion, but I appreaciate the repsone. Thank you!!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Cursors in Oracle: A cursor is used to process individual rows returned by database system for a query. In oracle every SQL statement executed by the oracle server has a private area. This area contains information about the SQL statement and the…
I remember the day when someone asked me to create a user for an application developement. The user should be able to create views and materialized views and, so, I used the following syntax: (CODE) This way, I guessed, I would ensure that use…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
This video shows how to Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.
Suggested Courses

777 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