Case in a Group By
Hi Friends,
Belwo is my qyery.
SELECT papf.employee_number OHR,
papf.business_group_id,
(CASE
WHEN (paaf.effective_start_date = Min("paaf.effective_start_date" From Below From condition))
THEN
TO_CHAR (
(SELECT ORIGINAL_DATE_OF_HIRE
FROM apps.per_all_people_f p1
WHERE p1.employee_number = papf.employee_number
AND p1.current_employee_flag = 'Y'),
'DD-MON-YYYY'
ELSE
TO_CHAR (MIN (paaf.effective_start_date), 'DD-MON-YYYY')
END) EFFECTIVE_START_DATE,
DECODE (TO_CHAR (MAX (paaf.effective_end_date), 'DD-MON-RRRR'),
'31-DEC-4712', NULL,
TO_CHAR (MAX (paaf.effective_end_date), 'DD-MON-RRRR')) EFFECTIVE_END_DATE,
TRIM(SUBSTR (PAAF.ASS_ATTRIBUTE21,
INSTR (PAAF.ASS_ATTRIBUTE21, '-') + 1)) PREVIOUS_COE,
TRIM(SUBSTR (PAAF.ASS_ATTRIBUTE22,
INSTR (PAAF.ASS_ATTRIBUTE22, '-') + 1)) PREVIOUS_SDO,
UPPER (paaf.ass_attribute24) PREVIOUS_PID,
pg.name BAND,
UPPER (paaf.ass_attribute23) PREVIOUS_LOCATION,
xx_ijp_get_loc (paaf.ass_attribute23) PREVIOUS_COUNTRY,
pj.name DESIGNATION,
UPPER (paaf.ass_attribute25) PREVIOUS_COST_CODE
FROM apps.per_all_people_f papf,
apps.per_all_assignments_f paaf,
apps.per_grades pg,
apps.per_jobs pj
WHERE papf.person_id = paaf.person_id
AND papf.employee_number = '2130'
AND papf.current_employee_flag = 'Y'
AND paaf.grade_id = pg.grade_id
AND paaf.job_id = pj.job_id
AND ( paaf.ass_attribute21 IS NOT NULL
OR paaf.ass_attribute22 IS NOT NULL
OR paaf.ass_attribute23 IS NOT NULL
OR paaf.ass_attribute24 IS NOT NULL
OR paaf.ass_attribute25 IS NOT NULL)
AND EXISTS
(SELECT pap.EMPLOYEE_NUMBER
FROM APPS.PER_ALL_PEOPLE_F PAP
WHERE TRUNC (SYSDATE) BETWEEN PAP.EFFECTIVE_START_DATE AND PAP.EFFECTIVE_END_DATE
AND PAP.CURRENT_EMPLOYEE_FLAG = 'Y'
AND PAP.EMPLOYEE_NUMBER = PAPF.EMPLOYEE_NUMBER)
AND PAAF.EFFECTIVE_START_DATE >
NVL (
(SELECT MAX (PAAFA.EFFECTIVE_START_DATE)
EFFECTIVE_START_DATE
FROM APPS.PER_ALL_ASSIGNMENTS_F PAAFA,
APPS.PER_PERIODS_OF_SERVICE PPOSA
WHERE PAAFA.PERIOD_OF_SERVICE_ID =
PPOSA.PERIOD_OF_SERVICE_ID
AND (UPPER (PPOSA.LEAVING_REASON) NOT LIKE
'%TRANSFER%')
AND PPOSA.ACTUAL_TERMINATION_DATE < SYSDATE
AND PAAFA.PERSON_ID IN
(SELECT DISTINCT PERSON_ID
FROM apps.per_all_people_f
WHERE employee_number =
PAPF.EMPLOYEE_NUMBER)),
TO_DATE ('01-JAN-1881')
GROUP BY papf.employee_number,
papf.person_id,
paaf.assignment_id,
fnd_date.canonical_to_date (papf.ATTRIBUTE29),
papf.business_group_id,
TRIM(SUBSTR (PAAF.ASS_ATTRIBUTE21,
INSTR (PAAF.ASS_ATTRIBUTE21, '-') + 1)),
TRIM(SUBSTR (PAAF.ASS_ATTRIBUTE22,
INSTR (PAAF.ASS_ATTRIBUTE22, '-') + 1)),
UPPER (paaf.ass_attribute23),
UPPER (paaf.ass_attribute24),
UPPER (paaf.ass_attribute25),
xx_ijp_get_loc (paaf.ass_attribute23),
pg.name,
pj.name
ORDER BY papf.employee_number, MIN (paaf.effective_start_date) DESC
The Problem i am facing is in Case.
(CASE
WHEN (paaf.effective_start_date = Min("paaf.effective_start_date" From Below From condition))
THEN
TO_CHAR (
(SELECT ORIGINAL_DATE_OF_HIRE
FROM apps.per_all_people_f p1
WHERE p1.employee_number = papf.employee_number
AND p1.current_employee_flag = 'Y'),
'DD-MON-YYYY'
ELSE
TO_CHAR (MIN (paaf.effective_start_date), 'DD-MON-YYYY')
END) EFFECTIVE_START_DATE,
I am not able to use teh below conditions of where clause to get the Desired out put which my qusey is giving me as output so i can capture min of the Effective_start_date because my where condition is having group by condition.
Please suggest any alternate or any solution.
Thanks
Bachan
Edited by: Bachan on Oct 14, 2010 3:08 PM
Edited by: Bachan on Oct 14, 2010 3:08 PM
SELECT papf.employee_number OHR,
papf.business_group_id,
(CASE
WHEN (paaf.effective_start_date = Min("paaf.effective_start_date" From Below Where condition))
THEN
TO_CHAR (
(SELECT ORIGINAL_DATE_OF_HIRE
FROM apps.per_all_people_f p1
WHERE p1.employee_number = papf.employee_number
AND p1.current_employee_flag = 'Y'),
'DD-MON-YYYY'
ELSE
TO_CHAR (MIN (paaf.effective_start_date), 'DD-MON-YYYY')
END) EFFECTIVE_START_DATE,
--paaf.effective_start_date,
DECODE (TO_CHAR (MAX (paaf.effective_end_date), 'DD-MON-RRRR'),
'31-DEC-4712', NULL,
TO_CHAR (MAX (paaf.effective_end_date), 'DD-MON-RRRR')) EFFECTIVE_END_DATE,
TRIM(SUBSTR (PAAF.ASS_ATTRIBUTE21,
INSTR (PAAF.ASS_ATTRIBUTE21, '-') + 1)) PREVIOUS_COE,
TRIM(SUBSTR (PAAF.ASS_ATTRIBUTE22,
INSTR (PAAF.ASS_ATTRIBUTE22, '-') + 1)) PREVIOUS_SDO,
UPPER (paaf.ass_attribute24) PREVIOUS_PID,
pg.name BAND,
UPPER (paaf.ass_attribute23) PREVIOUS_LOCATION,
xx_ijp_get_loc (paaf.ass_attribute23) PREVIOUS_COUNTRY,
pj.name DESIGNATION,
UPPER (paaf.ass_attribute25) PREVIOUS_COST_CODE
FROM apps.per_all_people_f papf,
apps.per_all_assignments_f paaf,
apps.per_grades pg,
apps.per_jobs pj
WHERE papf.person_id = paaf.person_id
AND papf.employee_number = '2130'
AND papf.current_employee_flag = 'Y'
AND paaf.grade_id = pg.grade_id
AND paaf.job_id = pj.job_id
AND ( paaf.ass_attribute21 IS NOT NULL
OR paaf.ass_attribute22 IS NOT NULL
OR paaf.ass_attribute23 IS NOT NULL
OR paaf.ass_attribute24 IS NOT NULL
OR paaf.ass_attribute25 IS NOT NULL)
AND EXISTS
(SELECT pap.EMPLOYEE_NUMBER
FROM APPS.PER_ALL_PEOPLE_F PAP
WHERE TRUNC (SYSDATE) BETWEEN PAP.EFFECTIVE_START_DATE AND PAP.EFFECTIVE_END_DATE
AND PAP.CURRENT_EMPLOYEE_FLAG = 'Y'
AND PAP.EMPLOYEE_NUMBER = PAPF.EMPLOYEE_NUMBER)
AND PAAF.EFFECTIVE_START_DATE >
NVL (
(SELECT MAX (PAAFA.EFFECTIVE_START_DATE)
EFFECTIVE_START_DATE
FROM APPS.PER_ALL_ASSIGNMENTS_F PAAFA,
APPS.PER_PERIODS_OF_SERVICE PPOSA
WHERE PAAFA.PERIOD_OF_SERVICE_ID =
PPOSA.PERIOD_OF_SERVICE_ID
AND (UPPER (PPOSA.LEAVING_REASON) NOT LIKE
'%TRANSFER%')
AND PPOSA.ACTUAL_TERMINATION_DATE < SYSDATE
AND PAAFA.PERSON_ID IN
(SELECT DISTINCT PERSON_ID
FROM apps.per_all_people_f
WHERE employee_number =
PAPF.EMPLOYEE_NUMBER)),
TO_DATE ('01-JAN-1881')
GROUP BY papf.employee_number,
papf.person_id,
paaf.assignment_id,
fnd_date.canonical_to_date (papf.ATTRIBUTE29),
papf.business_group_id,
TRIM(SUBSTR (PAAF.ASS_ATTRIBUTE21,
INSTR (PAAF.ASS_ATTRIBUTE21, '-') + 1)),
TRIM(SUBSTR (PAAF.ASS_ATTRIBUTE22,
INSTR (PAAF.ASS_ATTRIBUTE22, '-') + 1)),
UPPER (paaf.ass_attribute23),
UPPER (paaf.ass_attribute24),
UPPER (paaf.ass_attribute25),
xx_ijp_get_loc (paaf.ass_attribute23),
pg.name,
pj.name
ORDER BY papf.employee_number, MIN (paaf.effective_start_date) DESCHope it will help you to understand the code.
Bachan
Similar Messages
-
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 -
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. -
I Need To Group By Case having sub query is it possible
select
segment3,
( case when segment3 like '210%' and segment4 in ( select child_m from apps.XXNBE_PAR_PROD) then (select parent_m from apps.XXNBE_PAR_PROD where segment4=child_m) else ' ' end ) AS PC,
sum(br_egp) IN_BR_EGP
from apps.XXNBE_FIN_R7 R7
group by
segment3
,( case when segment3 like '210%' and segment4 in ( select child_m from apps.XXNBE_PAR_PROD) then (select parent_m from apps.XXNBE_PAR_PROD where segment4=child_m) else ' ' end );
It's Returne The Error subquery expressions not allowed hereYou cant use sub-queries in group by. Try this
select a.segment3,
case when a.segment3 like '210%' and
b.child_m is not null
then b.parent_m
else ' '
end as pc
from app.XXNBE_FIN_R7 a
left
join apps.XXNBE_PAR_PROD b
on a.segement4 = b.child_m
group
by a.segment3,
case when a.segment3 like '210%' and
b.child_m is not null
then b.parent_m
else ' '
end -
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 -
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 -
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 -
Currency tranlation in case of Multiple group currency
From SAP help:
You want to report in two currencies on the subgroup level: You disclose the subgroup result in the first group currency while reporting the subgroup result to corporate headquarters in their group currency
My scenario:
I have subgroups B,C and top consolidation group as A
Subgroups B, C consolidate in EUR and Group A in USD.
1)
When i want to run my consolidation for sub groups -
All i have to do is go and change the group currency in my Permanent parameters.
Is this correct?
Also - can i use a copy function in this case (i.e. without creating any versions)?
(System does not allow me to specify a source and target grp currency without specifying the source and target versions)
2)
If I want to achieve this by multiple versions
I configure -3 consolidation versions - 001, 002 n 003
Make consolidation unit hierarchy as version dependent (define and assign Spl versions)
In my versions 002, 003 i maintain only the consolidation subgroups B n C respectively.
Create a Copy function which involves-
Three Copy methods X, Y n Z;
One copy Task with method assignment dependent on Company
Assign methods accordingly to each company.
Is this the correct way to do it or is there an alternative through special versions definition for Copy?
Do i have to worry about anything else?
Thanks in Advance!1. Yes - change grp curr in parameters and execute copy and translation - source and target versions may be the same
2. because you have chosen to use different version for each grp curr, the above mentioned copy must be for differing source and target versions
I see no reason to make hierarchy version-dependent other than to exclude group A from having both EUR and USD grp currs. However, it is perfectly okay to have this dependency.
Unless there is missing information, I see no reason for separate copy methods/tasks or for assigning copy methods based on company. -
Error when use"select case when sum() group by"
SELECT REGION_CODE
,REGION_DESC
,CASE WHEN SUM(UP_SMS_TIMES) BETWEEN T2.SMS_MIN AND T2.SMS_MAX THEN T2.SMS_LVL_CODE END
FROM PMID.TB_MID_PAR_EXT_USER_MON T1, PCDE.TB_CDE_SMS_LVL T2
WHERE DEAL_DATE='200811'
GROUP BY REGION_CODE, REGION_DESC
{color:#ff0000}error:ORA-00979: not a GROUP BY expression
{color}{color:#000000}How can i fix the error,Thanks{color}-- CREATE TABLE TB_MID_PAR_EXT_USER_MO
create table TB_MID_PAR_EXT_USER_MON
DEAL_DATE CHAR(6),
REGION_CODE CHAR(2),
REGION_DESC VARCHAR2(20),
UP_SMS_TIMES NUMBER(14) default 0,
--INSERT SAMPLE DATA TO TB_MID_PAR_EXT_USER_MO
insert into TB_MID_PAR_EXT_USER_MON(DEAL_DATE,REGION_CODE,REGION_DESC,UP_SMS_TIMES)
VALUES('200812','BJ','BEIJING'11)
insert into TB_MID_PAR_EXT_USER_MON(DEAL_DATE,REGION_CODE,REGION_DESC,UP_SMS_TIMES)
VALUES('200812','SH','SHANGHAI',7)
insert into TB_MID_PAR_EXT_USER_MON(DEAL_DATE,REGION_CODE,REGION_DESC,UP_SMS_TIMES)
VALUES('200812','SH','SHANGHAI',9)
insert into TB_MID_PAR_EXT_USER_MON(DEAL_DATE,REGION_CODE,REGION_DESC,UP_SMS_TIMES)
VALUES('200812','BJ','BEIJING',10)
insert into TB_MID_PAR_EXT_USER_MON(DEAL_DATE,REGION_CODE,REGION_DESC,UP_SMS_TIMES)
VALUES('200812','BJ','BEIJING',5)
insert into TB_MID_PAR_EXT_USER_MON(DEAL_DATE,REGION_CODE,REGION_DESC,UP_SMS_TIMES)
VALUES('200812','BJ','BEIJING',19)
---- CREATE TABLE TB_CDE_SMS_LVL
create table TB_CDE_SMS_LVL
SMS_LVL_CODE INTEGER,
SMS_LVL_DESC VARCHAR2(20),
SMS_MIN NUMBER(14,2),
SMS_MAX NUMBER(14,2)
--INSERT SAMPLE DATA TO TB_CDE_SMS_LVL
INSERT INTO TB_CDE_SMS_LVL(SMS_LVL_CODE,SMS_LVL_DESC,SMS_MIN,SMS_MAX)
VALUES(1,'0 ITEM',-1.00,0.00)
INSERT INTO TB_CDE_SMS_LVL(SMS_LVL_CODE,SMS_LVL_DESC,SMS_MIN,SMS_MAX)
VALUES(2,'0-20 ITEM',0.00,20.00)
INSERT INTO TB_CDE_SMS_LVL(SMS_LVL_CODE,SMS_LVL_DESC,SMS_MIN,SMS_MAX)
VALUES(3,'20-40 ITEM',20.00,40.00)
INSERT INTO TB_CDE_SMS_LVL(SMS_LVL_CODE,SMS_LVL_DESC,SMS_MIN,SMS_MAX)
VALUES(4,'40- ITEM',40.00,9999999.00)
--NOW,LET'S EXEC THE FOLLOWING STATEMENT
SELECT REGION_CODE,REGION_DESC
,MAX(CASE WHEN SUM(UP_SMS_TIMES) BETWEEN T2.SMS_MIN AND T2.SMS_MAX THEN T2.SMS_LVL_CODE END) AS SMS_LVL
FROM PMID.TB_MID_PAR_EXT_USER_MON T1,PCDE.TB_CDE_SMS_LVL T2
WHERE DEAL_DATE='200811' AND UP_SMS_TIMES>0
GROUP BY REGION_CODE, REGION_DESC
--MY REQUIRED RESULT IS
REGION_CODE REGION_DESC SMS_LVL
SH SHANGHAI 2
BJ BEIJING 4
Thanks you! -
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. -
SET UPDATE CASE WHEN HAVING GROUP BY all in one
I'm a little stuck with the code below that doesn't work.
Basically in my table i return 3 rows per Name and they should all have the same location too, e.g. 3 Davids and 3 USA. But sometimes I might have 3 Davids and 2 USA and 1 EU - so in this case I need to update a new column called SplitValue
and enter something like 1 or 0 depending on whether the name has 3 locations that are the same, just so we can report on it.
WITH cte AS (
SELECT name, location,
CASE WHEN [Location] < 2
THEN 'split'
ELSE 'NO Split'
END AS [SplitValue]
FROM [dbo].[Names]
GROUP BY name, location HAVING COUNT(location) < 2
UPDATE [dbo].[Names]
SET [Split] = cte.[splitvalue]You're almost there. Try this.
-- SETTING UP SOME TEST DATA TO MIMIC YOUR SOURCE DATA
DECLARE @Names TABLE (name varchar(30), location varchar(30), split varchar(10))
INSERT @Names (name, location) VALUES
('David', 'USA'), ('David', 'USA'), ('David', 'USA'),
('Bob', 'USA'), ('Bob', 'USA'), ('Bob', 'EU'),
('Jane', 'AUS'), ('Jane', 'USA'), ('Jane', 'EU'),
('Alex', 'EU'), ('Alex', 'EU'), ('Alex', 'EU')
;WITH NamesLocationGroups AS
SELECT name
,location
,CASE WHEN COUNT(*) < 2 THEN 'split'
ELSE 'NO Split'
END as splitvalue
FROM @Names
GROUP BY name
,location
UPDATE A SET A.split = B.splitvalue
FROM @Names A
JOIN NamesLocationGroups B
ON A.name = B.name
AND A.location = B.location
SELECT *
FROM @Names
Results will look like this:
name location
split
David USA
NO Split
David USA
NO Split
David USA
NO Split
Bob USA
NO Split
Bob USA
NO Split
Bob EU
split
Jane AUS
split
Jane USA
split
Jane EU
split
Alex EU
NO Split
Alex EU
NO Split
Alex EU
NO Split -
SELECT
E.ACTV_DESC AS ACTV_DESC,
E.ACTV_CDE AS ACTV_CDE,
COUNT(E.ACTV_CDE) AS CNT_ACTV_CDE,
CASE WHEN B.VET_OTHR_ELIG_CDE ='01' THEN 'NO' ELSE NULL END AS VET_NO, -- new column to be added
CASE WHEN B.VET_OTHR_ELIG_CDE IN ('02','03','04') THEN 'YES' ELSE NULL END AS VET_YES --new column to be added
FROM
ETR_RGT_PARTC A,
ETR_INT_PARTC_INTK_JS B,
ETR_SDT_PARTC_PGM_ACTV C,
ETR_VM_SM_STFFNME D,
ETR_SMT_ACTV_MSTR E
WHERE
A.PARTC_ID = B.PARTC_ID AND
B.PARTC_ID = C.PARTC_ID AND
B.APPLC_ID = C.APPLC_ID AND
B.PGM_ID = C.PGM_ID AND
C.RESP_STFF_ID = D.STFF_ID AND
C.PGM_ID = E.PGM_ID AND
C.ACTV_ID = E.ACTV_ID AND
(D.RACF_ID LIKE :P_RACF_ID||'%') AND
C.LOC_ID LIKE 'ES%' AND
(TRUNC(C.CRTE_DTE) >= :P_FROM_DATE AND
TRUNC(C.CRTE_DTE) <=:P_TO_DATE) AND
D.STFF_LOC_ID = 'ES00'||:P_LOCL_OFFC_ID
AND D.CLOS_DTE IS NULL
GROUP BY
ACTV_DESC,
ACTV_CDE
ORDER BY ACTV_CDE;
we cant group by using alias in this situation what shud i do ?
Thanks in advanceIf you want to group by using the added new columns as well, then all you need to do is - put the full CASE statements into group by clause without alias.
But if you do not want to group by your data based on the added new columns then you can use the inline view and do the group by first based on first 2 columns and then do the CASE afterwards.
Also, one error I noticed in the give query is that you are doing COUNT on the column E.ACTV_CDE which is also your group by column - E.ACTV_CDE. This means that you will always receive 1 as count for each actv_cde as you are doing the grouping on the same column. -
CASE Statement in Where Condition with Multi Valued parameter in SSRS
Hi All,
I am little confused while using CASE statement in Where condition in SSRS. Below is my scenario:
SELECT
Logic here
WHERE
Date IN (@Date)AND
(CASE
WHEN NAME LIKE 'ABC%' THEN 'GROUP1'
WHEN ID IN ('123456', '823423','74233784') THEN 'GROUP2'
WHEN ABC_ID IS NULL THEN 'GROUP3'
ELSE 'GROUP4'
END ) IN (@GROUP)
So above query uses WHERE condition with CASE statement from @GROUP parameter. I want to pass this parameter as multi- valued parameter and hence I have used CASE statement IN (@GROUP).
For @Date one dataset will pass the available and default values and
for @GROUP parameters, another dataset will pass the available and default values.
But this is not working as expected. Please suggest me where I am making mistake in the query.
Maruthu | http://sharepoint-works.blogspot.comHi Maruthu,
According to your description, I create a sample report in my local environment. It works as I expected. In your scenario, if the selected values from the Date parameter contains some of the Date field values, the selected values from the GROUP parameter
contains some of GROUPS (‘GROUP1’,’GROUP2’,’GROUP3’,’GROUP4’) and the corresponding when statement is executed , then the dataset returns the corresponding values.
In order to trouble shoot this issue, could you tell us what results are you get and what’s your desired results? If possible, you can post the sample data with sample dataset, then we can make further analysis and help you out.
Thanks,
Katherine Xiong
Katherine Xiong
TechNet Community Support -
AP Tax Calculation issue with SINGLE TAX vs TAX GROUP
Hi Gurus,
i need your help on below, please advise!!
i have to calculate ap VAT tax on AP invoice,
(Rounding = nearest, precession=2, tax calcualtion= Include tax)
if i calculate 5% is the tax rate, then the tax amount is 0.47cents,this is in case of single tax calculation.
here my requirement was i need calculate 2 Taxes(TAX A AND TAX B (Rates are 5 AND 5%)
EG:
Invoce Base amount = 10 dollars
in case of single tax = 5/105 * 10 = 0.4761 cents(this is 48cents in apps with rouning nearest and precession 2)
tax mode = Include tax
In case of tax group = Tax A and Tax B = 5 + 5 = 10%, when i calculate this in apps its showing 45cents and 45 cents as tax A and B
why this tax caluclation is different with single tax and tax group.
tax code actual amount tax amount remaining amount
single tax 10 0.4761 10 - 0.48cents = 9.52 cents
tax group 10 45+45=90 cents 9.10 cents
Please Help !!!
Thanks,
SatishHi Vineeth,
This is Kathy from BSI Support. I wanted to make sure that you understood that the TF80 Like Reciprocal flag was made available in TF90 for testing purposes only. This was meant as a tool for customers to be able to compare their TF90 results to their TF80 output, to insure a successful upgrade. This funcitonality, however, was never intended to be utilized going forward. There have been significant changes implemented in BSI TaxFactory 9.0 regarding multi-state withholding (also known as reciprocity). There is information available on our website that explains these changes. If you log onto our website, please look under the "Whats New" section for an explanation of reciprocal functionality in BSI TaxFactoryu2122 9.0
If you have specific scenarios that you need help with, please contact us and we will be happy to assist you.
Regards,
BSI Support - Kathy -
Copy members from one AD Group to another
I'm still learning powershell so be gentle please
I've got the following that works
get-adgroup -filter {name -like "RDS*"} | select SamAccountName | foreach {$_.SamAccounName} {Get-ADGroupMember $_.SamAccountName}
Which works so I attempted to use that as a base to copy group members of one group to another, which lead me to
Get-adgroupmember RDS| select SamAccountName | foreach {$_.SamAccounName} {ADD-ADGroupMember NEW_RDS $_.SamAccountName}
All this does is error.
If I do the following $users = Get-adgroupmember RDS | select SamAccountName and the just type $users I get the expected the list so I' assuming the 2nd part is where I'm going wrong
Can it be done this simply?Hi,
Yes, you can do this pretty easily. Add-ADGroupMember has a -Members parameter you can use:
Add-ADGroupMember -Identity 'New Group' -Members (Get-ADGroupMember -Identity 'Old Group' -Recursive)
The -Recursive switch is used in case the old group contains nested groups.
EDIT: Link to syntax:
http://technet.microsoft.com/en-us/library/ee617193.aspx
Don't retire TechNet! -
(Don't give up yet - 13,085+ strong and growing)
This worked perfectly thank you and even better I understand why.
Maybe you are looking for
-
How to register a FaceTime account on a Mac
I have asked a close relative of mine who has FaceTime on her MacBook to sign in on FaceTime, but to my surprise when I called her it came up with this, Is there any way of "registering"
-
Urgent : Flat File Active Sync Error
Hello, I'm trying to set up a FlatFileActiveSync for update of accounts in IDM 7.1. I want to update accounts which match on the accountId and do nothing for the others accounts which do not match. It works for accounts which match on the accountId b
-
G5/iSight Behavior Changes/Problems
Couple of things changed on my iMac. First loading cd's on to iTunes take forever and it used to start playing the disc after the first song was loaded now it doesn't play at all. if I hit play while the disc is loading it goes crazy making all kinds
-
Editing office documents online after Integrating them from Windows
Hello all, After following these instructions http://help.sap.com/saphelp_erp2005vp/helpdata/en/ed/b334ea02a2704388d1d2fc3e4298ad/content.htm to integrate our windows system into the portal, I'd like users to have the ability to edit documents onlin
-
Missing applications after upgrade to belle
i just upgrade anna to belle... but most of my games apps were lost... is it really lost or just the shortcuts were lost... i tried searching them but still cant find them...