HELP FOR ABAP SELECT STATEMENT
I am writing below query and getting below current result which is four rows. i would like to have result mentioned below in expected result which is single row.. any idea how can i do that? It can be easily done in sql-plus using decode or union clause. but please suggest how to do it in abap.
select distinct
qmsm~qmnum
qmel~qmtxt
qmsm~mncod
qmsm~pster
qmsm~peter
ihpa~parvw
ihpa~parnr
from qmsm
inner join qmma
on qmsmqmnum = qmmaqmnum
inner join qmel
on qmmaqmnum = qmelqmnum
inner join ihpa
on qmelobjnr = ihpaobjnr
into table ztstnotifications
where
qmel~qmnum = '000100000166'
and qmma~material = wa_material
and qmsm~mncod in ('2','4')
and ihpa~parvw in ('1A','ZY')
order by qmsm~qmnum
qmel~qmtxt
qmsm~mncod
qmsm~pster
qmsm~peter.
current result
100000166 will it work 2 22.10.2009 31.10.2009 SP 1000688
100000166 will it work 2 22.10.2009 31.10.2009 ZY AE001
100000166 will it work 4 01.01.2010 15.01.2010 SP 1000688
100000166 will it work 4 01.01.2010 15.01.2010 ZY AE001
expected result
100000166 will it work 2 22.10.2009 31.10.2009 4 01.01.2010 15.01.2010 SP 1000688 ZY AE001
Thanks
I doubt you'll be able to do so in a straight way. I would try to use some imaginative ways, like define an addition internal table with the key fields of your query, and a "long string" field, where I'll add the returned rows.
Or something like that. if you know the max number of rows this kind of join will return, add that number times the fields needed (parvw1, parnr1, parvw2, parnr2....) and use a LOOP to populate it.
Similar Messages
-
One search help for multiple select-options in webdynpro abap
Hi,
I need a way to use one search help for multiple select-options fields. My scenario is :
I have a table for keeping different organizational units' values of different systems. I have pasted some sample data from this table at the end of this mail. On the screen I want to have 1 select-options filed for werks, and 1 select-options filed for vkorg. (In fact I will have more org. unit fields...) In the beginning of my application the user will select sid.
If the user selects ADS as SID, when he opens search-help for the first org. unit (werks), he will see the records with SID: ADS, VARBL = $WERKS, LANGU = SY-LANGU.
If the user selects AGT as SID, when he opens search-help for the second org. unit (vkorg), he will see the records with SID: AGT, VARBL = $VKORG, LANGU = SY-LANGU.
I have created a search-help taking SIDD, VARBL and LANGU as import parameters; used field mapping and bound this search help to my table. I have created 2 context nodes : org1 and org2 having attributes SID, VARBL, VALUE, LANGU .
I have assigned related SID, VARBL and Langu values to these attributes at runtime as I needed. That way, if I use input field and reference to the related context attributes org1-value and org2-value2 accordingly, search help works well as I want.
However, when I use select-options field , I can not bind the field to the context data. I can give reference only to ddic structure. Is there any way to reference to a context attribute? I searched for this in SDN, but could find nothing.
I think I won't be able to use this way. What do you say?
As I read from forums maybe using OVS help will be suitable for me. But I have to use one search-help for all select-options fields. Do you know how I can determine the active select-options field and pass its name (for instance "werks" ) as parameter to this OVS search help. (Also I'll pass SID and LANGU.)
MY TABLE (ZBYYT080) CONTENTS:
SID VARBL VALUE LANGU VTEXT
ADS $WERKS 1 T Werk 0001
ADS $WERKS 11 T OZYAS GIDA URETIM YERI
ADS $WERKS 5501 T BOYA GEBZE FABRİKASI
ADS $WERKS 5502 T BOYA CIGLI FABRİKASI
AGT $WERKS 2301 T KAMLI DAMIZLIK
AGT $WERKS 9601 T PANAR DENIZ URETIM YERI
ADS $VKORG 22 T AA KİMYASALLAR
ADS $VKORG 8001 T İINSAAT BOYALARI
AGT $VKORG 6500 T DAMk St.Org
AGT $VKORG 5400 T PANAR St.Org.
I wish I'm clear enough..
I will be gald if someone answers me as soon as possible...
Thanks İn advance..
MERALHi,
Your ques is how to refer to a DDIC search help to refer to selection screen parameter ?
Am I right ?
If Yes, then in the interface IF_WD_SELECT_OPTIONS
method ADD_SELECTION_FIELD, ADD_PARAMETER_FIELD etc
have importing param like I_VALUE_HELP_TYPE and I_VALUE_HELP_ID, I_VALUE_HELP_MODE, I_VALUE_HELP_STRUCTURE
etc which may help you to link your create DDIC Search help to selection screen params.
this is just a clue from my side. I haven't tried it myself.
You can go to the where used list of this method and find some sample implementations which use these params.
Hope this helps.
Regards
Manas Dua -
How to create a mapping for a select statement containing DENSE_RANK( )?
Hi,
I want help with a select statement that I want to make a mapping of in OWB 11.1 g. Can anyone please tell me how is code can be incorporated in a mapping?
SELECT DISTINCT MAX (dimension_key) KEEP (DENSE_RANK FIRST ORDER BY day DESC) OVER (PARTITION BY calendar_week_name),
MAX (day) KEEP (DENSE_RANK FIRST ORDER BY DAY DESC) OVER (PARTITION BY calendar_week_name), calendar_week_end_date, calendar_week_number
FROM time_dim;I have been trying to use the Aggregator operator but I am not entirely sure how to go about it. Any help will be highly appreciated.
Thanks in advance,
Ann.Hi Ann
You can just use an EXPRESSION operator. Configure the mapping to be set based only code generation and operating mode.
You will have an expression output attribute for each one of your projected columns;
MAX (dimension_key) KEEP (DENSE_RANK FIRST ORDER BY day DESC) OVER (PARTITION BY calendar_week_name),
MAX (day) KEEP (DENSE_RANK FIRST ORDER BY DAY DESC) OVER (PARTITION BY calendar_week_name),
calendar_week_end_date,
calendar_week_number
Cheers
David -
F4 Help for the selection screen field
Hi Gurus,
I have to display F4 help for a selection screen field. i am using following code:
CALL FUNCTION 'F4IF_INT_TABLE_VALUE_REQUEST'
EXPORTING
retfield = 'CODE'
dynpprog = lw_repid
dynpnr = sy-dynnr
VALUE_ORG = 'S'
TABLES
value_tab = gi_text
field_tab = li_fields_tab
return_tab = pi_return_tab
EXCEPTIONS
parameter_error = 1
no_values_found = 2
OTHERS = 3.
My internal table gi_emp has two fields CODE & TEXT. When i select 1 particular value, it returns the CODE value in the pi_return_tab table.
But my requirement is i have to capture the corresponding TEXT value for the Returned CODE. Ex: following is the F4 help being displayed: -
001 test
001 test1
but is i select code 001(First Value), i also need to capture it's corresponding Text value. In return table pi_return_tab i have only Code value.
Please help me out.
Note: Based on return code i can't search into the internal table gi_code, because as shown in the example, code has multiple text.Sachin,
I think this is what you need. The following code will return the key and the text (OR any other columns), if both columns are in the selection screen the selected values (both columns) will also be passed to the corresponding parameters. If you only need the text to be in the return table and not passed to the selection screen then set the parameter for text with NO-DISPLAY option.
REPORT zktest01 .
DATA :
BEGIN OF value_tab OCCURS 0,
field LIKE e070-trkorr,
text LIKE e07t-as4text,
END OF value_tab.
DATA: t_fldtab LIKE dfies OCCURS 0 WITH HEADER LINE,
t_rettab LIKE ddshretval OCCURS 0 WITH HEADER LINE,
t_dynmap LIKE dselc OCCURS 0 WITH HEADER LINE.
PARAMETERS : p_field LIKE value_tab-field,
p_text LIKE value_tab-text. "NO-DISPLAY.
INITIALIZATION.
t_fldtab-tabname = 'VALUE_TAB'.
t_fldtab-fieldname = 'FIELD'.
t_fldtab-langu = 'E'.
t_fldtab-position = 1.
t_fldtab-offset = 0.
t_fldtab-fieldtext = 'Key'.
t_fldtab-reptext = 'Key'.
t_fldtab-leng = 20.
t_fldtab-intlen = 20.
t_fldtab-outputlen = 20.
t_fldtab-datatype = 'CHAR'.
t_fldtab-inttype = 'C'.
t_fldtab-headlen = 20.
t_fldtab-keyflag = 'X'.
t_fldtab-lowercase = ' '.
APPEND t_fldtab.
t_fldtab-tabname = 'VALUE_TAB'.
t_fldtab-fieldname = 'TEXT'.
t_fldtab-position = 2.
t_fldtab-offset = 20.
t_fldtab-fieldtext = 'Text'.
t_fldtab-reptext = 'Text'.
t_fldtab-leng = 60.
t_fldtab-intlen = 60.
t_fldtab-outputlen = 60.
t_fldtab-headlen = 60.
t_fldtab-keyflag = ' '.
t_fldtab-lowercase = ' '.
APPEND t_fldtab.
value_tab-field = '101'.
value_tab-text = 'dddd'.
APPEND value_tab.
value_tab-field = '202'.
value_tab-text = 'aaaa'.
APPEND value_tab.
t_dynmap-fldname = 'FIELD'.
t_dynmap-dyfldname = 'P_FIELD'.
APPEND t_dynmap.
t_dynmap-fldname = 'TEXT'.
t_dynmap-dyfldname = 'P_TEXT'.
APPEND t_dynmap.
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_field.
CALL FUNCTION 'F4IF_INT_TABLE_VALUE_REQUEST'
EXPORTING
retfield = 'FIELD'
dynpprog = 'ZKTEST01'
dynpnr = '1000'
dynprofield = 'P_FIELD'
value_org = 'S'
TABLES
field_tab = t_fldtab
value_tab = value_tab
return_tab = t_rettab
dynpfld_mapping = t_dynmap
EXCEPTIONS
parameter_error = 1
no_values_found = 2
OTHERS = 3.
IF sy-subrc EQ 0.
ENDIF.
START-OF-SELECTION.
Jeffrey Satriadi -
Search help for PERNR select options in Webdynpro
Hi,
I am using the method: lv_r_helper_class->add_selection_field
and passing the value:
i_value_help_type = if_wd_value_help_handler=>CO_PREFIX_SEARCHHELP
to get search help for pernr select option in the webdynpro application.
But can anyone please tell me what value i have to pass to
i_value_help_id = ??hi,
pass your search help name there so that your desired search help ll come there..
use this,
i_value_help_type = if_wd_value_help_handler=>CO_PREFIX_SEARCHHELP
i_value_help_id = 'SEARCHHELPNAME'
if you want standard SH means comment this stmt,
if you dont want SH means,
use tis,
i_value_help_type = if_wd_value_help_handler=>CO_PREFIX_NONE
hope this helps,
Mathan R. -
Can we create serach help for a selection screen field
Can we create serach help for a selection screen field with out creating searchhelp object.
I mean is it possible to create serchelp in the program itself with some specific values i want to give.
E.g say we can define any internal table and fill it with values and using that into selection screen fields
Regards
MaveAT SELECTION-SCREEN ON VALUE-REQUEST FOR paymeth.
PERFORM PAYMENT_HELP.
FORM PAYMENT_HELP.
DATA: begin of DESCR_TAB occurs 0,
DESCR LIKE BKPF-BKTXT,
END OF DESCR_TAB.
DATA: gd_repid like sy-repid.
gd_repid = sy-repid.
DESCR_TAB-DESCR = 'aaaaa'.
append DESCR_TAB.
DESCR_TAB-DESCR = 'bbbbb'.
append DESCR_TAB.
DESCR_TAB-DESCR = 'ccccc'.
append DESCR_TAB.
CALL FUNCTION 'F4IF_INT_TABLE_VALUE_REQUEST'
EXPORTING
retfield = 'DESCR'
DYNPPROG = gd_repid
DYNPNR = '1000'
DYNPROFIELD = 'paymeth'
VALUE_ORG = 'S'
DISPLAY = ' '
TABLES
value_tab = DESCR_TAB.
endform.
Svetlin -
How to find for which select statement performance is more
hi gurus
can anyone suggest me
if we have 2 select statements than
how to find for which select statement performance is more
thanks®ards
kals.hi check this..
1 .the select statement in which the primary and secondary keys are used will gives the good performance .
2.if the select statement had select up to i row is good than the select single..
go to st05 and check the performance..
regards,
venkat -
Search help for a select options
Hi ,
I need to place a search help for a select-options field on my report selection screen. It should work for a z-table field . I can create a search help for the Z-table field , but I do not know how to make it available for the select-options.
Thank you .hi Krish,
SELECT OPTIONS ... MATCHCODE OBJECT name_of_search_help.
hope this helps
ec -
F4 HELP FOR ONLY SELECTED FIELD VALUES
Hi,
I have to display the F4 help for the selection screen field parameter. From the ICON table, I need to display the field ID values. In the ICON table, I have lot of field values for the field ID, but I need to display only 3 field values into F4 help.(like say @07@, @08@, @0A@ only at the F4 help).
Thanks in advance.
Regards
Ramesh mavilla.Hi,
See the follwoing code example
REFRESH: I_DYNPREAD .
CLEAR : I_DYNPREAD .
I_DYNPREAD-FIELDNAME = 'P_KAPPL'.
APPEND I_DYNPREAD.
CLEAR I_DYNPREAD.
L_REPID = SY-REPID .
L_DYNNR = '1000' .
CALL FUNCTION 'DYNP_VALUES_READ'
EXPORTING
DYNAME = L_REPID
DYNUMB = L_DYNNR
TABLES
DYNPFIELDS = I_DYNPREAD.
IF SY-SUBRC EQ 0.
READ TABLE I_DYNPREAD INDEX 1.
P_KAPPL = I_DYNPREAD-FIELDVALUE.
ENDIF.
REFRESH: I_DYNPREAD .
CLEAR : I_DYNPREAD .
REFRESH: I_KSCHL.
TRANSLATE P_KAPPL TO UPPER CASE.
TRANSLATE P_KVEWE TO UPPER CASE.
SELECT KAPPL KVEWE KSCHL VTEXT FROM T685T
INTO CORRESPONDING FIELDS OF TABLE I_KSCHL
WHERE KAPPL = P_KAPPL
AND KVEWE = P_KVEWE
AND SPRAS = 'EN'.
IF SY-SUBRC <> 0.
MESSAGE E001(ZZ) WITH 'No Condition Type exist for Application'.
*p_kappl ' and Usage' p_kvewe.
ELSE.
REFRESH: I_FIELDS.
I_FIELDS-TABNAME = 'T685T'.
I_FIELDS-FIELDNAME = 'KAPPL'.
I_FIELDS-SELECTFLAG = ''.
APPEND I_FIELDS.
I_FIELDS-TABNAME = 'T685T'.
I_FIELDS-FIELDNAME = 'KVEWE'.
I_FIELDS-SELECTFLAG = ''.
APPEND I_FIELDS.
I_FIELDS-TABNAME = 'T685T'.
I_FIELDS-FIELDNAME = 'KSCHL'.
I_FIELDS-SELECTFLAG = 'X'.
APPEND I_FIELDS.
I_FIELDS-TABNAME = 'T685T'.
I_FIELDS-FIELDNAME = 'VTEXT'.
I_FIELDS-SELECTFLAG = ' '.
APPEND I_FIELDS.
CLEAR: V_SEL.
CALL FUNCTION 'HELP_VALUES_GET_NO_DD_NAME'
EXPORTING
CUCOL = 0
CUROW = 0
DISPLAY = ' '
SELECTFIELD = 'KSCHL'
TITEL = 'Cond Type'
IMPORTING
IND =
SELECT_VALUE = V_SEL
TABLES
FIELDS = I_FIELDS
FULL_TABLE = I_KSCHL
IF SY-SUBRC = 0.
MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
ENDIF. "SY-SUBRC.
And also go throw the link which it clears more about F4
Check the link below for creating collective search help
http://help.sap.com/saphelp_nw04/helpdata/en/cf/21ee86446011d189700000e8322d00/content.htm
Check the ink below for creating elementary search help
http://help.sap.com/saphelp_erp2005/helpdata/en/cf/21ee5f446011d189700000e8322d00/content.htm
http://help.sap.com/saphelp_erp2005/helpdata/en/3d/e53642e2a3ab04e10000000a1550b0/content.htm
<b>Reward with points if helpful.</b>
Regards,
Vijay -
Is any HTML help for ABAP???
Is any HTML help for ABAP??? Are any HTML help downloads there in anywhere?? if anyone knows let me know.
Are you asking for help documentation on ABAP? If so....
http://help.sap.com/saphelp_nw2004s/helpdata/en/43/41341147041806e10000000a1553f6/frameset.htm
http://help.sap.com/saphelp_nw2004s/helpdata/en/ef/d94b78ebf811d295b100a0c94260a5/frameset.htm
Regards,
RIch Heilman -
ABAP select statements takes too long
Hi,
I have a select statement as shown below.
SELECT * FROM BSEG INTO TABLE ITAB_BSEG
WHERE BUKRS = CO_CODE
AND BELNR IN W_DOCNO
AND GJAHR = THISYEAR
AND AUGBL NE SPACE.
This select statement runs fine in all of R/3 systems except for 1. The problem that is shown with this particular system is that the query takes very long ( up to an hour for if W_DOCNO consists of 5 entries). Sometimes, before the query can complete, an ABAP runtime error is encountered as shown below:
<b>Database error text........: "ORA-01555: snapshot too old: rollback segment
number 7 with name "PRS_5" too small?"
Internal call code.........: "[RSQL/FTCH/BSEG ]"
Please check the entries in the system log (Transaction SM21). </b>
Please help me on this issue. However, do not give me suggestions about selecting from a smaller table (bsik, bsak) as my situation does not permit it.
I will reward points.dont use select * ....
instead u declare ur itab with the required fields and then in select refer to the fields in the select .
data : begin of itab,
f1
f2
f3
f4
end of itab.
select f1 f2 f3 f4 ..
into table itab
from bseg where ...
. this improves the performance .
select * is not advised .
regards,
vijay -
Need help for SQL SELECT query to fetch XML records from Oracle tables having CLOB field
Hello,
I have a scenario wherein i need to fetch records from several oracle tables having CLOB fields(which is holding XML) and then merge them logically to form a hierarchy XML. All these tables are related with PK-FK relationship. This XML hierarchy is having 'OP' as top-most root node and ‘DE’ as it’s bottom-most node with One-To-Many relationship. Hence, Each OP can have multiple GM, Each GM can have multiple DM and so on.
Table structures are mentioned below:
OP:
Name Null Type
OP_NBR NOT NULL NUMBER(4) (Primary Key)
OP_DESC VARCHAR2(50)
OP_PAYLOD_XML CLOB
GM:
Name Null Type
GM_NBR NOT NULL NUMBER(4) (Primary Key)
GM_DESC VARCHAR2(40)
OP_NBR NOT NULL NUMBER(4) (Foreign Key)
GM_PAYLOD_XML CLOB
DM:
Name Null Type
DM_NBR NOT NULL NUMBER(4) (Primary Key)
DM_DESC VARCHAR2(40)
GM_NBR NOT NULL NUMBER(4) (Foreign Key)
DM_PAYLOD_XML CLOB
DE:
Name Null Type
DE_NBR NOT NULL NUMBER(4) (Primary Key)
DE_DESC NOT NULL VARCHAR2(40)
DM_NBR NOT NULL NUMBER(4) (Foreign Key)
DE_PAYLOD_XML CLOB
+++++++++++++++++++++++++++++++++++++++++++++++++++++
SELECT
j.op_nbr||'||'||j.op_desc||'||'||j.op_paylod_xml AS op_paylod_xml,
i.gm_nbr||'||'||i.gm_desc||'||'||i.gm_paylod_xml AS gm_paylod_xml,
h.dm_nbr||'||'||h.dm_desc||'||'||h.dm_paylod_xml AS dm_paylod_xml,
g.de_nbr||'||'||g.de_desc||'||'||g.de_paylod_xml AS de_paylod_xml,
FROM
DE g, DM h, GM i, OP j
WHERE
h.dm_nbr = g.dm_nbr(+) and
i.gm_nbr = h.gm_nbr(+) and
j.op_nbr = i.op_nbr(+)
+++++++++++++++++++++++++++++++++++++++++++++++++++++
I am using above SQL select statement for fetching the XML records and this gives me all related xmls for each entity in a single record(OP, GM, DM. DE). Output of this SQL query is as below:
Current O/P:
<resultSet>
<Record1>
<OP_PAYLOD_XML1>
<GM_PAYLOD_XML1>
<DM_PAYLOD_XML1>
<DE_PAYLOD_XML1>
</Record1>
<Record2>
<OP_PAYLOD_XML2>
<GM_PAYLOD_XML2>
<DM_PAYLOD_XML2>
<DE_PAYLOD_XML2>
</Record2>
<RecordN>
<OP_PAYLOD_XMLN>
<GM_PAYLOD_XMLN>
<DM_PAYLOD_XMLN>
<DE_PAYLOD_XMLN>
</RecordN>
</resultSet>
Now i want to change my SQL query so that i get following output structure:
<resultSet>
<Record>
<OP_PAYLOD_XML1>
<GM_PAYLOD_XML1>
<GM_PAYLOD_XML2> .......
<GM_PAYLOD_XMLN>
<DM_PAYLOD_XML1>
<DM_PAYLOD_XML2> .......
<DM_PAYLOD_XMLN>
<DE_PAYLOD_XML1>
<DE_PAYLOD_XML2> .......
<DE_PAYLOD_XMLN>
</Record>
<Record>
<OP_PAYLOD_XML2>
<GM_PAYLOD_XML1'>
<GM_PAYLOD_XML2'> .......
<GM_PAYLOD_XMLN'>
<DM_PAYLOD_XML1'>
<DM_PAYLOD_XML2'> .......
<DM_PAYLOD_XMLN'>
<DE_PAYLOD_XML1'>
<DE_PAYLOD_XML2'> .......
<DE_PAYLOD_XMLN'>
</Record>
<resultSet>
Appreciate your help in this regard!Hi,
A few questions :
How's your first query supposed to give you an XML output like you show ?
Is there something you're not telling us?
What's the content of, for example, <OP_PAYLOD_XML1> ?
I don't think it's a good idea to embed the node level in the tag name, it would make much sense to expose that as an attribute.
What's the db version BTW? -
Can't figure out the correct syntax for this select statement
Hello,
The following statement works great and gives the desired results:
prompt
prompt Using WITH t
prompt
with t as
select a.proj_id,
a.proj_start,
a.proj_end,
case when (
select min(a.proj_start)
from v b
where (a.proj_start = b.proj_end)
and (a.proj_id != b.proj_id)
is not null then 0 else 1
end as flag
from v a
order by a.proj_start
select proj_id,
proj_start,
proj_end,
flag,
-- the following select statement is what I am having a hard time
-- "duplicating" without using the WITH clause
select sum(t2.flag)
from t t2
where t2.proj_end <= t.proj_end
) s
from t;As an academic exercise I wanted to rewrite the above statement without using the WITH clause, I tried this (among dozens of other tries - I've hit a mental block and can't figure it out):
prompt
prompt without with
prompt
select c.proj_id,
c.proj_start,
c.proj_end,
c.flag,
-- This is what I've tried as the equivalent statement but, it is
-- syntactically incorrect. What's the correct syntax for what this
-- statement is intended ?
select sum(t2.flag)
from c t2
where t2.proj_end <= c.proj_end
) as proj_grp
from (
select a.proj_id,
a.proj_start,
a.proj_end,
case when (
select min(a.proj_start)
from v b
where (a.proj_start = b.proj_end)
and (a.proj_id != b.proj_id)
is not null then 0 else 1
end as flag
from v a
order by a.proj_start
) c;Thank you for helping, much appreciated.
John.
PS: The DDL for the table v used by the above statements is:
drop table v;
create table v (
proj_id number,
proj_start date,
proj_end date
insert into v values
( 1, to_date('01-JAN-2005', 'dd-mon-yyyy'),
to_date('02-JAN-2005', 'dd-mon-yyyy'));
insert into v values
( 2, to_date('02-JAN-2005', 'dd-mon-yyyy'),
to_date('03-JAN-2005', 'dd-mon-yyyy'));
insert into v values
( 3, to_date('03-JAN-2005', 'dd-mon-yyyy'),
to_date('04-JAN-2005', 'dd-mon-yyyy'));
insert into v values
( 4, to_date('04-JAN-2005', 'dd-mon-yyyy'),
to_date('05-JAN-2005', 'dd-mon-yyyy'));
insert into v values
( 5, to_date('06-JAN-2005', 'dd-mon-yyyy'),
to_date('07-JAN-2005', 'dd-mon-yyyy'));
insert into v values
( 6, to_date('16-JAN-2005', 'dd-mon-yyyy'),
to_date('17-JAN-2005', 'dd-mon-yyyy'));
insert into v values
( 7, to_date('17-JAN-2005', 'dd-mon-yyyy'),
to_date('18-JAN-2005', 'dd-mon-yyyy'));
insert into v values
( 8, to_date('18-JAN-2005', 'dd-mon-yyyy'),
to_date('19-JAN-2005', 'dd-mon-yyyy'));
insert into v values
( 9, to_date('19-JAN-2005', 'dd-mon-yyyy'),
to_date('20-JAN-2005', 'dd-mon-yyyy'));
insert into v values
(10, to_date('21-JAN-2005', 'dd-mon-yyyy'),
to_date('22-JAN-2005', 'dd-mon-yyyy'));
insert into v values
(11, to_date('26-JAN-2005', 'dd-mon-yyyy'),
to_date('27-JAN-2005', 'dd-mon-yyyy'));
insert into v values
(12, to_date('27-JAN-2005', 'dd-mon-yyyy'),
to_date('28-JAN-2005', 'dd-mon-yyyy'));
insert into v values
(13, to_date('28-JAN-2005', 'dd-mon-yyyy'),
to_date('29-JAN-2005', 'dd-mon-yyyy'));
insert into v values
(14, to_date('29-JAN-2005', 'dd-mon-yyyy'),
to_date('30-JAN-2005', 'dd-mon-yyyy'));Hi, John,
Not that you asked, but as you proabably know, analytic functions are much better at doing this kind of thing.
You may be amazed (as I continually am) by how simple and efficient these queries can be.
For example:
WITH got_grp AS
SELECT proj_id, proj_start, proj_end
, proj_end - SUM (proj_end - proj_start) OVER (ORDER BY proj_start) AS grp
FROM v
SELECT ROW_NUMBER () OVER (ORDER BY grp) AS proj_grp
, MIN (proj_start) AS proj_start
, MAX (proj_end) AS proj_end
FROM got_grp
GROUP BY grp
ORDER BY proj_start
;Produces the results you want:
PROJ_GRP PROJ_START PROJ_END
1 01-Jan-2005 05-Jan-2005
2 06-Jan-2005 07-Jan-2005
3 16-Jan-2005 20-Jan-2005
4 21-Jan-2005 22-Jan-2005
5 26-Jan-2005 30-Jan-2005This is problem is an example of Neighbor-Defined Groups . You want to GROUP BY something that has 5 distinct values, to get the 5 rows above, but there's nothing in the table itself that tells you to which group each row belongs. The groups are not defined by any column in hte table, but by relationships between rows. In this case, a row is in the same group as its neighbor (the row immediatly before or after it when sorted by proj_start or proj_end) if proj_end of the earlier row is the same as proj_start of the later row. That is, there is nothing about 03-Jan-2005 that says the row with proj_id=2 is in the first group, or even that it is in the same group with its neighbor, the row with proj_id=3. Only the relation between those rows, the fact that the earlier row has end_date=03-Jan-2005 and the later row has start_date=03-Jan-2003, that says these neighbors belong to the same group.
You're figuring out when a new group starts, and then counting how many groups have already started to see to which group each row belongs. That's a prefectly natural procedural way of approaching the problem. But SQL is not a procedural language, and sometimes another approach is much more efficient. In this case, as in many others, a Constant Difference defines the groups. The difference between proj_end (or proj_start, it doesn't matter in this case) and the total duratiojn of the rows up to that date determines a group. The actual value of that difference means nothing to you or anybody else, so I used ROW_NUMBER in the query above to map those distinct values into consecutive integers 1, 2, 3, ... which are a much simpler way to identify the groups.
Note that the query above only requires one pass through the table, and only requires one sub-query. It does not need a WITH clause; you could easily make got_grp an in-line view.
If you used analytic functions (LEAD or LAG) to compute flag, and then to compute proj_grp (COUNT or SUM), you would need two sub-queries, one for each analytic function, but you would still only need one pass through the table. Also, those sub-queries could be in-line views; yiou would not need to use a WITH clause. -
Using BEGIN TRAN for a SELECT statement
I have inherited a number of stored procedures that are using explicit transactions and structured error handling.
I have had to review the procs due to deadlocks occurring and have found that many of them are SELECT statements.
The syntax is like this:
BEGIN TRAN
BEGIN TRY
SELECT ####
END TRY
BEGIN CATCH
IF @@TRANCOUNT >0
BEGIN
ROLLBACK TRAN
INSERT ErrorLog
XXXXXX
END CATCH
IF @@TRANCOUNT > 0
BEGIN
COMMIT TRAN
END
It is very obvious that there is no purpose for explicit transactions in the select, but I am wondering if this could be a factor in the deadlocks. From what I tested using BEGIN TRAN for a select still only uses a shared lock.
Is it possible that this is a factor in the deadlocks?
David Dye My BlogThere is no need of explicit transaction in select statement what exactly would you want to rollback, nothing. Although you might use error handling depending on complexity of select involved.
Plus drawbacks is locks would be held for longer duration which MIGHT provide some help in deadlock
Please mark this reply as answer if it solved your issue or vote as helpful if it helped so that other forum members can benefit from it
My Technet Wiki Article
MVP -
Input help for a selection screen field
I have a selection screen with field species and checkoff activity .
For a given species('sc','sb' or 'ss') , the input values allowed for the
check off activity must be 'SCCK' for species 'SC' ,'SBCK' for species 'SB' and so on.
I need to provide input help for check off activity based on the value of the species.How do i do that?PARAMETER : p_val(10) TYPE c ,
p_val2(10) TYPE c.
AT SELECTION-SCREEN on p_val.
IF p_val = 'SC'.
p_val2 = 'SCCK'.
ENDIF.
Maybe you are looking for
-
Moving the data from multiple internal tables into a single one
Hello everyone, I am creating a classical report which uses the following tables. tables : ekko, ekpo, mara, makt,lfa1. my input parameter is Select-options Purchase Order number Following fields are getting used. Doc no EKKO-EBELN
-
Hp Laser Jet CM1015 printing problem
Hi Guys !!!I'll try not to make it long for you, pictures speaks for me.I would need to say that I had that printer since 2007 or a little after, the problem started after using some kids coloring program which prints the pictures after coloring it.
-
Mavericks slow response for text fields
Has anyone noticed that any text field is really really slow in response after upgrading to Mavericks? I have a mid 2011 mac mini with 8 GB ram, 2.5 TB external disk drives. It is really driving me crazy having to wait to enter text in. Most notice
-
Crystal Reports Server - Scheduling Report Exports
Dear Experts, We are using Crystal Reports Server XI Edition and we would like to schedule report exports. Crystal Reports Server configuration console allows us to automate report exports but apparently we could not find a way to pass report paramet
-
Cups-canon-3.00 build issue
Hi, I'm having trouble installing this package cups-canon-3.00 from AUR, here is the entire output http://pastebin.com/Ya1d2u0D . I modified the PGKBUILD from: MODELS=(ip1900 ip3600 ip4600 mp190 mp240 mp540 mp630) to MODELS=(mp240) to build only for