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
Similar Messages
-
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 -
Query Tuning - using CASE statement in the WHERE clause
Hi All,
My query has been modified to use a CASE statement in the WHERE clause to consider data from certain columns based on a parameter value. This modified query is doing a full table scan and running endlessly. Please suggest what may be done to improve its performance:
Query:
SELECT LAST_DAY(TRUNC(TO_TIMESTAMP(os.requestdatetime, 'yyyymmddhh24:mi:ss.ff4'))) AS summary_date,
os.acctnum,
os.avieworigin_refid,
COUNT(1) cnt_articleview,
SUM(NVL(autocompletedterm,0)) cnt_autocompletedterm
FROM TABLE1 os
WHERE os.acctnum IS NOT NULL
AND os.avieworigin_refid IS NOT NULL
AND os.requestdatetime IS NOT NULL
AND UPPER(os.success_ind) = 'S'
AND CASE WHEN
Param_ValueToCheck = 'FULL' AND get_date_timestamp(os.requestdatetime) BETWEEN
TO_DATE('01-MAY-2011 00:00:00','DD-MON-YYYY HH24:MI:SS') AND
TO_DATE('31-MAY-2011 00:00:00','DD-MON-YYYY HH24:MI:SS')
THEN 1
WHEN
Param_ValueToCheck = 'INCR' AND os.entry_createddate BETWEEN
TO_DATE('01-MAY-2011 00:00:00','DD-MON-YYYY HH24:MI:SS') AND
TO_DATE('31-MAY-2011 00:00:00','DD-MON-YYYY HH24:MI:SS')
THEN 1
END = 1
AND CASE WHEN
Param_ValueToCheck = 'FULL' AND os.entry_CreatedDate BETWEEN
TO_DATE('01-APR-2011 00:00:00','DD-MON-YYYY HH24:MI:SS') AND
TO_DATE('07-JUN-2011 00:00:00','DD-MON-YYYY HH24:MI:SS')
THEN 1
WHEN
Param_ValueToCheck = 'INCR' THEN 1
END = 1
GROUP BY LAST_DAY(TRUNC(TO_TIMESTAMP(os.requestdatetime, 'yyyymmddhh24:mi:ss.ff4'))), os.acctnum,os.avieworigin_refid;Table Description:
(Number of rows : approx > 600,000,000)
Name Null Type
ARTICLEID NOT NULL NUMBER(20)
USERKEY NUMBER(10)
AVIEWORIGIN_REFID VARCHAR2(10)
SUCCESS_IND VARCHAR2(2)
ENTRY_CREATEDDATE DATE
CREATED_BY VARCHAR2(10)
FILENUMBER NUMBER(10)
LINENUMBER NUMBER(10)
ACCTNUM VARCHAR2(10)
AUTOCOMPLETEDTERM NUMBER(2)
REQUESTDATETIME VARCHAR2(19)Explain Plan
SQL> select * from table(dbms_xplan.display);
PLAN_TABLE_OUTPUT
Plan hash value: 2224314832
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | Pstart| Pstop |
| 0 | SELECT STATEMENT | | 590 | 33040 | 2501K (1)| 08:20:15 | | |
| 1 | HASH GROUP BY | | 590 | 33040 | 2501K (1)| 08:20:15 | | |
| 2 | PARTITION RANGE ALL| | 590 | 33040 | 2501K (1)| 08:20:15 | 1 |1048575|
|* 3 | TABLE ACCESS FULL | TABLE1 | 590 | 33040 | 2501K (1)| 08:20:15 | 1 |1048575|
PLAN_TABLE_OUTPUT
Predicate Information (identified by operation id):
3 - filter(UPPER("OS"."SUCCESS_IND")='S' AND CASE WHEN ('FULL'='FULL' AND
"OS"."ENTRY_CREATEDDATE">=TO_DATE(' 2011-04-01 00:00:00', 'syyyy-mm-dd hh24:mi:ss') AND
"OS"."ENTRY_CREATEDDATE"<=TO_DATE(' 2011-06-07 00:00:00', 'syyyy-mm-dd hh24:mi:ss')) THEN 1 WHEN
'FULL'='INCR' THEN 1 END =1 AND "OS"."REQUESTDATETIME" IS NOT NULL AND CASE WHEN ('FULL'='FULL'
AND "ODS"."GET_DATE_TIMESTAMP"("REQUESTDATETIME")>=TO_DATE(' 2011-05-01 00:00:00', 'syyyy-mm-dd
hh24:mi:ss') AND "ODS"."GET_DATE_TIMESTAMP"("REQUESTDATETIME")<=TO_DATE(' 2011-05-31 00:00:00',
'syyyy-mm-dd hh24:mi:ss')) THEN 1 WHEN ('FULL'='INCR' AND "OS"."ENTRY_CREATEDDATE">=TO_DATE('
2011-05-01 00:00:00', 'syyyy-mm-dd hh24:mi:ss') AND "OS"."ENTRY_CREATEDDATE"<=TO_DATE('
PLAN_TABLE_OUTPUT
2011-05-31 00:00:00', 'syyyy-mm-dd hh24:mi:ss')) THEN 1 END =1 AND "OS"."ACCTNUM" IS NOT NULL AND
"OS"."AVIEWORIGIN_REFID" IS NOT NULL)Edited by: Chaitanya on Jun 9, 2011 2:44 AM
Edited by: Chaitanya on Jun 9, 2011 2:47 AMHi Dom,
Modified Query:
SELECT LAST_DAY(TRUNC(TO_TIMESTAMP(os.requestdatetime, 'yyyymmddhh24:mi:ss.ff4'))) AS summary_date,
os.acctnum,
os.avieworigin_refid,
COUNT(1) cnt_articleview,
SUM(NVL(autocompletedterm,0)) cnt_autocompletedterm
FROM TABLE1 os
WHERE os.acctnum IS NOT NULL
AND os.avieworigin_refid IS NOT NULL
AND os.requestdatetime IS NOT NULL
AND UPPER(os.success_ind) = 'S'
AND (('FULL' = 'FULL'
AND (get_date_timestamp(os.requestdatetime) BETWEEN TO_DATE('01-MAY-2011 00:00:00','DD-MON-YYYY HH24:MI:SS')
AND TO_DATE('31-MAY-2011 00:00:00','DD-MON-YYYY HH24:MI:SS')
AND os.entry_CreatedDate BETWEEN TO_DATE('01-APR-2011 00:00:00','DD-MON-YYYY HH24:MI:SS')
AND TO_DATE('07-JUN-2011 00:00:00','DD-MON-YYYY HH24:MI:SS')
OR ('FULL' = 'INCR'
AND os.entry_createddate BETWEEN TO_DATE('01-MAY-2011 00:00:00','DD-MON-YYYY HH24:MI:SS')
AND TO_DATE('31-MAY-2011 00:00:00','DD-MON-YYYY HH24:MI:SS') ))
GROUP BY LAST_DAY(TRUNC(TO_TIMESTAMP(os.requestdatetime, 'yyyymmddhh24:mi:ss.ff4'))), os.acctnum,os.avieworigin_refid;Execute Plan:
PLAN_TABLE_OUTPUT
Plan hash value: 3615447714
| Id | Operation | Name | Rows | Bytes |TempSpc| Cost (%CPU)| Time | Pstart| Pstop |
| 0 | SELECT STATEMENT | | 25125 | 1374K| | 407K (1)| 01:21:36 | | |
| 1 | HASH GROUP BY | | 25125 | 1374K| 3768K| 407K (1)| 01:21:36 | | |
| 2 | PARTITION RANGE ITERATOR| | 25125 | 1374K| | 407K (1)| 01:21:32 | 29 | 31 |
|* 3 | TABLE ACCESS FULL | TABLE1 | 25125 | 1374K| | 407K (1)| 01:21:32 | 29 | 31 |
PLAN_TABLE_OUTPUT
Predicate Information (identified by operation id):
3 - filter("OS"."ENTRY_CREATEDDATE">=TO_DATE(' 2011-04-01 00:00:00', 'syyyy-mm-dd hh24:mi:ss') AND
UPPER("OS"."SUCCESS_IND")='S' AND "OS"."REQUESTDATETIME" IS NOT NULL AND
"ODS"."GET_DATE_TIMESTAMP"("REQUESTDATETIME")>=TO_DATE(' 2011-05-01 00:00:00', 'syyyy-mm-dd hh24:mi:ss') AND
"ODS"."GET_DATE_TIMESTAMP"("REQUESTDATETIME")<=TO_DATE(' 2011-05-31 00:00:00', 'syyyy-mm-dd hh24:mi:ss') AND
"OS"."ACCTNUM" IS NOT NULL AND "OS"."AVIEWORIGIN_REFID" IS NOT NULL AND "OS"."ENTRY_CREATEDDATE"<=TO_DATE('
2011-06-07 00:00:00', 'syyyy-mm-dd hh24:mi:ss'))Edited by: Chaitanya on Jun 9, 2011 4:51 AM -
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,
JoeHi,
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 -
Count(*) in select statement having group by clause
Hi
I would like to use count(*) in select statement having group by clause. say for example there is a state with a number of cities listed. I would like to get the count of cities for each state.
the sql stement is grouped by city and it is joined with 5 more tables.
ThanksI suspect you want to look into analytic functions (assuming you have a recent version of Oracle). asktom.oracle.com has numerous examples if you do a search.
Justin -
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 in order by clause
Hi,
I have written the below query which is having CASE statement in ORDER BY clause. Please let me know what mistake i have done in the query because am getting "Missing Keyword" Error.
SELECT opn_quest_id, seq_nbr
FROM opinion_question
order by case when :p=1 then
opn_quest_id,seq_nbr
else
opn_quest_id
end;
Thanks,
Santhosh.STry Ignore the following solution.
SELECT opn_quest_id, seq_nbr
FROM opinion_question
ORDER BY CASE
WHEN : p = 1 THEN opn_quest_id || seq_nbr
ELSE opn_quest_id
END;
What are the data type of the corresponding columns used in the CASE Statement? I have assumed it to be strings.
!http://www.mysmiley.net/imgs/smile/sad/sad0049.gif! My Apologies....
Regards,
Jo
Edited by: Joice John on Jul 13, 2009 3:07 AM
Wrong Solution. Corrected by Sven. -
Aggregate fuction with group by clause
Hello,
Following is assignment is given but i dont get correct output
so please i am request to all of us write code to solve my problem.
There can be multiple records for one customer in VBAK tables with different combinations.
Considering that we do not need details of each sales order,
use Aggregate functions with GROUP BY clause in SELECT to read the fields.
<garbled code removed>
Moderator Message: Please paste the relevant portions of the code
Edited by: Suhas Saha on Nov 18, 2011 1:48 PMSo if you need not want all the repeated records, then you select all the values to an Internal table,
and declare an internal table of same type and Usee COLLECT
for ex:
itab1 type <xxxx>.
wa_itba like line of itab1.
itab2 type <xxxx>. "<-This should be same type of above.
select * from ..... into table itab1.
and now...
loop at itab1 into wa_itab.
collect wa_itab1 into itab2.
endloop.
then you will get your desired result.. -
Query Tuning - using CASE statement in the WHERE clause - Duplicate Post
Duplicate Post by mistake.
Please check
Query Tuning - using CASE statement in the WHERE clause
Edited by: Chaitanya on Jun 9, 2011 2:45 AM
Edited by: Chaitanya on Jun 9, 2011 2:46 AMDuplicate Post by mistake.
Please check
Query Tuning - using CASE statement in the WHERE clause
Edited by: Chaitanya on Jun 9, 2011 2:45 AM
Edited by: Chaitanya on Jun 9, 2011 2:46 AM -
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. -
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,
GuruUse 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 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:07Hi,
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
; -
Custom IKM Knowledge Modules are not working with Group By Clause
Hi All,
I am facing an issue with custom IKM knowledge modules. Those are IKM Sql Incremental Update and IKM Sql Control Append.
My Scenario is
1. Created an interface with table on source and temporary datastore with some columns in target.
2. In the Interface on the target i defined one column as UD1 and other column as UD2 for which group by to be implemented .
3. Customized IKM Sql Incremental Update with " Group by " by making modification in my IKM Sql Incremental Update
detail step "Insert flow into I$ table" i.e., i replaced like this whereever i find this <%=odiRef.getGrpBy()%> API
Group By
<%=snpRef.getColList("","[EXPRESSION]","","","UD1")%>,
<%=snpRef.getColList("","[EXPRESSION]","","","UD2")%>
Up to UD5.
. Here in the place of [EXPRESSION] i passed column names for UD1 similarly other column name for [EXPRESSION] of UD2.
4.Made all the proper mappings and also selected the KM's needed for that interface like CKM Sql ,IKM Sql Incremental Update.
5. Executed the Interface with global context.
Error i am getting in this case is :
Caused By: java.sql.SQLSyntaxErrorException: ORA-00979: not a GROUP BY expression
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:462)
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:405)
at oracle.jdbc.driver.T4C8Oall.processError(T4C8Oall.java:931)
at oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.java:481)
at oracle.jdbc.driver.T4CTTIfun.doRPC(T4CTTIfun.java:205)
at oracle.jdbc.driver.T4C8Oall.doOALL(T4C8Oall.java:548)
at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:217)
at oracle.jdbc.driver.T4CPreparedStatement.executeForRows(T4CPreparedStatement.java:1115)
Here the columns in the select clause are there in Group By also.
I did the same scenario using IKM Sql to file append .In that case i am able to do it. But not with the above mentioned KM's. Please let me know if any one know it or tried with this, as it is
high priority for me.
Please help me out.
Thanks,
keerthiHi Keerthi,
If your are transfering data from Oracle to Oracle (I means source is oracle Db and target is also oracle DB) then use below KM's and group by will come automatically based on the key values you selected on interface target datastore
1) CKM Oracle
2) LKM Oracle to Oracle (DBLINK)
3) IKM Oracle Incremental Update (MERGE)
Hope this will helps to resolve your issue
Regards,
Phanikanth -
How to include case stmt in group by clause
Hi i have a question,
How do i include a case statement in the group by clause?
For example:
Select
(case when x.ctry is null then y.ctry else x.ctry end) as coo,
sum (x.in_amt)
from
tbl1 x,
tbl2 y
where
x.id = y.id
group by
(case when x.ctry is null then y.ctry else x.ctry end)
Assume, I have got millions of records in both the tables, then my guess is, the above query might take huge time to complete.
Any alternate method to do this?cd/ wrote:
To remove the expression from the GROUP BY clause. I didn't advocate any performance improvements, did I?No you didn't. And your advice can indeed remove the expression from the GROUP BY clause. But I'm still puzzled as to why that would be a goal in itself.
Regards,
Rob. -
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 - ProductionYou 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
Maybe you are looking for
-
Re: links to the bbc news player don't start video streams
this old thread has been archived (no replies allowed), but i think i've come up with a workaround to safari's refusal to play the proper bbc video clip on subsequent attempts...autoplay the file externally. if you use pithHelmet, you can get the scr
-
Hi Experts, i never worked on ALE/EDI/IDOC .. Please send me the links and material about ALE/EDI/IDOC. Thanks in Advance Thanks, Venkatt.
-
Hi all, I have database A and database B. I have a table in A that I want to grant insert access to B. I have a DB link(Blink) created on B. I executed the following statement with errors though. Can someone please advise. GRANT INSERT, SELECT ON A.t
-
Empty results while importing outlook contact list with PHP
Hello. Could you help me with such problem: I'm always receiving an empty result while importing contact list from my outlook mail account. I've had successfully created live app (https://account.live.com/developers/applications). I'm using REST api
-
HT1349 Transferring itunes to new computer
I have transferred my itunes playlist to my new computer ( to laptop from desk top) via memory stick and authorised my new computer. The playlist appears but when I click on a tune it says that it cannot open as it cannot locate the file. What do I