Select query, case statment
Hi,
I have two tables Registration and Place.
"Registration" table has Name and Registration_id column and "place" table has Registration_id, task_id and state columns. So both tables have inner join on Registration_id column.
now one Registration_id has two task_id
so for example,
Name
Registration_id
task_id
State
Vicky
101
11
NY
Vicky
101
12
CA
Now in output table "Person", I have 2 columns, one is Birth and one is Death.
So one person has 1 Registration_id associated and that Registration_id has two records in "Place" table,
Now condition is like this, for same Registration_id if task_id = 11 put state value in Birth column and if task_id = 12 put state value in Death column.
So for example output (Select query for Person table)should looks like below,
Name
Registration_id
Birth
Death
Vicky
101
NY
CA
So please tell me select query for this output from above 2 source tables which I use to insert records into "Person" table. (Just for information - I have 12 source tables and there are many columns I have to select from these source tables
and this is the part of that select statement where I am stuck for now). Please let me know if you need any more detail on this.
I think, we have to write subquery in select statement. like,
Select
Birth =(select
STATE from PLACE pl inner join on Registration R ON R.Registration_id = pl.Registration_id
where
TASK_ID = 136)
, Death
=(select
STATE from PLACE pl inner join on Registration R ON R.Registration_id = pl.Registration_id
where
TASK_ID = 141)
, other columns
From other source tables.
Thank you in advance.
Vicky
>> I have two tables Registration and Place [only one?]. <<
Please post DDL, so that people do not have to guess what the keys, constraints, Declarative Referential Integrity, data types, etc. in your schema are. Learn how to follow ISO-11179 data element naming conventions and formatting rules. Temporal data should
use ISO-8601 formats. Code should be in Standard SQL as much as possible and not local dialect.
This is minimal polite behavior on sql forums. You have been told about this before!! Why do you still behave like this? PLEASE EXPLAIN TO EVERYONE HERE WHY THE MOST BASIC SOCIAL RULES DO NOT APPLY TO YOU??
>> Please let me know if you need any more detail on this. <<
WE NEED EVERYTHING! You posted nothing. You do not even know that you should have had “state_code CHAR(2) NOT NULL” in some DDL. You still not know that rows are not records, that tables models sets not single items, etc.
Please quit posting until you come up to the level of a total noob who read the forum rules.
We get tired of doing homework for people who are ignorant, incompetent and rude.
--CELKO-- Books in Celko Series for Morgan-Kaufmann Publishing: Analytics and OLAP in SQL / Data and Databases: Concepts in Practice Data / Measurements and Standards in SQL SQL for Smarties / SQL Programming Style / SQL Puzzles and Answers / Thinking
in Sets / Trees and Hierarchies in SQL
Similar Messages
-
TSQL - Dynamic Query Case statment update issue - to update table by left shifting the rows
Hi all
I need an help on my tsql query in 2000 to fix the case statement.
I have a table like shown below.
On this table where ever I see PortID as 101 and 105 I need to remove it and shift the rows from right to left.
ie on First row PortID1 is 101 so I need to remove it and replace it with PortId2 and similarly PortID2 replaced my PortID3 and so on.
ie like on excel if you delete cell we need to shift the rows to left .
I wrote an dynamic update statement i,m not sure on the assigning dynamic case statment.
CREATE TABLE [TravelRank]
[Destination] [varchar] (50) NULL,
[PortID1] [int] NULL ,
[Distance1] [int] NULL ,
[Rating1] [int] NULL ,
[PortID2] [int] NULL ,
[Distance2] [int] NULL ,
[Rating2] [int] NULL ,
[PortID3] [int] NULL ,
[Distance3] [int] NULL ,
[Rating3] [int] NULL ,
[PortID4] [int] NULL ,
[Distance4] [int] NULL ,
[Rating4] [int] NULL
INSERT into [TravelRank]
select 'Virgin Islands','101','10','5','102','20','5','103','31','5','109','41','5'
Union all
select 'Guinea','101','15','3','102','22','3','105','32','2','110','45','4'
Union all
select 'Benin ','102','12','4','106','28','4','104','33','3','109','48','2'
Union all
select 'Ecuador','102','18','5','101','29','5','108','34','1','111','45','5'
Union all
select 'Belarus ','103','17','4','105','24','4','108','45','4','112','46','3'
Union all
select 'Cook Islands','105','11','2','108','23','2','101','32','2','107','42','4'
Here is my code to fix
declare @SQL varchar(4000)
declare @left varchar(1)
declare @right varchar(1)
select @left = '1',@right = '2'
while @left < 4
begin
select @SQL = '
update t1.PortID' + @left + ' = t2.PortID' + @right + '
t1.Distance' + @left + ' = t2.Distance' + @right + '
t1.Rating' + @left + ' = t2.Rating' + @right + '
'case @left
when '1' then
' t1.PortID' + cast(cast(@left as int)+1 as varchar(1)) + ' = t2.PortID' + cast(cast(@right as int)+1 as varchar(1)) +'
t1.PortID' + cast(cast(@left as int)+2 as varchar(1)) + ' = t2.PortID' + cast(cast(@right as int)+2 as varchar(1)) +'
t1.PortID' + cast(cast(@left as int)+3 as varchar(1)) + ' = t2.PortID' + cast(cast(@right as int)+3 as varchar(1)) +'
t1.Distance' + cast(cast(@left as int)+1 as varchar(1)) + ' = t2.Distance' + cast(cast(@right as int)+1 as varchar(1)) +'
t1.Distance' + cast(cast(@left as int)+2 as varchar(1)) + ' = t2.Distance' + cast(cast(@right as int)+2 as varchar(1)) +'
t1.Distance' + cast(cast(@left as int)+3 as varchar(1)) + ' = t2.Distance' + cast(cast(@right as int)+3 as varchar(1)) +'
t1.Rating' + cast(cast(@left as int)+1 as varchar(1)) + ' = t2.Rating' + cast(cast(@right as int)+1 as varchar(1)) +'
t1.Rating' + cast(cast(@left as int)+2 as varchar(1)) + ' = t2.Rating' + cast(cast(@right as int)+2 as varchar(1)) +'
t1.Rating' + cast(cast(@left as int)+3 as varchar(1)) + ' = t2.Rating' + cast(cast(@right as int)+3 as varchar(1)) +'
' when '2' then
' t1.PortID' + cast(cast(@left as int)+1 as varchar(1)) + ' = t2.PortID' + cast(cast(@right as int)+1 as varchar(1)) +'
t1.PortID' + cast(cast(@left as int)+2 as varchar(1)) + ' = t2.PortID' + cast(cast(@right as int)+2 as varchar(1)) +'
t1.Distance' + cast(cast(@left as int)+1 as varchar(1)) + ' = t2.Distance' + cast(cast(@right as int)+1 as varchar(1)) +'
t1.Distance' + cast(cast(@left as int)+2 as varchar(1)) + ' = t2.Distance' + cast(cast(@right as int)+2 as varchar(1)) +'
t1.Rating' + cast(cast(@left as int)+1 as varchar(1)) + ' = t2.Rating' + cast(cast(@right as int)+1 as varchar(1)) +'
t1.Rating' + cast(cast(@left as int)+2 as varchar(1)) + ' = t2.Rating' + cast(cast(@right as int)+2 as varchar(1)) +'
' when '3' then
' t1.PortID' + cast(cast(@left as int)+1 as varchar(1)) + ' = t2.PortID' + cast(cast(@right as int)+1 as varchar(1)) +'
t1.Distance' + cast(cast(@left as int)+1 as varchar(1)) + ' = t2.Distance' + cast(cast(@right as int)+1 as varchar(1)) +'
t1.Rating' + cast(cast(@left as int)+1 as varchar(1)) + ' = t2.Rating' + cast(cast(@right as int)+1 as varchar(1)) +'
' when '3' then
' t1.PortID' + @left + ' = null '
t1.Distance' + @left + ' = null '
t1.Rating' + @left + ' = null '
else '' end'
from [TravelRank] t1
inner join [TravelRank] t2
on t1.destination = t2.destination
where t1.PortID1 = 101'
print @SQL
-- exec (@SQL)
select @left = cast(cast(@left as int) + 1 as varchar(1))
, @right = cast(cast(@right as int) + 1 as varchar(1))
end
Thanks a lot in advance.declare @SQL varchar(4000)
declare @left varchar(1)
declare @right varchar(1)
select @left = '1',@right = '2'
while @left < 4
begin
select @SQL = '
update t1.PortID' + @left + ' = t2.PortID' + @right + '
t1.Distance' + @left + ' = t2.Distance' + @right + '
t1.Rating' + @left + ' = t2.Rating' + @right + '
'+CASE @left
when '1' then
' t1.PortID' + cast(cast(@left as int)+1 as varchar(1)) + ' = t2.PortID' + cast(cast(@right as int)+1 as varchar(1)) +'
t1.PortID' + cast(cast(@left as int)+2 as varchar(1)) + ' = t2.PortID' + cast(cast(@right as int)+2 as varchar(1)) +'
t1.PortID' + cast(cast(@left as int)+3 as varchar(1)) + ' = t2.PortID' + cast(cast(@right as int)+3 as varchar(1)) +'
t1.Distance' + cast(cast(@left as int)+1 as varchar(1)) + ' = t2.Distance' + cast(cast(@right as int)+1 as varchar(1)) +'
t1.Distance' + cast(cast(@left as int)+2 as varchar(1)) + ' = t2.Distance' + cast(cast(@right as int)+2 as varchar(1)) +'
t1.Distance' + cast(cast(@left as int)+3 as varchar(1)) + ' = t2.Distance' + cast(cast(@right as int)+3 as varchar(1)) +'
t1.Rating' + cast(cast(@left as int)+1 as varchar(1)) + ' = t2.Rating' + cast(cast(@right as int)+1 as varchar(1)) +'
t1.Rating' + cast(cast(@left as int)+2 as varchar(1)) + ' = t2.Rating' + cast(cast(@right as int)+2 as varchar(1)) +'
t1.Rating' + cast(cast(@left as int)+3 as varchar(1)) + ' = t2.Rating' + cast(cast(@right as int)+3 as varchar(1)) +'
' when '2' then
' t1.PortID' + cast(cast(@left as int)+1 as varchar(1)) + ' = t2.PortID' + cast(cast(@right as int)+1 as varchar(1)) +'
t1.PortID' + cast(cast(@left as int)+2 as varchar(1)) + ' = t2.PortID' + cast(cast(@right as int)+2 as varchar(1)) +'
t1.Distance' + cast(cast(@left as int)+1 as varchar(1)) + ' = t2.Distance' + cast(cast(@right as int)+1 as varchar(1)) +'
t1.Distance' + cast(cast(@left as int)+2 as varchar(1)) + ' = t2.Distance' + cast(cast(@right as int)+2 as varchar(1)) +'
t1.Rating' + cast(cast(@left as int)+1 as varchar(1)) + ' = t2.Rating' + cast(cast(@right as int)+1 as varchar(1)) +'
t1.Rating' + cast(cast(@left as int)+2 as varchar(1)) + ' = t2.Rating' + cast(cast(@right as int)+2 as varchar(1)) +'
' when '3' then
' t1.PortID' + cast(cast(@left as int)+1 as varchar(1)) + ' = t2.PortID' + cast(cast(@right as int)+1 as varchar(1)) +'
t1.Distance' + cast(cast(@left as int)+1 as varchar(1)) + ' = t2.Distance' + cast(cast(@right as int)+1 as varchar(1)) +'
t1.Rating' + cast(cast(@left as int)+1 as varchar(1)) + ' = t2.Rating' + cast(cast(@right as int)+1 as varchar(1)) +'
' when '3' then
' t1.PortID' + @left + ' = null '+
' t1.Distance' + @left + ' = null '+
' t1.Rating' + @left + ' = null '
else '' END + '
from [TravelRank] t1
inner join [TravelRank] t2
on t1.destination = t2.destination
where t1.PortID1 = 101'
print @SQL
-- exec (@SQL)
select @left = cast(cast(@left as int) + 1 as varchar(1))
, @right = cast(cast(@right as int) + 1 as varchar(1))
end
You were missing a couple of concats. This runs now, but I'm not completely sure if it's the output you were expecting. -
Select query case insensitive for data type VARG
Hi Experts,
I am having trouble in retrieving the results from a table using select query.
I have a table (Users) as below
Name (VARG) Number(INTEGER)
Murthy 0001
murthy 0002
when I am querying the table with select query as -
select * from Users where Name = 'Murthy'
this query is returning only one record which matches with capital letter of 'M', though both the record names are same.
I have seen in IBM forum to make the select query as case insensitive using the UPPER key word, I have tried this as below -
select * from Users where UPPER(Name) = 'MURTHY'
But this query is not working in my case...
My DB2 version is 8.1.5
Can any one please help in fixing this problem...
Thanks in Advance,
MurthyHi Murthy,
your query is the right to one to select both records. I don't see why it doesn't work.
Are you sure you posted the query that you have executed ?
What is the result of this query ?
But i don't really know how an VARG (Varying-length graphic string) will work. It could be that the UPPER-Function will work in another way as it works on VARCHAR.
regards
Kay -
Using case when statement in the select query to create physical table
Hello,
I have a requirement where in I have to execute a case when statement with a session variable while creating a physical table using a select query. let me explain with an example.
I have a physical table based on a select table with one column.
SELECT 'VALUEOF(NQ_SESSION.NAME_PARAMETER)' AS NAME_PARAMETER FROM DUAL. Let me call this table as the NAME_PARAMETER table.
I also have a customer table.
In my dashboard that has two pages, Page 1 contains a table with the customer table with column navigation to my second dashboard page.
In my second dashboard page I created a dashboard report based on NAME_PARAMETER table and a prompt based on customer table that sets the NAME_ PARAMETER request variable.
EXECUTION
When i click on a particular customer, the prompt sets the variable NAME_PARAMETER and the NAME_PARAMETER table shows the appropriate customer.
everything works as expected. YE!!
Now i created another table called NAME_PARAMETER1 with a little modification to the earlier table. the query is as follows.
SELECT CASE WHEN 'VALUEOF(NQ_SESSION.NAME_PARAMETER)'='Customer 1' THEN 'TEST_MART1' ELSE TEST_MART2' END AS NAME_PARAMETER
FROM DUAL
Now I pull in this table into the second dashboard page along with the NAME_PARAMETER table report.
surprisingly, NAME_PARAMETER table report executes as is, but the other report based on the NAME_PARAMETER1 table fails with the following error.
Error Codes: OPR4ONWY:U9IM8TAC:OI2DL65P
State: HY000. Code: 10058. [NQODBC] [SQL_STATE: HY000] [nQSError: 10058] A general error has occurred. [nQSError: 16001] ODBC error state: S1000 code: 1756 message: [Oracle][ODBC][Ora]ORA-01756: quoted string not properly terminated. [nQSError: 16014] SQL statement preparation failed. (HY000)
SQL Issued: SET VARIABLE NAME_PARAMETER='Novartis';SELECT NAME_PARAMETER.NAME_PARAMETER saw_0 FROM POC_ONE_DOT_TWO ORDER BY saw_0
If anyone has any explanation to this error and how we can achieve the same, please help.
Thanks.Hello,
Updates :) sorry.. the error was a stupid one.. I resolved and I got stuck at my next step.
I am creating a physical table using a select query. But I am trying to obtain the name of the table dynamically.
Here is what I am trying to do. the select query of the physical table is as follows.
SELECT CUSTOMER_ID AS CUSTOMER_ID, CUSTOMER_NAME AS CUSTOMER_NAME FROM 'VALUEOF(NQ_SESSION.SCHEMA_NAME)'.CUSTOMER.
The idea behind this is to obtain the data from the same table from different schemas dynamically based on what a session variable. Please let me know if there is a way to achieve this, if not please let me know if this can be achieved in any other method in OBIEE.
Thanks. -
Select query in case of Multiple line items
Hi Gurus ,
I've a doubt in general SQL select query. I want to know , if suppose I've an internal table - itab . I've fetched G/L Account numbers 1st, based on the input selections . Next , I want to loop on those G/L accounts. However, if the G/L account has multiple line items, then I personally use this select query -- >
loop at itab.
select <field> from <table> appending corresponding fields of <itab1> where hkont eq itab-hkont.
endloop.
Now, the execution time for this query is longer than expected. The biggest problem here is, i've to sum up the totals as well. So totaling is an added load. I want to reduce the execution time of this. Kindly suggest me some good method in case u have any.
I've pasted the code which I've written , for u ppl to understand--
SELECT DISTINCT HKONT BELNR
FROM BSIS
INTO CORRESPONDING FIELDS OF TABLE OTAB
WHERE HKONT IN S_RACCT
* AND PRCTR IN P_PRCTR
AND MONAT IN S_POPER
AND BUKRS EQ P_BUKRS
AND GJAHR EQ P_GJAHR
AND PRCTR IN S_PRCTR.
***The code below takes a lot of time to execute.***
LOOP AT OTAB .
SELECT DMBTR HKONT
FROM BSIS APPENDING CORRESPONDING FIELDS OF TABLE CREDITS
WHERE HKONT EQ OTAB-HKONT
AND BELNR EQ OTAB-BELNR
AND MONAT IN S_POPER
AND BUKRS EQ P_BUKRS
AND GJAHR EQ P_GJAHR
AND PRCTR IN S_PRCTR
AND SHKZG EQ 'H'.
COLLECT CREDITS.
ENDLOOP.Hi,
First of all try to avoid doing select into corresponding fields. THis would improve the performance of the program.
Try to do a single fetch from the BSIS table . fetch the hkont, belnr, dmbtr fields in to a master internal table. Manipulate and play with the data as required. Don't hit the data base table more than once (unless it is required) . This would improve the performance of your code.
Try to code this way.
types: begin of ty_bsis,
hkont type hkont,
belnr type belnr_d,
dmbtr type dmbtr,
end of ty_bsis.
data: it_bsis type standard table of ty_bsis,
wa_bsis type ty_bsis,
select hkont belnr dmbtr
from bsis
into table it_bsis
WHERE HKONT IN S_RACCT
AND PRCTR IN P_PRCTR
AND MONAT IN S_POPER
AND BUKRS EQ P_BUKRS
AND GJAHR EQ P_GJAHR
AND PRCTR IN S_PRCTR.
Using the data availabe in the it_bsis, you can manipulate as required.
Hope this would be helpful
Regards
Ramesh Sundaram -
SQL query problem - select max (case... aggregate function)
Hi,
I have a problem with below sql query, it gives me problem/error message 'ORA-00937: not a single-group group function', why?
select sag.afdeling, sag.sagsnr, to_char(sag.start_dato, 'yyyy-mm-dd'), sag.stat, BOGF_TRANS.TRANSTYPE,
max (case when BOGF_TRANS.TRANSTYPE = 'K' then sum(bogf_trans.belobdkk) end) + -- as "TRANSTYPE K",
max (case when BOGF_TRANS.TRANSTYPE = 'D' then sum(bogf_trans.belobdkk) end) as "TRANSTYPE K & D",
max (case when BOGF_TRANS.TRANSTYPE = 'S' then sum(bogf_trans.belobdkk) end) as "SUM TRANSTYPE S"
from sag
join bogf_trans on sag.selskab = bogf_trans.selskab and sag.sagsnr = bogf_trans.sagsnr and sag.afdeling = bogf_trans.afdeling
where SAG.SELSKAB=37 and SAG.AFDELING = 'SUS' AND SAG.SAGSNR = 10876
group by sag.afdeling, sag.sagsnr, sag.start_dato, sag.stat, BOGF_TRANS.TRANSTYPE
If I exclude (columns) as below it give me correct summations (max (case... sum(...)) but then I miss some important info that I need
select
max (case when BOGF_TRANS.TRANSTYPE = 'K' then sum(bogf_trans.belobdkk) end) + -- as "TRANSTYPE K",
max (case when BOGF_TRANS.TRANSTYPE = 'D' then sum(bogf_trans.belobdkk) end) as "TRANSTYPE K & D",
max (case when BOGF_TRANS.TRANSTYPE = 'S' then sum(bogf_trans.belobdkk) end) as "SUM TRANSTYPE S"
from sag
join bogf_trans on sag.selskab = bogf_trans.selskab and sag.sagsnr = bogf_trans.sagsnr and sag.afdeling = bogf_trans.afdeling
where SAG.SELSKAB=37 and SAG.AFDELING = 'SUS' AND SAG.SAGSNR = 10876
group by sag.afdeling, sag.sagsnr, sag.start_dato, sag.stat, BOGF_TRANS.TRANSTYPE
Any ideas?Moved to more sutable forum, sorry.
-
Can we write select query regardless of case sensitivity?
Hello Everyone,
I have written one function module. And in this FM i am using one import parameter which has Data element and domain as type. In that i have checked Upper/Lower case checkbox, to retain case of the data in table. But when firing query to database i have to supply same case as it is stored in table.
so, can i ignore the case of the parameter in select query and can i fire the query regardless of the case of the where conition??
Thanks in Advance,
BhavikHi Bhavik,
You can do this without adding a column to the table.
Just Fetch all the data in one internal table then loop that table inside that loop check whether that field contains that string or not. For this you use CP operator, that checks for string irrespective of the case. This can perform wild serach also. The following code may help you,
SELECT * FROM /cpd/d_mp_hdr_s INTO TABLE it_mast_hdr_desc1.
LOOP AT it_mast_hdr_desc1 INTO wa_mast_hdr_desc.
IF wa_mast_hdr_desc-text CP lv_mp_text.
APPEND wa_mast_hdr_desc TO it_mast_hdr_desc.
CLEAR wa_mast_hdr_desc.
ENDIF.
ENDLOOP.
Here both the internal table and the work area are of type /cpd/d_mp_hdr_s.
Hope this helps you.
With Regards,
Ajeet Pratap Singh -
Case-sensitive wild-cards in Select Query
Hi,
I have to make a search help for Portal like it is in standard SAP's Who's Who search help in HR-ESS.
I have to fetch data (wildcards) from Portal & on the basis of that I need to give them the actual no. of matching data.
In short, I'll take only one data, say 'Short Text of Organizational Unit' - T527X-ORGTX .... ( 'Organizational unit' - PA0002-ORGEH ).
Here, in this case, I'm converting all '*' with '%' with this statement :
ORGTX1 = ORGTX.
REPLACE ALL OCCURRENCES OF '*' IN
ORGTX1 WITH '%' .
SELECT ORGEH FROM T527X
INTO CORRESPONDING FIELDS OF TABLE itab
WHERE ORGTX like ORGTX1
and SPRSL = 'EN'.
My problem comes, when the user enters data like HR dep and the actual data in the table is HR Department, then the select query doesn't work.
I'm providing some other cases:
Provided data by User Actual Data in Syatem
emp Employee
EMP1* Employee1
hr HR
Hr HR
so many, other permutations & combinations like this.
I've checked so many threads, but didn't get the actual answer. Some persons are saying to use translate, but I can only use translate after fetching the data. here the problem is with fetching itself & I can't judge at runtime that which letter the user will put in upper case & which in lowercase.
Can someone please guide me, how to tackle this problem ???Hello Mohit,
First of all, welcome to SDN!
What you have done as far as the implementation of the solution is concerned is quite correct. Please understand that wildcard search in SAP is case sensitive - so if the user enters data like HR dep * and the actual data in the table is HR Department then the user will not get any results back which is fully justified i.e. that's the way it should be. If you refer back to the table T527X and try a wildcard search on field ORGTX, the result is the same if you enter what the user is entering on the Portal i.e. HR dep*.
I think the users should be aware that this is how wildcard searches are designed to behave in SAP - this could be part of their training or you can even produce a message in your program when the select query fails.
The other option is of course to use TRANSLATE but there could be so many scenarios / permutation combinations - I don't suggest anyone goes that way to "guess" (or read) user's mind then deliver the solution because there will come a scenario when the program will be unable to "read" the user's mind as a result the select query will inevitably fail.
Hope this helps.
Cheers,
Sougata. -
Performance issue after Upgrade from 4.7 to ECC 6.0 with a select query
Hi All,
There is a Performance issue after Upgrade from 4.7 to ECC 6.0 with a select query in a report painter.
This query is working fine when executed in 4.7 system where as it is running for more time in ECC6.0.
Select query is on the table COSP.
SELECT (FIELD_LIST)
INTO CORRESPONDING FIELDS OF TABLE I_COSP PACKAGE SIZE 1000
FROM COSP CLIENT SPECIFIED
WHERE GJAHR IN SELR_GJAHR
AND KSTAR IN SELR_KSTAR
AND LEDNR EQ '00'
AND OBJNR IN SELR_OBJNR
AND PERBL IN SELR_PERBL
AND VERSN IN SELR_VERSN
AND WRTTP IN SELR_WRTTP
AND MANDT IN MANDTTAB
GROUP BY (GROUP_LIST).
LOOP AT I_COSP .
COSP = I_COSP .
PERFORM PCOSP USING I_COSP-_COUNTER.
CLEAR: $RWTAB, COSP .
CLEAR CCR1S .
ENDLOOP.
ENDSELECT.
I have checked with the table indexes, they were same as in 4.7 system.
What can be the reson for the difference in execution time. How can this be reduced without adjusting the select query.
Thanks in advance for the responses.
Regards,
Dedeepya.Hi,
ohhhhh....... lots of problems in select query......this is not the way you should write it.
Some generic comments:
1. never use SELECT
endselect.
SELECT
into table
for all entries in table
where.
use perform statment after this selection.
2. Do not use into corresponding fields. use exact structure type.
3. use proper sequence of fields in the where condition so that it helps table go according to indexes.
e.g in your case
sequence should be
LEDNR
OBJNR
GJAHR
WRTTP
VERSN
KSTAR
HRKFT
VRGNG
VBUND
PARGB
BEKNZ
TWAER
PERBL
sequence should be same as defined in table.
Always keep select query as simple as possible and perform all other calculations etc. afterwords.
I hope it helps.
Regards,
Pranaya -
Modify a SELECT Query on ISU DB tables to improve performance
Hi Experts,
I have a SELECT query in a Program which is hitting 6 DB tables by means of 5 inner joins.
The outcome is that the program takes an exceptionally long time to execute, the SELECT statement being the main time consumer.
Need your expertise on how to split the Query without affecting functionality -
The Query :
SELECT fkkvkpgpart eablablbelnr eabladat eablistablart
FROM eabl
INNER JOIN eablg ON eablgablbelnr = eablablbelnr
INNER JOIN egerh ON egerhequnr = eablequnr
INNER JOIN eastl ON eastllogiknr = egerhlogiknr
INNER JOIN ever ON everanlage = eastlanlage
INNER JOIN fkkvkp ON fkkvkpvkont = evervkonto
INTO TABLE itab
WHERE eabl~adat GT [date which is (sy-datum - 3 years)]
Thanks in advance,
PDHi Prajakt
There are a couple of issues with the code provided by Aviansh:
1) Higher Memory consumption by extensive use of internal tables (possible shortdump TSV_NEW_PAGE_ALLOC_FAILED)
2) In many instances multiple SELECT ... FOR ALL ENTRIES... are not faster than a single JOIN statement
3) In the given code the timeslices tables are limited to records active of today, which is not the same as your select (taking into account that you select for the last three years you probably want historical meter/installation relationships as well*)
4) Use of sorted/hashed internal tables instead of standard ones could also improve the runtime (in case you stick to all the internal tables)
Did you create an index on EABL including columns MANDT, ADAT?
Did you check the execution plan of your original JOIN Select statement?
Yep
Jürgen
You should review your selection, because you probably want business partner that was linked to the meter reading at the time of ADAT, while your select doesn't take the specific Contract / Device Installation of the time of ADAT into account.
Example your meter reading is from 16.02.2010
Meter 00001 was in Installation 3000001 between 01.02.2010 and 23.08.2010
Meter 00002 was in Installation 3000001 between 24.08.2010 and 31.12.9999
Installation 3000001 was linked to Account 4000001 between 01.01.2010 and 23.01.2011
Installation 3000001 was linked to Account 4000002 between 24.01.2010 and 31.12.9999
This means with your select returns four lines and you probably want only one.
To achieve that you have to limit all timeslices to the date of EABL-ADAT (selects from EGERH, EASTL, EVER).
Update:
Coming back to point one and the memory consumption:
What are you planning to do with the output of the select statment?
Did you get a shortdump TSV_NEW_PAGE_ALLOC_FAILED with three years meter reading history?
Or did you never run on production like volumes yet?
Dependent on this you might want to redesign your program anyway.
Edited by: sattlerj on Jun 24, 2011 10:38 AM -
Missing Expression Error When Case Statment is used as field
I have tried to write an expression that will calculate how many days ago a request was submitted and return a user defined value if the calculation falls within the criteria in the case statment. The SQL View for the Query I am using is shown below:
SELECT A.F_POSN_REQUEST_ID, A.F_REQ_ECP_STATUS, TO_CHAR(A.REQUEST_DTTM,'YYYY-MM-DD-HH24.MI.SS."000000"'), TO_CHAR(A.F_REQ_ST_DT,'YYYY-MM-DD-HH24.MI.SS."000000"'), TO_CHAR(A.F_REQ_ECP_ST_DT,'YYYY-MM-DD-HH24.MI.SS."000000"'), TO_CHAR(A.F_REQ_ECP_PROC_DT,'YYYY-MM-DD-HH24.MI.SS."000000"'), CASE WHEN(sysdate - TO_DATE(SUBSTR( TO_CHAR(A.F_REQ_ST_DT,'YYYY-MM-DD-HH24.MI.SS."000000"'),1,16),'YYYY-MM-DD-HH24.MI'))>60 THEN '>60' ELSE(CASE WHEN(sysdate - TO_DATE(SUBSTR( TO_CHAR(A.F_REQ_ST_DT,'YYYY-MM-DD-HH24.MI.SS."000000"'),1,16),'YYYY-MM-DD-HH24.MI')) >30 AND <=60 THEN '30 - 60' ELSE(CASE WHEN(sysdate - TO_DATE(SUBSTR( TO_CHAR(A.F_REQ_ST_DT,'YYYY-MM-DD-HH24.MI.SS."000000"'),1,16),'YYYY-MM-DD-HH24.MI'))>=15 AND <=30 THEN '15 - 30' ELSE(CASE WHEN(sysdate - TO_DATE(SUBSTR( TO_CHAR(A.F_REQ_ST_DT,'YYYY-MM-DD-HH24.MI.SS."000000"'),1,16),'YYYY-MM-DD-HH24.MI'))<15 THEN 'Less Than 15' ELSE NULL END)END)END)END))
FROM PS_F_POSN_REQUEST A
WHERE A.F_REQ_ECP_STATUS IN ('I','O','P')
GROUP BY A.F_POSN_REQUEST_ID, A.F_REQ_ECP_STATUS, TO_CHAR(A.REQUEST_DTTM,'YYYY-MM-DD-HH24.MI.SS."000000"'), TO_CHAR(A.F_REQ_ST_DT,'YYYY-MM-DD-HH24.MI.SS."000000"'), TO_CHAR(A.F_REQ_ECP_ST_DT,'YYYY-MM-DD-HH24.MI.SS."000000"'), TO_CHAR(A.F_REQ_ECP_PROC_DT,'YYYY-MM-DD-HH24.MI.SS."000000"')When I try to run the query I get Message=ORA-00936: missing expression (50,380)
Any suggestions you could provide to help me clear this error would be greatly appreciated.
Edited by: 992737 on Mar 8, 2013 9:07 AMHi,
Welcome to the forum!
I think you want something like this:
CASE
WHEN a.f_req_st_dt < TRUNC (SYSDATE) - 60 THEN '>60'
WHEN a.f_req_st_dt < TRUNC (SYSDATE) - 30 THEN '30.1-60'
WHEN a.f_req_st_dt < TRUNC (SYSDATE) - 15 THEN '15.1-30'
WHEN a.f_req_st_dt < TRUNC (SYSDATE) THEN '15 or less'
ENDNested CASE expressions aren't needed very much. Each WHEN clause is evaluated only if all the ealier ones have failed. For example, when testing to see if a row is in the 301.-60 range above (that is, the 2nd WHEN clause), there's no need to see if the difference is greater than or equal to 60; it wouldn't be evaluating that clause if the 1st WHEN condition was TRUE.
This assumes that a.f_req_st_dt is a TIMESTAMP or DATE; either datatype can be compared with DATEs, such as SYSDATE-60, so there's no need to conevert them to strings, and then convert them back into DATEs.
I hope this answers your question.
If not, post a little sample data (CREATE TABLE and INSERT statements, relevant columns only) for all tables involved, and also post the results you want from that data.
Simplify the problem as much as possible. Show only the parts that you don't already know how to do.
Explain, using specific examples, how you get those results from that data.
Always say which version of Oracle you're using (e.g., 11.2.0.2.0).
See the forum FAQ {message:id=9360002}
Never write, let alone post, unformatted code. Indent the code to show the exent and structure of clauses (SELECT, FROM, etc.), and complex expressions (such as CASE). The forum FAQ explains how to use \ tags when posting formatted text on this site. -
Issue with Case Statment Bit Urgent.
Hi Friends,
I am using a Case in my select statment but problem is i have used group by clause.
This is the main query.
SELECT papf.employee_number OHR,
papf.business_group_id,
(CASE
WHEN (paaf.effective_start_date = Min("paaf.effective_start_date" From Below Where condition))
THEN
TO_CHAR (
(SELECT ORIGINAL_DATE_OF_HIRE
FROM apps.per_all_people_f p1
WHERE p1.employee_number = papf.employee_number
AND p1.current_employee_flag = 'Y'),
'DD-MON-YYYY'
ELSE
TO_CHAR (MIN (paaf.effective_start_date), 'DD-MON-YYYY')
END) EFFECTIVE_START_DATE,
DECODE (TO_CHAR (MAX (paaf.effective_end_date), 'DD-MON-RRRR'),
'31-DEC-4712', NULL,
TO_CHAR (MAX (paaf.effective_end_date), 'DD-MON-RRRR')) EFFECTIVE_END_DATE,
FROM apps.per_all_people_f papf,
apps.per_all_assignments_f paaf,
apps.per_grades pg,
apps.per_jobs pj
WHERE papf.person_id = paaf.person_id
AND papf.employee_number = '2130'
AND papf.current_employee_flag = 'Y'
AND paaf.grade_id = pg.grade_id
AND paaf.job_id = pj.job_id
AND ( paaf.ass_attribute21 IS NOT NULL
OR paaf.ass_attribute22 IS NOT NULL
OR paaf.ass_attribute23 IS NOT NULL
OR paaf.ass_attribute24 IS NOT NULL
OR paaf.ass_attribute25 IS NOT NULL)
AND EXISTS
(SELECT pap.EMPLOYEE_NUMBER
FROM APPS.PER_ALL_PEOPLE_F PAP
WHERE TRUNC (SYSDATE) BETWEEN PAP.EFFECTIVE_START_DATE AND PAP.EFFECTIVE_END_DATE
AND PAP.CURRENT_EMPLOYEE_FLAG = 'Y'
AND PAP.EMPLOYEE_NUMBER = PAPF.EMPLOYEE_NUMBER)
AND PAAF.EFFECTIVE_START_DATE >
NVL (
(SELECT MAX (PAAFA.EFFECTIVE_START_DATE)
EFFECTIVE_START_DATE
FROM APPS.PER_ALL_ASSIGNMENTS_F PAAFA,
APPS.PER_PERIODS_OF_SERVICE PPOSA
WHERE PAAFA.PERIOD_OF_SERVICE_ID =
PPOSA.PERIOD_OF_SERVICE_ID
AND (UPPER (PPOSA.LEAVING_REASON) NOT LIKE
'%TRANSFER%')
AND PPOSA.ACTUAL_TERMINATION_DATE < SYSDATE
AND PAAFA.PERSON_ID IN
(SELECT DISTINCT PERSON_ID
FROM apps.per_all_people_f
WHERE employee_number =
PAPF.EMPLOYEE_NUMBER)),
TO_DATE ('01-JAN-1881')
GROUP BY papf.employee_number,
papf.person_id,
paaf.assignment_id,
fnd_date.canonical_to_date (papf.ATTRIBUTE29),
papf.business_group_id,
TRIM(SUBSTR (PAAF.ASS_ATTRIBUTE21,
INSTR (PAAF.ASS_ATTRIBUTE21, '-') + 1)),
TRIM(SUBSTR (PAAF.ASS_ATTRIBUTE22,
INSTR (PAAF.ASS_ATTRIBUTE22, '-') + 1)),
UPPER (paaf.ass_attribute23),
UPPER (paaf.ass_attribute24),
UPPER (paaf.ass_attribute25),
xx_ijp_get_loc (paaf.ass_attribute23),
pg.name,
pj.name
ORDER BY papf.employee_number, MIN (paaf.effective_start_date) DESC -----------------
Output of the query
EFFECTIVE_START_DATE EFFECTIVE_END_DATE
13-JAN-2010
15-DEC-2009 12-JAN-2010
09-JUN-2009 14-DEC-2009
05-JUN-2009 08-JUN-2009
22-SEP-2008 04-JUN-2009
21-APR-2008 21-SEP-2008
21-JAN-2008 20-APR-2008
01-JAN-2008 20-JAN-2008
04-APR-2007 31-DEC-2007While using when in case statment i want to put the condition from where condition of main query and pick the min value of EFFECTIVE_START_DATE column i.e 04-APR-2007 so i can replace this with employee Date of Joining through Case condtion.
Any solution for this.
Please suggest it's bit urgent.
Thanks
Bachan.Bachan wrote:
Please suggest it's bit urgent.Ok, I'll suggest it's a bit urgent...
"it's a bit urgent"
But only to you.
Everybody who posts a question would like it answering as soon as possible. Do you not think it's rude of you to expect that your question demands more immediate attention than someone elses? Is that fair on all the other people who've just asked their question and are patient enough to wait for answers? Or should they be happy that you're jumping up and down saying "me first! me first!"
What about everybody who's giving up their own time volunteering to answer questions? They have their own jobs to do as well. Do you not think it's rude to expect them to drop what they're doing to just help you because you think your question is "urgent".
Typically, you will find that less people will come to help you if you say your question is urgent because most people will ignore it as they consider it rude. The best way to ask you question is to give as much information as possible and ensure you state what your database version is, as well as providing table structures, example data and expected output, so that the most appropriate answer can be given.
Looking at your code, it's not clear what your problem is as we don't have the input data to try it ourselves.
One clear issue is:
TO_DATE ('01-JAN-1881')As you don't specify a format mask so it will rely on your NLS_DATE_FORMAT mask being set to DD-MON-YYYY which, if it's not, will cause the code to either give incorrect results or error. -
Select query in materialized view with two dblinks
Hi All,
We have oracle 10g On windows.
We are trying to create materialized view. Scenario is we have base table on other database and we are creating mview on different database.
Basa database have two schema's and i am selecting records from that two schema's using two private db links.
But when i am tryin gto create mview its not getting created. After 15 hrs. its still showing creation command and not finished.
Query is :-
Is it good practice to have two db links in select query of materialized view.Billy Verreynne wrote:
Chanchal Wankhade wrote:
Is it good practice to have two db links in select query of materialized view.Same db link being used twice, or two different db links?
If the former, you ideally want the local Oracle db to send the join to the remote database, and for the remote database to drive the join between those 2 tables. There is a hint (<i>driving_site</i>) that can be used - or the join query can be defined on the remote database as a view, and the local materialised view can then use that remote view.
If you have 2 different db links and joining across these - usually a bad idea to perform distributed database joins. There are lots of limitations as to how the tables can be joined. Worse case, full table scans of both remote tables, pulling all the rows from the 2 remote database tables to the local database, and joining these on the local database.
I have seen some severe performance issues in the past as a result of distributed joins. I'll rather use 2 materialised views for pulling both distributed tables's data locally, and then do the join on local data (using indexes, partition pruning, etc)Hi Billy,
My scenario is i have two database database A and database B. Database A is having two schema's SCOTT AND HR. SCOTT schema have select privileges on HR schema.
DB LINK is between Database B to Database A. name is db.link.B.A.oracle.com.
What if i priovide while creating materialized view, the schema name before the table name in database B for this particuler table so it will pick up the table from that schema using same DB LINK(db.link.B.A.oracle.com.) that i am using to fetch records from SCOTT schema.
Above schnario is like two base schema's and one db link using two schema. -
Issue returning a count of rows in a SELECT QUERY...
I am working in Oracle 11.2g and I have a query where I want to return the count of DAILY records as well as WEEKLY records for specific marketers.
Here is my query:
Select Mka_Mktr_No,
Case When Rat_Freq_Cd = 'D' Then Count(*) Else 0 End DailyCount,
case when Rat_Freq_Cd = 'W' then count(*) Else 0 End WeeklyCount
From Marketer_Account, Acct
Where Mka_Exp_Dt >= '01-NOV-2012'
And Mka_Eff_Dt <= '30-NOV-2012'
and rat_acct_no = mka_acct_no
And Rat_Usage_Cd = 'P'
and rat_freq_cd != 'M'
Group By Mka_Mktr_No, Rat_Freq_Cd
Order By Mka_Mktr_No;I would prefer to have the results show up on the SAME row, but instead I get the following results:
MKA_MKTR_NO DAILYCOUNT WEEKLYCOUNT
10005 68 0
10005 0 2 Note how it shows each count on a seperate row. Is there a way to tweak the query to show the counts on the same row:
MKA_MKTR_NO DAILYCOUNT WEEKLYCOUNT
10005 68 2 and not have two seperate rows?
Many thanks,
SeanDon't group by rat_freq_cd
SQL> with test_data as
2 (
3 select 1 mka_mktr_no, 'W' rat_freq_cd from dual union all
4 select 1 mka_mktr_no, 'W' rat_freq_cd from dual union all
5 select 1 mka_mktr_no, 'D' rat_freq_cd from dual union all
6 select 2 mka_mktr_no, 'D' rat_freq_cd from dual union all
7 select 2 mka_mktr_no, 'W' rat_freq_cd from dual union all
8 select 2 mka_mktr_no, 'W' rat_freq_cd from dual union all
9 select 2 mka_mktr_no, 'D' rat_freq_cd from dual union all
10 select 2 mka_mktr_no, 'D' rat_freq_cd from dual union all
11 select 2 mka_mktr_no, 'D' rat_freq_cd from dual union all
12 select 3 mka_mktr_no, 'D' rat_freq_cd from dual union all
13 select 3 mka_mktr_no, 'D' rat_freq_cd from dual union all
14 select 3 mka_mktr_no, 'W' rat_freq_cd from dual union all
15 select 3 mka_mktr_no, 'D' rat_freq_cd from dual union all
16 select 3 mka_mktr_no, 'D' rat_freq_cd from dual union all
17 select 3 mka_mktr_no, 'W' rat_freq_cd from dual
18 )
19 select
20 mka_mktr_no,
21 count(case when rat_freq_cd = 'D' then rat_freq_cd end) dailycount,
22 count(case when rat_freq_cd = 'W' then rat_freq_cd end) weeklycount
23 from
24 test_data
25 group by mka_mktr_no;
MKA_MKTR_NO DAILYCOUNT WEEKLYCOUNT
1 1 2
2 4 2
3 4 2 -
Can I retrieve data from a multiple select query?
I recently have been able to consolidate many queries into one large one, or into large compound queries. I found an instance where I am using a select query in a for loop that may execute up to 400 times. I tried building a huge compound query, and using DB Tools Execute Query vi, followed by the DB Tools Fetch Recordset Data vi. The query executes at the database without an error, but the Fetch Recordset Data vi only returns the first instance. Is there a way to retrieve all the data without having to send up to 400 separate select queries?
Sorry I didn't replt earlier, I was on vacation. The query I am using is to check serial numbers, and determine if they are all valid. The programs purpose is to define a serial number to a pre-existing part number. Our company makes inclinometers and accelerometers, and this entire series of LabVIEW programs is designed to automate the calibration and testing of these units. The part number definitions can contain 3 or 4 hundred parameters, so the database itself consistes of 44 tables with potentially several hundred columns per table. It is designed to not only provide definitions to every part number, but also to store all potential raw unit data to be calculated and formed into a report at any time. The logistics of getting that much data in and out of the database have forced me to do things more effeciently. The actual query in question is to take each serial number either manually entered, or automatically picked, and see if they already exist with the part number they are being defined as. If there are any duplicates, then the program will alert the operator that serial numbers x, y, and z for instance have already been asigned as the part number in question. Currently I run a simple query once for each serial number. This works, but there may be 200 serial numbers assigned. Also the serial numbers can contain upper or lower case letters. By making all the serial number letters into capitals, then into lower case, it could mean up to 400 individual queries going out over the LAN. This is a bandwidth hog, and time consuming. I started experimenting with compound queries. The actual query used is below.
SELECT SERIALNO FROM "maintable" WHERE PARTNO = '475196-001' AND SERIALNO = '3000005';SELECT SERIALNO FROM "maintable" WHERE PARTNO = '475196-001' AND SERIALNO = '3000006';SELECT SERIALNO FROM "maintable" WHERE PARTNO = '475196-001' AND SERIALNO = '3000007';SELECT SERIALNO FROM "maintable" WHERE PARTNO = '475196-001' AND SERIALNO = '3000008';SELECT SERIALNO FROM "maintable" WHERE PARTNO = '475196-001' AND SERIALNO = '3000009'
When I execute this query, SQL Server 2000 has no problem with it, but the DB Tools Fetch Recordset Data vi only returns the first match. I think my answer may lie with OR statements. Rather than sending what amounts to potentially dozens of individual queries, I should be able to chain them into one query with a lot of OR statements. As long as the OR statement is not an exclusive OR statement, I think it should work. I haven't tried it yet, and it may take some time to get the syntax right. The query is built in a for loop with the number of iterations equal to the number of serial numbers being defined. Once I get this working I will alter it to include both upper and lower case letters that can be included in the query. Any suggestiona of how the query should be structured would be most helpful, or another way to achieve what I am trying to accomplish.
SciManStev
Maybe you are looking for
-
CS6 beta uninstall issues.
I'm running OS 10.6.8 SL on my mac and have enjoyed using CS6 beta but I'm receiving mulitiple error messages upon trying to uninstall. There are apparently some missing or corrupt files per the error messages. The error messages suggested using the
-
/ the only supposedly "mounted" partition, fs segfaults / 3.1.0-4
[Update: Rebooting fixed the fs segfaults (were they segfaults?) although the "mounting filesystems... [FAIL]" message is still present.] I should probably have paid closer attention to the fact that ever since I went from 2.6 to 3.0 Arch has been sa
-
Hi, We have a Hierarchy where in we have 10 levels. The user is able to see all the data earlier and no he wants to see only up to level4 (ie the aggregate data). How can we handle this? Thanks.
-
Hi, I have install minisap 4.6 successfully . On the logon screen, I entered the - username: bcuser and password: minisap All in small letter. And when i press the ENTER key, it show an error message on the status bar: "No logon possible (no hw ID re
-
Reaching out for Enterprise Security Help
My current environment is a medium size hospital with mulitple campuses. We have a number of different types of devices; Laptops, CoW's (Computer on Wheels) 7921's, BlackBerry's. Currently the majority of my clients are running WPA/WPA2-PSK. Personal