Problem in Matrix Like Select Statement.
Hi All,
I have table like this
Name Null? Type
ROLL NUMBER
DD DATE
ATT VARCHAR2(2)
Along with the data--
SELECT * FROM ATT;
ROLL DD AT
2 02-FEB P
2 01-FEB P
2 03-FEB P
2 04-FEB P
2 05-FEB P
2 06-FEB P
2 07-FEB A
2 09-FEB A
1 01-FEB A
1 02-FEB P
1 03-FEB A
1 04-FEB A
1 05-FEB P
1 06-FEB P
1 07-FEB P
1 09-FEB P
I want to write the select statement which fetch the data in matrix like I shown below.
ROLL 01-FEB = 02-FEB = 03-FEB = 04-FEB
1 A P A A
2 P P P P
I tried with sub-queries but not working Any Help...
SKM
sorry i did not look at the problem complete.
the first solution works with the number of values in the date columns are known.
then you can hardcode them , but when the values are unknown you should use something like this.
i have given my example, you just change the queries in the code accordingly and it works great. Thanks to Thomas Kyte who provided this example, i took it and modified for my own.
CREATE TABLE TABL_RESULTDATA
Loadpoints VARCHAR2(125)
, LoadPointSteps VARCHAR2(125)
, Variable VARCHAR2(125)
, Step NUMBER
, Value NUMBER
, VariableType VARCHAR2(20)
, ReadBackValue NUMBER
insert into tabl_resultdata values('HA_000_LP_000','S_001', 'AD1Out(ADcard_1)',1,5, 'SET', 10)
insert into tabl_resultdata values('HA_000_LP_000','S_001','N',1,100,'SET', 10)
insert into tabl_resultdata values('HA_000_LP_000','S_001','Load(DDE)',1,1,'SET', null)
insert into tabl_resultdata values('HA_000_LP_000','S_002','Q_quer(Emi2)',1,9.5,'MEAS', null)
insert into tabl_resultdata values('HA_000_LP_000','S_002','T_I(Emi2)',1,0,'MEAS', null)
insert into tabl_resultdata values('HA_000_LP_000','S_002','AD1Out(ADcard_1)',1,0,'SET', null)
insert into tabl_resultdata values('HA_000_LP_000','S_002','N',1,100,'SET', null)
insert into tabl_resultdata values('HA_000_LP_000','S_002','Load(DDE)',1,1,'SET', null)
insert into tabl_resultdata values('HA_000_LP_001','S_001','Q_Ueber(KMM)',1,49.83,'MEAS', null)
insert into tabl_resultdata values('HA_000_LP_001','S_001','T_Volkor(KMM)',1,0,'MEAS', null)
insert into tabl_resultdata values('HA_000_LP_001','S_001','C1(LeCroy)',1,0,'MEAS', null)
insert into tabl_resultdata values('HA_000_LPSCHL_000','S_001','AD1Out(ADcard_1)',1,10,'SET', null)
insert into tabl_resultdata values('HA_000_LPSCHL_000','S_001','AD1Out(ADcard_1)',2,12,'SET', 17)
insert into tabl_resultdata values('HA_000_LPSCHL_000','S_001','AD1Out(ADcard_1)',3,12,'SET', 19)
insert into tabl_resultdata values('HA_000_LPSCHL_000','S_001','AD1(ADcard_1)',1,9.025,'MEAS', null)
insert into tabl_resultdata values('HA_000_LPSCHL_000','S_001','AD1(ADcard_1)',2,9.05,'MEAS', null)
create or replace package pivot
as
type rc is ref cursor;
procedure data(p_cursor in out rc);
end;
create or replace package body pivot
as
procedure data( p_cursor in out rc )
is
l_stmt long;
l_index number := 0;
begin
l_stmt := 'select loadpoints, loadpointsteps ,step ';
for x in ( select distinct variable, variabletype from tabl_resultdata order by 2 )
loop
l_index := l_index + 1;
l_stmt := l_stmt ||', max(decode(variable,' || ''''|| x.variable||''''||', value )) ' ||replace(replace(x.variable||'('||x.variabletype||')', '('), ')');
end loop;
l_stmt := l_stmt || ' from tabl_Resultdata group by loadpoints,loadpointsteps, step order by loadpoints';
open p_cursor for l_stmt;
end;
end;
set linesize 198
column loadpoints format a17
column loadpointsteps format a5
column step format 999
column value_1 format 999.999
column value_2 format 999.999
column value_3 format 999.999
column value_4 format 999.999
column value_5 format 999.999
column value_6 format 999.999
column value_7 format 999.999
column value_8 format 999.999
column value_9 format 999.999
variable x refcursor
set autoprint on
exec pivot.data( :x );
Similar Messages
-
Problem with writing SQL Select statement.
Table structure
Temp_tab
App_no Number
Cln_no Number
Example data
APP_NO CLN_NO
1 1
1 2
1 4
2 3
3 5
3 7
3 6
4 9
4 8
7 13
Output
APP_NO CLN_NO
1 1,2,4
2 3
3 5,7,6
4 9,8
7 13
You should not create any function to get this output. It should be a Select statement. You can use 8i features. You can exclude the comma between the values instead of that you can put spaces. No hard coding.
Thank you in advance.
Samujjwal BasuTable structure
Temp_tab
App_no Number
Cln_no Number
Example data
APP_NO CLN_NO
1 1
1 2
1 4
2 3
3 5
3 7
3 6
4 9
4 8
7 13
Output
APP_NO CLN_NO
1 1,2,4
2 3
3 5,7,6
4 9,8
7 13
You should not create any function to get this output. It should be a Select statement. You can use 8i features. You can exclude the comma between the values instead of that you can put spaces. No hard coding.
Thank you in advance.
Samujjwal Basu -
Problem to write a select statement
[\code]
/* Formatted on 6/9/2011 5:30:17 PM (QP5 v5.149.1003.31008) */
CREATE TABLE temp
AS
SELECT 111 id, '72 ( 1/02/2011);73 ( 1/02/2011);' dt FROM DUAL
UNION
SELECT 232 id, '74 ( 1/02/2011);' FROM DUAL
UNION
SELECT 242 id, '73 ( 1/02/2011);72 ( 1/02/2011); ' FROM DUAL
UNION
SELECT 252 id, '74 ( 1/02/2011);13 ' FROM DUAL
UNION
SELECT 262 id, '77 ( 1/02/2011);72' FROM DUAL
UNION
SELECT 252 id, '53 ( 1/02/2011);44 ' FROM DUAL
UNION
SELECT 252 id, '55 ( 1/02/2011); 45 ' FROM DUAL
UNION
SELECT 252 id, '55 ( 1/02/2011);13 ' FROM DUAL
UNION
SELECT 252 id, '56 ( 1/02/2011);13 ' FROM DUAL
UNION
SELECT 2992 id, '56 ( 1/02/2011);13 : 72 73,74 ' FROM DUAL;
note : DT column is a STRING (varchar2(400))
i want to get rows with 72 ,73,74 and 13
if the row has both numbers mentioned above then my result should be
id 72 73 74 13
111 1 1 0 0
232 0 0 1 0
242 1 1 0 0
252 0 0 1 0
2992 1 1 1 1
sample data:
784027892 13 (12/13/10); 300 (12/13/10);
784027919 72 (06/06/11);
784027977 72 (06/06/11); 73 (06/06/11);
784028005 74 (06/06/11);
78402802 73 (06/06/11);
281398723 300 (06/09/11); 74 (06/09/11);
281399095 72 (06/09/11); 13 (06/09/11);
281399162 73 (06/09/11);
281399177 74 (06/09/11);
can any one help me
Thanks
[\code]
Edited by: 846773 on Jun 9, 2011 6:26 PM
Edited by: 846773 on Jun 9, 2011 6:28 PMHi,
846773 wrote:
[\code]
/* Formatted on 6/9/2011 5:30:17 PM (QP5 v5.149.1003.31008) */
CREATE TABLE temp
AS ...
SELECT 252 id, '74 ( 1/02/2011);13 ' FROM DUAL
UNION
SELECT 252 id, '55 ( 1/02/2011);13 ' FROM DUAL
UNION
SELECT 252 id, '56 ( 1/02/2011);13 ' FROM DUAL
UNION
... So there are 3 rows that have id=252 and contain '13'
i want to get rows with 72 ,73,74 and 13
if the row has both numbers mentioned above then my result should be
id 72 73 74 13
111 1 1 0 0
232 0 0 1 0
242 1 1 0 0
252 0 0 1 0Why do you want 0 in the "13" column for id=252? Why not 3, corresponding to the 3 rows I pointed out earlier?
sample data:
784027892 13 (12/13/10); 300 (12/13/10);
784027919 72 (06/06/11);
784027977 72 (06/06/11); 73 (06/06/11);
784028005 74 (06/06/11);
78402802 73 (06/06/11);
281398723 300 (06/09/11); 74 (06/09/11);
281399095 72 (06/09/11); 13 (06/09/11);
281399162 73 (06/09/11);
281399177 74 (06/09/11); What is this?
I think you want something like this:
WITH got_delimited_nums AS
SELECT id
, ' ' || TRIM ( REGEXP_REPLACE ( REGEXP_REPLACE ( dt
, '\([^)]*\)'
, '[^0-9]+'
|| ' ' AS delimited_nums
FROM temp
SELECT id
, SUM (SIGN (INSTR (delimited_nums, ' 72 '))) AS n_72
, SUM (SIGN (INSTR (delimited_nums, ' 73 '))) AS n_73
, SUM (SIGN (INSTR (delimited_nums, ' 74 '))) AS n_74
, SUM (SIGN (INSTR (delimited_nums, ' 13 '))) AS n_13
FROM got_delimited_nums
GROUP BY id
ORDER BY id
;Output:
ID N_72 N_73 N_74 N_13
111 1 1 0 0
232 0 0 1 0
242 1 1 0 0
252 0 0 1 3
262 1 0 0 0
2992 1 1 1 1This ignores the numbers inside parentheses.
If the same number appears 2 (or more) times on the same row, it will be counted only once.
Whenever you have a problem, say which version of Oracle you're running. The query above will work in Oracle 10.1 (and up).
Edited by: Frank Kulash on Jun 9, 2011 10:33 PM -
CREATE OR REPLACE PROCEDURE return data like SELECT statement
"SELECT * FROM SEARCH_KEYWORD" is successfully done and return the data of the tables. Now, I want to run that on server side using PROCEDURE.
I successfully executed the below scripts. Please teach me how to call the sp_test procedure. OR if you have other way or maybe my scripts are wrong.
CREATE OR REPLACE PACKAGE GLOBALPKG
AS
TYPE RCT1 IS REF CURSOR;
TRANCOUNT INTEGER := 0;
IDENTITY INTEGER;
END;
CREATE OR REPLACE PROCEDURE LPG.sp_test
RCT1 IN OUT GLOBALPKG.RCT1
AS
BEGIN
OPEN RCT1 FOR
SELECT *
FROM SEARCH_KEYWORD;
END;
Here is my table definition:
CREATE TABLE LPG.SEARCH_KEYWORD
FRACTION VARCHAR2(50),
KEYWORD VARCHAR2(50),
PURPOSE VARCHAR2(50),
REMARKS VARCHAR2(50),
DATE_INSERTED DATE DEFAULT sysdate
PCTFREE 10
PCTUSED 40
MAXTRANS 255
TABLESPACE SYSTEM
STORAGE(INITIAL 64K MINEXTENTS 1 MAXEXTENTS 2147483645 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT)
NOCACHE
LOGGING
Eros
JapanWrong forum. You should ask this question in the SQL and PL/SQL forum.
Couple of comments though. Oracle is not SQL-Server. So calling a procedure that returns a ref cursor is different from a T-SQL macro procedure that functions like a view.
Also, there is no need to use the SQL-Server standard (and silly one at that) of prefixing stored procedures using the text "sp_". In fact, the whole concept of Hungarian-like notation is something of the past and has no place in today's paradigm of software engineering.
As for calling a a proc that returns a ref cursor, from SQL*Plus it will look as follows:
SQL> var c refcursor
SQL>
SQL> exec LPG.sp_test( :c )
SQL> print c
Ref cursor needs to be supported by the client language you use to make this call to Oracle. -
Hi Experts,
I am facing the problem in the select statement where it giving the short dump
DBIF_RSQL_INVALID_RSQL CX_SY_OPEN_S.
i have searched many forms, but i found that the select option s_matnr have the limitaion 2000 entreis, but i am passing same s_matnr to other select statement with more than 2000 entries but it is not giving me any short dump.
but i am facing problem with only one select statement where if i pass select option s_matnr more than 1500 entris also giving short dump.
my select statement is
SELECT * FROM bsim
INTO CORRESPONDING FIELDS OF TABLE g_t_bsim_lean
FOR ALL ENTRIES IN t_bwkey WHERE bwkey = t_bwkey-bwkey
AND matnr IN matnr
AND bwtar IN bwtar
AND budat >= datum-low.
in the internal table g_t_bsim_lean internal table contain all the fields of the table bsim with 2 fields from other table.
Please let me know whether i need to change the select statement or any other solution for this.
Regards,
udupimy select query is like this:
DATA: BEGIN OF t_bwkey OCCURS 0, "184465
bwkey LIKE bsim-bwkey, "184465
END OF t_bwkey. "184465
LOOP AT g_t_organ WHERE keytype = c_bwkey.
MOVE g_t_organ-bwkey TO t_bwkey-bwkey.
COLLECT t_bwkey. "184465
ENDLOOP. "184465
READ TABLE t_bwkey INDEX 1. "184465
CHECK sy-subrc = 0. "184465
SELECT * FROM bsim "n443935
INTO CORRESPONDING FIELDS OF TABLE g_t_bsim_lean "n443935
FOR ALL ENTRIES IN t_bwkey WHERE bwkey = t_bwkey-bwkey
AND matnr IN matnr
AND bwtar IN bwtar
AND budat >= datum-low. -
SELECT statement in FROM clause - syntax help
Hi,
I want to have a SELECT statement in the FROM clause. I get syntax error when doing this and since I am new to ABAP I need som help.
I want to do the following, (and if anyone has any SELECT statement from their own system which you know there is no syntax error in please post it here so I can analyze the syntax):
*I leave the INTO itab clause out, since I only want to demonstrate the functionality
I am trying to get.
<b>SELECT</b> tableOne~someField
<b>FROM</b> tab <b>AS</b> tableOne (<b>SELECT</b> someField
<b>FROM</b> tab
<b>WHERE</b> someFiled > 1) <b>AS</b> tableTwo
<b>WHERE</b> tableOnesomeField > tableTwosomeField
like I said, the problem is that the select statement in the parenthesis in the from clause seems to be incorrect because I get "wrong expression" when trying to compile. Is this because I cannot have a select statement in the from clause or is it because of a minor syntax error, such as I forgot a dot, or some other sign?
thanks and regards
Baransorry i am not enough familiar with sub queries but some error i can see which i will state here.
1> you have to use sub queries i.e. select in ( ) after where clause because here you are fulfilling a where clause by another select statement.
like this
SELECT * FROM SFLIGHT
INTO WA
WHERE SEATSOCC = ( SELECT MAX( SEATSOCC ) FROM SFLIGHT ).
2> you cannot specify field name without into clause either you have to use
select * or select f1 into tab-f1 like this...
but if you are using select * without into you have to define
tables : dbtab.
selec * from dbtab. like that.
regards
shiba dutta -
Inline select statement and SQL optimizatino
Hi everyone,
I am trying to optimze a script but can't get the fulle explainplan in PL/SQL.
The problem is that the select statements in de column defenition is not explained. below are the 2 scripts I want to compare:
SQL1:
Select
z.title
, (select name from members where id =z.id) as name
, z.id
from job z
SQL2
Select
z.title
, d.name
, z.id
from job z
left outer join members d on z.id=d.id
My question is: what is the effect on performance and cost of the 'inline select' statement of name in SQL1
hope to hear form you soon
Harm
Edited by: HALI on 25-nov-2010 1:14In respons to your question below is the output:
I altered the used names, tablenames etc. according to our privacy pollacy.
A far as I can see the inline statement is not traced.
The query with ansi-join:
xplain plan for
select
id as Interne
, mv.ie_id as Act
, 'H_W' as Rl
from testing i
left outer join resutino m on i.hand=m.id
left outer join vert mv on m.voor || case when m.voeg is not null then ' '|| m.voeg end ||' '||m.naam = mv.source and mv.field = 'employee'
explain plan succeeded.
select * from table(dbms_xplan.display)
PLAN_TABLE_OUTPUT
| Id | Operation | Name | Rows | Bytes | Cost |
| 0 | SELECT STATEMENT | | 684 | 47880 | 19 |
| 1 | HASH JOIN OUTER | | 684 | 47880 | 19 |
| 2 | HASH JOIN OUTER | | 684 | 22572 | 15 |
| 3 | TABLE ACCESS FULL | testing | 684 | 5472 | 7 |
| 4 | TABLE ACCESS FULL | resutino | 308 | 7700 | 7 |
| 5 | TABLE ACCESS FULL | VERT | 166 | 6142 | 3 |
Note: cpu costing is off, 'PLAN_TABLE' is old version
13 rows selected
The inline select query:
xplain plan for
SELECT
case when (select ie_id from vert xov where xov.field = 'field' and source in
(select m.voor || case when m.voeg is not null then ' '|| m.voegend ||' '||m.naam from resultino m where i.hand= m.id)) is not null then
'X'||(select ie_id from vert xov where xov.field = 'field' and source in
(select m.voor || case when m.voeg is not null then ' '|| m.voegend ||' '||m.naam from resultino m where i.hand= m.id))
else 'X' || (select ie_id from vert xov where xov.source = 'none' and xov.veld = 'employee')
end as Act,
'ADM' as Rol,
i.id as Interne
FROM testing i
explain plan succeeded.
select * from table(dbms_xplan.display)
PLAN_TABLE_OUTPUT
| Id | Operation | Name | Rows | Bytes | Cost |
| 0 | SELECT STATEMENT | | 684 | 5472 | 7 |
| 1 | TABLE ACCESS FULL | testing | 684 | 5472 | 7 |
Note: cpu costing is off, 'PLAN_TABLE' is old version
9 rows selected -
Hi and good morning,
I've problem in getting the data..... I guess the problem is in my select statement.....please help?
a b c d
w1 h01 h12 12/04/06
w2 wc1 hr8 01/08/04
where a,b and c are of char type and d is of date type... my select statement is
select a b c d from mara into corresponding
fields of table it_tab2 where a = pa_a and c in
s_c and b in s_b.
it is not working please suggest.....Hi,
Check the following.
1) Make sure you have the records in the table MARA for the corresponding combination give in the selection screen...
2) Also make sure the parameter pa_a is entered in the selection-screen..
Since in the where clause..You are using EQ operator..Which means if the value is not give in the selection screen..It will look for blank value..
Make the parameter as mandatory OR if it is optional parameter..Use SELECT-OPTIONS NO INTERVALS NO-EXTENSION addition..And use IN in your select statement..
Example
SELECT-OPTIONS SO_A FOR MARA-A.
select a b c d from mara into corresponding
fields of table it_tab2 where <b>A IN SO_A</b> and c in
s_c and b in s_b.
Thanks,
Naren -
A trouble with "LIKE" in a select statement
Hi!
I'm having trouble with "LIKE" in a select statement...
With Access I can make the following and everything works well:
SELECT name, birthday
FROM client
WHERE birthday LIKE '*/02/*';
but if try to do it in my application (it uses Access), it doesn't work - I just can't understand that!!!
In my application the "month" is always the currently month taken from the "System". Look what I'm doing...
String query1 = "SELECT name, birthday " +
"FROM client " +
"WHERE birthday " +
"LIKE '*/" +
pMonth +
"/*' " +
"ORDER BY birthday ASC ";
ResultSet rs = statement1.executeQuery(consulta1);
boolean moreRecords = rs.next();
The variable "moreRecords" is always "false", the query returns nothing although the table "client" has records that attend the query.
Please, anyone can help me?! It's a little bit urgent.
Thanks,
Katia.Hi Katia,
I'll bet the problem lies with the characters you're using to escape the LIKE clause. You're using the ones that Access likes to see, but that's not necessarily what's built into the JDBC-ODBC driver class.
You can find out what the correct escape wildcard characters are from the java.sql.DatabaseMetaData.getSearchStringEscape() method. It'll tell you what to use in the LIKE clause.
I'm not 100% sure about your code. It doesn't use query1 anywhere. I'd do this:
String query = "SELECT name, birthday FROM client WHERE birthday LIKE ? ORDER BY birthday ASC";
PreparedStatement statement = connection.createStatement(query);
String escape = connection.getMetaData().getSearchStringEscape();
String test = escape + '/' + pMonth + '/' + escape;
statement.setString(1, test);
ResultSet rs = statement.executeQuery();
while (rs.hasNext())
// load your data into a data structure to pass back.
rs.close();
statement.close();Let me know if that works. - MOD -
Problem with Select Statements
Hi All,
I have a performance problem for my report because of the following statements.
How can i modify the select statements for improving the performance of the report.
DATA : shkzg1h LIKE bsad-shkzg,
shkzg1s LIKE bsad-shkzg,
shkzg2h LIKE bsad-shkzg,
shkzg2s LIKE bsad-shkzg,
shkzg1hu LIKE bsad-shkzg,
shkzg1su LIKE bsad-shkzg,
shkzg2hu LIKE bsad-shkzg,
shkzg2su LIKE bsad-shkzg,
kopbal1s LIKE bsad-dmbtr,
kopbal2s LIKE bsad-dmbtr,
kopbal1h LIKE bsad-dmbtr,
kopbal2h LIKE bsad-dmbtr,
kopbal1su LIKE bsad-dmbtr,
kopbal2su LIKE bsad-dmbtr,
kopbal1hu LIKE bsad-dmbtr,
kopbal2hu LIKE bsad-dmbtr.
*These statements are in LOOP.
SELECT shkzg SUM( dmbtr )
INTO (shkzg1s , kopbal1s)
FROM bsid
WHERE bukrs = ibukrs
AND kunnr = ktab-kunnr
AND budat < idate-low
AND shkzg = 'S'
AND umskz EQ ''
GROUP BY shkzg.
ENDSELECT.
SELECT shkzg SUM( dmbtr )
INTO (shkzg1su , kopbal1su)
FROM bsid
WHERE bukrs = ibukrs
AND kunnr = ktab-kunnr
AND budat < idate-low
AND shkzg = 'S'
AND umskz IN zspgl
GROUP BY shkzg.
ENDSELECT.
SELECT shkzg SUM( dmbtr )
INTO (shkzg1h , kopbal1h)
FROM bsid
WHERE bukrs = ibukrs
AND kunnr = ktab-kunnr
AND budat < idate-low
AND shkzg = 'H'
AND umskz EQ ''
GROUP BY shkzg.
ENDSELECT.
SELECT shkzg SUM( dmbtr )
INTO (shkzg1hu , kopbal1hu)
FROM bsid
WHERE bukrs = ibukrs
AND kunnr = ktab-kunnr
AND budat < idate-low
AND shkzg = 'H'
AND umskz IN zspgl
GROUP BY shkzg.
ENDSELECT.
SELECT shkzg SUM( dmbtr )
INTO (shkzg2s , kopbal2s)
FROM bsad
WHERE bukrs = ibukrs
AND kunnr = ktab-kunnr
AND budat < idate-low
AND shkzg = 'S'
AND umskz EQ ''
GROUP BY shkzg.
ENDSELECT.
SELECT shkzg SUM( dmbtr )
INTO (shkzg2su , kopbal2su)
FROM bsad
WHERE bukrs = ibukrs
AND kunnr = ktab-kunnr
AND budat < idate-low
AND shkzg = 'S'
AND umskz IN zspgl
GROUP BY shkzg.
ENDSELECT.
SELECT shkzg SUM( dmbtr )
INTO (shkzg2h , kopbal2h)
FROM bsad
WHERE bukrs = ibukrs
AND kunnr = ktab-kunnr
AND budat < idate-low
AND shkzg = 'H'
AND umskz EQ ''
GROUP BY shkzg.
ENDSELECT.
SELECT shkzg SUM( dmbtr )
INTO (shkzg2hu , kopbal2hu)
FROM bsad
WHERE bukrs = ibukrs
AND kunnr = ktab-kunnr
AND budat < idate-low
AND shkzg = 'H'
AND umskz IN zspgl
GROUP BY shkzg.
ENDSELECT.>
Siegfried Boes wrote:
> Please stop writing answers if you understrand nothing about database SELECTS!
> All above recommendations are pure nonsense!
>
> As always with such questions, you must do an analysis before you ask! The coding itself is perfectly o.k., a SELECT with an aggregate and a GROUP BY can not be changed into a SELECT SINGLE or whatever.
>
> But your SELECTS mustr be supported by indexes!
>
> Please run SQL Trace, and tell us the results:
>
> I see 8 statements, what is the duration and the number of records coming back for each statement?
> Maybe only one statement is slow.
>
> See
> SQL trace:
> /people/siegfried.boes/blog/2007/09/05/the-sql-trace-st05-150-quick-and-easy
>
>
> Siegfried
Nice point there Siegfried. Instead of giving constructive suggestion, people here give a very bad suggestion on using SELECT SINGLE combined with SUM and GROUP BY.
I hope the person already look at your reply before he try using select single and wondering why he has error.
Anyway, the most important thing is how many loop expected for those select statements?
If you have like thousands of loop, you can expect a poor performance.
So, you should also look at how many times the select statement is called and not only performance for each select statement when you're doing SQL trace.
Regards,
Abraham -
Performance problem(ANEA/ANEP table) in Select statement
Hi
I am using below select statement to fetch data.
Does the below where statement have performance issue?
can you Pls suggest.
1)In select of ANEP table, i am not using all the Key field in where condition. will it have performance problem?
2)does the order of where condition should be same as in table, if any one field order change also will have effect performance
SELECT bukrs
anln1
anln2
afabe
gjahr
peraf
lnran
bzdat
bwasl
belnr
buzei
anbtr
lnsan
FROM anep
INTO TABLE o_anep
FOR ALL ENTRIES IN i_anla
WHERE bukrs = i_anla-bukrs
AND anln1 = i_anla-anln1
AND anln2 = i_anla-anln2
AND afabe IN s_afabe
AND bzdat =< p_date
AND bwasl IN s_bwasl.
SELECT bukrs
anln1
anln2
gjahr
lnran
afabe
aufwv
nafal
safal
aafal
erlbt
aufwl
nafav
aafav
invzv
invzl
FROM anea
INTO TABLE o_anea
FOR ALL ENTRIES IN o_anep
WHERE bukrs = o_anep-bukrs
AND anln1 = o_anep-anln1
AND anln2 = o_anep-anln2
AND gjahr = o_anep-gjahr
AND lnran = o_anep-lnran
AND afabe = o_anep-afabe.
Moderator message: Please Read before Posting in the Performance and Tuning Forum
Edited by: Thomas Zloch on Aug 9, 2011 9:37 AM1. Yes. If you have only a few primary keys in youe WHERE condition that does affect the performance. But some times requirement itself may be in that way. We may not be knowing all the primary keys to given them in WHER conditon. If you know the values, then provide them without fail.
2. Yes. It's better to always follow the sequence in WHERE condition and even in the fields being fetched.
One important point is, whenever you use FOR ALL ENTRIES IN, please make sure that the itab IS NOT INITIAL i.e. the itab must have been filled in. So, place the same conditin before both the SELECT queries like:
IF i_anla[] IS NOT INITIAL.
SELECT bukrs
anln1
anln2
afabe
gjahr
peraf
lnran
bzdat
bwasl
belnr
buzei
anbtr
lnsan
FROM anep
INTO TABLE o_anep
FOR ALL ENTRIES IN i_anla
WHERE bukrs = i_anla-bukrs
AND anln1 = i_anla-anln1
AND anln2 = i_anla-anln2
AND afabe IN s_afabe
AND bzdat =< p_date
AND bwasl IN s_bwasl.
ENDIF.
IF o_anep[] IS NOT INITIAL.
SELECT bukrs
anln1
anln2
gjahr
lnran
afabe
aufwv
nafal
safal
aafal
erlbt
aufwl
nafav
aafav
invzv
invzl
FROM anea
INTO TABLE o_anea
FOR ALL ENTRIES IN o_anep
WHERE bukrs = o_anep-bukrs
AND anln1 = o_anep-anln1
AND anln2 = o_anep-anln2
AND gjahr = o_anep-gjahr
AND lnran = o_anep-lnran
AND afabe = o_anep-afabe.
ENDIF. -
Problem with SELECT statement. What is wrong with it?
Why is this query....
<cfquery datasource="manna_premier" name="kit_report">
SELECT Orders.ID,
SaleDate,
Orders.UserID,
Distributor,
DealerID,
Variable,
TerritoryManager,
US_Dealers.ID,
DealerName,
DealerAddress,
DealerCity,
DealerState,
DealerZIPCode,
(SELECT SUM(Quantity)
FROM ProductOrders PO
WHERE PO.OrderID = Orders.ID) as totalProducts,
FROM Orders, US_Dealers
WHERE US_Dealers.ID = DealerID AND SaleDate BETWEEN #CreateODBCDate(FORM.Start)# AND #CreateODBCDate(FORM.End)# AND Variable = '#Variable#'
</cfquery>
giving me this error message...
Error Executing Database Query.
[Macromedia][SequeLink JDBC Driver][ODBC Socket][Microsoft][ODBC Microsoft Access Driver] The SELECT statement includes a reserved word or an argument name that is misspelled or missing, or the punctuation is incorrect.
The error occurred in D:\Inetpub\mannapremier\kit_report2.cfm: line 20
18 : WHERE PO.OrderID = Orders.ID) as totalProducts,
19 : FROM Orders, US_Dealers
20 : WHERE US_Dealers.ID = DealerID AND SaleDate BETWEEN #CreateODBCDate(FORM.Start)# AND #CreateODBCDate(FORM.End)# AND Variable = '#Variable#'
21 : </cfquery>
22 :
SQLSTATE
42000
SQL
SELECT Orders.ID, SaleDate, Orders.UserID, Distributor, DealerID, Variable, TerritoryManager, US_Dealers.ID, DealerName, DealerAddress, DealerCity, DealerState, DealerZIPCode, (SELECT SUM(Quantity) FROM ProductOrders PO WHERE PO.OrderID = Orders.ID) as totalProducts, FROM Orders, US_Dealers WHERE US_Dealers.ID = DealerID AND SaleDate BETWEEN {d '2009-10-01'} AND {d '2009-10-31'} AND Variable = 'Chick Days pre-book'
VENDORERRORCODE
-3504
DATASOURCE
manna_premier
Resources:
I copied it from a different template where it works without error...
<cfquery name="qZVPData" datasource="manna_premier">
SELECT UserID,
TMName,
UserZone,
(SELECT COUNT(*)
FROM Sales_Calls
WHERE Sales_Calls.UserID = u.UserID) as totalCalls,
(SELECT COUNT(*)
FROM Orders
WHERE Orders.UserID = u.UserID) as totalOrders,
(SELECT SUM(Quantity)
FROM ProductOrders PO
WHERE PO.UserID = u.UserID AND PO.NewExisting = 1) as newItems,
(SELECT SUM(NewExisting)
FROM ProductOrders PO_
WHERE PO_.UserID = u.UserID) as totalNew,
SUM(totalOrders)/(totalCalls) AS closePerc
FROM Users u
WHERE UserZone = 'Central'
GROUP BY UserZone, UserID, TMName
</cfquery>
What is the problem?It's hard to say: what's your request timeout set to?
700-odd records is not much of a fetch for a decent DB, and I would not expect that to case the problem. But then you're using Access which doesn't fit the description of "decent DB" (or "fit for purpose" or "intended for purpose"), so I guess all bets are off one that one. If this query is slow when ONE request is asking for it, what is going to happen when it goes live and multiple requests are asking for it, along with all the other queries your site will want to run? Access is not designed for this. It will really struggle, and cause your site to run like a dog. One that died serveral weeks ago.
What else is on the template? I presume you're doing something with the query once you fetch it, so could it be that code that's running slowly? Have you taken any steps to isolate which part of the code is taking so long?
How does the query perform if you take the subquery out of the select line? Is there any other way of getting that data? What subquery will be running once for every row of the result set... not very nice.
Adam -
Problem with select statement using Ranges
Hi Guys,
I have used Ranges and used a select statement for selecting those ranges but I am facing a problem.
RANGES: r_doctyp for EDIDC-DOCTYP.
r_doctyp-sign = 'I'.
r_doctyp-option = 'EQ'.
r_doctyp-low = 'DEBMAS'.
append r_doctyp.
r_doctyp-sign = 'I'.
r_doctyp-option = 'EQ'.
r_doctyp-low = 'MATMAS'.
append r_doctyp.
r_doctyp-sign = 'I'.
r_doctyp-option = 'EQ'.
r_doctyp-low = 'PRICAT'.
append r_doctyp.
r_doctyp-sign = 'I'.
r_doctyp-option = 'EQ'.
r_doctyp-low = 'ORDERS'.
append r_doctyp.
r_doctyp-sign = 'I'.
r_doctyp-option = 'EQ'.
r_doctyp-low = 'INVOIC'.
append r_doctyp.
Select DOCNUM " IDoc number
DOCTYP " IDoc Type
from EDIDC into table IT_ZEDIDC
where CREDAT EQ s_credat-low
and DOCTYP EQ r_doctyp " IDOC Types
and DIRECT EQ '1'.
Here my select statement is only taking INVOIC.
But my statement should take any document type.
Thanks,
Prasad.Hi...,
Your following select statement is correct.
Select DOCNUM " IDoc number
DOCTYP " IDoc Type
from EDIDC into table IT_ZEDIDC
where CREDAT IN s_credat
and DOCTYP IN r_doctyp " IDOC Types
and DIRECT EQ '1'.
Why you are not getting result..
1. structure of the IT_ZEDIDC is having two fields DOCNUM , DOCTYP with same data lengths. If not it should be...
2. Order in the database table is must be similer to the order you maintained in the select statement.
3. As you are hard coding the input ranges make sure about every letter.
4. take a look at other where condition fields too.
5. check the table of the ranges in debugging mode.
6. why can't you declare separate work area and table for ranges...?
like .... data: r_tab type range of <field>
data: wa_tab like line of r_tab.
7. Use clear work area statement after the append statment.
--Naveen Inuganti. -
Joining select statements-performance problem
Here two cases are there.Is it posiible to combine the two cases and make it one? I am using OR in the select statement, performance is very bad, i want to improve it.Is there any chance of avoiding that...
1.
select vbeln from vbfa into itab-vbeln where vbelv = final_data-xblnr1
or vbelv = final_data-xblnr2.
select single vbeln from vbrk into tabvbrk-vbeln
where vbeln = itab-vbeln and fkart = 'RTS'.
if sy-subrc eq 0.
move tabvbrk-vbeln to final_data-vbeln1.
modify final_data.
endif.
endselect.
2.
select vbeln from vbfa into itab-vbeln where vbelv = final_data-xblnr1
or vbelv = final_data-xblnr2.
select single vbeln from vbrk into tabvbrk-vbeln
where vbeln = itab-vbeln and fkart = 'ZTR'.
if sy-subrc eq 0.
move tabvbrk-vbeln to final_data-vbeln2.
modify final_data.
endif.
endselect.
Thanks,
fractalThe first main thing is dont use select... endselect.
select vbeln from vbfa into itab-vbeln where vbelv = final_data-xblnr1
or vbelv = final_data-xblnr2.
U can use
select vbeln from vbfa into
corresponding fields of table itab
for all entries in final_data
where vbelv = final_data-xblnr1
or vbelv = final_data-xblnr2.
Instead of this
select single vbeln from vbrk into tabvbrk-vbeln
where vbeln = itab-vbeln and fkart = 'RTS'.
if sy-subrc eq 0.
move tabvbrk-vbeln to final_data-vbeln1.
modify final_data.
endif.
endselect.
Use
select vbeln from vbrk
appending table final_data
where vbeln = itab-vbeln
and fkart = 'RTS'.
if sy-subrc eq 0.
endif.
Try like this.
Hope this helps.
Kindly reward points for the answer which helped u and helped to solve the problem. -
Use of LIKE in where clause of select statement for multiple records
Hi Experts,
I have a account number field which is uploaded from a file. Now this account numbers uploaded does not match fully with sap table account numbers but it contains all of the numbers provided in the file mostly in the upright positions.
For example in file we have account number as 2ARS1 while in sap table the value is 002ARS1.
And i want to fetch data from sap table based on account number uploaded. So, i am trying to use LIKE with for all entries but its not working as mentioned below but LIKE is not working with FOR ALL ENTRIES.
data : begin of t_dda occurs 0,
dda(19) type c,
end of t_dda.
data : begin of t_bukrs occurs 0,
bukrs type t012k-bukrs,
end of t_bukrs.
data : dda type t012k-bankn,
w_dda type t012k-bankn.
CONCATENATE '%'
'2ARS1'
INTO W_DDA.
MOVE W_DDA TO T_DDA-DDA.
APPEND T_DDA.
CLEAR T_DDA.
free t_bukrs.
SELECT BUKRS
FROM T012K
into TABLE t_bukrs
for all entries in t_dda
WHERE BANKN like t_dda-dda.
Can anybody suggest what should i use to get the data for multiple account numbers using one select statement only instead on using SELECT UP TO 1 ROWS in LOOP....ENDLOOP ?
Thanks in advance,
AkashHi,
yes, For All entries won't work for LIKE with '% '.
I think the other alternative is go for Native SQL by writing sub-query
sample code is here:
data: begin of i_mara occurs 0,
matnr like mara-matnr,
matkl like mara-matkl,
end of i_mara.
exec sql.
select matnr, matkl from mara where matnr in (select matnr from marc) and matnr like '%ma' into :i_mara
endexec.
loop at i_mara.
write:/ i_mara-matnr, i_mara-matkl.
endloop.
hope u got it.
regards
Mahesh
Edited by: Mahesh Reddy on Jan 21, 2009 2:32 PM
Maybe you are looking for
-
Is the bind variable syntax supported in procedure? The following produces error: VARIABLE salary number CREATE OR REPLACE PROCEDURE bindvar is eid employees.employee_id%type; BEGIN SELECT employee_id INTO eid FROM EMPLOYEES WHERE employee_id > :sala
-
I have uninstalled any programs that looked starge or might be causing conflict. uninstalled everythign to do with itunes or apple, rebnooting and diong a fresh install. my older ipod works fine. updated intel usb chipset drivers. i have tried any in
-
Upgraded my Mac Air Leopard to the Lion 2 days ago and had problem with streaming video. The Adobe Flash Player prompted for permission to allow storing on my local disk however no response when I tried to activate the allow option. This happened on
-
I'm trying to get Netbeans to connect to a Firebird database so I can try out the basic database application template. I am able to make a connection and it logs in to the database. It then takes me to the Advanced tab and asks about which database s
-
Reinstalling standard apps on a MacBook Air
How can I reinstall such apsp as iCal and Mail when I'm on the MacBook Air and don't have a disc?