Group by and decode function
Can I use a group by on a decode?
I am using the sql below and get an error message when I
try to use the group by on a decode.
select decode(assignment.catcodeid,
'D', 'SERVICE REQUESTS AND MAINTENANCE',
'U', 'SERVICE REQUESTS AND MAINTENANCE',
'P', 'SERVICE REQUESTS AND MAINTENANCE',
'E', 'SERVICE REQUESTS AND MAINTENANCE',
'R', 'SERVICE REQUESTS AND MAINTENANCE',
'T', 'EDUCATION',
'O', 'OVERHEAD',
'L', 'UNPAID LEAVE',
'V', 'VACATION',
'S', 'SICK PAY',
'H', 'HOLIDAY',
'C', 'COMP TIME',
'UNKNOWN ACTIVITY') "ACTIVITY",
assignment.catcodeid, assignment.esthrs,
SUM(ASSIGNMENT.ESTHRS)
FROM ASSIGNMENT
GROUP BY ACTIVITY, catcodeid, esthrs
The error is
ORA-00904: "ACTIVITY": invalid identifier
It is understood that Activity is not a field
on the table.
But, is there a way that I can group by the result of
a decode function or what other method/advice I can use to
group by the result of this.
Thanks is advance.
Hello
You just need to put the decode statement in the group by as well, you can't just use the alias...And if you just group by the catcodeid, you won't be putting all of the SERVICE REQUESTS AND MAINTENANCE rows together. But, because you are selecting the catcodeid aswell as decoding it, that will happen anyway.
HTH
Similar Messages
-
Case statement and Decode function both are not working in Select cursor.
I have tried both the Case statement and Decode function in Select cursor, but both the things are not working. On the other hand both the things work in just select statement.
See the first column in select (PAR_FLAG), I need to have this evaluated along with other fields. Can you please suggest some thing to make this work. And also I would like to
know the reason why decode is not working, I heard some where Case statement do not work with 8i.
Author : Amit Juneja
Date : 06/20/2011
Description:
Updates the Diamond MEMBER_MASTER table with the values from
INC.MEM_NJ_HN_MEMBER_XREF table.
declare
rec_cnt number(12) := 0;
commit_cnt number(4) := 0;
cursor select_cur is
Select DECODE(1,
(Select 1
from hsd_prov_contract R
where R.seq_prov_id = PM.seq_prov_id
and R.line_of_business = H.line_of_business
and R.PCP_FLAG = 'Y'
and R.participation_flag = 'P'
and SYSDATE between R.EFFECTIVE_DATE AND
NVL(R.TERM_DATE,
TO_DATE('31-DEC-9999', 'DD-MON-YYYY'))),
'Y',
'N') PAR_FLAG,
H.SEQ_ELIG_HIST,
H.SEQ_MEMB_ID,
H.SEQ_SUBS_ID,
H.SUBSCRIBER_ID,
H.PERSON_NUMBER,
H.EFFECTIVE_DATE,
H.TERM_DATE,
H.TERM_REASON,
H.RELATIONSHIP_CODE,
H.SEQ_GROUP_ID,
H.PLAN_CODE,
H.LINE_OF_BUSINESS,
H.RIDER_CODE_1,
H.RIDER_CODE_2,
H.RIDER_CODE_3,
H.RIDER_CODE_4,
H.RIDER_CODE_5,
H.RIDER_CODE_6,
H.RIDER_CODE_7,
H.RIDER_CODE_8,
H.MEDICARE_STATUS_FLG,
H.OTHER_STATUS_FLAG,
H.HIRE_DATE,
H.ELIG_STATUS,
H.PREM_OVERRIDE_STEP,
H.PREM_OVERRIDE_AMT,
H.PREM_OVERRIDE_CODE,
H.SEQ_PROV_ID,
H.IPA_ID,
H.PANEL_ID,
H.SEQ_PROV_2_ID,
H.SECURITY_CODE,
H.INSERT_DATETIME,
H.INSERT_USER,
H.INSERT_PROCESS,
H.UPDATE_DATETIME,
H.UPDATE_USER,
H.UPDATE_PROCESS,
H.USER_DEFINED_1,
H.SALARY,
H.PEC_END_DATE,
H.REASON_CODE,
H.PEC_WAIVED,
H.BILL_EFFECTIVE_FROM_DATE,
H.BILLED_THRU_DATE,
H.PAID_THRU_DATE,
H.SUBSC_DEPT,
H.SUBSC_LOCATION,
H.USE_EFT_FLG,
H.BENEFIT_START_DATE,
H.SEQ_ENROLLMENT_RULE,
H.MCARE_RISK_ACCRETION_DATE,
H.MCARE_RISK_DELETION_DATE,
H.MCARE_RISK_REFUSED_DATE,
H.COMMENTS,
H.USER_DEFINED_2,
H.USER_DEFINED_3,
H.RATE_TYPE,
H.PCPAA_OCCURRED,
H.PRIVACY_ON,
H.PCP_CHANGE_REASON,
H.SITE_CODE,
H.SEQ_SITE_ADDRESS_ID,
PM.seq_prov_id rendered_prov
from hsd_member_elig_history H,
INC.PCP_REASSIGN_RPRT_DATA P,
hsd_prov_master PM
where P.subscriber_id = H.subscriber_id
and P.rendered_pcp = PM.provider_ID
and H.elig_status = 'Y'
and (H.term_date is NULL or H.term_date >= last_day(sysdate))
order by H.Seq_memb_id;
begin
for C in select_cur loop
rec_cnt := rec_cnt + 1;
update hsd_member_elig_history
set term_date = TRUNC(SYSDATE - 1),
term_reason = 'PCPTR',
update_datetime = SYSDATE,
update_user = USER,
update_process = 'TD33615'
where seq_elig_hist = C.seq_elig_hist
and seq_memb_id = C.seq_memb_id;
INSERT INTO HSD_MEMBER_ELIG_HISTORY
(SEQ_ELIG_HIST,
SEQ_MEMB_ID,
SEQ_SUBS_ID,
SUBSCRIBER_ID,
PERSON_NUMBER,
EFFECTIVE_DATE,
TERM_DATE,
TERM_REASON,
RELATIONSHIP_CODE,
SEQ_GROUP_ID,
PLAN_CODE,
LINE_OF_BUSINESS,
RIDER_CODE_1,
RIDER_CODE_2,
RIDER_CODE_3,
RIDER_CODE_4,
RIDER_CODE_5,
RIDER_CODE_6,
RIDER_CODE_7,
RIDER_CODE_8,
MEDICARE_STATUS_FLG,
OTHER_STATUS_FLAG,
HIRE_DATE,
ELIG_STATUS,
PREM_OVERRIDE_STEP,
PREM_OVERRIDE_AMT,
PREM_OVERRIDE_CODE,
SEQ_PROV_ID,
IPA_ID,
PANEL_ID,
SEQ_PROV_2_ID,
SECURITY_CODE,
INSERT_DATETIME,
INSERT_USER,
INSERT_PROCESS,
UPDATE_DATETIME,
UPDATE_USER,
UPDATE_PROCESS,
USER_DEFINED_1,
SALARY,
PEC_END_DATE,
REASON_CODE,
PEC_WAIVED,
BILL_EFFECTIVE_FROM_DATE,
BILLED_THRU_DATE,
PAID_THRU_DATE,
SUBSC_DEPT,
SUBSC_LOCATION,
USE_EFT_FLG,
BENEFIT_START_DATE,
SEQ_ENROLLMENT_RULE,
MCARE_RISK_ACCRETION_DATE,
MCARE_RISK_DELETION_DATE,
MCARE_RISK_REFUSED_DATE,
COMMENTS,
USER_DEFINED_2,
USER_DEFINED_3,
RATE_TYPE,
PCPAA_OCCURRED,
PRIVACY_ON,
PCP_CHANGE_REASON,
SITE_CODE,
SEQ_SITE_ADDRESS_ID)
values
(hsd_seq_elig_hist.nextval,
C.SEQ_MEMB_ID,
C.SEQ_SUBS_ID,
C.SUBSCRIBER_ID,
C.PERSON_NUMBER,
trunc(SYSDATE),
C.TERM_DATE,
C.TERM_REASON,
C.RELATIONSHIP_CODE,
C.SEQ_GROUP_ID,
C.PLAN_CODE,
C.LINE_OF_BUSINESS,
C.RIDER_CODE_1,
C.RIDER_CODE_2,
C.RIDER_CODE_3,
C.RIDER_CODE_4,
C.RIDER_CODE_5,
C.RIDER_CODE_6,
C.RIDER_CODE_7,
C.RIDER_CODE_8,
C.MEDICARE_STATUS_FLG,
C.OTHER_STATUS_FLAG,
C.HIRE_DATE,
C.ELIG_STATUS,
C.PREM_OVERRIDE_STEP,
C.PREM_OVERRIDE_AMT,
C.PREM_OVERRIDE_CODE,
C.SEQ_PROV_ID,
C.IPA_ID,
C.PANEL_ID,
C.SEQ_PROV_2_ID,
C.SECURITY_CODE,
SYSDATE,
USER,
'TD33615',
SYSDATE,
USER,
'TD33615',
C.USER_DEFINED_1,
C.SALARY,
C.PEC_END_DATE,
C.REASON_CODE,
C.PEC_WAIVED,
C.BILL_EFFECTIVE_FROM_DATE,
C.BILLED_THRU_DATE,
C.PAID_THRU_DATE,
C.SUBSC_DEPT,
C.SUBSC_LOCATION,
C.USE_EFT_FLG,
C.BENEFIT_START_DATE,
C.SEQ_ENROLLMENT_RULE,
C.MCARE_RISK_ACCRETION_DATE,
C.MCARE_RISK_DELETION_DATE,
C.MCARE_RISK_REFUSED_DATE,
C.COMMENTS,
C.USER_DEFINED_2,
C.USER_DEFINED_3,
C.RATE_TYPE,
C.PCPAA_OCCURRED,
C.PRIVACY_ON,
C.PCP_CHANGE_REASON,
C.SITE_CODE,
C.SEQ_SITE_ADDRESS_ID);
commit_cnt := commit_cnt + 1;
if (commit_cnt = 1000) then
dbms_output.put_line('Committed updates for 1000 records.');
commit;
commit_cnt := 0;
end if;
end loop;
commit;
dbms_output.put_line('Total number of MEMBER_ELIG_HISTROY records inserted : ' ||
rec_cnt);
exception
when others then
raise_application_error(-20001,
'An error was encountered - ' || sqlcode ||
' -error- ' || sqlerrm);
end;user10305724 wrote:
I have tried both the Case statement and Decode function in Select cursor, but both the things are not working. Please define what you mean by not working even if your computer screen is near the internet we can't see it.
You should also look at the FAQ about how to ask a question
SQL and PL/SQL FAQ
Particularly *9) Formatting with {noformat}{noformat} Tags* and posting your version.
know the reason why decode is not working, I heard some where Case statement do not work with 8i.
Does this mean you are using 8i? Then scalar sub queries - selects within the select list, are not supported, along with CASE in PL/SQL.
Select DECODE(1,
* (Select 1
from hsd_prov_contract R
where R.seq_prov_id = PM.seq_prov_id
and R.line_of_business = H.line_of_business
and R.PCP_FLAG = 'Y'
and R.participation_flag = 'P'
and SYSDATE between R.EFFECTIVE_DATE AND
NVL(R.TERM_DATE,
TO_DATE('31-DEC-9999', 'DD-MON-YYYY')))*,
'Y',
'N') PAR_FLAG,
>
exception
when others then
raise_application_error(-20001,
'An error was encountered - ' || sqlcode ||
' -error- ' || sqlerrm);
http://tkyte.blogspot.com/2008/01/why-do-people-do-this.html -
SQL Loader : Trim and Decode functions help please
Hi,
I have to load data from a flat file, for some columns i need to use TRIM and DECODE functions.It is a pipe delimited file.
I get syntax errors (one is below) same error listed for TRIM.
SQL*Loader-350: Syntax error at line xx.
Expecting "," or ")", found "DECODE".
===========
,FINAL_BILL_DATE CHAR(30) "TRIM(:FINAL_BILL_DATE)"
,BUSINESS_ID "DECODE(:BUSINESS_ID,'B',1,'C',2,'E',3,'G',4,'O',5,'R',6,'T',7,'U',8,'H',9,-1)"
Can anyone please help.
Thanks
CherrishHello Cherrish.
The error you are receiving leads me to believe that at some point prior to the DECODE on the line for BUSINESS_ID, probably some line even before the FINAL_BILL_DATE line, there a syntactical error causing the quotes before the DECODE to actually terminate some other syntax. Without all of the lines that could actually contribute to this, including the header details, this is the best I can advise.
Hope this helps,
Luke
Please mark the answer as helpful or answered if it is so. If not, provide additional details.
Always try to provide create table and insert table statements to help the forum members help you better. -
SQL*Loader and DECODE function
Hi All,
I am loading data from data files into oracle tables and while loading the data using SQL*Loader, the following requirement needs to be fulfilled.
1) If OQPR < 300, RB = $ 0-299, SC = "SC1"
2) If 300 < OQPR < 1200, RB = $ 300-1199, SC = "SC2"
3) If 1200 < OQPR < 3000, RB = $ 1200-2999, SC = "SC3"
4) If OQPR > 3000 USD, RB = > $3000, SC = "SC4"
Here OPQR is a field in the data file.
Can anyone suggest how do we handle this using DECODE function? Triggers and PL/SQL functions are not to be used.
TIA.
Regards,
Ravi.The following expression gives you different values for your different intervals and boundaries :
SIGN(:OQPR - 300) + SIGN(:OQPR - 1200) + SIGN(:OQPR - 3000) -
Regarding case statement and decode function
Hi Experts,
I have question.....regarding case statement and decode statement....
Can you please explain me that which one will be efficient,to place in insert statement...
insert statement(
(case when ........then
case when ....then
else
end)
else
end)
or
insert statement(
case when.....then
decode(....)
else
end)
Can you people explain me which one is more efficient method?
Thanks in advance.......The are major differences to talk about in case of CASE vs DECODE, but performance wise both are pretty much the same.
Have a look at Tom's thread
Ask Tom &quot;better performance - case or decode&quot;
I would suggest to use CASE whenever possible. Don't worry about the performance part. -
How to "group by" and use functions on grouped records
I group rows and wantto select the max date row in date column. The other columns can include same data in its rows, thus i wantto select distinct data in the rows. I use the following query, but ir throws the message "ORA-00936: missing expression."
select max(t2.tarih), distinct (t1.ihlkodu),distinct(t1.ihlaltktgkodu), distinct(t1.kuladi),distinct(t4.ad) from ihale_katilimcilar t1, ihale_hareket t2, ihale_sirket t4
where t1.id_iliski=t2.id_iliski and t1.kuladi=t4.kullaniciadi and t1.ihlkodu=145 and t1.ihlaltktgkodu=135 and t1.aktif=1
group by t2.tarih, t1.ihlkodu,t1.ihlaltktgkodu,t1.kuladi,t4.ad
Here t2.tarih is a field of date and i wantto select the max of this.
t1.ihlkodu has all the same data in its rows.
t1.ihlaltktgkodu too has all the same data in its rows.
t1.kuladi may have ,let me say, 5 rows of same data and 6 rows of another same data in this 6 but different then the first 5 rows of same data. That is, the content of the 6 and 5 is the same in 6 and 5 but different then each other.
and finally[b] t4.ad too may have ,let me say, 5 rows of same data and 6 rows of another same data in this 6 but different then the first 5 rows of same data.(That is, the content of the 6 and 5 is the same in 6 and 5 but different then each other)
How could i group them?Here is the script of tables:
-- Create table
create table IHALE_SIRKET
SRKTKODU NUMBER not null,
AD VARCHAR2(30) not null,
ADRES VARCHAR2(50),
TEL1 VARCHAR2(15),
TEL2 VARCHAR2(15),
FAX VARCHAR2(15),
EMAIL VARCHAR2(20),
KARALISTE NUMBER default -1,
AKTIF NUMBER(1) default -1,
SICILNO VARCHAR2(20) not null,
VERGINO VARCHAR2(20) not null,
TICSICILGAZSAYI VARCHAR2(10) not null,
TICSICILGAZTARIH DATE not null,
ODASICILNO VARCHAR2(20),
TESCILTARIHI DATE,
SERMAYE VARCHAR2(20),
DERECE NUMBER(2),
ODAKAYITTARIHI DATE,
KULLANICIADI VARCHAR2(15)
tablespace USERS
pctfree 10
initrans 1
maxtrans 255
storage
initial 64K
minextents 1
maxextents unlimited
-- Create/Recreate primary, unique and foreign key constraints
alter table IHALE_SIRKET
add constraint CONSTRAINT_SRKTKODUPK primary key (SRKTKODU);
-- Create/Recreate indexes
create unique index IHALE_SIRKET_SRKTKODU on IHALE_SIRKET (SRKTKODU)
tablespace USERS
pctfree 10
initrans 2
maxtrans 255
storage
initial 64K
minextents 1
maxextents unlimited
-- Create table
create table IHALE_KATILIMCILAR
KULADI VARCHAR2(20),
SONUC NUMBER(1),
AKTIF NUMBER(1) default 1,
SRKTKODU VARCHAR2(10) not null,
SONBULUNMAZAMANI DATE default sysdate,
SIRA NUMBER(1) default 0,
EKSURETALEP VARCHAR2(50),
ID NUMBER(6) not null,
IHLKODU NUMBER not null,
IHLALTKTGKODU VARCHAR2(10) not null,
ID_ILISKI NUMBER
tablespace USERS
pctfree 10
initrans 1
maxtrans 255
storage
initial 64K
minextents 1
maxextents unlimited
-- Create/Recreate primary, unique and foreign key constraints
alter table IHALE_KATILIMCILAR
add constraint CONSTRAINT_IHLKTLMIDPK primary key (ID)
using index
tablespace USERS
pctfree 10
initrans 2
maxtrans 255
storage
initial 64K
minextents 1
maxextents unlimited
-- -- -- Create table
create table IHALE_HAREKET
ID_ILISKI NUMBER not null,
TEKLIF VARCHAR2(15) not null,
TARIH DATE default sysdate,
ID NUMBER not null
tablespace USERS
pctfree 10
initrans 1
maxtrans 255
storage
initial 64K
minextents 1
maxextents unlimited
-- Create/Recreate primary, unique and foreign key constraints
alter table IHALE_HAREKET
add constraint CONSTRAINT_IHLHAREKETPK primary key (ID)
using index
tablespace USERS
pctfree 10
initrans 2
maxtrans 255
storage
initial 64K
minextents 1
maxextents unlimited
Here is the content of those tables:
prompt Loading IHALE_HAREKET...
insert into IHALE_HAREKET (ID_ILISKI, TEKLIF, TARIH, ID)
values (189, '1400YTL', to_date('12-02-2008 14:35:07', 'dd-mm-yyyy hh24:mi:ss'), 1);
insert into IHALE_HAREKET (ID_ILISKI, TEKLIF, TARIH, ID)
values (203, '1200YTL', to_date('12-02-2008 16:03:16', 'dd-mm-yyyy hh24:mi:ss'), 2);
insert into IHALE_HAREKET (ID_ILISKI, TEKLIF, TARIH, ID)
values (203, '1100YTL', to_date('12-02-2008 16:03:16', 'dd-mm-yyyy hh24:mi:ss'), 3);
insert into IHALE_HAREKET (ID_ILISKI, TEKLIF, TARIH, ID)
values (189, '1500 YTL', to_date('12-02-2008 14:34:30', 'dd-mm-yyyy hh24:mi:ss'), 0);
prompt 4 records loaded
prompt Loading IHALE_KATILIMCILAR...
insert into IHALE_KATILIMCILAR (KULADI, SONUC, AKTIF, SRKTKODU, SONBULUNMAZAMANI, SIRA, EKSURETALEP, ID, IHLKODU, IHLALTKTGKODU, ID_ILISKI)
values (null, null, 0, '42', to_date('12-02-2008 13:33:32', 'dd-mm-yyyy hh24:mi:ss'), 0, null, 63, 141, '134', 191);
insert into IHALE_KATILIMCILAR (KULADI, SONUC, AKTIF, SRKTKODU, SONBULUNMAZAMANI, SIRA, EKSURETALEP, ID, IHLKODU, IHLALTKTGKODU, ID_ILISKI)
values (null, null, 0, '46', to_date('12-02-2008 13:33:32', 'dd-mm-yyyy hh24:mi:ss'), 0, null, 64, 141, '134', 192);
insert into IHALE_KATILIMCILAR (KULADI, SONUC, AKTIF, SRKTKODU, SONBULUNMAZAMANI, SIRA, EKSURETALEP, ID, IHLKODU, IHLALTKTGKODU, ID_ILISKI)
values ('KEM.ESM', null, 1, '44', to_date('12-02-2008 11:00:41', 'dd-mm-yyyy hh24:mi:ss'), 0, null, 61, 145, '135', 189);
insert into IHALE_KATILIMCILAR (KULADI, SONUC, AKTIF, SRKTKODU, SONBULUNMAZAMANI, SIRA, EKSURETALEP, ID, IHLKODU, IHLALTKTGKODU, ID_ILISKI)
values (null, null, 0, '45', to_date('12-02-2008 13:33:42', 'dd-mm-yyyy hh24:mi:ss'), 0, null, 69, 141, '133', 197);
insert into IHALE_KATILIMCILAR (KULADI, SONUC, AKTIF, SRKTKODU, SONBULUNMAZAMANI, SIRA, EKSURETALEP, ID, IHLKODU, IHLALTKTGKODU, ID_ILISKI)
values (null, null, 0, '42', to_date('12-02-2008 13:33:36', 'dd-mm-yyyy hh24:mi:ss'), 0, null, 65, 141, '132', 193);
insert into IHALE_KATILIMCILAR (KULADI, SONUC, AKTIF, SRKTKODU, SONBULUNMAZAMANI, SIRA, EKSURETALEP, ID, IHLKODU, IHLALTKTGKODU, ID_ILISKI)
values (null, null, 0, '45', to_date('12-02-2008 13:33:36', 'dd-mm-yyyy hh24:mi:ss'), 0, null, 66, 141, '132', 194);
insert into IHALE_KATILIMCILAR (KULADI, SONUC, AKTIF, SRKTKODU, SONBULUNMAZAMANI, SIRA, EKSURETALEP, ID, IHLKODU, IHLALTKTGKODU, ID_ILISKI)
values (null, null, 0, '46', to_date('12-02-2008 13:33:36', 'dd-mm-yyyy hh24:mi:ss'), 0, null, 67, 141, '132', 195);
insert into IHALE_KATILIMCILAR (KULADI, SONUC, AKTIF, SRKTKODU, SONBULUNMAZAMANI, SIRA, EKSURETALEP, ID, IHLKODU, IHLALTKTGKODU, ID_ILISKI)
values (null, null, 0, '42', to_date('12-02-2008 13:33:42', 'dd-mm-yyyy hh24:mi:ss'), 0, null, 68, 141, '133', 196);
insert into IHALE_KATILIMCILAR (KULADI, SONUC, AKTIF, SRKTKODU, SONBULUNMAZAMANI, SIRA, EKSURETALEP, ID, IHLKODU, IHLALTKTGKODU, ID_ILISKI)
values (null, null, 0, '44', to_date('12-02-2008 13:34:23', 'dd-mm-yyyy hh24:mi:ss'), 0, null, 70, 142, '252', 198);
insert into IHALE_KATILIMCILAR (KULADI, SONUC, AKTIF, SRKTKODU, SONBULUNMAZAMANI, SIRA, EKSURETALEP, ID, IHLKODU, IHLALTKTGKODU, ID_ILISKI)
values (null, null, 0, '45', to_date('12-02-2008 13:34:23', 'dd-mm-yyyy hh24:mi:ss'), 0, null, 71, 142, '252', 199);
insert into IHALE_KATILIMCILAR (KULADI, SONUC, AKTIF, SRKTKODU, SONBULUNMAZAMANI, SIRA, EKSURETALEP, ID, IHLKODU, IHLALTKTGKODU, ID_ILISKI)
values (null, null, 0, '44', to_date('12-02-2008 13:34:40', 'dd-mm-yyyy hh24:mi:ss'), 0, null, 72, 143, '253', 200);
insert into IHALE_KATILIMCILAR (KULADI, SONUC, AKTIF, SRKTKODU, SONBULUNMAZAMANI, SIRA, EKSURETALEP, ID, IHLKODU, IHLALTKTGKODU, ID_ILISKI)
values (null, null, 0, '46', to_date('12-02-2008 13:34:40', 'dd-mm-yyyy hh24:mi:ss'), 0, null, 73, 143, '253', 201);
insert into IHALE_KATILIMCILAR (KULADI, SONUC, AKTIF, SRKTKODU, SONBULUNMAZAMANI, SIRA, EKSURETALEP, ID, IHLKODU, IHLALTKTGKODU, ID_ILISKI)
values (null, null, 0, '45', to_date('12-02-2008 11:00:38', 'dd-mm-yyyy hh24:mi:ss'), 0, null, 60, 145, '136', 188);
insert into IHALE_KATILIMCILAR (KULADI, SONUC, AKTIF, SRKTKODU, SONBULUNMAZAMANI, SIRA, EKSURETALEP, ID, IHLKODU, IHLALTKTGKODU, ID_ILISKI)
values (null, null, 0, '44', to_date('12-02-2008 15:59:21', 'dd-mm-yyyy hh24:mi:ss'), 0, null, 74, 145, '136', 202);
insert into IHALE_KATILIMCILAR (KULADI, SONUC, AKTIF, SRKTKODU, SONBULUNMAZAMANI, SIRA, EKSURETALEP, ID, IHLKODU, IHLALTKTGKODU, ID_ILISKI)
values ('KE.ES', null, 1, '45', to_date('12-02-2008 15:59:25', 'dd-mm-yyyy hh24:mi:ss'), 0, null, 75, 145, '135', 203);
commit;
prompt 15 records loaded
prompt Loading IHALE_SIRKET...
insert into IHALE_SIRKET (SRKTKODU, AD, ADRES, TEL1, TEL2, FAX, EMAIL, KARALISTE, AKTIF, SICILNO, VERGINO, TICSICILGAZSAYI, TICSICILGAZTARIH, ODASICILNO, TESCILTARIHI, SERMAYE, DERECE, ODAKAYITTARIHI, KULLANICIADI)
values (42, 'KEMMM', 'asddd', '03123456444', '03123456444', '03123456444', '[email protected]', 0, 1, '325999444', '234999444', '34599944', to_date('04-02-2008', 'dd-mm-yyyy'), '23/29444', to_date('04-02-2008', 'dd-mm-yyyy'), '4.000.000.11YTL', 1, to_date('04-02-2008', 'dd-mm-yyyy'), null);
insert into IHALE_SIRKET (SRKTKODU, AD, ADRES, TEL1, TEL2, FAX, EMAIL, KARALISTE, AKTIF, SICILNO, VERGINO, TICSICILGAZSAYI, TICSICILGAZTARIH, ODASICILNO, TESCILTARIHI, SERMAYE, DERECE, ODAKAYITTARIHI, KULLANICIADI)
values (46, 'MLK', 'fgh', '456', '456', '456', '[email protected]', 0, 1, '2346', '2346', '2346', to_date('27-01-2008', 'dd-mm-yyyy'), '2346', to_date('25-01-2008', 'dd-mm-yyyy'), '2346', 1, to_date('22-01-2008', 'dd-mm-yyyy'), null);
insert into IHALE_SIRKET (SRKTKODU, AD, ADRES, TEL1, TEL2, FAX, EMAIL, KARALISTE, AKTIF, SICILNO, VERGINO, TICSICILGAZSAYI, TICSICILGAZTARIH, ODASICILNO, TESCILTARIHI, SERMAYE, DERECE, ODAKAYITTARIHI, KULLANICIADI)
values (44, 'ASD', 'asd', '123', '123', '123', '[email protected]', 0, 1, '123', '123', '123', to_date('10-01-2008', 'dd-mm-yyyy'), '123', to_date('17-01-2008', 'dd-mm-yyyy'), '123', 1, to_date('11-01-2008', 'dd-mm-yyyy'), 'KEM.ESM');
insert into IHALE_SIRKET (SRKTKODU, AD, ADRES, TEL1, TEL2, FAX, EMAIL, KARALISTE, AKTIF, SICILNO, VERGINO, TICSICILGAZSAYI, TICSICILGAZTARIH, ODASICILNO, TESCILTARIHI, SERMAYE, DERECE, ODAKAYITTARIHI, KULLANICIADI)
values (45, 'JKL', 'jkl', '234', '234', '345', '[email protected]', 0, 1, '234', '234', '234', to_date('04-01-2008', 'dd-mm-yyyy'), '234', to_date('27-01-2008', 'dd-mm-yyyy'), '234', 2, to_date('19-01-2008', 'dd-mm-yyyy'), 'KE.ES');
When i use the following sql:
select t2.teklif, t2.tarih, t1.ihlkodu, t1.ihlaltktgkodu, t1.kuladi,t4.ad from ihale_katilimcilar t1, ihale_hareket t2, ihale_sirket t4
where t1.id_iliski=t2.id_iliski and t1.kuladi=t4.kullaniciadi and t1.ihlkodu=145 and t1.ihlaltktgkodu=135 and t1.aktif=1
group by t2.teklif, t2.tarih, t1.ihlkodu,t1.ihlaltktgkodu,t1.kuladi,t4.ad
The result i get is:
1100YTL 12.02.2008 16:08:16 145 135 KE.ES JKL ***
1200YTL 12.02.2008 16:03:16 145 135 KE.ES JKL
1400YTL 12.02.2008 14:35:07 145 135 KEM.ESM ASD ***
1500 YTL 12.02.2008 14:34:30 145 135 KEM.ESM ASD
The one i wantto get is the rows of the latest date in each group:
1100YTL 12.02.2008 16:08:16 145 135 KE.ES JKL [b]***
1400YTL 12.02.2008 14:35:07 145 135 KEM.ESM ASD ***
max date -->latest date , most up to date
Message was edited by:
user611878
Message was edited by:
kem06.ora -
GROUP BY and analytical functions
Hi all,
I need your help with grouping my data.
Below you can see sample of my data (in my case I have view where data is in almost same format).
with test_data as(
select '01' as code, 'SM' as abbreviation, 1010 as groupnum, 21 as pieces, 4.13 as volume, 3.186 as avgvolume from dual
union
select '01' as code, 'SM' as abbreviation, 2010 as groupnum, 21 as pieces, 0 as volume, 3.186 as avgvolume from dual
union
select '01' as code, 'SM' as abbreviation, 3000 as groupnum, 21 as pieces, 55 as volume, 3.186 as avgvolume from dual
union
select '01' as code, 'SM' as abbreviation, 3010 as groupnum, 21 as pieces, 7.77 as volume, 3.186 as avgvolume from dual
union
select '02' as code, 'SMP' as abbreviation, 1010 as groupnum, 30 as pieces, 2.99 as volume, 0.1 as avgvolume from dual
union
select '03' as code, 'SMC' as abbreviation, 1010 as groupnum, 10 as pieces, 4.59 as volume, 0.459 as avgvolume from dual
union
select '40' as code, 'DB' as abbreviation, 1010 as groupnum, 21 as pieces, 5.28 as avgvolume, 0.251 as avgvolume from dual
select
DECODE (GROUPING (code), 1, 'report total:', code) as code,
abbreviation as abbreviation,
groupnum as pricelistgrp,
sum(pieces) as pieces,
sum(volume) as volume,
sum(avgvolume) as avgvolume
--sum(sum(distinct pieces)) over (partition by code,groupnum) as piecessum,
--sum(volume) volume,
--round(sum(volume) / 82,3) as avgvolume
from test_data
group by grouping sets((code,abbreviation,groupnum,pieces,volume,avgvolume),null)
order by 1,3;Select statement which I have written returns the output below:
CODE ABBR GRPOUP PIECES VOLUME AVGVOL
01 SM 1010 21 4.13 3.186
01 SM 2010 21 0 3.186
01 SM 3000 21 55 3.186
01 SM 3010 21 7.77 3.186
02 SMP 1010 30 2.99 0.1
03 SMC 1010 10 4.59 0.459
40 DB 1010 21 5.28 0.251
report total: 145 79.76 13.554Number of pieces and avg volume is same for same codes (01 - pieces = 21, avgvolume = 3.186 etc.)
What I need is to get output like below:
CODE ABBR GRPOUP PIECES VOLUME AVGVOL
01 SM 1010 21 4.13 3.186
01 SM 2010 21 0 3.186
01 SM 3000 21 55 3.186
01 SM 3010 21 7.77 3.186
02 SMP 1010 30 2.99 0.1
03 SMC 1010 10 4.59 0.459
40 DB 1010 21 5.28 0.251
report total: 82 79.76 0.973Where total number of pieces is computed as sum of distinct numbers of pieces for each code -> *82 = 21 + 30 + 10 +21*.
Total volume is just sum of volumes in each row -> *79.76 = 4.13+0+55+7.77+2.99+4.59+5.28*.
And Average volume is computed as total volume / total number of pieces -> *0.973 = 79.76 / 82*.
I was trying to use analytical function (sum() over (partition by)) to get desired output, but without good results.
Could anyone help me with this issue?
Thanks in advance!
Regards,
JiriHi, Jiri,
Jiri N. wrote:
Hi all,
I need your help with grouping my data.
Below you can see sample of my data (in my case I have view where data is in almost same format).I assume the view guarantees that all rows with the same code (or the same code and groupnum) will always have the same pieces and the same avgvolume.
with test_data as( ...Thanks for posting this; it's very helpful.
What I need is to get output like below:
CODE ABBR GRPOUP PIECES VOLUME AVGVOL
01 SM 1010 21 4.13 3.186
01 SM 2010 21 0 3.186
01 SM 3000 21 55 3.186
01 SM 3010 21 7.77 3.186
02 SMP 1010 30 2.99 0.1
03 SMC 1010 10 4.59 0.459
40 DB 1010 21 5.28 0.251
report total: 82 79.76 0.973
Except for the last row, you're just displaying data straight from the table (or view).
It might be easier to get the results you want uisng a UNION. One branch of the UNION would get the"report total" row, and the other branch would get all the rest.
>
Where total number of pieces is computed as sum of distinct numbers of pieces for each code -> *82 = 21 + 30 + 10 +21*.It's not just distinct numbers. In this example, two different codes have pieces=21, so the total of distinct pieces is 61 = 21 + 30 + 10.
>
Total volume is just sum of volumes in each row -> *79.76 = 4.13+0+55+7.77+2.99+4.59+5.28*.
And Average volume is computed as total volume / total number of pieces -> *0.973 = 79.76 / 82*.
I was trying to use analytical function (sum() over (partition by)) to get desired output, but without good results. I would use nested aggregate functions to do that:
SELECT code
, abbreviation
, groupnum AS pricelistgrp
, pieces
, volume
, avgvolume
FROM test_data
UNION ALL
SELECT 'report total:' AS code
, NULL AS abbreviaion
, NULL AS pricelistgrp
, SUM (MAX (pieces)) AS pieces
, SUM (SUM (volume)) AS volume
, SUM (SUM (volume))
/ SUM (MAX (pieces)) AS avgvolume
FROM test_data
GROUP BY code -- , abbreviation?
ORDER BY code
, pricelistgrp
;Output:
CODE ABB PRICELISTGRP PIECES VOLUME AVGVOLUME
01 SM 1010 21 4.13 3.186
01 SM 2010 21 0.00 3.186
01 SM 3000 21 55.00 3.186
01 SM 3010 21 7.77 3.186
02 SMP 1010 30 2.99 .100
03 SMC 1010 10 4.59 .459
40 DB 1010 21 5.28 .251
report total: 82 79.76 .973It's unclear if you want to GROUP BY just code (like I did above) or by both code and abbreviation.
Given that this data is coming from a view, it might be simpler and/or more efficient to make separate version of the view, or to replicate most of the view in a query. -
SQL query using Group by and Aggregate function
Hi All,
I need your help in writing an SQL query to achieve the following.
Scenario:
I have table with 3 Columns. There are 3 possible values for col3 - Success, Failure & Error.
Now I need a query which can give me the summary counts for distinct values of col3 for each GROUP BY of col1 and col2 values. When there are no values for col3 then it should return ZERO count.
Example Data:
Col1 Col2 Col3
abc 01 success
abc 02 success
abc 01 success
abc 01 Failure
abc 01 Error
abc 02 Failure
abc 03 Error
xyz 07 Failure
Required Output:
c1 c2 s_cnt F_cnt E_cnt (Heading)
abc 01 2 1 1
abc 02 1 1 0
abc 03 0 0 1
xyz 07 0 1 0
s_cnt = Success count; F_cnt = Failure count; E_cnt = Error count
Please note that the output should have 5 columns with col1, col2, group by (col1,col2)count(success), group by (col1,col2)count(failure), group by (col1,col2)count(error)
and where ever there are NO ROWS then it should return ZERO.
Thanks in advance.
Regards,
ShivaHi,
user13015050 wrote:
Thanks TTT. Unfortunately I cannot use this solution because I have huge data for this.T's solution is basically the same as mine. The first 23 lines just simulates your table. Since you actually have a table, you would start with T's line 24:
SELECT col1 c1, col2 c2, SUM(decode(col3, 'success', 1, 0)) s_cnt, ...
user13015050 wrote:Thanks a lot Frank. It helped me out. I just did some changes to this as below and have no issues.
SELECT col1
, col2
, COUNT ( CASE
WHEN col3 = 'SUCCESS'
THEN 1
END
) AS s_cnt
, COUNT ( CASE
WHEN col3 = 'FAILED'
THEN 1
END
) AS f_cnt
, COUNT ( CASE
WHEN col3 = 'ERROR'
THEN 1
END
) AS e_cnt
FROM t1
WHERE c2 in ('PURCHASE','REFUND')
and c4 between to_date('20091031000000','YYYYMMDDHH24MISS') AND to_date('20100131235959','YYYYMMDDHH24MISS')
GROUP BY c1, c2
ORDER BY c1, c2;
Please let me know if you see any issues in this query.It's very hard to read.
This site normally compresses spaces. Whenever you post formatted text (such as queries or results) on this site, type these 6 characters:
\(small letters only, inside curly brackets) before and after each section of formatted text, to preserve spacing.
Also, post exactly what you're using. The code above is SELECTing col1 and col2, but there's no mention of either in the GROUP BY clause, so I don't believe it's really what you're using.
Other than that, I don't see anything wrong or suspicious in the query. -
2.1 Autogenerate GROUP BY and anlaytic functions
I love the auto-generate group by feature, but I use analytic functions more frequently these days and that makes the feature a little less usable.
Would it be possible for SQLDeveloper to remove a group by that is automatically added if when the OVER clause follows an aggregate? Or have it generate after the cursor has moved to a new line, giving the auto-complete time to decide if it's an aggregate or an analytic?I love the auto-generate group by feature, but I use analytic functions more frequently these days and that makes the feature a little less usable.
Would it be possible for SQLDeveloper to remove a group by that is automatically added if when the OVER clause follows an aggregate? Or have it generate after the cursor has moved to a new line, giving the auto-complete time to decide if it's an aggregate or an analytic? -
Problem with group by and sum function
In one of the report I'm using grioup by clause on one column. However when I use this function the sum function on other column is not working.
Any idea how we can resolve this?Hi Tintin,
Follow the below steps:
1) On the order number column formula, use the max function.
2) Duplicate the revenue column in the RPD, set the aggregation to sum and level to transaction. This will give you the sum of all the orders in a particular trasaction, where as in report you will see only the max account number.
Assign points if helpful.
Thanks,
-Amith. -
Sql query usig round and decode together
can i get one sql quer for returning whole number using round and decode function
eg:
the value 47
if value=47
then
the query should return 50
if the value =44
then
the quer should return 40
pls give me sql query for above example using round and decode function
Edited by: user11368167 on Jul 9, 2009 3:35 AMA wild guess. If you want the ROUND Function to work only when your column value is 44 or 47 then you could use something like:
SQL> WITH test_tab AS
2 (SELECT 44 col_1
3 FROM DUAL
4 UNION ALL
5 SELECT 15 col_1
6 FROM DUAL
7 UNION ALL
8 SELECT 47 col_1
9 FROM DUAL
10 UNION ALL
11 SELECT 45 col_1
12 FROM DUAL)
13 -- end of test data
14 SELECT col_1,
15 DECODE (col_1,
16 44, ROUND (col_1, -1),
17 47, ROUND (col_1, -1),
18 col_1
19 ) rounded_value
20 FROM test_tab
21 /
COL_1 ROUNDED_VALUE
44 40
15 15
47 50
45 45
4 rows selected.
SQL> CASE Statement can be used as well instead of DECODE.
Good Luck,
Regards,
Jo -
Hi all,
could anyone would be able to tell me which statement is faster among decode and case and why?
thanx
Newbieuser531731 wrote:
The difference is in the syntax, and case statement is standards compliance and decode function is a user defined function.I guess you mean: CASE is an ANSI standard, while DECODE is an Oracle Specific function -
Does xMII have encoding and decoding functuinality?
hello,
does xMII have encoding and decoding functionality in xMII application level?
or xMII does not have application level encoding and decoding and users just use SSL technology or network device level encode / decode technology when they want to have data communication security?
Sincerely, Shiroh Kinoshita - SAP JapanHello (Konnichiha) Vivek,
Thanks for your answer.
I understand those database level and server level authorization functionality. What I mean is :
I mean security for data leakage during data communication between something and xMII. does xMII have any encode and decode function during communication?
Sincerely, Shiroh Kinoshita -
DECODE function to validate date value and sort the records
Hi Friends,
I am looking for some query which can give me the required output,
I need to do this using SQL query only and I have tried using the MIN() and MAX() functions it was working fine with limited data, now after inserting the last record(in the below table) which has date for start_range and end_range in(mm/dd/yyyy hh24:mi:ss) format.
Because the data type is VARCHAR2 if I am using the MIN() function it is sorting as a string value, hence the min date is incorrect. I tried using validating the value to date or non date and tried to using MIN() and MAX() functions using the DECODE function, I am getting this error "ORA-01840: input value not long enough for date format".
select table_name,
DECODE(substr(START_RANGE,3,1)||substr(START_RANGE,6,1)||substr(START_RANGE,11,1)||substr(START_RANGE,14,1)||substr(START_RANGE,17,1),'// ::',
to_char(min(to_date(start_range,'mm/dd/yyyy hh24:mi:ss')),'MM/DD/YYYY HH24:MI:SS'),min(start_range)) MIN_RUNS_START_RANGE,
DECODE(substr(END_RANGE,3,1)||substr(A.END_RANGE,6,1)||substr(END_RANGE,11,1)||substr(END_RANGE,14,1)||substr(END_RANGE,17,1),'// ::',
to_char(max(to_date(END_RANGE,'mm/dd/yyyy hh24:mi:ss')),'MM/DD/YYYY HH24:MI:SS'),max(END_RANGE)) MAX_RUNS_END_RANGE
from MY_TABLE
GROUP BY table_name,
(substr(START_RANGE,3,1)||substr(START_RANGE,6,1)||substr(START_RANGE,11,1)||substr(START_RANGE,14,1)||substr(START_RANGE,17,1)),
(substr(END_RANGE,3,1)||substr(END_RANGE,6,1)||substr(END_RANGE,11,1)||substr(END_RANGE,14,1)||substr(END_RANGE,17,1))
Can sombody please advise what is the best way I can query this data with the required output.
The following are the source table and records and the out put records using the sql query.
MY_TABLE
TABLE_NAME(VARCHAR2),START_RANGE(VARCHAR2),END_RANGE(VARCHAR2)
TABLE1,1000,10000
TABLE2,ABCD,EEEE
TABLE3,01/12/2010 00:00:00,12/31/2010 23:59:59
TABLE1,10001,20000
TABLE2,EEEF,GGGG
TABLE3,01/01/2011 00:00:00,01/31/2011 23:59:59
OUTPUT :
TABLE_NAME,MIN(START_RANGE),MAX(END_RANGE)
TABLE1,1000,20000
TABLE2,ABCD,GGGG
TABLE3,01/12/2010 00:00:00,01/31/2011 23:59:59
Thanks
Kalycsi also think this is a very bad table design ...
but if you are not able to change it, you could split the select (date and non-date data) and combine the result with UNION like this:
with t as
SELECT 'TABLE1' table_name,'1000' start_range,'10000' end_range FROM dual UNION
SELECT 'TABLE2','ABCD','EEEE' FROM dual UNION
SELECT 'TABLE3','01/12/2010 00:00:00','12/31/2010 23:59:59' FROM dual UNION
SELECT 'TABLE1','10001','20000' FROM dual UNION
SELECT 'TABLE2','EEEF','GGGG' FROM dual UNION
SELECT 'TABLE3','01/01/2011 00:00:00','01/31/2011 23:59:59' FROM dual
SELECT table_name,
TO_CHAR(MIN(TO_DATE(start_range, 'MM/DD/YYYY HH24:MI:SS')), 'MM/DD/YYYY HH24:MI:SS'),
TO_CHAR(MAX(TO_DATE(end_range, 'MM/DD/YYYY HH24:MI:SS')), 'MM/DD/YYYY HH24:MI:SS')
FROM t
WHERE start_range LIKE '%/%/%:%:%'
GROUP BY table_name
UNION
SELECT table_name,
MIN(start_range),
MAX(end_range)
FROM t
WHERE start_range NOT LIKE '%/%/%:%:%'
GROUP BY
table_name;
TABLE_ MIN_VALUE MAX_VALUE
TABLE1 1000 20000
TABLE2 ABCD GGGG
TABLE3 01/12/2010 00:00:00 01/31/2011 23:59:59 -
No entries in Define conversion group and external function module setting.
Hi all,
After active bussiness function SAP Oil & Gas, my system have a problem in SPRO.
SPRO --> IMG --> Industry solution Oil & Gas (Downstream) --> HPM --> QCI configuration --> Define conversion group and external function module setting.
Problem appear: Has no entries in there!
Please, help me! Thank you so much!
Tks & best regards,
DatHTDat,
There are few entries normally delivered by SAP and which are good for a regular implementation. It is possible that those are not copied over from the reference client. Second option is to check the BC set (if) available for this activity and pull those in. Check the SAP reference client first, most likely that has entries which you can copy over to your regular client.
Regards,
Maybe you are looking for
-
UWL Notification is not getting displayed under Notificattion TAB
Hi experts, I have a requirement to Display the notification Under "NOTIFICATION" tab in UWL. For that I followed the below steps: 1) Deployed required SDA as well as par file. 2) Created the new connector picking the Somic Connector from the drop
-
How GET Output to Excel with Oracle9i Report using OC4J
how GET Output to Excel with Oracle9i Report using OC4J. I FINISHED THE SETPS CONCIDERING THE JSP CODE NEEDED TO GET THE EXCEL OUTPUT ON LOCAL MACHINE. I NEED TO PUBLISH THIS REPORT THROUGH APPLICATION SERVER. THE QUESTION IS: 1- HOW TO START AN OC4J
-
Problem while developing Front End
Hi all I am developing GUI ( Front End ) where a user is allowed to add labels, buttons at runtime . Also he can add images to the screen at run time. He should be able to resize the size of the image at runtime. Also he should be able to resize the
-
Pages on my Mac will not open...
when opening the open file comes up but a small window with a blue scrolling bar comes up and I cannot open, close, or do anything at all. A force quit and reboot, or cutting the net hasn't helped either.
-
Hi Gurus, Can anyone tell me the PDF object name for the sales orders. Thanks KeviN Mike