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

Similar Messages

  • 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 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 function with group by

    Hi,
      I am having a scenario like :
    Column 1:  BrokerList(dimension1)
    Column 2 : Broker(dimension2)
    Column 3 : Metric value(measure)
    so i am having a case when (dimension 3) Custodian = 'ss' then sum(metirc) group by dimension1,dimension2 but the result value is not matching
    BrokerList
    Broker
    Metric
    a1
    a
    10
    b
    20
    c
    30
    a1 :total
    60
    a2
    a
    50
    c
    60
    d
    10
    a2:total
    120
    Grand total
    180
    Here the metric is based on other case condition.. so the total value is not matching.. Is there any other way to do a case function with group by funtions. Please advise.
    regards,
    Guru

    Use filter on metric by ss value and then go for group by from Criteria
    something like
    sum(FILTER(metric USING (Custodian = 'ss')) by dimension1,dimension2)
    mark if helps
    ~ http://cool-bi.com

  • 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 and Group By issues

    Hi this is my initial query which works perfectly:
    select ff.cla_case_no, Cost_before_Decision, Cost_Of_Claim, decline, Decline_Description from
    fraud_nov_14_final ff
    ,(select cla_case_no, (sum(total_cost_adj_old) + sum(decline_estimate)) Cost_before_Decision
    from reporting.ci_final@test
    group by cla_case_no) z
    where ff.cla_case_no = z.cla_case_noI now want to add in a condition based on a column called decline:
    select ff.cla_case_no, Cost_before_Decision, Cost_Of_Claim, ff.decline, Decline_Description from
    fraud_nov_14_final ff
    ,(select cla_case_no, (case when decline = 1 or decline = 2  THEN (sum(total_cost_adj_old) + sum(decline_estimate)) ELSE Total_Cost END) Cost_before_Decision
    from reporting.ci_final@test
    group by cla_case_no) z
    where ff.cla_case_no = z.cla_case_noThe error message I receive is :
    ORA-00979: not a GROUP BY expression
    00979. 00000 - "not a GROUP BY expression"
    Thanks in advance for your help!!
    Banner:
    Oracle Database 11g Release 11.2.0.2.0 - 64bit Production
    PL/SQL Release 11.2.0.2.0 - Production
    "CORE 11.2.0.2.0 Production"
    TNS for Linux: Version 11.2.0.2.0 - Production
    NLSRTL Version 11.2.0.2.0 - Production

    You would need DECLINE in the group by clause of your inner query.
    Additionally you would need to change TOTAL_COST to SUM(TOTAL_COST) in where case statement.
    But then you would retrieve one row for each value of DECLINE in this query.
    You also could use this:
    (I don't know if it will meet your requirements)
    SELECT ff.cla_case_no,
                     cost_before_decision,
                     cost_of_claim,
                     ff.decline,
                     decline_description
      FROM fraud_nov_14_final ff,
                     (SELECT   cla_case_no,
                               SUM (decode(decline,1,total_cost_adj_old+decline_estimate,
                                                   2,total_cost_adj_old+decline_estimate,
                                                   total_cost))  cost_before_decision
                                    FROM reporting.ci_final@test
                      GROUP BY cla_case_no) z
    WHERE ff.cla_case_no = z.cla_case_no;(untested code, because I don't have your tables)
    Edited by: hm on 23.11.2011 06:52

  • 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                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

  • Case statement and group by

    Hi,
    When I try to group by a case statement , i get not "ORA-00979 not a group by emxpression"
    select
    CASE WHEN I.transaction_cd IN ('PAY0','PAY1') THEN
              CASE When I.acct_cd In (Select 3201040 from dual) Then 'SP'
    Else 'FYP'
    End
              END x
    FROM Pol_daily I
    group by
    CASE WHEN I.txttransaction_cd IN ('PAY0','PAY1') THEN
              CASE When I.txtacct_cd In (Select 3201040 from dual) Then 'SP'
    Else 'FYP'
    End
              END
    Can someone help me please.

    But result will be different..
    SQL> select case when sal <1000 then '1' else '2' end c1, sum(sal) sl
      2  from emp
      3  group by case when sal <1000 then '1' else '2' end ;
    C         SL
    1       1750
    2      29275
    SQL> ed
    Wrote file afiedt.buf
      1  select case when sal <1000 then '1' else '2' end c1, sum(sal) sl
      2  from emp
      3* group by sal
    SQL> /
    C         SL
    2       2450
    2       5000
    2       1300
    2       2500
    2       2850
    2       2975
    2       1100
    2       2000
    2       6000
    1        800
    2       1600
    C         SL
    2       1500
    1        950
    13 rows selected.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       

  • 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

  • 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 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 

  • 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

  • Update Statement with group by query

    I need to an update statement that updates the records return from this query:
    SELECT
    GROUP,
    SECTION,
    MAX(ASSIGNED_DATE)
    FROM
    ASSIGNMENTS
    GROUP BY GROUP, SECTION
    I need to update all those records and set HISTORY = 1
    I'm not sure how to form the update statement.
    UPDATE ASSIGNMENTS SET HISTORY = 1 WHERE (SELECT
    GROUP,
    SECTION,
    MAX(ASSIGNED_DATE)
    FROM
    ASSIGNMENTS
    GROUP BY GROUP, SECTION) ?
    Edited by: user9179751 on Apr 18, 2011 11:23 AM

    Just doing a select query: (these are the records that were updated when I ran the update statement you posted)
    SELECT * FROM ASSIGNMENS WHERE (GROUP, SECTION, ASSIGNED_DATE) IN (SELECT GROUP,
    SECTION, MAX(ASSIGNED_DATE)FROM ASSIGNMENTS GROUP BY GROUP, SECTION)
    Records:
    GROUP SECTION    ASSIGNED_DATE
    0010        050098       20011108
    0334    000003       19980116
    0505        320000       20000712
    0676        380000       19980914
    0676        067931       20011221
    0801        600500       19910409
    0801    601701       20090903These are the records I want updated:
    Which I would get using:
    SELECT GROUP, SECTION, MAX(ASSIGNED_DATE)FROM ASSIGNMENTS GROUP BY GROUP, SECTION
    GROUP SECTION ASSIGNED_DATE
    0002                  20100519
    0003                  20090724
    0005          
    0007       360000     
    0007                  20040913
    0009                  19970814
    0010       050098     20011108
    0010                  20000926
    0011          I'm assuming it skips over null values?

Maybe you are looking for

  • Ipod causes Windows Vista to stutter

    Hi, I have recently upgraded to Vista and when i plug my ipod in, it causes my system to slow to a crawl and stutter, mouse moves really slowly, things take ages to action. I had this problem when i installed my USB hard drive, but that seemed to cur

  • I can't begin to describe this, firefox opens to websites and I don't know why

    PLEASE HELP I never had issues with this browser which I use almost exclusively. Now over the past week, I am having nothing but. I have windows 7 pro 64 bit. Once again, I uninstalled and reinstalled firefox after it seemed to be going haywire. I se

  • Pl/sql and oracle portal session

    Hi all i use portal      v.10.1.2.2.0 and i would like to play with a session variable. all i do is grant execute on wwsto_api_session to myportal from portal schema. and then i want to do l_store := portal.wwsto_api_session.load_session (p_domain, p

  • IChat camera issues

    i just got a macally IceCam2 which i want to use with iChat. my problem is that i keep get the message "camera is in use by another application" however, no other applications are open. i've installed the most current updates and ugrades but still no

  • Portrait orientated Fotos are mainly white after synch with iPad2

    Hello Folks! Perhaps another one have the same problem as me: After synching my iPad2 under 10.6.8 with a MacPro 99 portrait orientated fotos from 100 are with huge white areas sometimes at the top, at the bottom or even both sides. Non of the horizo