CASE statement with Oacle 8i

I have a problem. I have this SQL :
select SLA_MET_TAB.SLA_MET_COL, count(*),((count(*)/Total)*100), Total
from
(select (case
when (completion_date - approval_date) -
                              (case
                         when to_char(approval_date,'D') < 5 then 0
     when to_char(approval_date,'D') = 5 then 3
                    when to_char(approval_date,'D') = 6 then 2
                         when to_char(approval_date,'D') = 7 then 1
               end -
                    case
                              when to_char(approval_date,'hh24') > 15 then 1
               when to_char(approval_date,'MI') > 0 and to_char(approval_date,'hh24') = 15 then 1
                         else 0
               end -
               case
                         when completion_date in (select holiday from csacweb_owner.holiday) then 1
                    else 0
                         end)< 1.375 then 0
               else 1
          end) SLA_MET_COL
from csacweb.orders
where community_id = 166 and completion_date is not null and approval_date is not null) SLA_MET_TAB,
(select count(*) Total from csacweb.orders where community_id = 166)
group by SLA_MET_TAB.SLA_MET_COL, Total;
Then problem is that when I'm trying to execute, I always have an error message that says : not a GROUP BY expression ... anyone can help me ?

Which version of the database are you running? This simplification of your query runs on my 9.2.0.6 install...
SQL> select object_type, count(*) as my_funcs, total as all_funcs, count(*)/total as pct
  2  from   all_objects o
  3         , ( select count(*) as total from all_objects
  4             where object_type = 'FUNCTION' )
  5  where  o.object_type = 'FUNCTION'
  6  and    o.owner = USER
  7  group by object_type, total
  8  /
OBJECT_TYPE          MY_FUNCS  ALL_FUNCS        PCT
FUNCTION                    1         48 .020833333
SQL> Cheers, APC

