Tuning of SQL query in PO base tables
Hi all,
Is there any way to optimize the below query as its taking too many hours to exectute when I pass the dates parameters for long period like eg. 6months to 1 year. Am I missing something...?
SELECT poh.org_id,
rcvt.organization_id,
mcat.segment1,
pov.vendor_name,
poh.vendor_id,
rcvt.transaction_type,
rcvt.primary_quantity,
poh.rate,
pol.unit_price,
NULL,
pvs.attribute8,
pvs.address_line1,
pvs.address_line2,
pvs.city,
pvs.state,
pvs.zip,
pvs.country
FROM rcv_transactions rcvt,
mtl_item_categories icat,
po_line_locations_all poll,
po_lines_all pol,
mtl_system_items_b msi,
po_headers_all poh,
po_vendors pov,
mtl_categories_b mcat,
po_vendor_sites_all pvs,
mtl_categories_b mcatedi
WHERE pov.vendor_id = poh.vendor_id
AND poh.po_header_id = pol.po_header_id
AND msi.inventory_item_id = pol.item_id
AND pov.vendor_id = pvs.vendor_id
AND pvs.vendor_site_id = poh.vendor_site_id
AND msi.organization_id = 117
AND mcatedi.category_id = pol.category_id
AND poll.po_line_id = pol.po_line_id
AND icat.organization_id = msi.organization_id +0
AND icat.inventory_item_id = msi.inventory_item_id +0
AND icat.category_set_id = 1
AND icat.category_id = mcat.category_id
AND rcvt.po_line_location_id = poll.line_location_id
AND rcvt.transaction_date BETWEEN p_Start_Date AND p_End_Date
AND ((rcvt.transaction_type = 'RETURN TO VENDOR') OR (rcvt.transaction_type = 'RECEIVE' ))
AND ((rcvt.transaction_type = 'RECEIVE') OR (rcvt.transaction_type = 'RETURN TO VENDOR')
AND nvl(rcvt.reason_id, 110) IN
SELECT reason_id
FROM mtl_transaction_reasons
WHERE disable_date IS NULL AND reason_id NOT IN(20 , 21 , 22 , 23, 24 , 25 , 26 ,27 , 28 , 29 , 50 , 51 , 52 , 53 , 54 , 55 ,100 ,102, 103, 122 ,123 , 124)
Thank you all in advance....
This is the explain I have in my instance. Looks like there is a different in indexes.
I have no full table scan on any table in the SQL I sent you. There are the changes I have made:
Removing the table --, mtl_categories_b mcatedi. It is not necessary to link the categories to PO lines unless you are entering POs without items. That I think you are not (looks like) as you are looking at the transactions line Receive and Retrun to vendor so I assumed these inventory purchases where you are using item. Hence removed that.
One more possibility is that you are reciving the expense POs also (accruing on receipt for expese purchases). If you want to add that table and condition (AND mcatedi.category_id = pol.category_id) add that and test.
Mistakenly commented out AND pvs.vendor_site_id = poh.vendor_site_id. You can add that back.
Added this where condition:
AND rcvt.organization_id = msi.organization_id
Reason: You are receiving into organization where the item is assigned to. If you remove this where condition it will do full table scan on the tables you mentioned along with the po_vendors_all. This is what making all the difference.
Remaining all is your SQL only.
SQL
====================================
SELECT poh.org_id, rcvt.organization_id, mcat.segment1, pov.vendor_name, poh.vendor_id
, rcvt.transaction_type, rcvt.primary_quantity, poh.rate, pol.unit_price, NULL, pvs.attribute8
, pvs.address_line1, pvs.address_line2, pvs.city, pvs.state, pvs.zip, pvs.country
FROM po_headers_all poh
, po_lines_all pol
, po_line_locations_all poll
, po_vendors pov
, po_vendor_sites_all pvs
, rcv_transactions rcvt
, mtl_system_items_b msi
, mtl_item_categories icat
, mtl_categories_b mcat
, mtl_categories_b mcatedi
WHERE poh.po_header_id = pol.po_header_id
AND pol.po_line_id = poll.po_line_id
AND poh.vendor_id = pov.vendor_id
AND pov.vendor_id = pvs.vendor_id
AND pol.item_id = msi.inventory_item_id
AND rcvt.organization_id = msi.organization_id
AND pvs.vendor_site_id = poh.vendor_site_id
AND msi.organization_id = 117
AND mcatedi.category_id = pol.category_id
AND msi.organization_id = icat.organization_id
AND msi.inventory_item_id = icat.inventory_item_id
AND icat.category_set_id = 1
AND icat.category_id = mcat.category_id
AND rcvt.po_line_location_id = poll.line_location_id
AND trunc(rcvt.transaction_date) BETWEEN ('01-JAN-2008') AND ('31-MAR-2008')
AND ((rcvt.transaction_type = 'RETURN TO VENDOR') OR (rcvt.transaction_type = 'RECEIVE'))
AND ( (rcvt.transaction_type = 'RECEIVE')
OR (rcvt.transaction_type = 'RETURN TO VENDOR')
AND NVL (rcvt.reason_id, 110) IN (
SELECT reason_id
FROM mtl_transaction_reasons
WHERE disable_date IS NULL
AND reason_id NOT IN
(20
, 21
, 22
, 23
, 24
, 25
, 26
, 27
, 28
, 29
, 50
, 51
, 52
, 53
, 54
, 55
, 100
, 102
, 103
, 122
, 123
, 124
==================
Plan
======================
SELECT STATEMENT CHOOSECost: 388 Bytes: 306 Cardinality: 1
30 FILTER
26 NESTED LOOPS Cost: 388 Bytes: 306 Cardinality: 1
24 NESTED LOOPS Cost: 388 Bytes: 302 Cardinality: 1
21 NESTED LOOPS Cost: 387 Bytes: 290 Cardinality: 1
18 NESTED LOOPS Cost: 385 Bytes: 150 Cardinality: 1
15 NESTED LOOPS Cost: 384 Bytes: 122 Cardinality: 1
12 NESTED LOOPS Cost: 383 Bytes: 91 Cardinality: 1
10 NESTED LOOPS Cost: 381 Bytes: 73 Cardinality: 1
8 NESTED LOOPS Cost: 380 Bytes: 64 Cardinality: 1
5 NESTED LOOPS Cost: 379 Bytes: 41 Cardinality: 1
2 TABLE ACCESS BY INDEX ROWID PO.RCV_TRANSACTIONS Cost: 378 Bytes: 31 Cardinality: 1
1 INDEX RANGE SCAN NON-UNIQUE PO.RCV_TRANSACTIONS_N22 Cost: 11 Cardinality: 3,490
4 TABLE ACCESS BY INDEX ROWID PO.PO_LINE_LOCATIONS_ALL Cost: 1 Bytes: 10 Cardinality: 1
3 INDEX UNIQUE SCAN UNIQUE PO.PO_LINE_LOCATIONS_U1 Cardinality: 1
7 TABLE ACCESS BY INDEX ROWID PO.PO_LINES_ALL Cost: 1 Bytes: 23 Cardinality: 1
6 INDEX UNIQUE SCAN UNIQUE PO.PO_LINES_U1 Cardinality: 1
9 INDEX UNIQUE SCAN UNIQUE INV.MTL_SYSTEM_ITEMS_B_U1 Cost: 1 Bytes: 9 Cardinality: 1
11 INDEX RANGE SCAN UNIQUE INV.MTL_ITEM_CATEGORIES_U1 Cost: 2 Bytes: 18 Cardinality: 1
14 TABLE ACCESS BY INDEX ROWID PO.PO_HEADERS_ALL Cost: 1 Bytes: 31 Cardinality: 1
13 INDEX UNIQUE SCAN UNIQUE PO.PO_HEADERS_U1 Cardinality: 1
17 TABLE ACCESS BY INDEX ROWID PO.PO_VENDORS Cost: 1 Bytes: 28 Cardinality: 1
16 INDEX UNIQUE SCAN UNIQUE PO.PO_VENDORS_U1 Cardinality: 1
20 TABLE ACCESS BY INDEX ROWID PO.PO_VENDOR_SITES_ALL Cost: 2 Bytes: 140 Cardinality: 1
19 INDEX RANGE SCAN UNIQUE PO.PO_VENDOR_SITES_U1 Cost: 1 Cardinality: 1
23 TABLE ACCESS BY INDEX ROWID INV.MTL_CATEGORIES_B Cost: 1 Bytes: 12 Cardinality: 1
22 INDEX UNIQUE SCAN UNIQUE INV.MTL_CATEGORIES_B_U1 Cardinality: 1
25 INDEX UNIQUE SCAN UNIQUE INV.MTL_CATEGORIES_B_U1 Bytes: 4 Cardinality: 1
29 FILTER
28 TABLE ACCESS BY INDEX ROWID INV.MTL_TRANSACTION_REASONS Cost: 1 Bytes: 4 Cardinality: 1
27 INDEX UNIQUE SCAN UNIQUE INV.MTL_TRANSACTION_REASONS_U1 Cardinality: 1
Thanks
Nagamohan
Similar Messages
-
Give me the sql query which calculte the table size in oracle 10g ecc 6.0
Hi expert,
Please give me the sql query which calculte the table size in oracle 10g ecc 6.0.
RegardsOrkun Gedik wrote:
select segment_name, sum(bytes)/(1024*1024) from dba_segments where segment_name = '<TABLE_NAME>' group by segment_name;
Hi,
This delivers possibly wrong data in MCOD installations.
Depending on Oracle Version and Patchlevel dba_segments does not always have the correct data,
at any time esp. for indexes right after being rebuild parallel (Even in DB02 because it is using USER_SEGMENTS).
Takes a day to get the data back in line (never found out, who did the correction at night, could be RSCOLL00 ?).
Use above statement with "OWNER = " in WHERE for MCOD or connect as schema owner and use USER_SEGMENTS.
Use with
segment_name LIKE '<TABLE_NAME>%'
if you like to see the related indexes as well.
For partitioned objects, a join from dba_tables / dba_indexes to dba_tab_partitions/dba_ind_partitions to dba_segments
might be needed, esp. for hash partitioned tables, depending on how they have been created ( partition names SYS_xxxx).
Volker -
Query on Non-base-table column in form
Hi,
Does anyone have an example of code to allow users to query on non-base-table column from forms? For example, a personnel form displays a persons' name and department name. The form is based on PERSON table, which contains the department id, not the name. The form uses post-query to display the department name. When in query mode, the user wants to enter department name, and display the people in the department (still using this form, which is based on PERSON table.
Thanks!Try this:
For data block person set these properties as follows:
1.Set Query Data Source Name = "person , departments"
2.Set DML data target name = "person"
3.Set Where Clause to join i.e. condition "person.dno = departments.dno"
For filed department name set these properties as follows:
1.Data base item = "Yes"
2.Column Name = "departments.dname"
3.Query Only = "Yes"
4.Insert Allowed = "No"
5.Update Allowed= "No"
6.Keyboard Navigable = "No"
If you face ambiguous column name check for the same filed name exist on both tables and set column name for this filed to person.filed_name
Regards
null -
Store SQL query results in db table
Hi,
I have a SQL query that produces a report table.
Is it possible to automatically store the query results (or the report table) as a db table - without interrupting the current report building proces?
Thanks,
Dave
Message was edited by:
Dave JudgeHi Dave,
You can also insert records into an existing table:
INSERT INTO TABLEB (colA, colB, colC, etc) SELECT valA, valB, valC, etc FROM VIEWA WHERE etc etc
This can be done during a page process that runs "Before Header" and you can base your report on the TABLEB. Obviously, you will need to maintain that table to ensure that it is only truncated where necessary, that one user doesn't try to access another user's data on that table and that each time your page is loaded it doesn't try to repopulate the table when you don't need it to.
Another possiblity is to use a collection - which is user session based
Andy -
Performance tuning in SQL query using join of views
Hi,
Am trying to tune a query of the format
select ........ from view1,view2
where view1.keyfield = view2.keyfield
The base tables of the views view1 and view2 have indexes specified on 'keyfield'.
However, when I do an explain plan of the query, Full table scan of those base tables are performed.
Even tried using Hints in this query to force Oracle to use those indexes, but still not successful.
Any pointers on how to tune this kind of query would be highly welcome.
Regards,
BaishIf your query is really of the same form as you posted, then full table scans then a hash or merge join may be the most efficient plan.
You are asking for all of the rows from view1 and the matching rows from view2, because of this, Oracle dedcided that the cost of full table scans using multi-block reads is cheaper than using single block reads to get the rowids from the index, then single block reads to get the rows from the table.
FULL SCAN <> BAD
John -
Neea a sql query for inverting the table
Hi,
I am facing the problem in forming a SQL statement in oracle 10g
Table structure:
user field1 field2 field3 field4 field5 field6 .......
one 11 12 13 14 15 16
two 21 22 23 24 25 26
three 31 32 33 34 35 36
i want a SQL query where the out put is
field1 11 21 31
field2 12 22 32
field3 13 23 33
field4 14 24 34
Regards,
Balu CHcheck this Forums page
<http://forums.oracle.com/forums/search.jspa?threadID=&q=Rows+to+Columns&objID=f75&dateRange=last90days&userID=&numResults=15>
will help u.
Rgds
Sudar -
Loading an Oracle SQL query into an MSSQL table
I have a select query on an Oracle table. I want to load this data into an MSSQL table.
Problem is that I cannot create an interim table on the Oracle database (lack of privelages)
Is there a way to load an Oracle query into the MSSQL table without an interim table?
Many thanks
ZYes,
1) Create an ODI procedure
2) Create a step inside
3) at source tab put the oracle query
4) at target tab put the sql insert code
Refer to the oracle returned values like "#column_name_from_oracle_query"
Make sense?
Cezar Santos
[www.odiexperts.com] -
Using SQL query on user defined table
Hi all,
i'am currently working on a project which use user defined table and i would like to know if sap allow us to do insertion/deletion/update on those table using sql query or if we have to use the DI only?
thanks
Best regards,Hi,
Definitely you can insert in UDT. You can update but you cant change the Code field after added the rest of the field
can be updated. You cant delete in UDT but you cant delete anytime in SQL Management Studio.
THanks.
CLint -
Sql query on many child tables
I have an SQL database with many linkesd tables.
If I do:
SELECT [CaseNumber]<o:p></o:p>
,[CenterCode]<o:p></o:p>
,[Outcome]<o:p></o:p>
it will give me all numbers. However, for example, all outcome verbatium definitions are in a table Outcome Def. same goes for the field CenterCode
How can I write the SQL command to give me the [caseNumber], [CenterCode] description which is in another table and [Outcome] description which is on another table.
Many Thanks,
Raul
Raul RegoHello Raul,
you have to join all required tables via there relation (Primary key/foreign key); see
Join Fundamentals
Olaf Helper
[ Blog] [ Xing] [ MVP] -
I recently posted this in the Gallery. If anyone has any modifications or enhancements please feel free to contribute! One thing that I'd like to be able to add to it is the ability to only display information for applications/packages that are
still published, so if anyone can figure out a crafty way to do it... Thanks!
http://gallery.technet.microsoft.com/App-V-Reporting-SQL-query-6695d3c3I recently posted this in the Gallery. If anyone has any modifications or enhancements please feel free to contribute! One thing that I'd like to be able to add to it is the ability to only display information for applications/packages that are
still published, so if anyone can figure out a crafty way to do it... Thanks!
http://gallery.technet.microsoft.com/App-V-Reporting-SQL-query-6695d3c3 -
Hi
I have an SQL that has poor performance when querying a 3 table view.
I have tried to tune the view by adding an additional index but the EXPLAIN plan does not change. In fact, the plan is ONLY using fields specified in the the views join conditions and not the WHERE clause.
I have created an index EKET~Z1 with the column for table (T003)
EINDT
and I have also tried another index EKKO~Z2 with the columns for table T001
BSART
BSTYP
EKORG
The query is as follows:
SQL Statement
SELECT
"MANDT" , "EBELN" , "EBELP" , "ETENR" , "EKKO_LOEKZ" , "BSTYP" ,
"BSART" , "BUKRS" , "EKORG" , "EKGRP" , "STATU" , "AEDAT" , "ERNAM" ,
"BEDAT" , "LIFNR" , "ADRNR" , "WAERS" , "EKPO_LOEKZ" , "EKPO_BSTYP" ,
"TXZ01" , "MATNR" , "EMATN" , "WERKS" , "MATKL" , "IDNLF" , "MENGE" ,
"MEINS" , "NETPR" , "NETWR" , "PSTYP" , "KNTTP" , "WEPOS" , "ANFNR" ,
"PRDAT" , "KONNR" , "KTPNR" , "ELIKZ" , "EREKZ" , "EINDT" , "SLFDT" ,
"LPEIN" , "EKET_MENGE" , "EKET_WEMNG" , "BANFN" , "BNFPO" , "MBDAT" ,
"WADAT" , "WEBRE" , "FRGRL" , "ZZKONNR" , "ZZKTPNR" , "MAHNZ" ,
"MAHN1" , "MAHN2" , "MAHN3" , "INFNR"
FROM
"ZPO_DETAIL"
WHERE
"MANDT" = ? AND "EKORG" = ? AND "BSTYP" = ? AND "BSART" = ? AND
"WERKS" = ? AND "EINDT" < ? AND "EKPO_LOEKZ" <> ? AND "ELIKZ" <> ?
ORDER BY
"EBELN" , "EBELP" , "ETENR"
VIEW DETAILS
CREATE VIEW ZPO_DETAIL
(MANDT,
EBELN,
EBELP,
ETENR,
EKKO_LOEKZ,
BSTYP,
BSART,
BUKRS,
EKORG,
EKGRP,
STATU,
AEDAT,
ERNAM,
BEDAT,
LIFNR,
ADRNR,
WAERS,
EKPO_LOEKZ,
EKPO_BSTYP,
TXZ01,
MATNR,
EMATN,
WERKS,
MATKL,
IDNLF,
MENGE,
MEINS,
NETPR,
NETWR,
PSTYP,
KNTTP,
WEPOS,
ANFNR,
PRDAT,
KONNR,
KTPNR,
ELIKZ,
EREKZ,
EINDT,
SLFDT,
LPEIN,
EKET_MENGE,
EKET_WEMNG,
BANFN,
BNFPO,
MBDAT,
WADAT,
WEBRE,
FRGRL,
ZZKONNR,
ZZKTPNR,
MAHNZ,
MAHN1,
MAHN2,
MAHN3 )
AS SELECT
T0003."MANDT",
T0003."EBELN",
T0003."EBELP",
T0001."ETENR",
T0002."LOEKZ",
T0002."BSTYP",
T0002."BSART",
T0002."BUKRS",
T0002."EKORG",
T0002."EKGRP",
T0002."STATU",
T0002."AEDAT",
T0002."ERNAM",
T0002."BEDAT",
T0002."LIFNR",
T0002."ADRNR",
T0002."WAERS",
T0003."LOEKZ",
T0003."BSTYP",
T0003."TXZ01",
T0003."MATNR",
T0003."EMATN",
T0003."WERKS",
T0003."MATKL",
T0003."IDNLF",
T0003."MENGE",
T0003."MEINS",
T0003."NETPR",
T0003."NETWR",
T0003."PSTYP",
T0003."KNTTP",
T0003."WEPOS",
T0003."ANFNR",
T0003."PRDAT",
T0003."KONNR",
T0003."KTPNR",
T0003."ELIKZ",
T0003."EREKZ",
T0001."EINDT",
T0001."SLFDT",
T0001."LPEIN",
T0001."MENGE",
T0001."WEMNG",
T0001."BANFN",
T0001."BNFPO",
T0001."MBDAT",
T0001."WADAT",
T0003."WEBRE",
T0002."FRGRL",
T0003."ZZKONNR",
T0003."ZZKTPNR",
T0003."MAHNZ",
T0003."MAHN1",
T0003."MAHN2",
T0003."MAHN3"
FROM
SAPCEP."EKET" T0001,
SAPCEP."EKKO" T0002,
SAPCEP."EKPO" T0003
WHERE
T0002."MANDT" = T0003."MANDT" AND
T0002."EBELN" = T0003."EBELN" AND
T0003."MANDT" = T0001."MANDT" AND
T0003."EBELN" = T0001."EBELN" AND
T0003."EBELP" = T0001."EBELP"
EXPLAIN PLAN
Execution Plan for SQL Optimizer
TABLENAME COLUMN OR INDEX STRATEGY PAGECOUNT
T0003 RANGE CONDITION FOR KEY 15758
MANDT (USED KEY COLUMN)
T0001 JOIN VIA MULTIPLE KEY COLUMNS 8632
MANDT (USED KEY COLUMN)
EBELN (USED KEY COLUMN)
T0002 JOIN VIA MULTIPLE KEY COLUMNS 40109
MANDT (USED KEY COLUMN)
EBELN (USED KEY COLUMN)
EBELP (USED KEY COLUMN)
NO TEMPORARY RESULTS CREATED
SHOW RESULT IS COPIED , COSTVALUE IS 31186
SHOW QUERYREWRITE - APPLIED RULES:
SHOW MergeFromSelectOrView 1
The statistics are up-to-date.
Any tips would be welcome.
Thanks
Doug> I have created an index EKET~Z1 with the column for table (T003)
> EINDT
>
> and I have also tried another index EKKO~Z2 with the columns for table T001
> BSART
> BSTYP
> EKORG
>
SQL Statement
>
> SELECT
> "MANDT" , "EBELN" , "EBELP" , "ETENR" , "EKKO_LOEKZ" , "BSTYP" ,
> FROM
> "ZPO_DETAIL"
> WHERE
> "MANDT" = ? AND "EKORG" = ? AND "BSTYP" = ? AND "BSART" = ? AND
> "WERKS" = ? AND "EINDT" < ? AND "EKPO_LOEKZ" <> ? AND "ELIKZ" <> ?
> ORDER BY
> "EBELN" , "EBELP" , "ETENR"
>
> EXPLAIN PLAN
> -
>
> Execution Plan for SQL Optimizer
>
> TABLENAME COLUMN OR INDEX STRATEGY PAGECOUNT
>
> T0003 RANGE CONDITION FOR KEY 15758
> MANDT (USED KEY COLUMN)
>
> T0001 JOIN VIA MULTIPLE KEY COLUMNS 8632
> MANDT (USED KEY COLUMN)
> EBELN (USED KEY COLUMN)
>
> T0002 JOIN VIA MULTIPLE KEY COLUMNS 40109
> MANDT (USED KEY COLUMN)
> EBELN (USED KEY COLUMN)
> EBELP (USED KEY COLUMN)
>
> NO TEMPORARY RESULTS CREATED
> SHOW RESULT IS COPIED , COSTVALUE IS 31186
> SHOW QUERYREWRITE - APPLIED RULES:
> SHOW MergeFromSelectOrView 1
>
>
> The statistics are up-to-date.
> Any tips would be welcome.
Hi Doug,
here we go...
What MaxDB version are you using? Did you check whether the DB parameters all comply to the recommendations?
If so, what are the cardinalities of the columns you indexed (a.k.a. how many different values do each of them contain?)
If the specification of BSART, BSTYP and EKORG does not reduce the number of rows to be retrieved to a large extent, then the indexaccess is likely to produce additional work.
Anyhow, the join order looks a bit odd here - this might be the problem caused by having set OPTIMIZE_OPERATOR_JOIN_COSTFUNC = YES in a MaxDB version between 7.6.04 and 7.6.06.
So make sure it's set to NO (see note 814704 MaxDB Version 7.6 parameter settings for OLTP/BW).
regards,
Lars -
SQL Query - Report on multiple table
Hi Experts
Firstly, I am an amateur at generating SQL Queries!
I am trying to create a Report of All Invoices/Credit Notes generated in a date range combined with all open Sales Orders.
This is my attempt - I am getting all Sales Orders but only want to have Open Sales Orders.
SELECT T0.[DocNum], T0.[DocType], T0.[cardcode] AS 'Cust. ID', T0.[cardName] AS 'Customer', T0.[TransId], T0.[TaxDate] AS 'Date', T1.[SlpName] AS 'AC Mgr', (T0.[DocTotal] -T0.[VatSum]) AS'NETT', T0.[DocStatus],T0.[U_BusCategory] AS 'Business Cat.', T0.[U_TERM] AS 'TERM', T2.OpprId, T2.Name
FROM [dbo].[OINV] T0 INNER JOIN OSLP T1 ON T0.SlpCode = T1.SlpCode
LEFT OUTER JOIN OOPR T2 ON T0.U_RTNo = T2.DocNum
WHERE CONVERT(nchar(8), T0.DocDate, 112) >= '[%0]' AND CONVERT(nchar(8), T0.DocDate, 112) <= '[%1]'
UNION
SELECT T0.[DocNum], T0.[DocType], T0.[cardcode], T0.[cardName], T0.[TransId], T0.[TaxDate] AS 'Date', T1.[SlpName] AS 'AC Mgr', -(T0.[DocTotal] -T0.[VatSum]) AS'NETT', T0.[DocStatus],T0.[U_BusCategory],T0.[U_TERM] AS 'TERM', T2.OpprId AS 'Oppr. ID.', T2.Name AS 'Oppr. Name'
FROM [dbo].[ORIN] T0 INNER JOIN OSLP T1 ON T0.SlpCode = T1.SlpCode
LEFT OUTER JOIN OOPR T2 ON T0.U_RTNo = T2.DocNum
WHERE CONVERT(nchar(8), T0.DocDate, 112) >= '[%0]' AND CONVERT(nchar(8), T0.DocDate, 112) <= '[%1]'
UNION
SELECT T0.[DocNum], T0.[DocType], T0.[CardCode], T0.[CardName], T0.[TransId], T0.[TaxDate], T1.[SlpName], (T0.[DocTotal]-T0.[VatSum]), T0.[DocStatus], T0.[U_BusCategory], T0.[U_TERM], T2.OpprId AS 'Oppr. ID.', T2.Name AS 'Oppr. Name'
FROM ORDR T0 INNER JOIN OSLP T1 ON T0.SlpCode = T1.SlpCode
LEFT OUTER JOIN OOPR T2 ON T0.U_RTNo = T2.DocNum
WHERE CONVERT(nchar(8), T0.DocDate, 112) >= '[%0]' AND CONVERT(nchar(8), T0.DocDate, 112) <= '[%1]'
Any help would be much appreciated
DerekDear Mr Vard,
The field in the ORDR table that will tell you if the document is closed or open is the DocStatus.
C = Closed and O = Open
You could add another where clause:
where t0.DocStatus = 'c'
to the last query you added (the one referring to the ORDR table).
Please, let me know if this helps you.
Marcella Rivi
SAP Business One Forums Team -
Parse SQL query and extract source tables and columns
Hello,
I have a set of SQL queries and I have to extract the source tables and columns from them.
For example:
Let's imagine that we have two tables
CREATE TABLE T1 (col1 number, col2 number, col3 number)
CREATE TABLE T2 (col1 number, col2 number, col3 number)
We have the following query:
SELECT
T1.col1,
T1.col2 + T1.col3 as field2
FROM T1 INNER JOIN T2 ON T1.col2=T2.col2
WHERE T2.col1 = 1
So, as a result I would like to have:
Order Table Column
1 T1 col1
2 T1 col2
2 T1 col3
Optionally, I would like to have a list of all dependency columns (columns used in "ON", "WHERE" and "GROUP BY" clauses:
Table Column
T1 col2
T2 col1
T2 col2
I have tried different approaches but without any success. Any help is appreciated. Thank you in advance.
Best regards,
BeroetzI have a set of SQL queries and I have to extract the source tables and columns from them. In a recent db version you can use Re: sql injection question for this.
-
Help with SQL Query Involving Three Database Tables
Hi,
My SQL is very rusty since I have not touched it in over one year.
I was given an SQL question in a job interview and I am curious to know the right answer.
This was a pre-prepared written test and the interviewer did not know the answer.
There are three database tables: STUDENTS, COURSES and STUDENT_COURSES
Table STUDENTS has STUDENT_ID and STUDENT_NAME columns.
Table COURSES has COURSE_ID and COURSE_DESCRIPTION columns.
Table STUDENT_COURSES has columns STUDENT_ID and COURSE_ID.
Provide a query that returns all the students that are enrolled in all the courses.
Thanks,
Avi.It is probably good to say that this task may be solved such way, if database normalized and there are references
Basically here just is a variant of your solution
DROP TABLE student_course;
DROP TABLE student;
DROP TABLE course;
CREATE TABLE student
(student_id NUMBER(9) PRIMARY KEY,
student_name VARCHAR2(30));
CREATE TABLE course
(course_id NUMBER(9) PRIMARY KEY,
dscr VARCHAR2(100));
CREATE TABLE student_course
(student_id NUMBER(9),
course_id NUMBER(9));
ALTER TABLE student_course
ADD CONSTRAINT pk_st_crs PRIMARY KEY
(student_id, course_id);
ALTER TABLE student_course
ADD CONSTRAINT fk_student
FOREIGN KEY (student_id)
REFERENCES student(student_id);
ALTER TABLE student_course
ADD CONSTRAINT fk_course
FOREIGN KEY (course_id)
REFERENCES course(course_id);
INSERT INTO student
VALUES (1, 'NAME1');
INSERT INTO student
VALUES (2, 'NAME2');
INSERT INTO student
VALUES (3, 'NAME3');
INSERT INTO course
VALUES (101, 'Desc 1');
INSERT INTO course
VALUES (102, 'Desc 2');
INSERT INTO course
VALUES (103, 'Desc 3');
INSERT INTO student_course
VALUES (1, 101);
INSERT INTO student_course
VALUES (1, 102);
INSERT INTO student_course
VALUES (2, 101);
INSERT INTO student_course
VALUES (2, 103);
INSERT INTO student_course
VALUES (3, 101);
INSERT INTO student_course
VALUES (3, 102);
INSERT INTO student_course
VALUES (3, 103);
COMMIT ;
WITH st_crs_cnt AS
(SELECT student_id,
COUNT (*) tot
FROM student_course
GROUP BY student_id)
SELECT sc.student_id,
sc.tot
FROM st_crs_cnt sc
WHERE sc.tot = (SELECT COUNT (*) FROM course); -
How to insert a sql query statement in the table
I have a stored procedure which has different insert,update statements on different tables. I am writing a trigger on these tables such that when the record is updated or inserted in these tables a trigger should fire and should write the old value,new value and the query (insert or update) that caused the trigger to fire in the seperate table. Is there any way to find out the exact query statement that fired the trigger?
Rather than inventing your own demi-donkeyed solution I recommend you consider using Oracle's Fine-Grained Auditing which does precisely this.
Cheers, APC
Maybe you are looking for
-
How can i add a new row in System Matrix passing itemcode and quantaty
Hi All, I have to add new lines in the matrix system only through the itemcode and item quantity. I tried several ways without success. Maybe the following code help to explain what I'm trying to do. Someone already inserted rows in the matrix system
-
New user creation in FND_USER using link on fly
Hi, How can we stop OID Provisioning template to stop creating CUSTOMER_ID and PERSON_PARTY_ID for new users in FND_USER. Is there a function stored some where which we can change for the ProvOIDToApps.tmp? Please let me know.
-
Dreamweaver CS6 Installation Failed
I've tried to download Dreamweaver CS6 from the Creative Cloud on 3 different occasions and every time the installation fails. Here is a summary of the errors I encountered: -------------------------------------- Summary ---------------------------
-
I can't open a file, unexpected error
I can't open a file saved with Keynote; A mask error say: Unexspexted error, close and open again Keynote. I try to delete keynote plts; and reinstalling the software. always the same error. thanks, a lot G.
-
Does logic send 5.1 out via optical out mac pro ? Im going into a Denon AVR 4802 which I think is capable on decoding.