The Select Query
Hi All,
SELECT matnr FROM mara INTO TABLE gt_mara
WHERE matnr IN s_matnr
AND mtart = p_mtart.
IF NOT gt_mara[] IS INITIAL.
SORT gt_mara BY matnr.
SELECT aufnr objnr aufpl FROM caufv INTO TABLE gt_caufv
FOR ALL ENTRIES IN gt_mara
WHERE aufnr IN s_aufnr
AND auart = p_auart
AND werks = p_werks
AND plnbez = gt_mara-matnr.
ENDIF.
I have replaced the above for all entries query by the below inner join. Found in the debugg both have same number of records in the (DEV server). Performance wise also this one is better. Let me know both are exactly same or not.
SELECT a~matnr
b~aufnr
b~objnr
b~aufpl
INTO TABLE gt_caufv FROM mara AS a INNER JOIN caufv AS b
ON amatnr = bplnbez WHERE matnr IN s_matnr
AND mtart =<b> p_mtart</b>
AND aufnr IN s_aufnr
AND auart = p_auart
AND werks = p_werks.
My concern with the join is <b>amatnr = bplnbez</b> is whether this will give me unique records or duplicate records. It seems to be good and unique in the DEV but not sure about the PROD server . Kindly Clarify.
Thanks,
The join by itself will allow duplicate records if they exist, since the same material could exist in more than one order.
However, the FOR ALL ENTRIES addition de-dups the result set; this is a feature of FOR ALL ENTRIES. This forces the original to be distinct apart from any other consideration.
If you want to guarantee the records are unique, why not just add DISTINCT?
e.g.
SELECT DISTINCT
a~matnr
b~aufnr
b~objnr
b~aufpl
INTO TABLE gt_caufv
FROM mara AS a
INNER JOIN caufv AS b
ON a~matnr = b~plnbez
WHERE matnr IN s_matnr
AND mtart = p_mtart
AND aufnr IN s_aufnr
AND auart = p_auart
AND werks = p_werks.
Alternatively, you could SORT the resulting itab and DELETE ADJACENT DUPLICATES.
In the case of DISTINCT, the sorting and deleting of duplicates occurs on the database server; in the case of explicit SORT and DELETE ADJACENT DUPLICATES, it occurs on the application server. This may be a consideration for performance.
Lastly, the join and FOR ALL ENTRIES are different in one other way.
If the itab gt_mara in FOR ALL ENTRIES is empty, the WHERE clause is ignored and the whole table is loaded. This is a feature of FOR ALL ENTRIES, which you can confirm in SAP HELP. If this is not the behavior that you expect, you need to check for it.
Good luck.
Brian
Edited for spelling, wording
Message was edited by:
Brian Sammond
Similar Messages
-
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. -
How to optimize the select query that is executed in a cursor for loop?
Hi Friends,
I have executed the code below and clocked the times for every line of the code using DBMS_PROFILER.
CREATE OR REPLACE PROCEDURE TEST
AS
p_file_id NUMBER := 151;
v_shipper_ind ah_item.shipper_ind%TYPE;
v_sales_reserve_ind ah_item.special_sales_reserve_ind%TYPE;
v_location_indicator ah_item.exe_location_ind%TYPE;
CURSOR activity_c
IS
SELECT *
FROM ah_activity_internal
WHERE status_id = 30
AND file_id = p_file_id;
BEGIN
DBMS_PROFILER.start_profiler ('TEST');
FOR rec IN activity_c
LOOP
SELECT DISTINCT shipper_ind, special_sales_reserve_ind, exe_location_ind
INTO v_shipper_ind, v_sales_reserve_ind, v_location_indicator
FROM ah_item --464000 rows in this table
WHERE item_id_edw IN (
SELECT item_id_edw
FROM ah_item_xref --700000 rows in this table
WHERE item_code_cust = rec.item_code_cust
AND facility_num IN (
SELECT facility_code
FROM ah_chain_div_facility --17 rows in this table
WHERE chain_id = ah_internal_data_pkg.get_chain_id (p_file_id)
AND div_id = (SELECT div_id
FROM ah_div --8 rows in this table
WHERE division = rec.division)));
END LOOP;
DBMS_PROFILER.stop_profiler;
EXCEPTION
WHEN NO_DATA_FOUND
THEN
NULL;
WHEN TOO_MANY_ROWS
THEN
NULL;
END TEST;The SELECT query inside the cursor FOR LOOP took 773 seconds.
I have tried using BULK COLLECT instead of cursor for loop but it did not help.
When I took out the select query separately and executed with a sample value then it gave the results in a flash of second.
All the tables have primary key indexes.
Any ideas what can be done to make this code perform better?
Thanks,
Raj.As suggested I'd try merging the queries into a single SQL. You could also rewrite your IN clauses as JOINs and see if that helps, e.g.
SELECT DISTINCT ai.shipper_ind, ai.special_sales_reserve_ind, ai.exe_location_ind
INTO v_shipper_ind, v_sales_reserve_ind, v_location_indicator
FROM ah_item ai, ah_item_xref aix, ah_chain_div_facility acdf, ah_div ad
WHERE ai.item_id_edw = aix.item_id_edw
AND aix.item_code_cust = rec.item_code_cust
AND aix.facility_num = acdf.facility_code
AND acdf.chain_id = ah_internal_data_pkg.get_chain_id (p_file_id)
AND acdf.div_id = ad.div_id
AND ad.division = rec.division;ALSO: You are calling ah_internal_data_pkg.get_chain_id (p_file_id) every time. Why not do it outside the loop and just use a variable in the inner query? That will prevent context switching and improve speed.
Edited by: Dave Hemming on Dec 3, 2008 9:34 AM -
How can i use index in select query.. facing problem with the select query.
Hi Friends,
I am facing a serious problem in one of the select query. It is taking a lot of time to fetch data in Production Scenario.
Here is the query:
SELECT * APPENDING CORRESPONDING FIELDS OF TABLE tbl_summary
FROM ztftelat LEFT JOIN ztfzberep
ON ztfzberep~gjahr = st_input-gjahr
AND ztfzberep~poper = st_input-poper
AND ztfzberepcntr = ztftelatrprctr
WHERE rldnr = c_telstra_accounting
AND rrcty = c_actual
AND rvers = c_ver_001
AND rbukrs = st_input-bukrs
AND racct = st_input-saknr
AND ryear = st_input-gjahr
And rzzlstar in r_lstar
AND rpmax = c_max_period.
There are 5 indices present for Table ZTFTELAT.
Indices of ZTFTELAT:
Name Description
0 Primary key( RCLNT,RLDNR,RRCTY,RVERS,RYEAR,ROBJNR,SOBJNR,RTCUR,RUNIT,DRCRK,RPMAX)
005 Profit (RCLNT,RPRCTR)
1 Ledger, company code, account (RLDNR,RBUKRS, RACCT)
2 Ledger, company code, cost center (RLDNR, RBUKRS,RCNTR)
3 Account, cost center (RACCT,RCNTR)
4 RCLNT/RLDNR/RRCTY/RVERS/RYEAR/RZZAUFNR
Z01 Activity Type, Account (RZZLSTAR,RACCT)
Z02 RYEAR-RBUKRS- RZZZBER-RLDNR
Can anyone help me out why it is taking so much time and how we can reduce it ? and also tell me if I want to use index number 1 then how can I use?
Thanks in advance.Hi Shiva,
I am using two more select queries with the same manner ....
here are the other two select query :
***************1************************
SELECT * APPENDING CORRESPONDING FIELDS OF TABLE tbl_summary
FROM ztftelpt LEFT JOIN ztfzberep
ON ztfzberep~gjahr = st_input-gjahr
AND ztfzberep~poper = st_input-poper
AND ztfzberepcntr = ztftelptrprctr
WHERE rldnr = c_telstra_projects
AND rrcty = c_actual
AND rvers = c_ver_001
AND rbukrs = st_input-bukrs
AND racct = st_input-saknr
AND ryear = st_input-gjahr
and rzzlstar in r_lstar
AND rpmax = c_max_period.
and the second one is
*************************2************************
SELECT * APPENDING CORRESPONDING FIELDS OF TABLE tbl_summary
FROM ztftelnt LEFT JOIN ztfzberep
ON ztfzberep~gjahr = st_input-gjahr
AND ztfzberep~poper = st_input-poper
AND ztfzberepcntr = ztftelntrprctr
WHERE rldnr = c_telstra_networks
AND rrcty = c_actual
AND rvers = c_ver_001
AND rbukrs = st_input-bukrs
AND racct = st_input-saknr
AND ryear = st_input-gjahr
and rzzlstar in r_lstar
AND rpmax = c_max_period.
for both the above table program is taking very less time .... although both the table used in above queries have similar amount of data. And i can not remove the APPENDING CORRESPONDING. because i have to append the data after fetching from the tables. if i will not use it will delete all the data fetched earlier.
Thanks on advanced......
Sourabh -
Problem in the "select" query....Just check out and help me
Hi there !!
I am getting the following errors while trying to execute the "select" query from JSP page:
java.sql.SQLException: [Microsoft][ODBC Driver Manager] Invalid cursor state
My code is :
String reg_no=(String)session.getAttribute("reg_no");
ResultSet rs=stmt.executeQuery("SELECT * FROM reg WHERE regno = "+reg_no);
(Note: the "regno" in the database is of numeric datatype)
Try to help me...
Thanx,
Ram.Just so everyone knows:
Ram Mohan joined the forum on 7-Sep-2003 and received
his 25 Dukes for joining.
Since then, he's posted three questions, each one
promising 5-10 Dukes, and never once awarded them.
His strategy seems to be to wave Dukes, ask his
hit-and-run question, and then bolt.
Answer his questions if you wish, but don't do it
expecting Dukes. - MODDuffy, you're getting just as cranky as I have. See what hanging around these forums does to oneself? :) -
Need some help with the Select query.
Need some help with the Select query.
I had created a Z table with the following fields :
ZADS :
MANDT
VKORG
ABGRU.
I had written a select query as below :
select single vkorg abgru from ZADS into it_rej.
IT_REJ is a Work area:
DATA : BEGIN OF IT_REJ,
VKORG TYPE VBAK-VKORG,
ABGRU TYPE VBAP-ABGRU,
END OF IT_REJ.
This is causing performance issue. They are asking me to include the where condition for this select query.
What should be my select query here?
Please suggest....
Any suggestion will be apprecaiated!
Regards,
DeveloperHello Everybody!
Thank you for all your response!
I had changes this work area into Internal table and changed the select query. PLease let me know if this causes any performance issues?
I had created a Z table with the following fields :
ZADS :
MANDT
VKORG
ABGRU.
I had written a select query as below :
I had removed the select single and insted of using the Structure it_rej, I had changed it into Internal table
select vkorg abgru from ZADS into it_rej.
Earlier :
IT_REJ is a Work area:
DATA : BEGIN OF IT_REJ,
VKORG TYPE VBAK-VKORG,
ABGRU TYPE VBAP-ABGRU,
END OF IT_REJ.
Now :
DATA : BEGIN OF IT_REJ occurs 0,
VKORG TYPE VBAK-VKORG,
ABGRU TYPE VBAP-ABGRU,
END OF IT_REJ.
I guess this will fix the issue correct?
PLease suggest!
Regards,
Developer. -
Restricting record in the select query
Is there any way to restrict the number of search (e.g 10) records in the SELECT query ?
Hi,
You can use as below:
select <fieldname> up to 10 rows from <tablename> into itab.
Thanks,
Sriram Ponna. -
Performance of the select query
i want to know wheter the conversion of the select query
SELECT * FROM mchb
WHERE matnr EQ mch1-matnr
AND charg EQ mch1-charg
AND werks EQ plant-werks.
itab-lgort = mchb-lgort.
itab-clabs = mchb-clabs.
APPEND itab.
ENDSELECT.
into following pattern
select lgort clabs
from mchb
into corresponding fields of itab
WHERE matnr EQ mch1-matnr
AND charg EQ mch1-charg
AND werks EQ plant-werks.
ENDSELECT.
will give the same results ?Hi,
It will results in performance issue because of end select statement.
select lgort clabs
from mchb
into table itab
WHERE matnr EQ mch1-matnr
AND charg EQ mch1-charg
AND werks EQ plant-werks.
Use like this you will get the values of lgort and clabs in the internal table itab.
Then use loop statement to use that values further.
Dont use end select it will be a performance issue.
In this code there is no need to use of ENDSELECT and use corresponding fields of table itab.
select lgort clabs
from mchb
into corresponding fields of itab
WHERE matnr EQ mch1-matnr
AND charg EQ mch1-charg
AND werks EQ plant-werks.
ENDSELECT.
Thanks,
Sakthi -
How to insert the select query result into table?
How to insert the select query result into table?
SELECT top 20 creation_time
,last_execution_time
,total_physical_reads
,total_logical_reads
,total_logical_writes
, execution_count
, total_worker_time
, total_elapsed_time
, total_elapsed_time / execution_count avg_elapsed_time
,SUBSTRING(st.text, (qs.statement_start_offset/2) + 1,
((CASE statement_end_offset
WHEN -1 THEN DATALENGTH(st.text)
ELSE qs.statement_end_offset END
- qs.statement_start_offset)/2) + 1) AS statement_text
FROM sys.dm_exec_query_stats AS qs
CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) st
ORDER BY total_elapsed_time / execution_count DESC;
Thanks,
Tirumala1. SELECT INTO
Below method will create table when data is inserted from one table to another table. Its useful when you need exactly same datatype as source table.
Use AdventureWorks2008R2;
Go
---Insert data using SELECT INTO
SELECT AddressLine1, City
INTO BothellAddresses
FROM Person.Address
where City = 'Bothell';
GO
---VERIFY DATA
Select AddressLine1, City
FROM BothellAddresses
---DROP TABLE
DROP TABLE BothellAddresses
GO
2. INSERT INTO SELECT
Below method will need table to be created prior to inserting data. Its really useful when table is already created and you want insert data from
another table.
Use AdventureWorks2008R2;
Go
---Create Table
CREATE TABLE BothellAddresses (AddressLine1 NVARCHAR(60), City NVARCHAR(30))
---Insert into above table using SELECT
INSERT INTO BothellAddresses(AddressLine1, City)
SELECT AddressLine1, City
FROM Person.Address
where City = 'Bothell';
---VERIFY DATA
Select AddressLine1, City
FROM BothellAddresses
---DROP TABLE
DROP TABLE BothellAddresses
GO
Regards,
Vishal Patel
Blog: http://vspatel.co.uk
Site: http://lehrity.com -
Using order by in the select query
Hi All,
I have query in which I need the latest record. To get the latest record, I can
1) Select all entries into an internal table and sort them descending and get the first one
2) Select all entries into an internal table and order by in the select statement itself.
Then read the first one.
Which approach should be better as for as the performance is concern.
The query is on MKPF table and I have the material document number. I need the latest entry.
Regards,
ShahuHi,
The first one would be better.At the most we should not use order by in select statement bcoz it directly sorts the data according to that in database itself reverting performance issues.
Otherwise you can use select max* if records are less.
Reward points if helpful.
Thanks,
Ponraj.s. -
Hi Experts,
I have a select query as follows:
select field1 field2 from db1 into table it1.
Now suppose the it1-field1 contains values a,b and c.
Now I want to select fields a,b and c from another db table db2.
Instead of hard coding like this:
select a b c from db2 into table it2.....
How can I do this?
Points will be awarded.
Regards,
Soumya.Hi Soumya ,
Pleas use FOR ALL ENTRIES syntax , in this way you can select the records from db2 which are available or selected in db1.
Like Header And Item table.
You select records frm header table . then for corres ponding header records you select from detail table db2 .....
Condition is the field containg a,b,c is key field in both tables ...
Reward points if it helps you .
Regards
Amber S -
Hi ,
Can we write a select query where we can pass varray in the where condition.
I have this query in my stored procedure.
eg :
SELECT * from emp
WHERE deptno in l_data1;
where l_data is varray of type varchar2(40) where I have all the deptno in l_data.
Thanks.This is business requirement.
Here is my code.
create or replace PROCEDURE getAttribute (p_product_model varchar2)
AS
l_data pattern_str_type := pattern_str_type();
l_count NUMBER;
l_child_name VARCHAR2(200);
l_product_model VARCHAR2(40);
l_data1 getAttrforModel := getAttrforModel();
i NUMBER;
BEGIN
i := 1;
l_data :=getattr();
for i in 1 .. l_data.count loop
l_data.extend;
l_data1.extend;
if(l_data(i).child_type = 'Attribute' and l_data(i).product_model_name <> p_product_model)THEN
l_data1(i):=l_data(i).child_name;
dbms_output.put_line(l_data1(i));
END If;
end loop;
SELECT count(*) into l_count from
WOC_ATTRIBUTE_CATEGORY WHERE category_name = 'Software' and Attribute_Name in l_data1;
EXCEPTION
WHEN OTHERS THEN
RAISE;
END ;
CREATE OR REPLACE FUNCTION getAttr RETURN pattern_str_type
IS
l_data pattern_str_type := pattern_str_type();
l_count NUMBER;
l_child_name VARCHAR2(200);
l_product_model VARCHAR2(40);
i NUMBER;
cursor getattr IS select child_name,child_type, product_model_name from woc_pattern_Structure;
BEGIN
i := 1;
for rec in getattr loop
l_data.extend;
l_data(i) := temp_table(rec.child_name,rec.child_type,rec.product_model_name);
--dbms_output.put_line(l_data(i).child_name||' '||l_data(i).child_type||' '||l_data(i).product_model_name);
i := i + 1;
end loop;
RETURN l_data;
EXCEPTION
WHEN OTHERS THEN
RAISE;
END ; -
How to write the select query with complex where condition
Hi all,
Can u help me in writing following select query.
select * from zu1cd_corr where time_stamp between firstday and lastday .
In the above query time_stamp contains the date and time.
where as firstday and lastday contains the dates.
I need to compare the date in the time_stamp with the firstday and lastday.
But time_stamp contains the time also and it is char of 14 characters length.Hi,
If that is the case u can do as advait specified....
if the firstday and secondday are select-options then declare two more variables having 14 character length and then concatenate '000000' to firstday variable and '240000' to last day variable and then write ur query.
CLEAR : lv_firstday,
lv_lastday.
concatenate firstday '000000' to lv_firstday.
concatenate lastday '240000' to lv_lastday.
ranges : r_Date for zu1cd_corr-time_stamp.
r_date-sign = 'I'.
r_date-option = 'BT'.
r_Date-low = lv_firstday.
r_Date-high = lv_lastday.
append r_date.
select * from zu1cd_corr into table it_zu1cd_corr where time_stamp in r_Date.
I hope it helps.
Regards,
Nagaraj -
Help reagrding the selection query
Hi All...
I have VBELN
LFART
VKORG
KUNNR
ERDAT
LFDAT
VSTEL
ROUTE in my selection-screen. All the fields are from LIKP.
Now I need to add one more field LAND1 from the table VBPA in the selection screen.
This is my logic to filter the previous requirement. Now how to validate the LAND1?
-> LIKP
select vbeln
into table i_likp
from likp
where vbeln in s_vbeln
and lfart in s_lfart
and vkorg in s_vkorg
and kunnr in s_kunnr
and erdat in s_erdat
and lfdat in s_lfdat
and vstel = p_vstel
and route in s_route.
if sy-subrc = 0.
-> LIPS
select vbeln posnr bwtar volum
into table i_lips
from lips
for all entries in i_likp
where vbeln = i_likp-vbeln.
Please help me in that. I will reward u with good points.
Reagrds
Arjun JainFor LAND1 validation do as below.
AT SELECTION-SCREEN ON s_land1.
SELECT land1 FROM t005
into t005-land1
where land1 IN s_land1.
if sy-subrc NE 0.
message e000 WITH 'Enter valid Land value'.
endif.
For the query u can use join
select a~vbeln b~posnr b~bwtar b~volum
from likp as a inner join lips as b
on a~vbeln =b~vbeln
into table i_tab
where a~vbeln in s_vbeln
and a~lfart in s_lfart
and a~vkorg in s_vkorg
and a~kunnr in s_kunnr
and a~erdat in s_erdat
and a~lfdat in s_lfdat
and a~vstel = p_vstel
and a~route in s_route.
This will solve ur problem.
U can reward for the helpful answers. -
Hi All
I want to know how exactly the internal flow of select query works. Can someone please explain
ThanksHi,
Thanks for the link.Is there any example for back end process of Select queryBingo.. ;)
See, a process (or) set of programs which are working internally. You can get some of the results about the statistics of query how much cpu time it consuming, fetches etc., check v$sql_monitor. Apart from that if you are looking for some thing else then kindly elaborate your requirement.
- Pavan Kumar N -
Need single record for single period. Modify the select query.
Requirement
THere are 6 programs for each company for each billing period. If anyof the program for one billing period and period_balance>0 then display outstanding balance YES. if period_balance <=0 for all the programs for one period then outstanding balance=NO
Below is the query i have used but i have to modify the query to get single record for each period whether outstandignbalance yes or no. Please help to resolve this issue. OR modify the query.
select distinct B_Billing_key, B_COMPANY_ID company ,
to_char(to_date('01/'||trim(substr(B_REPORT_PERIOD,5,2))||'/'||
trim(substr(B_REPORT_PERIOD,1,4)),'DD/MM/YYYY'),'Month YYYY') Billing,
Max(to_char(P_RECEIVED_DATE,'MM/DD/YY')) lastPaymentdate,
SURCH_AMOUNT,ADJUST_AMOUNT,PI_AMOUNT,AMOUNT,
(nvl(SURCH_AMOUNT,0)+nvl(ADJUST_AMOUNT,0)+nvl(PI_AMOUNT,0))-(nvl(sum(AMOUNT),0))"Period_Balance",
decode(sign((nvl(SURCH_AMOUNT,0)+nvl(ADJUST_AMOUNT,0)+nvl(PI_AMOUNT,0))-(nvl(sum(AMOUNT),0))),1, 'Yes'
,'No'
) outstandingbalance
p.PROGRAM_NAME_ID programname
FROM tuff_balance_view,MV_PROG_SURCH S,MV_PAYMENT_HOLDING H,MV_PROGRAM_DICT P where
b_company_id = 'U-7052-C'
and B_Billing_key=s.BILLING_KEY
and S.PROGRAM_KEY = P.PROGRAM_KEY
and P.PROGRAM_KEY= H.PROGRAM_KEY
GROUP BY B_Billing_key,B_COMPANY_ID,B_REPORT_PERIOD,SURCH_AMOUNT,ADJUST_AMOUNT,PI_AMOUNT,PROG_SURCH_KEY,
S.PROGRAM_KEY,p.PROGRAM_NAME_ID,AMOUNT
order by B_Billing_key desc
B_Billing_key is the primary key. I am looking for the output only one record for each biling perid. there are 6 programs for each billing period. if any of the program has period balance >0 then outstanding balance should be yes. For sample i am giving 2 programs.
Actual output from the above
biling_key company billing period period_balance outstandingbalance programname
123 xyz January 2011 $4 Yes ABC
123 xyz January 2011 $-5 NO DEF
456 xyz February 2011 $-3 NO ABC
456 xyz February 2011 $- 2 NO DEF
Need the output as below from the above query. Can you please help to simplify query. If anyof theprogram having outstanding balance for that particular period show the outstandigbalance as yes. Else NO.
Expected output as below
company billing period outstandingbalance programname
xyz January 2011 Yes ABC
xyz February 2011 No DEF
Thanks,
viHi G,
With the same query i am gettting outs as NO for all months which is not true. can you pelase look at the following data.
SELECT DISTINCT B_BILLING_KEY,
to_char(to_date('01/'||trim(substr(B_REPORT_PERIOD,5,2))||'/'||
trim(substr(B_REPORT_PERIOD,1,4)),'DD/MM/YYYY'),'Month YYYY') Billing,
B_company_id company,
sum((nvl(T.B_ORG_SURCH_AMOUNT,0)+nvl(T.B_ORG_PI_AMOUNT,0))-(nvl(T.P_AMOUNT,0))) "PeriodBalance",
Max(to_char(P_RECEIVED_DATE,'MM/DD/YY')) LastPaymentDate,
decode(sign(
(nvl(T.B_ORG_SURCH_AMOUNT,0)+nvl(T.B_ORG_PI_AMOUNT,0))-(nvl(T.P_AMOUNT,0))), 1,'Yes','No') Outs
FROM mv_program_dict P, tuff_balance_view T WHERE
b_company_id = 'U-7052-C' group by B_REPORT_PERIOD,B_company_id,B_BILLING_KEY,B_ORG_SURCH_AMOUNT,
B_ORG_PI_AMOUNT,P_AMOUNT
order by B_BILLING_KEY desc
Actual
Billing key Billing company periodbalance lastpayment date outs
110631534073 November 2010 U-7052-C 270 Yes
110631534073 November 2010 U-7052-C 690 Yes
110631534073 November 2010 U-7052-C 66 Yes
110461533197 October 2010 U-7052-C 4740 Yes
110461533197 October 2010 U-7052-C 27000 Yes
110461533197 October 2010 U-7052-C 0 No
110251532527 September 2010 U-7052-C 0 09/13/10 No
110251532527 September 2010 U-7052-C 0 No
110251532527 September 2010 U-7052-C -18 09/13/10 No
110251532484 August 2010 U-7052-C 0 09/13/10 No
110251532484 August 2010 U-7052-C 2001 09/13/10 Yes
110251532484 August 2010 U-7052-C 0 No
Expectedoutput(need only following columns)
Billing key Billing company lastpayment date outs
110631534073 November 2010 U-7052-C Yes
110461533197 October 2010 U-7052-C Yes
110251532527 September 2010 U-7052-C 09/13/10 No
110251532484 August 2010 U-7052-C 09/13/10 YES
By using below query i am getting all output as NO. HOw to modify it.
SELECT company,
billing,LastPaymentDate,
CASE
WHEN SUM (DECODE (outs, 'YES', 1, 0)) > 0 THEN 'YES'
ELSE 'NO'
END Outstanding
FROM (
SELECT DISTINCT B_BILLING_KEY,
to_char(to_date('01/'||trim(substr(B_REPORT_PERIOD,5,2))||'/'||
trim(substr(B_REPORT_PERIOD,1,4)),'DD/MM/YYYY'),'Month YYYY') Billing,
B_company_id company,
sum((nvl(T.B_ORG_SURCH_AMOUNT,0)+nvl(T.B_ORG_PI_AMOUNT,0))-(nvl(T.P_AMOUNT,0))) "PeriodBalance",
Max(to_char(P_RECEIVED_DATE,'MM/DD/YY')) LastPaymentDate,
decode(sign(
(nvl(T.B_ORG_SURCH_AMOUNT,0)+nvl(T.B_ORG_PI_AMOUNT,0))-(nvl(T.P_AMOUNT,0))), 1,'Yes','No') Outs
FROM mv_program_dict P, tuff_balance_view T WHERE
b_company_id = 'U-7052-C' group by B_REPORT_PERIOD,B_company_id,B_BILLING_KEY,B_ORG_SURCH_AMOUNT,
B_ORG_PI_AMOUNT,P_AMOUNT
order by B_BILLING_KEY desc)
GROUP BY company, billing,LastPaymentDate;
Note:in the actual out put max(lastpayment date) is returing null values. if there is any date in one billing return that date only remove null example is september. in september it should return only 09/13/10 this date not null date. but if there is no other within one biling then consider that as null example november..
Thanks,
v
Maybe you are looking for
-
Include filename in the Content/Binary
Hi, I'm using import files from Folder. When opening Content/Binary I get the records from all the files in the folder. I would like to add the filename to every record. Is that possible?
-
Hi SAP Gurus, Currently we are using KOB3 report to analyse Production Variance. However, when executed it is not bring in any data... even for a specific order also data is not being shown. It is simply says - NO DATA for the Selection. We have lot
-
Is there a way to trade in old ipods and get the correct value for them?
I have a 30gig video which i bought only 2 years ago for about 400 bucks, and its still in great condition(works perfectly, only has a couple scratches which arent even on the screen) what should i do to get the around $200 it's still worth, because
-
Checking the size of the image..
Hi All, i have attached am image from using attachments functionality from Purchasing module. the image get stored in table fnd_lobs.. Now i want to know the size of the image.. whether it is 30kb or 1MB.. Is there any function in Oracle to calculate
-
A Finder bug (or bad design) -- cmd+del while renaming
This reproducible behavior is quite confusing: 1. In a Finder window, click on a file twice to enter the renaming mode. 2. Type some new file name. 3. While still in renaming mode, press Command+Delete. 4. The file will be moved to the Trash, yet in