Similar Messages

  • Case statement with Subquery

    Case statement with Subquery
    select case when condition then
    i want to execute qr1
    else qr2
    How to implement this ?

    Hi,
    Welcoem to the forum!
    972471 wrote:
    Case statement with SubqueryHow does a sub-query, or a query, figure in this question?
    Be more specific. Post a complete script that you tried, and what output you expected from it. If you got an error, post the complete error message, including line numbers.
    See the forum FAQ {message:id=9360002}
    select case when condition then
    i want to execute qr1
    else qr2
    How to implement this ?Here's one way:
    BEGIN
        CASE
            WHEN  1 = 1
            THEN
                dbms_output.put_line ('Condition was TRUE');
            ELSE
                dbms_output.put_line ('Condition was FALSE or UNKNOWN');
        END CASE;
    END;
    /If the condition involves a query, then you may have to select the results into a variable first, and then use the variable in the CASE statement.

  • Case statement with having null gives default result

    Hello,
    When we write a case statement with a not equal condition <> on a column and if that columns has null value the it gives the else value.
    eg
    Select Column_Name, Case when Column_Name <>Condition then 0 else 1 end as case_test from Table_Name.
    Ideally null is not equal to the condition so the case should return 0 but it returns 1. Can anyone tell me why?

    In addition to the other posts:
    NULL is an unknown value, when you say
      col <> 1
    and col is NULL, we don't know whether col is 1 or something else. Thus the condition evaluates to UNKNOWN. How UNKNOWN is handled depends on the context. In WHEN clauses as well as the IF statments, the effect that the code bracketed by IF/WHEN is not
    entered. That is, UNKNOWN and FALSE yields the same result. In CHECK constraints, it's the other way round. If you have
     CHECK (WHEN col <> 0)
    rows where col is NULL are accepted.
    When you work with SQL, no matter the database product, it's imperative to master three-valued logic, because it comes up all the time.
    As I said, NULL is the unknown value. In practice, NULL has a distinct interpreation depending on the database column. For instance, NULL in a column called EndDate, usually means that the period (or whatever it is) is still open. So often when you work
    with NULL values you use isnull to replace it with some value that fits the context, and finding that value takes some consideration. Blindly doing isnull(col, '') can lead to serious bugs, particularly if col is not a string column.
    Erland Sommarskog, SQL Server MVP, [email protected]

  • CASE statement with MIN?

    I am trying to write a case statement which says when STAT is E select the MIN(TERM)
    If there is multiple TERM which are both low just pick any. Want one per row per ID.
    Is that possible by using case?
    Thanks.
    CREATE TABLE DAN_GR2
    (ID    VARCHAR2(12),
    TERM    VARCHAR2(12),
    STAT VARCHAR2(12))
    INSERT INTO DAN_GR2 (ID, TERM,STAT) VALUES ('1','5','E')
    INSERT INTO DAN_GR2 (ID, TERM,STAT) VALUES ('1','2','D')
    INSERT INTO DAN_GR2 (ID, TERM,STAT) VALUES ('1','9','E')
    INSERT INTO DAN_GR2 (ID, TERM,STAT) VALUES ('1','5','E')
    INSERT INTO DAN_GR2 (ID, TERM,STAT) VALUES ('2','1','E')
    INSERT INTO DAN_GR2 (ID, TERM,STAT) VALUES ('2','10','E')
    INSERT INTO DAN_GR2 (ID, TERM,STAT) VALUES ('3,'10','D')
    ID      TERM     STAT
    1     5     E
    1     2     D
    1     9     E
    1     5     E
    2     1     E
    2     10     E
    3       10      DWant
    ID   MIN_TERM  
    1     5     
    2     1
    3     NULLUsing:Oracle Database 10g Enterprise Edition Release 10.2.0.5.0 - 64bi
    Edited by: Chloe_19 on 12/09/2012 18:07

    Hi,
    So, you don't want to ignore the whole row when stat != 'E' (that would ignore the only row with id='3'). Putting the condition 'stat = 'E' in the WHERE clause sill ignore the row, so that's not what you want.
    You just want to ignore the term column. Put the same condition in a CASE expression that only contols that column.
    Here's one way:
    SELECT       id
    ,       MIN ( CASE
                     WHEN  stat = 'E'
                  THEN     term
                 END
               )          AS min_e_term
    FROM      dan_gr2
    GROUP BY  id
    ORDER BY  id
    ;

  • Case Statement with Group By

    Hello,
    I am trying to build a query that returns the 3 months of a quarter. Below are the two case statements that I am trying to use. I am getting a "Not a group by expression" in the first case statement, and a "nota a single-group group function in the second. Any help with this would be appreciated.
    Thanks, sck10
    SELECT Trev.Revenue_Year,
    CASE
    WHEN Trev.Revenue_Name = 'Jan' THEN Round(SUM(COALESCE(Trev.Revenue, 0)), 1)
    WHEN Trev.Revenue_Name = 'Apr' THEN Round(SUM(COALESCE(Trev.Revenue, 0)), 1)
    WHEN Trev.Revenue_Name = 'Jul' THEN Round(SUM(COALESCE(Trev.Revenue, 0)), 1)
    WHEN Trev.Revenue_Name = 'Oct' THEN Round(SUM(COALESCE(Trev.Revenue, 0)), 1)
    ELSE SUM(0)
    END AS Month_01,
    CASE
    WHEN Trev.Revenue_Name = 'Feb' THEN Round(SUM(COALESCE(Trev.Revenue, 0)), 1)
    WHEN Trev.Revenue_Name = 'May' THEN Round(SUM(COALESCE(Trev.Revenue, 0)), 1)
    WHEN Trev.Revenue_Name = 'Aug' THEN Round(SUM(COALESCE(Trev.Revenue, 0)), 1)
    WHEN Trev.Revenue_Name = 'Nov' THEN Round(SUM(COALESCE(Trev.Revenue, 0)), 1)
    ELSE SUM(0)
    END AS Month_02,
    CASE
    WHEN Trev.Revenue_Name = 'Mar' THEN Round(SUM(COALESCE(Trev.Revenue, 0)), 1)
    WHEN Trev.Revenue_Name = 'Jun' THEN Round(SUM(COALESCE(Trev.Revenue, 0)), 1)
    WHEN Trev.Revenue_Name = 'Sep' THEN Round(SUM(COALESCE(Trev.Revenue, 0)), 1)
    WHEN Trev.Revenue_Name = 'Dec' THEN Round(SUM(COALESCE(Trev.Revenue, 0)), 1)
    ELSE SUM(0)
    END AS Month_03
    FROM Stage_Report_Revenue Trev
    GROUP BY Trev.Revenue_Year;
    SELECT Trev.Revenue_Year,
    SUM(CASE
    WHEN Trev.Revenue_Name = 'Jan' THEN COALESCE(Trev.Revenue, 0)
    WHEN Trev.Revenue_Name = 'Apr' THEN COALESCE(Trev.Revenue, 0)
    WHEN Trev.Revenue_Name = 'Jul' THEN COALESCE(Trev.Revenue, 0)
    WHEN Trev.Revenue_Name = 'Oct' THEN COALESCE(Trev.Revenue, 0)
    ELSE 0
    END) AS Month_01,
    SUM(CASE
    WHEN Trev.Revenue_Name = 'Feb' THEN COALESCE(Trev.Revenue, 0)
    WHEN Trev.Revenue_Name = 'May' THEN COALESCE(Trev.Revenue, 0)
    WHEN Trev.Revenue_Name = 'Aug' THEN COALESCE(Trev.Revenue, 0)
    WHEN Trev.Revenue_Name = 'Nov' THEN COALESCE(Trev.Revenue, 0)
    ELSE 0
    END) AS Month_02,
    SUM(CASE
    WHEN Trev.Revenue_Name = 'Mar' THEN COALESCE(Trev.Revenue, 0)
    WHEN Trev.Revenue_Name = 'Jun' THEN COALESCE(Trev.Revenue, 0)
    WHEN Trev.Revenue_Name = 'Sep' THEN COALESCE(Trev.Revenue, 0)
    WHEN Trev.Revenue_Name = 'Dec' THEN COALESCE(Trev.Revenue, 0)
    ELSE SUM(0)
    END) AS Month_03
    FROM Stage_Report_Revenue Trev
    GROUP BY Trev.Revenue_Year;

    You are getting the "Not a group by expression" error because you are not using in aggregate functions in your select statement. (You are using the aggregate function SUM in the second query)
    The "not a a single-group group function" means you are returning multiple rows of a data when it only expects one.
    HTH

  • Help With A Case Statement With Multiple Variables

    I apologize if this is the incorrect Forum for this type of question, but it was the closest one that I could find. I'm pretty new with SQL and am stuck on this issue. I have roughly 26 dates that I need to compare to one another. Each date is tied to a step code. I also have a Stop value that is tied directly to the "max date" of the step codes. So, I need to compare 30 dates against one another to 1st - ID the max date; 2nd - ID if the Stop value is correct; 3rd - if the stop value is incorrect, identify what the correct value would be.
    At first, this seemed like it wouldn't be that hard. I wrote a query that found the max date for each step code. Then I realized that multiple step codes could have the same date. So, I tried using this case statement, but I did not get the expected results. Is there a more efficient way of getting what I need? This code seems like it's not necessary and probably the source of my issue.
    CASE
    WHEN FS25.ACTUAL_COMPLETION_DATE > FS.ACTUAL_COMPLETION_DATE AND FS25.ACTUAL_COMPLETION_DATE > FS1.ACTUAL_COMPLETION_DATE AND FS25.ACTUAL_COMPLETION_DATE > FS2.ACTUAL_COMPLETION_DATE AND FS25.ACTUAL_COMPLETION_DATE > FS3.ACTUAL_COMPLETION_DATE AND FS25.ACTUAL_COMPLETION_DATE > FS4.ACTUAL_COMPLETION_DATE AND FS25.ACTUAL_COMPLETION_DATE > FS5.ACTUAL_COMPLETION_DATE AND FS25.ACTUAL_COMPLETION_DATE > FS6.ACTUAL_COMPLETION_DATE AND FS25.ACTUAL_COMPLETION_DATE > FS7.ACTUAL_COMPLETION_DATE AND FS25.ACTUAL_COMPLETION_DATE > FS8.ACTUAL_COMPLETION_DATE AND FS25.ACTUAL_COMPLETION_DATE > FS9.ACTUAL_COMPLETION_DATE AND FS25.ACTUAL_COMPLETION_DATE > FS10.ACTUAL_COMPLETION_DATE AND FS25.ACTUAL_COMPLETION_DATE > FS11.ACTUAL_COMPLETION_DATE AND FS25.ACTUAL_COMPLETION_DATE > FS12.ACTUAL_COMPLETION_DATE AND FS25.ACTUAL_COMPLETION_DATE > FS13.ACTUAL_COMPLETION_DATE AND FS25.ACTUAL_COMPLETION_DATE > FS14.ACTUAL_COMPLETION_DATE AND FS25.ACTUAL_COMPLETION_DATE > FS15.ACTUAL_COMPLETION_DATE AND FS25.ACTUAL_COMPLETION_DATE > FS16.ACTUAL_COMPLETION_DATE AND FS25.ACTUAL_COMPLETION_DATE > FS17.ACTUAL_COMPLETION_DATE AND FS25.ACTUAL_COMPLETION_DATE > FS18.ACTUAL_COMPLETION_DATE AND FS25.ACTUAL_COMPLETION_DATE > FS19.ACTUAL_COMPLETION_DATE AND FS25.ACTUAL_COMPLETION_DATE > FS20.ACTUAL_COMPLETION_DATE AND FS25.ACTUAL_COMPLETION_DATE > FS21.ACTUAL_COMPLETION_DATE AND FS25.ACTUAL_COMPLETION_DATE > FS22.ACTUAL_COMPLETION_DATE AND FS25.ACTUAL_COMPLETION_DATE > FS23.ACTUAL_COMPLETION_DATE AND FS25.ACTUAL_COMPLETION_DATE > FS24.ACTUAL_COMPLETION_DATE AND L.FORECLOSURE_STOP_CODE <= '8' THEN '9'
    ELSE 'UH OH'
    END AS "CHANGE FC STOP TO"
    Any assistance is appreciated!

    I think Igor pointed out a working solution before.
    Applying it at your examples (you missed the operator after STOP_CODE, I assume it =):
    CASE
    WHEN FS25 = GREATEST(FS25, FS24, FS23) AND STOP_CODE = '9' THEN '9'
    ELSE 'UH OH'
    END AS 'CHANGE STOP CODE TO'
    {code}
    Be careful at the second example. You are checking:
    {code:sql}
    FS25 > FS24 OR FS25 IS NOT NULL AND FS24 IS NULL AND FS25 > FS23
    OR
    FS25 IS NOT NULL AND FS23 IS NULL AND STOP_CODE = '9'
    {code}
    Remember that AND has higher priority among operators than OR so if FS25 is greater than FS24 and FS23 the condition will be true even if STOP_CODE is not equal 9.
    Regards.
    Al                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

  • Sql case statement with link not opening new window

    Apex 4.2
    I am writing a query that has a case statement. I need the case statement to open up a new window, not a new tab. I have the following:
    SELECT
    case when SCHED_ID = 1 then
    '<a href="javascript:popupURL(''http://www.google.com'')">LINK</a>'
    end as MyLink
    FROM Table_Name
    When I plug this into a a column link in the URL field under a page item or report item, then a popup window appears. However, inside the query I am getting a new tab.
    ANy help on this matter would be great. Thanks in advance.

    Hi,
    Try adding a target attribute to you link:
    '<a href="javascript:popupURL(''http://www.google.com'')" target="_blank">LINK</a>' 
    Regards,
    Vincent

  • Case Statement with excel data source

    When trying to create a case statement in the RPD I get the following error in answers when selecting from the column.
    Listed is the RPD column select statement CASE EMPHOURS.EMPHOURS_DIM.PAYCODE WHEN 'OT' THEN 'Overtime' END
    Listed is the error I receive in answers:
    Error Codes: OPR4ONWY:U9IM8TAC:OI2DL65P
    State: HY000. Code: 10058. [NQODBC] [SQL_STATE: HY000] [nQSError: 10058] A general error has occurred. [nQSError: 16001] ODBC error state: 37000 code: -3100 message: [Microsoft][ODBC Excel Driver] Syntax error (missing operator) in query expression 'case T24.[PAYCODE] when 'OT' then 'Overtime' end'.. [nQSError: 16014] SQL statement preparation failed. (HY000)
    The statement works fine when I use oracle RDBMS as the data source and not excel.
    Regards

    Hi Sophia,
    Could you please edit Database>Features in this there will be a feature "CASE_IF_SUPPORTED" and another CASE related feature which you need to change and check in this features.
    Regards,
    Som

  • Case statement with group by clause

    SELECT STP, CASE WHEN Alternate IS NULL THEN 3 ELSE 4 END as Ind,
    CASE WHEN Alternate IS NULL THEN 'New Address' ELSE 'New Location' END as Description , Count(*) Rec_Cnt
    FROM t_Ids
    group by STP, CASE WHEN Alternate IS NULL THEN 3 ELSE 4 END, CASE WHEN Alternate IS NULL THEN 'New Address' ELSE 'New Location'
    ORDER BY 1,2,3
    I need a query something like this. Does anyone has any idea on this???

    You're missing the END on the GROUP BY Case statement, but otherwise this looks fine.
    What problem are you having?
    Also, please post DDL
    Thanks
    Carl

  • Case statement with Or

    declare @servdate datetime
    declare @paiddate datetime
    set @servdate='12/1/2015'
    set @paiddate='1/30/2016'
    select case
    when YEAR(@servdate)=YEAR(@paiddate) and MONTH(@servdate) IN (9,10,11) and MONTH(@paiddate) in (9,10,11) then 'Q1'
    when YEAR(@servdate)+1=YEAR(@paiddate) and MONTH(@servdate) IN (12) and MONTH(@paiddate) IN (1,2) then 'Q2' ELSE '' END +
    case when YEAR(@servdate)=YEAR(@paiddate) and MONTH(@servdate) IN (12) and MONTH(@paiddate) in (12) then 'Q2'
    when YEAR(@servdate)=YEAR(@paiddate) and MONTH(@servdate) IN (3,4,5) and MONTH(@paiddate) in (3,4,5) then 'Q3'
    when YEAR(@servdate)=YEAR(@paiddate) and MONTH(@servdate) IN (6,7,8) and MONTH(@paiddate) in (6,7,8) then 'Q4'
    else 'NQ' end
    I`m trying to derived Quarters in my query by using case expression. The Qtr logic is When Servdate and Paid date
    are '9/1/2014 and 11/30/2014 then its FiscalYear2015 Q1,
    When 
    Servdate and Paid date
    are '12/1/2014 and 02/28/2015 then its FiscalYear2015 Q2,
    When 
    Servdate and Paid date
    are '3/1/2015 and 05/31/2015 then its FiscalYear2015 Q3,
    When 
    Servdate and Paid date
    are '6/1/2015 and 08/31/2015 then its FiscalYear2015 Q4,
    With the above code snippet I`m using I think its working for all quarters except Q2 with above dates I get in my resultset
    " Q2NQ " .. Do i need to use a OR predicate am i missing something?
    FM

    select case when YEAR(@servdate)=YEAR(@paiddate) and MONTH(@servdate) IN (9,10,11) and MONTH(@paiddate) in (9,10,11) then 'Q1' 
    when YEAR(@servdate)+1=YEAR(@paiddate)  and  MONTH(@servdate) IN (12) and  MONTH(@paiddate) IN (1,2) then 'Q2' 
    when YEAR(@servdate)=YEAR(@paiddate)  and  MONTH(@servdate) IN (12) and MONTH(@paiddate) in (12) then 'Q2'  
    when YEAR(@servdate)=YEAR(@paiddate) and MONTH(@servdate) IN (3,4,5) and MONTH(@paiddate) in (3,4,5) then 'Q3' 
    when YEAR(@servdate)=YEAR(@paiddate) and MONTH(@servdate) IN (6,7,8) and MONTH(@paiddate) in (6,7,8) then 'Q4' 
    else 'NQ' end 

  • Multiple case statements with aggregates- SQL

    Hi guys
    Here is my ddl
    create table TestTable
      Bill_date                    DATE,
      Phone_no                     NUMBER(10),
      Planlvl1                     VARCHAR2(20),
      Planlvl2                     VARCHAR2(20)
      Revenue                      NUMBER                     
    INSERT INTO TestTable
    Values
    ('1/01/2014' , 64221, 'Bundle', 'SpecialPack1', '$23'),
    ('1/01/2014'' , 64221, 'Bundle', 'PlanA', '$32'),
    ('1/01/2014' , 65211, 'Bundle', 'SpecialPack2', '$3'),
    ('1/01/2014' , 65211, 'Bundle', 'SpecialPack1', '$23'),
    ('1/01/2014' , 66211, 'Bundle', 'PlanB', '$34'),
    ('1/01/2014' , 66211, 'Bundle', 'SpecialPack2', '$3'),
    ('1/01/2014' , 66222, 'Bundle', 'SpecialPack1', '$23'),
    ('1/01/2014' , 66222, 'Bundle', 'SpecialPack2', '$3'),
    ('1/01/2014' , 66222, 'Bundle', 'PlanB', '$65'),
    ('1/01/2014' , 32444, 'Non_bundle', 'Casual1', '$32'),
    ('1/01/2014' , 324441, 'Non_bundle', 'Casual2', '$76'),
    ('1/01/2014' , 65444, 'Non_bundle', 'Casual1', '$12'),
    ('1/01/2014' , 65444, 'Bundle', 'PlanB', '$98'),
    ('1/01/2014' , 54322, 'Bundle', 'PlanA', '$12'),
    ('1/01/2014' , 54322, 'Non_bundle', 'Casual', '$12')
    Expected Outcome:
    Bill_date PlanLvl2 PhonenoCount
    '01/01/2014' 'SpecialPack1' 1
    '01/01/2014' 'SpecialPack2' 3
    '01/01/2014' 'Casual1' 1
    '01/01/2014' 'Casual2' 1
    '01/01/2014' 'PlanA' 1
    '01/01/2014' 'PlanB' 1
    Basically I have to count all the phone nos grouped by plans in a certain way:
    If PlanLvl1 = Bundle, then I have to look  at planLvl2 and see if that phone no has ' SpecialPack%' ,and also another plan,them 'Special pack' gets the priority and the phone no gets counted in the special pack.
    In case when PlanLvl1 = Bundle and a phone no has both 'Special Packs' as their plans, then the phone no will be counted under the 'Special Pack' with higher Revenue.
    In case where PalnLvl is 'Bundle' and a phone no has multiple 'Special Packs' and a non special, that ph no will only be counted once under 'Special Pack; of higher rev as compared to the special pack of lower rev.
    Now there are also PlanLvl1 which are 'Non_Bundles' which means they do not have any 'SpecialPacks' but if a ph no with 'NonBundle' plan has 2 different plans under planLvl2, we need to count that ph no for eack planLvl2..
    Then there is a scenario where a ph no can have bundles as well as non bundles and in that case we just have to count that ph no in bundles and not in non-bundles.
    As I mentioned , 'SpecialPack2' will get a preference over special pack 1 if a phone no has got both of these
    Hope this helps

    I;m not sure exactly what your rules are since you specify both "In case when PlanLvl1 = Bundle and a phone no has both 'Special Packs' as their plans, then the phone no will be counted under the 'Special Pack' with higher Revenue" and "As I mentioned ,
    'SpecialPack2' will get a preference over special pack 1 if a phone no has got both of these".  I went with the rule "As I mentioned , 'SpecialPack2' will get a preference over special pack 1 if a phone no has got both of these" since that seemed to match
    the result you say you want.  I also changed the datatypes to valid SQL Server datatypes.
    create table TestTable
    Bill_date DATE,
    Phone_no Decimal(10,0),
    Planlvl1 VARCHAR(20),
    Planlvl2 VARCHAR(20),
    Revenue money
    INSERT INTO TestTable
    Values
    ('1/01/2014' , 64221, 'Bundle', 'SpecialPack1', '$23'),
    ('1/01/2014' , 64221, 'Bundle', 'PlanA', '$32'),
    ('1/01/2014' , 65211, 'Bundle', 'SpecialPack2', '$3'),
    ('1/01/2014' , 65211, 'Bundle', 'SpecialPack1', '$23'),
    ('1/01/2014' , 66211, 'Bundle', 'PlanB', '$34'),
    ('1/01/2014' , 66211, 'Bundle', 'SpecialPack2', '$3'),
    ('1/01/2014' , 66222, 'Bundle', 'SpecialPack1', '$23'),
    ('1/01/2014' , 66222, 'Bundle', 'SpecialPack2', '$3'),
    ('1/01/2014' , 66222, 'Bundle', 'PlanB', '$65'),
    ('1/01/2014' , 32444, 'Non_bundle', 'Casual1', '$32'),
    ('1/01/2014' , 324441, 'Non_bundle', 'Casual2', '$76'),
    ('1/01/2014' , 65444, 'Non_bundle', 'Casual1', '$12'),
    ('1/01/2014' , 65444, 'Bundle', 'PlanB', '$98'),
    ('1/01/2014' , 54322, 'Bundle', 'PlanA', '$12'),
    ('1/01/2014' , 54322, 'Non_bundle', 'Casual', '$12')
    ;With cte As
    (Select Bill_date, Phone_no, Planlvl1, Planlvl2, Revenue,
    Row_Number() Over (Partition By Bill_Date, Phone_no Order By
    Case When Planlvl1 = 'Bundle' And Planlvl2 Like 'SpecialPack2' Then 0 Else 1 End,
    Case When Planlvl1 = 'Bundle' And Planlvl2 Like 'SpecialPack1' Then 0 Else 1 End,
    Case When Planlvl1 = 'Bundle' Then Revenue Else -1 End Desc) As rn,
    Min(Case When Planlvl1 = 'Bundle' Then 0 Else 1 End) Over(Partition By Bill_Date, Phone_no) As NumberHasOnlyNonBundle
    From TestTable)
    Select Bill_date, Planlvl2, COUNT(*) As PhoneNoCount
    From cte
    Where rn = 1 Or NumberHasOnlyNonBundle = 1
    Group By Bill_date, Planlvl2;
    Tom

  • Case statement with where clause.

    I appreciate that there are much simpler ways to run this query but I wondered if anyone else had come across the following -
    select distinct
    product_table.a,
    product_table.b,
    product_table.c,
    CASE WHEN product_table.c IN ('A','AA') THEN 'AAA'     
    WHEN product_table.b = 'B' THEN 'BBB'
    WHEN product_table.c IN ('C','CC','CCC') THEN 'CCC'
    WHEN product_table.b IN ('D','DD') THEN 'DDD'
    WHEN product_table.b LIKE 'E%' THEN 'EEE'
    WHEN product_table.b LIKE 'F%' THEN 'FFF'
    WHEN product_table.b LIKE 'G%' THEN 'GGG'
    ELSE 'UNKNOWN' END
    from product_table                    
    where
    ( CASE WHEN product_table.c IN ('A','AA') THEN 'AAA'     
    WHEN product_table.b = 'B' THEN 'BBB'
    WHEN product_table.c IN ('C','CC','CCC') THEN 'CCC'
    WHEN product_table.b IN ('D','DD') THEN 'DDD'
    WHEN product_table.b LIKE 'E%' THEN 'EEE'
    WHEN product_table.b LIKE 'F%' THEN 'FFF'
    WHEN product_table.b LIKE 'G%' THEN 'GGG'
    ELSE 'UNKNOWN' END) = 'FFF'
    when this query runs its as if the where clause = 'FFF' does not exist and all rows are returned.
    Thanks, Sarah.

    Technical questions should be addressed to one of the technical forums. You'll probably find that the folks in the PL/SQL forum (Products | Database | PL/SQL) have some thoughts.
    Justin
    Distributed Database Consulting, Inc.
    http://www.ddbcinc.com/askDDBC

  • Problem In Case Statement

    HI ALL,
    in the program,i need to display description based on condition type.
    so we used CASE statement as below in LOOP. ITS not working .
    could anyone please suggest if anything wrong in the following code.
    LOOP AT gt_mat.
           MOVE gt_mat-type  TO gt_final-type.
      Condition description
    CASE gt_mat-type  .
    when  'N'.
    move  'New'  to gt_final-condes.
    when  'B'.
    move 'Block'   to gt_final4-condes.
    ENDCASE.
    ENDLOOP.
    please let me know whats the problem i above code and how to solve.
    thanks in advance.

    Hi,
    The only solution for this could be to debug the program and check if the coresponding values are populated and available. You could also try to replace the CASE statement with a simple IF statement and check if its working.
    LOOP AT gt_mat.
    MOVE gt_mat-type TO gt_final-type.
    * Condition description
    IF gt_mat-type = 'N'.
    gt_final-condes = 'New'.
    elseif gt_mat-type = 'B'.
    gt_final-condes ='Block'.
    append gt_final.
    clear gt_final.
    ENDIF.
    ENDLOOP.
    Also make sure the case of the value in gt_mat-type matches with the condition
    Regards,
    Vikranth

  • Case Statement in a Where clause help

    Oracle Database 11g Release 11.2.0.1.0 - 64bit Production
    PL/SQL Release 11.2.0.1.0 - Production
    "CORE     11.2.0.1.0     Production"
    TNS for Linux: Version 11.2.0.1.0 - Production
    NLSRTL Version 11.2.0.1.0 - Production
    Hello,
    I have an APEX application that I need to build a SQL statement for a LOV (List of Values). I have a hidden filed that contains the customer type which can be an 'R' or 'B'. The query needs to be able to display two different result sets based on the customer type of 'R' or 'B'.
    If the customer type is 'R' then:
    SELECT drg_descr d, drg_code r
    FROM distance_ranges
    WHERE  drg_min_miles IN (0,5)
    ORDER BY drg_min_milesIf the customer type is 'B' then:
    SELECT drg_descr d, drg_code r
    FROM distance_ranges
    WHERE  drg_min_miles IN (0,5,10,15,20)
    ORDER BY drg_min_milesCan someone help me with what I think needs to be a case statement?
    Thanks,
    Joe

    Hi,
    You can try CASE statement with WHERE clause
    SELECT drg_descr d, drg_code r
    FROM distance_ranges
    WHERE (CASE param_cust_type
    WHEN(param_cust_type='R') THEN (drg_min_miles IN (0, 5)
    WHEN (param_cust_type='B') THEN (drg_min_miles IN (0,5,10,15,20)
    END;
    Please try and let me know if anything wrong.
    Anyone from the forum comment my code if there is any wrong.
    Thanks!
    Naresh

  • Excel and CASE statement.

    Hello Experts,
    I am using a CASE statement on CHAR column as follows:
    CASE WHEN Sales.Ret='Y' THEN 'Returning' ELSE 'New' END
    It gives an error: Odbc driver returned an error (SQLExecDirectW).
    Whereas when I use another CASE statement with Numrical column it is working perfectly.
    CASE WHEN Sales.Amount<40 THEN 40 ELSE 100 END
    Am i missing some Excel data source setting related to CHAR data type?
    My source is MS Excel 2003 and OBIEE is 10.1.3.4.1
    Appreciate any comment, help.
    ~ Ash
    PS: The error details are as follows:*
    Error Codes: OPR4ONWY:U9IM8TAC:OI2DL65P
    State: HY000. Code: 10058. [NQODBC] [SQL_STATE: HY000] [nQSError: 10058] A general error has occurred. [nQSError: 16001] ODBC error state: 37000 code: -3100 message: [Microsoft][ODBC Excel Driver] Syntax error (missing operator) in query expression 'case when T46.[Ret] = 'Y' then 'Returning' else 'New' end'.. [nQSError: 16014] SQL statement preparation failed. (HY000)
    SQL Issued: SELECT Sales.Tran_date saw_0, CASE WHEN Sales.Ret='Y' THEN 'Returning' ELSE 'New' END saw_1, Network."Network Name" saw_2, Sales.Amount saw_3 FROM P4UD ORDER BY saw_0, saw_1, saw_2

    Hello Saichand,
    I would like to reconfirm that the CASE statement with Numeric column is working with EXCEL as source.
    The strange thing is that the CASE statement with CHAR column was also working! and from one fine day it it started throwing an error (i thought some source setting might got changed inadvertently ).
    Thank you for your response but I would like to see comments/suggestions from other experts..
    ~ Ash
    Edited by: OBIEE_Ash on Dec 20, 2010 10:27 PM

Maybe you are looking for

  • Do you recommend Server 2008 R2 to 2012 R2 inplace upgrade?

    Hi Do you recommend Server 2008 R2 to 2012 R2 in place upgrade for both DCs and member servers? Thanks Shane

  • Need to partition current internal drive first...help needed

    Hello, I'm currently installing a version of leopard on my macbook pro intel 2 core duo. Problem is I had previously swapped out the hard drive for a 320 and failed to partition it as a GUID so the leopard installation is saying that needs to be done

  • Mercury Playback Engine stop working in Premiere CC 2014

    Hello All, After upgrading to the latest CC 2014 I've been experiencing issues with real time editing when working in Premiere.   I've checked to make sure that CUDA is selected and it is, but I'm not getting the performance from the CUDA.  I've chan

  • Is my mac compatible for Photoshop?

    Its a Macbook Pro (mid 2010), version 10.10.2 OS X Yosemite. Memory 4GB. I need to learn photoshop so need to by it but haven't a clue whether my Mac is compatible! please help!

  • AVI glitches, color problems and tearing

    Hey, I've got a 640x480, 60fps avi that I am bringing into Premiere in order to add some titles, text etc. The codec used for compression is the Techsmith screen capture codec, the standard codec used when recording with Camtasia. However in the prev