[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Using SELECT in CASE STATEMENT

Posted on 2010-11-22
3
Medium Priority
?
830 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

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

Working with Network Access Control Lists in Oracle 11g (part 1) Part 2: http://www.e-e.com/A_9074.html So, you upgraded to a shiny new 11g database and all of a sudden every program that used UTL_MAIL, UTL_SMTP, UTL_TCP, UTL_HTTP or any oth…
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…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to take different types of Oracle backups using RMAN.

656 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