Query to join ohem and jdt1
Hi Everyone,
i make a query for employee Advance trial it working fine but it show me the result of employee that have any of the transaction between these dates.
i want all employee which have no transaction between these dates but have Opening.
for example empoyee a have no debit credit but have opening on 1 july 10000. this employee is not showing.
==========================================Employee Advance Trial===========================
With OB as
Select OHEM.empID, Sum(Jdt1.Debit- JDt1.Credit) as [Balance]
FROM
JDT1 INNER JOIN
OJDT ON JDT1.TransId = OJDT.TransId INNER JOIN
OACT ON OACT.AcctCode=JDT1.Account full outer JOIN --AND OACT.AcctCode=JDT1.ContraAct INNER JOIN --left outer join
OHEM on JDT1 .Ref2 = OHEM .empID
where JDT1.Account IN('_SYS00000000264' ,'_SYS00000000265', '_SYS00000000266','_SYS00000000267')
and Convert(date,JDT1.RefDate) < Convert(date,'1.jul.2014') and OHEM.empID BETWEEN 1 AND 1940
Group By OHEM .Empid
SELECT OHEM.firstName,OHEM.middleName,OHEM.Jobtitle, OHEM.jobtitle,OUDP.Name,OUBR.Name,
OHEM.empID, OHEM.lastName,
SUM(JDT1.Debit) AS Debit, SUM(JDT1.Credit) AS Credit --,isnull(Ob .Balance,0) AS OpeningBalance
FROM JDT1 INNER JOIN
OJDT ON JDT1.TransId = OJDT.TransId INNER JOIN
OACT ON OACT.AcctCode=JDT1.Account left JOIN --AND OACT.AcctCode=JDT1.ContraAct INNER JOIN --left outer join
OHEM on JDT1 .Ref2 = OHEM .empID LEFT JOIN
OACT OACT2 ON OACT2.AccTCode=JDT1.ContraAct
full outer join OB On OHEM .Empid = OB .EmpID
left outer join OUDP ON OUDP.Code=OHEM.dept left outer join
OUBR ON OUBR.Code=OHEM.branch
where JDT1.Account IN('_SYS00000000264' ,
'_SYS00000000265', '_SYS00000000266','_SYS00000000267') AND
Convert(date,JDT1.RefDate) BETWEEN Convert(date,'1.jul.14') AND Convert(date,'31.jul.14') AND OHEM.empID BETWEEN 1 AND 1940
GROUP BY OHEM.empID , OHEM.lastName,isnull(Ob .Balance,0),
OHEM.firstName, OHEM.middleName,OHEM.Jobtitle,
OHEM.jobtitle,OHEM.dept,OUDP.Name,OHEM.branch,OUBR.Name
========================================================================================
Hi Safeer Khan..,..
Try This
SELECT t.firstName,
t.middleName,
t.jobTitle,
t.jobTitle,
t.Name,
t.Name,
t.lastName,
SUM(debit),
SUM(credit),
SUM(t.OpeningBalance)
FROM
(SELECT OHEM.firstName,
OHEM.middleName,
OHEM.Jobtitle,
OUDP.Name AS OuName,
OUBR.Name,
OHEM.empID,
OHEM.lastName,
SUM(JDT1.Debit) AS Debit,
SUM(JDT1.Credit) AS Credit ,
0 AS OpeningBalance
FROM JDT1
INNER JOIN OJDT ON JDT1.TransId = OJDT.TransId
INNER JOIN OACT ON OACT.AcctCode=JDT1.Account
RIGHT OUTER JOIN --AND OACT.AcctCode=JDT1.ContraAct INNER JOIN --left outer join
OHEM ON JDT1 .Ref2 = OHEM .empID
LEFT JOIN OACT OACT2 ON OACT2.AccTCode=JDT1.ContraAct
LEFT OUTER JOIN OUDP ON OUDP.Code=OHEM.dept
LEFT OUTER JOIN OUBR ON OUBR.Code=OHEM.branch
WHERE JDT1.Account IN('_SYS00000000264' , '_SYS00000000265', '_SYS00000000266','_SYS00000000267')
AND Convert(date,JDT1.RefDate) BETWEEN Convert(date,'1.july.2014') AND Convert(date,'30.july.2014')-- AND OHEM.empID BETWEEN @Employeeid AND @EmployeeidTo
GROUP BY OHEM.empID ,
OHEM.lastName,
OHEM.firstName,
OHEM.middleName,
OHEM.Jobtitle,
OHEM.jobtitle,
OHEM.dept,
OUDP.Name,
OHEM.branch,
OUBR.Name
UNION ALL SELECT OHEM.firstName,
OHEM.middleName,
OHEM.jobtitle,
OUDP.Name AS OuName,
OUBR.Name,
OHEM.empID,
OHEM.lastName,
0 AS Debit,
0 AS Credit ,
(SUM(JDT1.Debit)- SUM(JDT1.Credit))AS OpeningBalance
FROM JDT1
INNER JOIN OJDT ON JDT1.TransId = OJDT.TransId
INNER JOIN OACT ON OACT.AcctCode=JDT1.Account
RIGHT OUTER JOIN --AND OACT.AcctCode=JDT1.ContraAct INNER JOIN --left outer join
OHEM ON JDT1 .Ref2 = OHEM .empID
LEFT JOIN OACT OACT2 ON OACT2.AccTCode=JDT1.ContraAct
LEFT OUTER JOIN OUDP ON OUDP.Code=OHEM.dept
LEFT OUTER JOIN OUBR ON OUBR.Code=OHEM.branch
WHERE JDT1.Account IN('_SYS00000000264' , '_SYS00000000265', '_SYS00000000266','_SYS00000000267')
AND Convert(date,JDT1.RefDate) < Convert(date,'1.july.2014') --AND OHEM.empID BETWEEN @Employeeid AND @EmployeeidTo
GROUP BY OHEM.empID ,
OHEM.lastName,
OHEM.firstName,
OHEM.middleName,
OHEM.Jobtitle,
OHEM.jobtitle,
OHEM.dept,
OUDP.Name,
OHEM.branch,
OUBR.Name) AS t
GROUP BY t.firstName,
t.middleName,
t.jobTitle,
t.Name,
t.Name,
t.lastName
Regards
Kennedy
Similar Messages
-
SQL query for join table and multiple values
Trying to join two tables , Emphours and EmpStatus to get
result which gives each emplyees hour
worked each day
in past say 1 year in what status. I need result similar to table 3 , Hours Can also be grouped per week
all I need Is Each employees hours in each week and his status and position at that time if possible
any help will be highly appreciated. Thank you
note: payday is every other Friday- week runs from Saturday through Friday
EmpStatus Table tracks when employees status changed
EmpHours
employee
workday
payday
hours
position
101
1/1/2014
1/3/2014
8
assistant
101
1/3/2014
1/3/2014
8
assistant
101
1/4/2014
1/17/2014
8
assistant
101
1/5/2014
1/17/2014
8
assistant
101
1/7/2014
1/17/2014
8
assistant
101
1/8/2014
1/17/2014
8
assistant
101
1/9/2014
1/17/2014
8
assistant
101
1/11/2014
1/17/2014
8
assistant
101
1/13/2014
1/17/2014
8
assistant
101
1/14/2014
1/17/2014
8
assistant
101
1/18/2014
2/14/2014
8
assistant
102
1/1/2014
1/3/2014
7
manager
102
1/25/2014
1/31/2014
7
manager
102
1/26/2014
1/31/2014
7
manager
102
1/28/2014
1/31/2014
7
manager
102
1/31/2014
1/31/2014
7
manager
103
1/1/2014
1/3/2014
5
intern
103
1/31/2014
1/31/2014
6
intern
104
1/14/2014
1/17/2014
5
supervisor
104
1/30/2014
1/31/2014
6
supervisor
EmpStatus
employee
start_date
status
101
1/1/2014
parttime
101
1/18/2014
fulltime
102
1/1/2014
seasonal
102
1/18/2014
fulltime
103
1/1/2014
partime
103
1/18/2014
fulltime
104
1/4/2014
parttime
104
1/18/2014
fulltime
Table 3
employee
status
hours
position
workday
weekend
payday
101
parttime
8
assistant
1/1/2014
1/3/2014
1/3/2014
101
parttime
8
assistant
1/3/2014
1/3/2014
1/3/2014
101
parttime
8
assistant
1/4/2014
1/10/2014
1/17/2014
101
parttime
8
assistant
1/5/2014
1/10/2014
1/17/2014
101
parttime
8
assistant
1/7/2014
1/10/2014
1/17/2014
101
parttime
8
assistant
1/8/2014
1/10/2014
1/17/2014
101
parttime
8
assistant
1/9/2014
1/10/2014
1/17/2014
101
parttime
8
assistant
1/11/2014
1/17/2014
1/17/2014
101
parttime
8
assistant
1/13/2014
1/17/2014
1/17/2014
101
parttime
8
assistant
1/14/2014
1/17/2014
1/17/2014
101
fulltime
8
assistant
1/18/2014
1/24/2014
2/14/2014
102
seasonal
7
manager
1/1/2014
1/3/2014
1/3/2014
102
fulltime
7
manager
1/25/2014
1/25/2014
2/14/2014
102
fulltime
7
manager
1/26/2014
1/26/2014
2/14/2014
102
fulltime
7
manager
1/28/2014
1/28/2014
2/14/2014
102
fulltime
7
manager
1/31/2014
1/31/2014
2/14/2014
103
parttime
5
intern
1/1/2014
1/3/2014
1/3/2014
103
fulltime
6
intern
1/31/2014
1/31/2014
2/14/2014
104
parttime
5
supervisor
1/14/2014
1/17/2014
1/17/2014
104
fulltime
6
supervisor
1/30/2014
1/31/2014
1/31/2014Hello David,
Try this query
set dateformat mdy;
declare @EmpHours table
(Employee int,workday date,payday date,hours int,position varchar(50));
insert into @EmpHours values
(101,'1/1/2014','1/3/2014',8,'assistant'),
(101,'1/3/2014','1/3/2014',8,'assistant'),
(101,'1/4/2014','1/17/2014',8,'assistant'),
(101,'1/5/2014','1/17/2014',8,'assistant'),
(101,'1/7/2014','1/17/2014',8,'assistant'),
(101,'1/8/2014','1/17/2014',8,'assistant'),
(101,'1/9/2014','1/17/2014',8,'assistant'),
(101,'1/11/2014','1/17/2014',8,'assistant'),
(101,'1/13/2014','1/17/2014',8,'assistant'),
(101,'1/14/2014','1/17/2014',8,'assistant'),
(101,'1/18/2014','2/14/2014',8,'assistant'),
(102,'1/1/2014','1/3/2014',7,'manager'),
(102,'1/25/2014','1/31/2014',7,'manager'),
(102,'1/26/2014','1/31/2014',7,'manager'),
(102,'1/28/2014','1/31/2014',7,'manager'),
(102,'1/31/2014','1/31/2014',7,'manager'),
(103,'1/1/2014','1/3/2014',5,'intern'),
(103,'1/31/2014','1/31/2014',6,'intern'),
(104,'1/14/2014','1/17/2014',5,'supervisor'),
(104,'1/30/2014','1/31/2014',6,'supervisor');
--select * from @EmpHours
declare @EmpStatus table
(employee int,start_date date,status varchar(20));
insert into @EmpStatus values
(101,'1/1/2014','parttime'),
(101,'1/18/2014','fulltime'),
(102,'1/1/2014','seasonal'),
(102,'1/18/2014','fulltime'),
(103,'1/1/2014','partime'),
(103,'1/18/2014','fulltime'),
(104,'1/4/2014','parttime'),
(104,'1/18/2014','fulltime');
WITH C AS
SELECT es.employee,es.start_date, es.status, ROW_NUMBER() OVER(partition by employee ORDER BY start_date) AS rownum
FROM @EmpStatus ES
CTE_RANGES as(
SELECT cur.employee,Cur.start_date start_range, cur.status,case when nxt.start_date is null then '2099-12-31' else dateadd(d,-1,Nxt.start_date) end AS end_range
FROM C AS Cur
left JOIN C AS Nxt
ON Nxt.rownum = Cur.rownum + 1 and cur.employee=nxt.employee)
select eh.*,es.status from @EmpHours EH join CTE_RANGES Es on EH.Employee =es.employee and EH.workday between es.start_range and es.end_range
--where es.employee=101
You will need a calender table too which can be joined to the output of the above query to get the weekend dates.
You can find the T-SQL code to generate the calender here
http://stackoverflow.com/questions/19191577/t-sql-function-to-generate-calendar-table
and posting the questions with necessary DDL , DML (like I have posted) would help us a lot.
Satheesh
My Blog -
Need complex query with joins and AGGREGATE functions.
Hello Everyone ;
Good Morning to all ;
I have 3 tables with 2 lakhs record. I need to check query performance.. How CBO rewrites my query in materialized view ?
I want to make complex join with AGGREGATE FUNCTION.
my table details
SQL> select from tab;*
TNAME TABTYPE CLUSTERID
DEPT TABLE
PAYROLL TABLE
EMP TABLE
SQL> desc emp
Name
EID
ENAME
EDOB
EGENDER
EQUAL
EGRADUATION
EDESIGNATION
ELEVEL
EDOMAIN_ID
EMOB_NO
SQL> desc dept
Name
EID
DNAME
DMANAGER
DCONTACT_NO
DPROJ_NAME
SQL> desc payroll
Name
EID
PF_NO
SAL_ACC_NO
SALARY
BONUS
I want to make complex query with joins and AGGREGATE functions.
Dept names are : IT , ITES , Accounts , Mgmt , Hr
GRADUATIONS are : Engineering , Arts , Accounts , business_applications
I want to select records who are working in IT and ITES and graduation should be "Engineering"
salary > 20000 and < = 22800 and bonus > 1000 and <= 1999 with count for males and females Separately ;
Please help me to make a such complex query with joins ..
Thanks in advance ..
Edited by: 969352 on May 25, 2013 11:34 AM969352 wrote:
why do you avoid providing requested & NEEDED details?I do NOT understand what do you expect ?
My Goal is :
1. When executing my own query i need to check expalin plan.please proceed to do so
http://docs.oracle.com/cd/E11882_01/server.112/e26088/statements_9010.htm#SQLRF01601
2. IF i enable query rewrite option .. i want to check explain plan ( how optimizer rewrites my query ) ? please proceed to do so
http://docs.oracle.com/cd/E11882_01/server.112/e16638/ex_plan.htm#PFGRF009
3. My only aim is QUERY PERFORMANCE with QUERY REWRITE clause in materialized view.It is an admirable goal.
Best Wishes on your quest for performance improvements. -
How to query 2 hierarchical tables and join?
Hi
I've been bashing my head against a well to get this to work efficiently in 10g. I can solve the following writing some pretty horrible SQL, but can't figure out how to do it elegantly and optimally.
We have two hierarchical tables as follows:
select * from data_categories dac
dac_code name parent_dac_code
10 MANAGEMENT
20 MEDICATION 10
30 PROCEDURE 10
40 SURVEY
50 ASSESS
60 NATURE 50
70 OBSERVE 60
select * from data_elements des
des_id name parent_des_id dac_code display_seq
100 DOSE MEDICATION 1
110 1MG 100 1
120 2MG 100 2
130 3MG 100 3
140 ROUTE MEDICATION 2
150 ET 140 1
160 EM 140 2
170 RESPONSE MEDICATION 3
180 IMPROVED 170 1
190 NOCHANGE 170 2
200 FILED MANAGEMENT 1
210 INPUT OBSERVE 1
You'll note:
1) We have hierarchies in both tables, and a fk from data_elements to data_categories via the dac_code.
2) The depth of both data_categories and data_elements is unlimited.
3) There is no single root node record in either table.
4) The appropriate PK and FK indexes exist.
We need to write a query that returns the following results:
root_dac_code parent_dac_code des_level des_id name display_seq
ASSESS OBSERVE 1 210 INPUT 1
MANAGEMENT MEDICATION 1 100 DOSE 1
MANAGEMENT MEDICATION 2 110 1MG 1
MANAGEMENT MEDICATION 2 120 2MG 2
MANAGEMENT MEDICATION 2 130 3MG 3
MANAGEMENT MEDICATION 1 140 ROUTE 1
MANAGEMENT MEDICATION 2 150 ET 1
MANAGEMENT MEDICATION 2 160 EM 2
MANAGEMENT MEDICATION 1 170 RESPONSE 3
MANAGEMENT MEDICATION 2 180 IMPROVED 1
MANAGEMENT MEDICATION 2 190 NOCHANGE 2
MANAGEMENT MANAGEMENT 1 200 FILED 1
You'll also note we need to return the data in order of the root_dac_code, then parent_dac_code, followed by the display_seq.
Does anybody know how to write this query in an elegant and optimal manner?
Many thanks for any help!
Cheers,
CM.Flakey model.
Why does data_elements.dac_code appear to refer to data_categories.name rather than data_categories.dac_code?
You do not appear to be ordering by root_dac_code, parent_dac_code and display_seq (if you were MANAGEMENT/MANAGEMENT would not be at the end). Did you perhaps mean ORDER BY root_dac_code, des_id, display_seq?
Something like this perhaps?
Oracle Database 10g Enterprise Edition Release 10.1.0.2.0 - 64bit Production
With the Partitioning, OLAP and Data Mining options
SQL> CREATE TABLE data_categories (
2 dac_code NUMBER,
3 name VARCHAR2 (30),
4 parent_dac_code NUMBER);
Table created.
SQL> INSERT INTO data_categories VALUES (10, 'MANAGEMENT', NULL);
1 row created.
SQL> INSERT INTO data_categories VALUES (20, 'MEDICATION', 10);
1 row created.
SQL> INSERT INTO data_categories VALUES (30, 'PROCEDURE', 10);
1 row created.
SQL> INSERT INTO data_categories VALUES (40, 'SURVEY', NULL);
1 row created.
SQL> INSERT INTO data_categories VALUES (50, 'ASSESS', NULL);
1 row created.
SQL> INSERT INTO data_categories VALUES (60, 'NATURE', 50);
1 row created.
SQL> INSERT INTO data_categories VALUES (70, 'OBSERVE', 60);
1 row created.
SQL> CREATE TABLE data_elements (
2 des_id NUMBER,
3 name VARCHAR2 (30),
4 parent_des_id NUMBER,
5 dac_code VARCHAR2 (30),
6 display_seq NUMBER);
Table created.
SQL> INSERT INTO data_elements VALUES (100, 'DOSE', NULL, 'MEDICATION', 1);
1 row created.
SQL> INSERT INTO data_elements VALUES (110, '1MG', 100, NULL, 1);
1 row created.
SQL> INSERT INTO data_elements VALUES (120, '2MG', 100, NULL, 2);
1 row created.
SQL> INSERT INTO data_elements VALUES (130, '3MG', 100, NULL, 3);
1 row created.
SQL> INSERT INTO data_elements VALUES (140, 'ROUTE', NULL, 'MEDICATION', 2);
1 row created.
SQL> INSERT INTO data_elements VALUES (150, 'ET', 140, NULL, 1);
1 row created.
SQL> INSERT INTO data_elements VALUES (160, 'EM', 140, NULL, 2);
1 row created.
SQL> INSERT INTO data_elements VALUES (170, 'RESPONSE', NULL, 'MEDICATION', 3);
1 row created.
SQL> INSERT INTO data_elements VALUES (180, 'IMPROVED', 170, NULL, 1);
1 row created.
SQL> INSERT INTO data_elements VALUES (190, 'NOCHANGE', 170, NULL, 2);
1 row created.
SQL> INSERT INTO data_elements VALUES (200, 'FILED', NULL, 'MANAGEMENT', 1);
1 row created.
SQL> INSERT INTO data_elements VALUES (210, 'INPUT', NULL, 'OBSERVE', 1);
1 row created.
SQL> SELECT dc.root_dac_code, de.parent_dac_code,
2 de.des_level, de.des_id, de.name, de.display_seq
3 FROM (SELECT LEVEL des_level, des_id, name, parent_des_id,
4 CONNECT_BY_ROOT (dac_code) parent_dac_code, display_seq
5 FROM data_elements da
6 START WITH parent_des_id IS NULL
7 CONNECT BY PRIOR des_id = parent_des_id) de,
8 (SELECT CONNECT_BY_ROOT (name) root_dac_code, name dac_code
9 FROM data_categories
10 START WITH parent_dac_code IS NULL
11 CONNECT BY PRIOR dac_code = parent_dac_code) dc
12 WHERE de.parent_dac_code = dc.dac_code
13 ORDER BY dc.root_dac_code, de.des_id, de.display_seq;
ROOT_DAC_CODE PARENT_DAC_CODE DES_LEVEL DES_ID NAME
ASSESS OBSERVE 1 210 INPUT
MANAGEMENT MEDICATION 1 100 DOSE
MANAGEMENT MEDICATION 2 110 1MG
MANAGEMENT MEDICATION 2 120 2MG
MANAGEMENT MEDICATION 2 130 3MG
MANAGEMENT MEDICATION 1 140 ROUTE
MANAGEMENT MEDICATION 2 150 ET
MANAGEMENT MEDICATION 2 160 EM
MANAGEMENT MEDICATION 1 170 RESPONSE
MANAGEMENT MEDICATION 2 180 IMPROVED
MANAGEMENT MEDICATION 2 190 NOCHANGE
MANAGEMENT MANAGEMENT 1 200 FILED
12 rows selected.
SQL> -
Duplicate rows on Query with journal entries and AP documents
Hi Experts,
I ahve the below query allowing me to have some information from journal entries and retrieve some other from AP docuements and PO.
The thing is the query duplicates rows because the AP docuements have several rows but i don t know how to do to remove them (i guess this has to do with JOIN function...)
SELECT
T2.[GroupMask] as 'Account Family',
T2.[FatherNum] as 'Parent Account',
T2.[AcctCode] AS 'Account Code',
T2.[AcctName] AS 'Account Name',
T0.[TaxDate] AS 'Document Date',
T0.[Number] AS 'Number',
T0.[DocSeries] AS 'Document Series',
T0.[BaseRef] AS 'Base Reference',
T0.[TransId] AS 'Transaction Number',
T0.[Ref2] AS 'Reference 2',
T1.[ContraAct] AS 'Offset Account',
T4.[BaseRef] AS 'PO code',
T1.[Project] AS 'Project Code',
T1.[ProfitCode] AS 'Distribution Rule',
T1.[OcrCode2] AS 'Costing Code 2',
T1.[OcrCode3] AS 'Costing Code 3',
T1.[OcrCode4] AS 'Costing Code 4',
(T1.[SYSdeb]-T1.[SYSCred]) AS 'Total Amount EUR',
(T1.[FCDebit]- T1.[FCCredit]) AS 'Total Amount (FC)',
T5.[DocTotalSy] AS 'PO Amount (EUR)'
FROM [dbo].[OJDT] T0
INNER JOIN [dbo].[JDT1] T1 ON T1.[TransId] = T0.[TransId]
INNER JOIN [dbo].[OACT] T2 ON T2.[AcctCode] = T1.[Account] AND T2.[AcctCode] = T1.[Account]
INNER JOIN [dbo].[OPCH] T3 ON T3.[DocNum] = T0.[BaseRef]
INNER JOIN [dbo].[PCH1] T4 ON T3.[DocEntry] = T4.[DocEntry]
LEFT JOIN [dbo].[OPOR] T5 ON T5.[DocNum] = T4.[BaseRef]
WHERE
(T2.[AcctCode] > (N'599999' ) ) AND (T2.[AcctCode] < (N'799999' ) )
AND (T0.[TaxDate]>= [%0] ) AND (T0.[TaxDate]<= [%1])Try This
SELECT T2.[GroupMask] AS 'Account Family',
T2.[FatherNum] AS 'Parent Account',
T2.[AcctCode] AS 'Account Code',
T2.[AcctName] AS 'Account Name',
T0.[TaxDate] AS 'Document Date',
T0.[Number] AS 'Number',
T0.[DocSeries] AS 'Document Series',
T0.[BaseRef] AS 'Base Reference',
T0.[TransId] AS 'Transaction Number',
T0.[Ref2] AS 'Reference 2',
T1.[ContraAct] AS 'Offset Account',
T4.[BaseRef] AS 'PO code',
T1.[Project] AS 'Project Code',
T1.[ProfitCode] AS 'Distribution Rule',
T1.[OcrCode2] AS 'Costing Code 2',
T1.[OcrCode3] AS 'Costing Code 3',
T1.[OcrCode4] AS 'Costing Code 4',
sum(T1.[SYSdeb]-T1.[SYSCred]) AS 'Total Amount EUR',
sum(T1.[FCDebit]- T1.[FCCredit]) AS 'Total Amount (FC)',
T6.[DocTotalSy] AS 'PO Amount (EUR)'
FROM OJDT T0
INNER JOIN JDT1 T1 ON T0.TransId = T1.TransId
INNER JOIN OACT T2 ON T1.Account = T2.AcctCode
INNER JOIN OPCH T3 ON T0.TransId = T3.TransId
INNER JOIN PCH1 T4 ON T3.DocEntry = T4.DocEntry
INNER JOIN POR1 T5 ON T4.[BaseEntry] = T5.[DocEntry]
AND T4.[BaseLine] = T5.[LineNum]
INNER JOIN OPOR T6 ON T5.DocEntry = T6.DocEntry
GROUP BY T2.[GroupMask],
T2.[FatherNum],
T2.[AcctCode],
T2.[AcctName],
T0.[TaxDate],
T0.[Number],
T0.[DocSeries],
T0.[BaseRef],
T0.[TransId],
T0.[Ref2],
T1.[ContraAct],
T4.[BaseRef],
T1.[Project],
T1.[ProfitCode],
T1.[OcrCode2],
T1.[OcrCode3],
T1.[OcrCode4],
T6.[DocTotalSy]
Regards,
Kennedy -
Different output of same query in SQL Server and Oracle
I have two tables table1 and table2
--table1 has two columns c1 int and c2 varchar. there are not constraints added in it. it has data as given below
c1 c2
6 d
5 j
102 g
4 g
103 f
3 h
501 j
1 g
601 n
2 m
--table2 has only one column c1 int. there are not constraints added in it. it has data as given below
c1
6
1
4
3
2
now when i run below given query in sql server and oracle it gives me different result
select *
from table1
inner join (SELECT ROW_NUMBER() OVER (order by c1 ASC) AS c1 from table2) table2 on table2.c1=table1.c1
sql server output
c1 c2 c1
1 g 1
2 m 2
3 h 3
4 g 4
5 j 5
oracle output
C1 C2 C1
5 j 5
4 g 4
3 h 3
1 g 1
2 m 2
If you notice the first column in both output. It is sorted in sql server and not in oracle.
Why it is behaving differently in oracle? Is there any way I can solve this in oracle?
Thanks,
JigsIt is NOT behaving "differently" in Oracle; you just haven't specified an order that you expect your results to be in, so you're going to get output in whatever order the database fancies displaying it (ie. no guarenteed order). This is an artifact of how the database chooses to put together the data, and different databases (or even datasets within the same database) can and most likely will behave differently.
Even SQL Server won't guarentee to always get your data in an ordered fashion if you exclude the order by clause, even if you think it has always output the data in an ordered fashion.
Your solution is to add an order by clause, in BOTH databases, to force the order of the output data. -
How to build a query to join on two tables without mapping
I did Automatic mapping by the workbench Directofield mapping with the table and java object.
Wanted to build a simple join query by joining on the same field on both the tables.Not the sql query through the toplink using expression builder.
Please help.............
Spent one full day for this................Thanks Don for the reply,sorry to bug you,but i need help.....
SELECT A.AGNCY_C,
A.TYPE_C,
A.RESN_C,
A.S_TYPE_C,
A.SUB_ID_C,
A.RY_C
FROM RATING A, REF B
WHERE A.ID_C = B._ID_C
AND A.ALPHA_C = B.ALPHA_C
AND A.EFF_D >= B.MATURITY_D
This is the real query i was talking about.I did mapping automatically through the workbench,generated java classes also throught the workbench.
Now they don't want to execute the raw sql.They wanted to get all the RATING objects with the where condition.
So how to build a query by using toplink.
tried your example
ExpressionBuilder builder = new ExpressionBuilder();
Expression creditRating = builder.getTable("RATING").getField("ID_C");
Expression issue_ref = builder.getTable("REF").getField("ID_C");
Expression join = creditRating.equal(issue_ref);
I am getting java.lang.OutOfMemoryError
error.
I selected the option generate classes and descriptors
from the tables (RATING,REF).,so it created the classes and dscriptors automatically.
In Database script for the table RATING like this
ALTER TABLE RATING ADD (
CONSTRAINT RATING_F1 FOREIGN KEY (ID_C, ALPHA_C)
REFERENCES REF (ID_C,ALPHA_C));
I think when i generate descriptor automatically it is keeping this association.
Please help me......... -
Query to join 2 tables by interval of value
Hi all
I need your help in creating a query to join 2 tables by interval of values.
The thing i want to d ois like this: i have 2 tables the first one containing list of serial number and the secon is containing interval of serial number (startnumber and end number) and other information like the user creating the serial number.
I want to list all serial numbers from the first table and the user creating each serial number from the second one if the serial number is between each interval of the second table.
here is an example>
SQL> create table test1 (code varchar2(1), serial number);
Table created.
SQL> create table test2 (requester varchar2(10),startno number, endno number);
Table created.
SQL> insert into test1 values('A',1);
1 row created.
SQL> insert into test1 values('B',3);
1 row created.
SQL> insert into test1 values('C',8);
1 row created.
SQL> insert into test1 values('D',12);
1 row created.
SQL> insert into test1 values('E',20);
1 row created.
SQL> commit;
Commit complete.
SQL> insert into test2 values('TATA',1,5);
1 row created.
SQL> insert into test2 values('TOTO',6,10);
1 row created.
SQL> commit;
Commit complete.
The result i want to get are:
TESt1.CODE TEST2.REQUESTER
A TATA
B TATA
C TOTO
D null
E nullUse outer join:
select code,
requester
from test1,
test2
where serial between startno(+) and endno(+)
order by code
C REQUESTER
A TATA
B TATA
C TOTO
D
E
SQL> SY. -
Opitmizing sql query using join
Hello all,
I have the following query that needs to be written using Joins. I am not sure if this is the correct place to post this question
Tables used:
1. activities: (a_id, n_id, market, amount, act_type, act_number, act_seq) --the money paid by the registrant for the market.
2. market_reg (a_id, n_id, market, reg_stage) -- for each market there are registrants
3. market (market) -- stores market details
I need to get sum of total_payment_amount using the condition. I know the query is not opitmized and its not the correct way to get info. I was
wondering if you guys can suggest me a query that will pull the information by using joins
SELECT a.a_id, a.n_id, SUM (a.total_payment_amount)
FROM activities a
WHERE a.market = 'marketname'
AND a.a_id||a.n_id IN
(SELECT mr.a_id||mr.n_id
FROM market_reg mr
WHERE mr.market = 'marketname'
AND mr.reg_stage = 'P'
AND mr.n_id <> 0)
AND (a.act_type = 'A'
OR (a.act_type IS NULL
AND a.act_number||act_seq IN
( SELECT a1.act_number||a1.act_seq
FROM activities a1
WHERE a1.market = 'marketname'
GROUP BY a1.act_number||a1.act_seq
HAVING COUNT (a1.act_number||a1.act_seq) = 1)))
GROUP BY a.a_id, a.n_id;
ThanksHi,
I see you have already re-posted this on a more appropriate forum, the SQL and PL/SQL forum.
Thanks,
Gary -
ANSI SQL Syntax - What belongs to join-clause and what to where-clause
Hello,
we currently have a discussion about the ANSI SQL Syntax where we do not agree what belongs to the join clause and what belongs to the where clause in an ANSI Sytnax SQL Query.
Lets say there is a query like this:
+SELECT *+
FROM employees emp, departments dept
WHERE emp.dept_country = dept.dept_country
AND emp.dept_name = dept.dept_name
AND dept.dept_type = 'HQ'
AND emp.emp_lastname = 'Smith'
Primary key of the departments table is on the columns dept_country, dept_name and dept_type. We have a Oracle database 10g.
Now I have rewritten the query to Ansi Syntax:
+SELECT *+
FROM employees emp
JOIN departments dept
ON emp.dept_country = dept.dept_country AND emp.dept_name = dept.dept_name
WHERE dept.dept_type = 'HQ'
AND emp.emp_lastname = 'Smith'
Another developer says that this is not completely correct, every filter on a column that belongs to the primary-key of the joined table has to be in the join clause, like this:
+SELECT *+
FROM employees emp
JOIN departments dept
+ON emp.dept_country = dept.dept_country AND emp.dept_name = dept.dept_name AND dept.dept_type = 'HQ'
WHERE emp.emp_lastname = 'Smith'
Can somebody tell me which on is correct?
Is there any definition for that? I couldn't find it in the Oracle Database definition.
I just found out the names of the ANSI documents here: http://docs.oracle.com/cd/B19306_01/server.102/b14200/ap_standard_sql001.htm#i11939
I had a look at the ANSI webstore but there you have to buy the PDF files. In my case thats exaggerated because both of the Queries work and i am just interessted if there is one correct way.
Thank you in advance
MarcoHi,
As i guideline i would say, answer the question: should the result of the join be filtered or should only filtered rows be joined from a particular table?
This is helpful in the case of outer joins also, for inner joins it doesnt matters as said already be former posters, where there may be hughe semantical differences depending of where the predicates are placed.
From performance view, if we talk about oracle, take a look a the execution plans. You will see that there is (probably) no difference in case of inner joins. Even in case of outer joins the optimizer pushes the predicate as a filter towards the table if it semantically possible.
Regards -
SQL query -- self-join question?
SQL> l
1* select originator,destination,oaddress,daddress from (select * from activity where rownum<=3)
SQL> /
10099 10004 16196344392 16199375530
10064 10002 18454644069 18456563415
10065 10006 18302650166 16416609306
looking at the above query, i am just performing a simple select from one of my tables. Now i require the carriername for both originator and destination columns, and the names for these are found on another table carrier.
so, i am re-write the above query as: (join with carrier table)
SQL>
SQL> select originator,destination,oaddress,daddress,carriername from (select * from activity,carrier where originator=carrier_code and rownum<=3);
10006 10099 19182772772 19189553062 USA1
10004 10311 15096701636 15096692171 USA2
10000 10003 15125898141 15122930569 USA3
Now, i got the carrier name for my originator, how would i find the carriername for my destination also (in the same query). One way of doing it is joining the carrier table twice, but is there any other better approach.
Hope i am clear, any help will be greatly appreciated. Thanks.select
A.originator, A.destination, A.oaddress, A.daddress
, B.carrier_name name_originator
, C.carrier_name name_destination
from
activity A
,carrier B
,carrier C
where
A.originator = B.carrier_code
and A.destination = C.carrier_code -
Error in update query with join
hi all,
im using oracle 10g in windows.
im not able to use this update query having join......
UPDATE
b
SET
b.is_stud = 1
FROM
boy b
INNER JOIN
relationship r
ON
b.id = r.boy_id;
thanks a lot..................887268 wrote:
hi, thanks,,,,,,,,,
create table emp ( id,name,date,empno);
create table emp_status(slno,ename,empno);
i need to update "emp.name" in "emp" table from "emp_status.ename"
where emp.empno=emp_status.empno;
i.e) for all matched "empno" from both table, update "emp.name" from "emp_status.ename"Whats the relationship between emp and emp_status tables? If there exists one to one mapping for empno in both tables, then try
update emp e
set e.name = (select es.ename
from emp_status es
where e.empno = es.empno) -
query/right join/
Ultimately we are trying to find the ZZOMTAB.STAFF
I know the value exist because I have queried this by
K_NO
By this query
Select
ZZOMTAB.STAFF
from
ZZOMTAB
where
K_NO = 146735
so it is there
but what I am supplied with is app_no=79219
I have tried this query and it does not work ( no value in the ZZOMTAB.STAFF)
SELECT
ZZOMTAB.STAFF
FROM TKASERTAB
RIGHT JOIN ZZOMTAB
ON TKASERTAB.K_NO = ZZOMTAB.K_NO
RIGHT JOIN APPTAB
ON TKASERTAB.app_no= APPTAB.app_no
where
APPTAB.app_no=79219
the value that I am trying to get is in ZZOMTAB.STAFF
and I am given with app_no=79219
the tables are
tables are
TKASERTAB with columns K_NO and APPNO
APPTAB with column APPNO
and
ZZOMTAB with K_NO
I am trying to get the value of ZZOMTAB.STAFF
Can someone help??
Thanks in advquery/right join/
I have tried this query and it does not work ( no
value in the ZZOMTAB.STAFF)
SELECT
ZZOMTAB.STAFF
FROM TKASERTAB
RIGHT JOIN ZZOMTAB
ON TKASERTAB.K_NO = ZZOMTAB.K_NO
RIGHT JOIN APPTAB
ON TKASERTAB.app_no= APPTAB.app_no
where
APPTAB.app_no=79219
the value that I am trying to get is in
ZZOMTAB.STAFF
and I am given with app_no=79219
Why do You think, that the value must be here ?
Regards
Dmytro -
Peoplesoft Query Outer join with MAX EFFDT
I have this criteria in my PS Query
where A.emplid = B.emplid
and B.effdt = (select max( B1.effdt ) from PS_TABLE_B B1 where B1.effdt <= SYSDATE )
Need to do an OUTER JOIN for B.
Ie. If no row found in B, then return blank so that row in A can be returned by query.
It's OK for A.emplid = B.emplid(+)
But how to approach for and B.effdt = (select max ... ?
Please adviseIf I remember it right, if you write outer joins using ( + ), there shouldn't be any other non-outer join criteria in your SQL, otherwise it will negate its being an 'outer join'.
so for your (1) this highlighted SQL block negates your 'outer join' condition
AND A.EMPLID = C.EMPLID(+)
AND ( C.EFFDT = (SELECT MAX( D.EFFDT)
FROM PS_D D
WHERE D.EMPLID = C.EMPLID
AND D.ACTION_REASON = 'MAT'
AND ( D.RETURN_DT IS NULL OR D.RETURN_DT > SYSDATE))
OR C.EFFDT IS NULL) )
you should see more from here http://docs.oracle.com/cd/B28359_01/server.111/b28286/queries006.htm#SQLRF52336
From that article, it says.. "If A and B are joined by multiple join conditions, then you must use the operator in all of these conditions. If you do not, then Oracle Database will return only the rows resulting from a simple join, but without a warning or error to advise you that you do not have the results of an outer join."
What I suggest, create a SQL view to replace C that retrieves exactly what C has to retrieve.. probably something like this
SELECT D.*
FROM PS_D D
WHERE D.ACTION_REASON = 'MAT'
AND( D.RETURN_DT IS NULL OR D.RETURN_DT > SYSDATE )
AND D.EFFDT = (SELECT MAX(EFFDT) ... )
and reference that new SQL view for C.. so that in your query, it should only look something like
AND A.EMPLID = C.EMPLID(+)
AND( C.EFFDT =
(SELECT MAX( D.EFFDT )
FROM PS_D D
WHERE D.EMPLID = C.EMPLID
AND D.ACTION_REASON = 'MAT'
AND( D.RETURN_DT IS NULL
OR D.RETURN_DT > SYSDATE )
OR C.EFFDT IS NULL ) )
Hope this helps -
Hi,
Please give me the Select query
I have to fetch EBELN,BUKRS,LIFNR,BSART,EKORG,EKGRP,WAERS,WKURS from EKKO and
EBELP,LGORT,MATKL ,WERKS,BEDNR,IDNLF,BPRME,NETPR from EKPO and
MENGE, SAKTO,AUFNR,ANLN1,GSBER,PS_PSP_PNR from EKKN and
LPEIN,EINDT,SLFDT, from EKET.
I have to fetch all the data into an single internal table(Mandatory) by a select query using Joins.
EKKO is having EBELN as primary key
EKPO is having EBELN,EBELP as primary key
EKKN is having EBELN,EBELP as primary key
EKET is having EBELN,EBELP as primary key
Can anybody Please give me the Select query.
With Regards
AjayHi ,
I am sending this sample coed for the Inner joins .Hope this will help you:
Inner joins using 3 tables
Try this :-
SELECT stpostlnr stpoidnrk mastmatnr maramtart stpo~menge
INTO CORRESPONDING FIELDS OF TABLE zmat1 FROM mast
JOIN stpo ON stpostlnr = maststlnr
JOIN mara ON maramatnr = mastmatnr
WHERE stpostlty = 'M' "AND stpoidnrk IN s_matnr
AND mast~werks = 1000.
Here s_matnr is a select-options on the selection-screen.
Or this.
Code:
Select single VbrkBukrs VbrkKunrg Vbrk~Vbeln
VbrkFkdat VbrkBstnk_Vf Vbrk~Zterm
Tvzbt~Vtext
VbakVbeln VbakBstdk
LikpVbeln Likplfdat Likp~Lfuhr
into w_vbrk
from vbrk
inner join Tvzbt on TvzbtZterm = VbrkZterm and
Tvzbt~Spras = sy-langu
Inner join Vbfa as SalesLnk
on SalesLnk~vbeln = pu_vbeln and
SalesLnk~vbtyp_v = c_order
inner join Vbak on VbakVbeln = SalesLnkVbelv
Inner join Vbfa as DeliveryLnk
on DeliveryLnk~vbeln = pu_vbeln and
DeliveryLnk~vbtyp_v = c_Delivery
inner join Likp on LikpVbeln = DeliveryLnkVbelv
where vbrk~vbeln = pu_Vbeln.
This code locates sales, delivery and payment terms info from a billing document number.
or
Here, this one also works fine :
select zfpcdcadivi zfpcdproforma zfpcdfactura zfpcdaniofactura
zfpcdmontousd zfpcdmontoap zfpcdebeln zfpcdinco1
zfpcdlifnr lfa1name1 zcdvsstatus zfpcdconint
into it_lista
from zfpcd inner join zcdvs
on zfpcdebeln = zcdvsebeln
and zfpcdproforma = zcdvsproforma
and zfpcdlifnr = zcdvslifnr
inner join lfa1
on zfpcdlifnr = lfa1lifnr
where zcdvs~status = '04'.
Also Here is another solution that just uses inner joins:
SELECT vbakvbeln vbfaerdat INTO (itab-vbeln, itab-wadat)
FROM ( vbak INNER JOIN vbap
ON vbapvbeln = vbakvbeln )
INNER JOIN vbfa ON vbakvbeln = vbfavbelv
WHERE vbak~kunnr = m_wm AND
vbak~vbtyp = 'C' AND
vbfa~erdat IN s_date AND
vbap~matnr = 'MZ1807F' AND
vbfaposnv = vbapposnr AND
vbfa~vbtyp_n = 'J'.
<REMOVED BY MODERATOR>
Cheers,
Chandra Sekhar.
Edited by: Alvaro Tejada Galindo on Apr 10, 2008 5:20 PM
Maybe you are looking for
-
How to get sum of bugs for particular Mnth and particular Year dynamically?
Hi All, I've a query related to dynamic date and year : select bug_id, category, count(*) Total_bugs, SUM(CASE when bug_date >= '10/1/2011' and bug_date <= '10/31/2011' Then 1 else 0 end) OCT_11, SUM(CASE when bug_date >= '9/1/2011' and bug_date<= '9
-
Dear Expert, When I was trying to run AJAB to close fiscal year 2007 and I got an error message "The planned depreciation was not completely posted to Financial Accounting for asset 017000000000-0000 in company code 0011 in depreciation area 01u201D
-
Dear Friends, could you please let me know what exactly the postings will be when the post closing step is done for multi-level price determination finished material. as i observed system is posting the below entries after post closing: 30.09.2008 Fi
-
Why does iCloud store only purchased music?
I have both purchased and downloaded from cd music on my iPad and iPod. I am unable to sync either with my pc as it says the content will be replaced with purchased items. Whilst I can load this stuff again ther are two purchased items, a film which
-
Capturing in HD, then creating SD DVD
I am about to buy a Canon HV20 HD camcorder. Of course I want to capture all my footage in HD but since neither I nor anyone who I will be sharing DVDs with have an HD DVD player, I will want to create SD DVDs. How difficult will this process be with