Joining two queries
hi every one...
i have to prepare report in the following way
srno asset_code asset_group asset_cost
here asset code and asset group i have to pick from flex field valueset...fr this the query is
SELECT FLEX_VALUE asset_code, DESCRIPTION asset_group
FROM FND_FLEX_VALUES_VL ffvh
WHERE ffvh.flex_value_set_id =
(select FLEX_VALUE_SET_ID from fnd_flex_value_sets
where FLEX_VALUE_SET_NAME = 'XXEAM_ACCOUNT_CODE_KFF')
AND ENABLED_FLAG = 'Y'
AND SUMMARY_FLAG = 'Y'
here the each asset_code has their child values and asset cost to that child value..
so i have to calculate that total asset cost for total child values of each parent(asset_code)...
for this the query is
SELECT sum(asset_cost) asset_cost
FROM csi_item_instances cii,
xxeam_depreciation_master xdm
WHERE cii.serial_number = xdm.serial_number
AND cii.attribute3 IN (
SELECT ffvh.flex_value
FROM fnd_flex_values_vl ffvh
WHERE ffvh.flex_value_set_id =
(SELECT flex_value_set_id
FROM fnd_flex_value_sets
WHERE flex_value_set_name =
'XXEAM_ACCOUNT_CODE_KFF')
AND enabled_flag = 'Y'
AND summary_flag = 'N'
AND ffvh.flex_value IN (
SELECT child_flex_value_low
FROM fnd_flex_value_norm_hierarchy ffvnh
WHERE ffvnh.range_attribute = 'C'
AND ffvnh.flex_value_set_id =
ffvh.flex_value_set_id
AND ffvnh.parent_flex_value ='10.100'))
here instead of mentioning ffvnh.parent_flex_value ='10.100'
i have to find for all parent values..
so im not able to find this and im not able to make joins..
so any one plz help me...
thanks in advance..
More or less a stab in the dark, but it sounds like you should cross join the groups and date tables with each other:
SELECT DATEADD(MONTH,- 1, A.Date_Date)AS Report_Date, GROUP_NAME
SELECT COUNT(B.Ticket_ID)AS Active_Ticket_Count
FROM [HPSM].[dbo].[HPSM_Assignments] B
WHERE B.Assignment_Scope= 'Group'
AND B.Assigned_Timestamp< A.Date_Date
AND B.Resolved_Timestamp> A.Date_Date
AND B.Group_Name= G.Assigned_To
) AS Active_Count
FROM [ITSReporting ].[dbo].[REF_Date]A
CROSS JOIN [HPSM].[dbo].[HPSM_Groups] G
WHERE Date_Is_Month_Start= 1
AND A.Date_Date>= dateadd(ms,- 3,dateadd(mm,- 12,dateadd(mm,datediff(mm,0,getdate()),0)))
AND A.Date_Date< dateadd(ms,- 3,dateadd(mm,1,dateadd(mm,datediff(mm,0,getdate()),0)))
Erland Sommarskog, SQL Server MVP, [email protected]
Similar Messages
-
I'm tryinng to join two queries that I made in sqlplus:
the first one is:
select dname,sum(hours) as proj_hrs
from IBL.works_on w ,IBL.project p,IBL.department d
where w.pno=p.pnumber
and p.dnum=d.dnumber
group by dname;
that output:
DNAME PROJ_HRS
Administration 115
Headquarters 25
Research 155
the second one is:
select dname,sum(hours) as emp_hrs
from IBL.works_on w,IBL.employee e,IBL.department d
where e.dno=d.dnumber
and w.essn=e.ssn
group by dname;
that output
DNAME EMP_HRS
Administration 115
Headquarters
Research 180
I'm trying to join both of them to get:
DNAME PROJ_HRS EMP_HRS
Administration 115 115
Headquarters 25
Research 155 180
Can anybody help me pleaseIf I understood you correctly, have you tried this? I did not try this, just think that it will work.
select a.dname, a.proj_hrs, b.emp_hrs from
select dname,sum(hours) as proj_hrs
from IBL.works_on w ,IBL.project p,IBL.department d
where w.pno=p.pnumber
and p.dnum=d.dnumber
group by dname
) a,
select dname,sum(hours) as emp_hrs
from IBL.works_on w,IBL.employee e,IBL.department d
where e.dno=d.dnumber
and w.essn=e.ssn
group by dname
) b
where a.dname = b.dname
You can use the NVL function to print out something special for the null values. -
Joining two queries with Visual Composer
Hallo,
I was working with visual composer to join two queries with the distinct operator. But on my table view, when i linked it with the distinct operator, i get only the key element customer (customer, customer_ext_key, customer_key). These are the queries that i want join:
Query 1:
Customer
Net Sales
Query 2:
Customer
Billed Quantity
What i want to see is the following result:
Customer
Net Sales
Billed Quantity
Can anybody help me with this problem?
Greeting,
Murat.Hallo,
I was working with visual composer to join two queries with the distinct operator. But on my table view, when i linked it with the distinct operator, i get only the key element customer (customer, customer_ext_key, customer_key). These are the queries that i want join:
Query 1:
Customer
Net Sales
Query 2:
Customer
Billed Quantity
What i want to see is the following result:
Customer
Net Sales
Billed Quantity
Can anybody help me with this problem?
Greeting,
Murat. -
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Prod
_1st query_
select
(CASE WHEN SUBSTR(R1.RA_NO,1,3)= 'NRA' THEN 'Damansara Uptown One Sdn. Bhd.'
WHEN SUBSTR(R1.RA_NO,1,3)= 'ORA' THEN 'Damansara Uptown Two Sdn. Bhd.'
WHEN SUBSTR(R1.RA_NO,1,3)= 'VRA' THEN 'Damansara Uptown Realty Sdn. Bhd.'
WHEN SUBSTR(R1.RA_NO,1,3)= 'URA' THEN 'Uptown Elite Sdn. Bhd.'
when SUBSTR(R1.RA_NO,1,3)= 'WRZ' THEN 'U5 Management Corporation Sdn. Bhd.'
WHEN SUBSTR(R1.RA_NO,1,2)= 'FR' THEN 'See Hoy Chan Facilities Management Sdn.Bhd.' END) Landlord
,TO_CHAR(C1.COLL_DTE,'RRRRMM') MONTH1
,SUM(C2.TRN_AMT) UPTO_31
, SUM(CASE WHEN to_char(C1.COLL_dte) BETWEEN to_date(:P_FROM_DATE,'dd.mm.yyyy') AND '16-'||to_date(:P_TO_DATE,'dd.mm.yyyy') THEN C2.TRN_AMT END) UPTO_15
from ra1 R1, COLLECT1 C1 , COLLECT2 C2
where
(to_char(C1.COLL_dte) between to_date(:P_FROM_DATE,'dd.mm.yyyy') AND to_date(:P_TO_DATE,'dd.mm.yyyy'))
and (SUBSTR(R1.RA_NO,1,2)= 'FR' or SUBSTR(R1.RA_NO,1,3) in ('NRA' ,'ORA','VRA','URA','WRZ'))
AND R1.RA_NO = C2.INV_NO
AND C2.COLL_NO = C1.COLL_NO
GROUP BY
(CASE WHEN SUBSTR(R1.RA_NO,1,3)= 'NRA' THEN 'Damansara Uptown One Sdn. Bhd.'
WHEN SUBSTR(R1.RA_NO,1,3)= 'ORA' THEN 'Damansara Uptown Two Sdn. Bhd.'
WHEN SUBSTR(R1.RA_NO,1,3)= 'VRA' THEN 'Damansara Uptown Realty Sdn. Bhd.'
WHEN SUBSTR(R1.RA_NO,1,3)= 'URA' THEN 'Uptown Elite Sdn. Bhd.'
when SUBSTR(R1.RA_NO,1,3)= 'WRZ' THEN 'U5 Management Corporation Sdn. Bhd.'
WHEN SUBSTR(R1.RA_NO,1,2)= 'FR' THEN 'See Hoy Chan Facilities Management Sdn.Bhd.' END)
,TO_CHAR(C1.COLL_DTE,'RRRRMM')
_2query_
select sum(decode(substr(ra_no,1,7),'NRA'
||to_char(to_date(:P_FROM_DATE,'dd.mm.yyyy'),'yymm'),tot_amt,0)) NRA
,sum(decode(substr(ra_no,1,7),'VRA'
||to_char(to_date(:P_FROM_DATE,'dd.mm.yyyy'),'yymm'),tot_amt,0)) VRA
,sum(decode(substr(ra_no,1,7),'ORA'
||to_char(to_date(:P_FROM_DATE,'dd.mm.yyyy'),'yymm'),tot_amt,0)) ORA
,sum(decode(substr(ra_no,1,7),'FR'
||to_char(to_date(:P_FROM_DATE,'dd.mm.yyyy'),'yymm'),tot_amt,0)) FR
,sum(decode(substr(ra_no,1,7),'WRZ'
||to_char(to_date(:P_FROM_DATE,'dd.mm.yyyy'),'yymm'),tot_amt,0)) WRZ
,sum(decode(substr(ra_no,1,7),'URA'
||to_char(to_date(:P_FROM_DATE,'dd.mm.yyyy'),'yymm'),tot_amt,0)) URA
from RA1
above are the two queries i need to join these two queries by naming the second query column name as total .1st query output is ----------
LOCATION MONTH1 UPTO_31 UPTO_15
U5 Management Corporation Sdn. Bhd. 201001 15250 8900
Uptown Elite Sdn. Bhd. 201001 3000 1500
See Hoy Chan Facilities Management Sdn.Bhd. 201001 917115.45 584876.5
Damansara Uptown Two Sdn. Bhd. 201001 757277.45 495362.95
Damansara Uptown One Sdn. Bhd. 201001 881558.65 404872.45
Damansara Uptown Realty Sdn. Bhd. 201001 321675.8 150508.6
2nd query output is -------
NRA ORA VRA URA WRZ FR
2323.31 95945 34367.8 34267 4343 343
Now what i need is
LOCATION MONTH1 UPTO_31 UPTO_15 TOTAL
U5 Management Corporation Sdn. Bhd. 201001 15250 8900 2323.31
Uptown Elite Sdn. Bhd. 201001 3000 1500 95945
See Hoy Chan Facilities Management Sdn.Bhd. 201001 917115.45 584876.5 34367.8
Damansara Uptown Two Sdn. Bhd. 201001 757277.45 495362.95 34267
Damansara Uptown One Sdn. Bhd. 201001 881558.65 404872.45 4343
Damansara Uptown Realty Sdn. Bhd. 201001 321675.8 150508.6 343above is the clear picture what i need , i don't know how to do it please help me
Edited by: user9093689 on Feb 21, 2010 8:06 PM -
Joining two queries causes big performance hit
There are two queries: one with a sub query and one without
although they both return the same data, one uses a full table scan of the indexed 'trade_index' table
and thus is much slower
any Ideas why it does this?:
SQL> select count(*) from trade_index ti, counterparty_xref cx
2 where ti.CPY_SID = cx.CPY_SID and ti.TRADE_OR_ARCHIVE = 'A'
3 and cx.XREF_VALUE in (select ml.VALUE from v_mis_lists ml where ml.LIST_NAME = 'Deutsche Bank DB')
4 and cx.XREF_TYPE = 'Client RXM'
5 and ti.START_DATE < to_date('01/01/2004','DD/MM/YYYY')
6 and (ti.END_DATE >= to_date('01/01/2003','DD/MM/YYYY') or ti.END_DATE = '01-Jan-1900') ;
COUNT(*)
6195
Elapsed: 00:00:15.08
Execution Plan
0 SELECT STATEMENT Optimizer=CHOOSE (Cost=2572 Card=1 Bytes=57)
1 0 SORT (AGGREGATE)
2 1 HASH JOIN (Cost=2572 Card=26581 Bytes=1515117)
3 2 HASH JOIN (Cost=460 Card=5619 Bytes=219141)
4 3 VIEW OF 'VW_NSO_1' (Cost=8 Card=16 Bytes=272)
5 4 SORT (UNIQUE) (Cost=8 Card=16 Bytes=960)
6 5 HASH JOIN (Cost=4 Card=16 Bytes=960)
7 6 TABLE ACCESS (BY INDEX ROWID) OF 'MIS_LISTS' (Cost=1 Card=2 Bytes=60)
8 7 INDEX (RANGE SCAN) OF 'MISL_NAT_UK' (UNIQUE) (Cost=2 Card=2)
9 6 TABLE ACCESS (FULL) OF 'MIS_LIST_VALUES' (Cost=2 Card=817 Bytes=24510)
10 3 TABLE ACCESS (FULL) OF 'COUNTERPARTY_XREF' (Cost=451 Card=35116 Bytes=772552)
11 2 TABLE ACCESS (FULL) OF 'TRADE_INDEX' (Cost=2056 Card=286928 Bytes=5164704)
SQL> select ml.VALUE from v_mis_lists ml where ml.LIST_NAME = 'Deutsche Bank DB';
VALUE
B49540
B60010
Elapsed: 00:00:00.00
Execution Plan
0 SELECT STATEMENT Optimizer=CHOOSE
1 0 NESTED LOOPS
2 1 TABLE ACCESS (BY INDEX ROWID) OF 'MIS_LISTS'
3 2 INDEX (RANGE SCAN) OF 'MISL_NAT_UK' (UNIQUE)
4 1 INDEX (RANGE SCAN) OF 'MISLV_PK' (UNIQUE)
SQL> select count(*) from trade_index ti, counterparty_xref cx
2 where ti.CPY_SID = cx.CPY_SID and ti.TRADE_OR_ARCHIVE = 'A'
3 and cx.XREF_VALUE in ('B49540','B60010')
4 and cx.XREF_TYPE = 'Client RXM'
5 and ti.START_DATE < to_date('01/01/2004','DD/MM/YYYY')
6 and (ti.END_DATE >= to_date('01/01/2003','DD/MM/YYYY') or ti.END_DATE = '01-Jan-1900') ;
COUNT(*)
6195
Elapsed: 00:00:02.06
Execution Plan
0 SELECT STATEMENT Optimizer=CHOOSE (Cost=532 Card=1 Bytes=40)
1 0 SORT (AGGREGATE)
2 1 NESTED LOOPS (Cost=532 Card=41 Bytes=1640)
3 2 TABLE ACCESS (FULL) OF 'COUNTERPARTY_XREF' (Cost=451 Card=1 Bytes=22)
4 2 TABLE ACCESS (BY INDEX ROWID) OF 'TRADE_INDEX' (Cost=81 Card=286928 Bytes=5164704)
5 4 INDEX (RANGE SCAN) OF 'CPY_SID_IDX' (NON-UNIQUE) (Cost=2 Card=286928)
What I dont understand is that:
cx.XREF_VALUE in (select ml.VALUE from v_mis_lists ml where ml.LIST_NAME = 'Deutsche Bank DB')
and
cx.XREF_VALUE in ('B49540','B60010')
are essentially the same thing
(ie the subquery returns the values I have used)
but the optimizer uses a totally different (slower) method to execute the rest of the former query?
Many thanks
IanHi,
Here is with join and not using the view: still have full table scan of large table trade_index
SQL> select count(*) from mis_lists ml, mis_list_values mlv, counterparty_xref cx, trade_index ti
2 where ti.CPY_SID = cx.CPY_SID and ti.TRADE_OR_ARCHIVE = 'A'
3 and cx.XREF_VALUE = mlv.VALUE
4 and mlv.MISL_SID = ml.MISL_SID
5 and ml.LIST_NAME = 'Deutsche Bank DB'
6 and cx.XREF_TYPE = 'Client RXM'
7 and ti.START_DATE < to_date('01/01/2004','DD/MM/YYYY')
8 and (ti.END_DATE >= to_date('01/01/2003','DD/MM/YYYY') or ti.END_DATE = '01-Jan-1900') ;
COUNT(*)
6195
Elapsed: 00:00:12.08
Execution Plan
0 SELECT STATEMENT Optimizer=CHOOSE (Cost=2602 Card=1 Bytes=10
0)
1 0 SORT (AGGREGATE)
2 1 HASH JOIN (Cost=2602 Card=26581 Bytes=2658100)
3 2 HASH JOIN (Cost=456 Card=5619 Bytes=460758)
4 3 HASH JOIN (Cost=4 Card=16 Bytes=960)
5 4 TABLE ACCESS (BY INDEX ROWID) OF 'MIS_LISTS' (Cost1 Card=2 Bytes=60)
6 5 INDEX (RANGE SCAN) OF 'MISL_NAT_UK' (UNIQUE) (Cost=2 Card=2)
7 4 TABLE ACCESS (FULL) OF 'MIS_LIST_VALUES' (Cost=2 Card=817 Bytes=24510)
8 3 TABLE ACCESS (FULL) OF 'COUNTERPARTY_XREF' (Cost=451Card=35116 Bytes=772552)
9 2 TABLE ACCESS (FULL) OF 'TRADE_INDEX' (Cost=2056 Card=286928 Bytes=5164704)
Statistics
0 recursive calls
47 db block gets
16519 consistent gets
4981 physical reads
0 redo size
205 bytes sent via SQL*Net to client
622 bytes received via SQL*Net from client
3 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
Thanks for you help,
Ian -
Need to join two queries A/R statemet and Activity report
I have two separate queries that need to be one. I have an A/R statement where I want to add notes next to each invoice. Currently these are two separate reports. How can I link the activity Report to the A/R Statement. Below are the two separate reports. I am not sure how to join the files. A/R Aging Report Declare @d datetime set @d /*Select 1 from jdt1 t where t.duedate*/=[%1] select * from ( SELECT T1.CardCode, T1.CardName, T0.RefDate, T0.Ref1 'Document Number', CASE WHEN T0.TransType=13 THEN 'Invoice' WHEN T0.TransType=14 THEN 'Credit Note' WHEN T0.TransType=30 THEN 'Journal' WHEN T0.TransType=24 THEN 'Receipt' END AS 'Document Type', T0.DueDate, (T0.Debit- T0.Credit) 'Balance' ,ISNULL((SELECT T0.Debit-T0.Credit WHERE DateDiff(day, T0.DueDate,@d)=0 and DateDiff(day, T0.DueDate,@d)30 and DateDiff(day, T0.DueDate,@d)60 and DateDiff(day, T0.DueDate,@d)90 and DateDiff(day, T0.DueDate,@d)=121),0) '121+ Days' FROM JDT1 T0 INNER JOIN OCRD T1 ON T0.ShortName = T1.CardCode WHERE T1.CardType = 'C' ) sub Activity Report SELECT T1.[ClgCode],T2.[U_NAME], T4.[Name] as Status, T0.[CardCode], T0.[CardName], T0.[DocNum], T0.[U_AIS_DVIInvName] as 'SF Invoice Name', (T0.U_AIS_DVISFSO) as 'SO#', T0.[DocDate],T0.Taxdate as 'Month Of Service',T0.[U_AIS_DVIOpptyNm],T0.[U_AIS_DVIAdvNm],T0.[U_AIS_DVIManNm],T0.[U_AIS_DVIAcctNm], T0.[DocTotal], T0.[DocStatus], T1.[Recontact], T1.[Details], T1.[Notes], T1.[CntctType], T3.[Name] FROM OINV T0 left join OCLG T1 on t0.docentry = t1.docentry and T0.[ObjType] = T1.[DocType] left outer JOIN OUSR T2 ON T1.UserSign = T2.USERID left outer join OCLT T3 ON T1.CntctType = T3.Code left outer join OCLS T4 ON T1.CntctSbjct = T4.Code where T1.[CntctType] =6 or T1.[CntctType] =7
This is what I have so far.. it finally produced results but I notice the amount on the Activity + Aging does not match what is on the Aging and that seems to be what the issue is... that was my check to know the report is working. Basically the report should show the aging + any activity associated with the open invoice. The amount on the report is lower than the aging.. I am attaching the aging query and the aging with activity Query. It looks like something is not joining correctly as the amounts do not match on the balance. Aging Report Declare @d datetime set @d /*Select 1 from jdt1 t where t.duedate*/=[%1] select * from ( SELECT T1.CardCode, T1.CardName, T0.RefDate, T0.Ref1 'Document Number', CASE WHEN T0.TransType=13 THEN 'Invoice' WHEN T0.TransType=14 THEN 'Credit Note' WHEN T0.TransType=30 THEN 'Journal' WHEN T0.TransType=24 THEN 'Receipt' END AS 'Document Type', T0.DueDate, (T0.Debit- T0.Credit) 'Balance' ,ISNULL((SELECT T0.Debit-T0.Credit WHERE DateDiff(day, T0.DueDate,@d)=0 and DateDiff(day, T0.DueDate,@d)30 and DateDiff(day, T0.DueDate,@d)60 and DateDiff(day, T0.DueDate,@d)90 and DateDiff(day, T0.DueDate,@d)=121),0) '121+ Days' FROM JDT1 T0 INNER JOIN OCRD T1 ON T0.ShortName = T1.CardCode WHERE T1.CardType = 'C' ) sub Activity + Aging Declare @d datetime set @d /*Select 1 from jdt1 t where t.duedate*/=[%1] select * from ( SELECT T1.CardCode, T1.CardName, T0.RefDate, T0.Ref1 'Document Number', CASE WHEN T0.TransType=13 THEN 'Invoice' WHEN T0.TransType=14 THEN 'Credit Note' WHEN T0.TransType=30 THEN 'Journal' WHEN T0.TransType=24 THEN 'Receipt' END AS 'Document Type', T0.DueDate, (T0.Debit- T0.Credit) 'Balance' ,ISNULL((SELECT T0.Debit-T0.Credit WHERE DateDiff(day, T0.DueDate,@d)=0 and DateDiff(day, T0.DueDate,@d)30 and DateDiff(day, T0.DueDate,@d)60 and DateDiff(day, T0.DueDate,@d)90 and DateDiff(day, T0.DueDate,@d)=121),0) '121+ Days', T6.[FirstName] as 'AP Contact First Name', T6.[LastName] as 'AP Contact Last Name' , T6.[E_MailL], T6.[Notes1] as ' AP Supervisor', T2.[ClgCode],T7.[U_NAME], T4.[Name] as Status, T5.[DocNum], T5.[U_AIS_DVIInvName] as 'SF Invoice Name', (T5.U_AIS_DVISFSO) as 'SO#', T5.[DocDate],T5.Taxdate as 'Month Of Service',T5.[U_AIS_DVIOpptyNm],T5.[U_AIS_DVIAdvNm],T5.[U_AIS_DVIManNm],T5.[U_AIS_DVIAcctNm], T5.[DocTotal], T5.[DocStatus], T2.[Recontact], T2.[Details], T2.[Notes], T2.[CntctType], T3.[Name] FROM JDT1 T0 INNER JOIN OCRD T1 ON T0.ShortName = T1.CardCode INNER JOIN OCLG T2 ON T1.CardCode = T2.CardCode INNER JOIN OCLT T3 ON T2.CntctType = T3.Code INNER JOIN OCLS T4 ON T2.CntctSbjct = T4.Code INNER JOIN OINV T5 ON t5.docentry = t2.docentry and T5.[ObjType] = T2.[DocType] left outer join OCPR T6 ON T2.CntctCode = T6.CntctCode left outer JOIN OUSR T7 ON T2.UserSign = T7.USERID WHERE T1.CardType = 'C' and (T2.[CntctType] =6 or T2.[CntctType] =7) ) sub
-
Hi
We recently inherited a database and we are re-wrting some queries. The first is
SELECT gu.GEOGRAPHIC_UNIT_ID, gu.GEOGRAPHIC_UNIT_DESC, aggDataC.DATA as Capacity, aggDataP.DATA as Production
FROM GEOGRAPHIC_UNIT_RELATIONSHIP gur, GEOGRAPHIC_UNIT
gu, AGGREGATED_DATA aggDataC, AGGREGATED_DATA aggDataP
WHERE gur.PARENT_GEOGRAPHIC_UNIT_ID = :geographicUnitId AND gur.CHILD_GEOGRAPHIC_UNIT_ID = gu.GEOGRAPHIC_UNIT_ID
AND aggDataC.GEOGRAPHIC_UNIT_ID = gu.GEOGRAPHIC_UNIT_ID
AND ((:CommodityGroupId IS NULL AND
aggDataC.COMMODITY_GROUP_ID IS NULL) OR (:CommodityGroupId = aggDataC.COMMODITY_GROUP_ID))
AND ((:CommodityTypeId IS NULL AND
aggDataC.COMMODITY_TYPE_ID IS NULL) OR (:CommodityTypeId = aggDataC.COMMODITY_TYPE_ID))
AND ((:PlantTypeId IS NULL AND aggDataC.PLANT_TYPE_ID
IS NULL) OR (:PlantTypeId = aggDataC.PLANT_TYPE_ID))
AND aggDataC.ORGANISATION_ID is NULL
AND aggDataC.YEAR = :Year
AND aggDataC.STAT_TYPE_ID = (SELECT stat_type_id FROM
stat_type WHERE stat_type = 'CAP')
AND aggDataC.STAT_PERIOD_TYPE_ID = :StatPeriodTypeId
AND aggDataP.GEOGRAPHIC_UNIT_ID = gu.GEOGRAPHIC_UNIT_ID
AND ((:CommodityGroupId IS NULL AND
aggDataP.COMMODITY_GROUP_ID IS NULL) OR (:CommodityGroupId = aggDataP.COMMODITY_GROUP_ID))
AND ((:CommodityTypeId IS NULL AND
aggDataP.COMMODITY_TYPE_ID IS NULL) OR (:CommodityTypeId = aggDataP.COMMODITY_TYPE_ID))
AND ((:PlantTypeId IS NULL AND aggDataP.PLANT_TYPE_ID
IS NULL) OR (:PlantTypeId = aggDataP.PLANT_TYPE_ID))
AND aggDataP.ORGANISATION_ID is NULL
AND aggDataP.YEAR = :Year
AND aggDataP.STAT_TYPE_ID = (SELECT stat_type_id FROM
stat_type WHERE stat_type = 'PRD')
The above query returns only the geographic units that have capacity and production figures.
I want to return all other regions that have a plantypeid regardless of whether they have capacity and production data.
I tried to use outer joins but this has not worked. The database is an oracledatabase
the below query returns all the geographic regions I need
SELECT gu.GEOGRAPHIC_UNIT_ID, gu.GEOGRAPHIC_UNIT_DESC
FROM GEOGRAPHIC_UNIT_RELATIONSHIP gur, GEOGRAPHIC_UNIT gu
where
gur.PARENT_GEOGRAPHIC_UNIT_ID = :geographicUnitId
AND gur.CHILD_GEOGRAPHIC_UNIT_ID = gu.GEOGRAPHIC_UNIT_ID
ANy idea how I merge the 2 queries?UNION
-
Where clause in COUNT function and joining two queries
I have a table that I am trying to count the number of course passed, and also list the modules passed as well.
the first problem I am having is what to put in the where variable , so that its not specific to a customer(I can use the query below for a particular customer and a particular course)but I will like a generic query where the result will be distinct in terms
of user and course like the one below
select FirstName,LastName,CourseTitle,Noofmodules, count (Coursecompleted) as modulescompleted from EStudentsprogress where Coursecompleted = '1'and EmailAddress = '[email protected]'
and CourseTitle = 'Microsoft MOS 2010 EXCEL' Group by FirstName, LastName, CourseTitle, Noofmodules ;
How can i make it list the result as above, whereby i dont specify the email address or course title(trying to get the result for all the clients )
. Also I have a query that list the courses that is passed by the customer, I will like the column with the list of courses passed be added to the result above, but as a column for each course.
select FirstName,LastName,CourseTitle, EmailAddress, CourseModule as coursepassed from EStudentsprogress where coursecompleted =1
cheersDo you mean this?
select FirstName,
LastName,
CourseTitle,
Noofmodules,
count (Coursecompleted) as modulescompleted,
STUFF((SELECT ',' + CourseTitle
FROM EStudentsprogress
WHERE FirstName = e.FirstName
AND LastName = e.LastName
WHERE Coursecompleted = '1'
FOR XML PATH('')),1,1,'') AS CoursesCompleted
from EStudentsprogress e
where Coursecompleted = '1'
Group by FirstName, LastName, CourseTitle, Noofmodules ;
If not please provide some sample data and explain the output you want
Please Mark This As Answer if it solved your issue
Please Mark This As Helpful if it helps to solve your issue
Visakh
My MSDN Page
My Personal Blog
My Facebook Page
I AM HAVING Incorrect syntax near the keyword 'WHERE'.
It was a typo
try this
select FirstName,
LastName,
CourseTitle,
Noofmodules,
count (Coursecompleted) as modulescompleted,
STUFF((SELECT ',' + CourseTitle
FROM EStudentsprogress
WHERE FirstName = e.FirstName
AND LastName = e.LastName
AND Coursecompleted = '1'
FOR XML PATH('')),1,1,'') AS CoursesCompleted
from EStudentsprogress e
where Coursecompleted = '1'
Group by FirstName, LastName, CourseTitle, Noofmodules ;
Please Mark This As Answer if it solved your issue
Please Mark This As Helpful if it helps to solve your issue
Visakh
My MSDN Page
My Personal Blog
My Facebook Page
its populating all the result for a particular customer, so i added another clause to it and it worked
select FirstName,
LastName,
CourseTitle,
Noofmodules,
count (Coursecompleted) as modulescompleted,
STUFF((SELECT ',' + CourseTitle
FROM EStudentsprogress
WHERE FirstName = e.FirstName
AND LastName = e.LastName
AND Coursecompleted = '1'
AND CourseTitle = e.CourseTitle
FOR XML PATH('')),1,1,'') AS CoursesCompleted
from EStudentsprogress e
where Coursecompleted = '1'
Group by FirstName, LastName, CourseTitle, Noofmodules ;
but the result of the column is long , so i tried to used the course module, which is a column with numbers, and i tried modifying the query , but i had Error converting data type varchar to float.( i checked and saw that stuff is for concatinating
strings) is there a way around it.
i used
select FirstName,
LastName,
CourseModule,
CourseTitle,
Noofmodules,
count (Coursecompleted) as modulescompleted,
STUFF((SELECT ',' + CourseModule
FROM EStudentsprogress
WHERE FirstName = e.FirstName
AND LastName = e.LastName
AND Coursecompleted = '1'
AND CourseTitle = e.CourseTitle
FOR XML PATH('')),1,1,'') AS CoursesCompleted
from EStudentsprogress e
where Coursecompleted = '1'
Group by FirstName, LastName,CourseModule, CourseTitle, Noofmodules ; -
Hi experts,
I need to join two queries but not sure how:
select id from test_table1;
select *
from table(f_function(null
,null
,1 -- the id
,sysdate);One query has IDs needed to run the second query. Is there a way to join those two?
The result should be all columns from test_table1 + all columns from f_function.
Best regards,
Igor
Edited by: Igor S. on Mar 8, 2013 5:18 AMHi,
Igor S. wrote:
select *
from test_table1,
table(
f_function(
null,
null,
id,
sysdate
select *
from test_table1,
table(
f_function(
null,
null,
id,
sysdate
) xyz
where test_table1.id = xyz.id
/So these two queries are the same?Try it and see.
You'll find that the 2nd one produces an error. But if you change it to
select *
from test_table1,
table(
f_function(
null,
null,
id,
sysdate
) -- No alias here
) xyz -- Alias here, instead
where test_table1.id = xyz.id
/Then, assuming f_function produces a column called id, it will work.
Whether it produces the same results or not depends on what the function returns, and whether either id is NULL.
If the id column that the function returns is the same as the id value that you pass to it, and is never NULL, then the 2 queries will produce the same results.
Either way, each row of test_table1 will be joined to each row that the function produces with the argument(s) from that row. The column names produced by the function and the values in those columns are determined by the function; they do not need to have anything in common with any table. In practice, a function like f_function will usually not return an column that is always identical to any of its inputs, since that value is already available from the input. -
How can I join to queries?
Hello all,
I would like to ask if there is a way to join two queries into another new one.
I've been trying including two structures from two queries (always from the same infoprovider) but the result is not what I'm looking for.
Has anyone any idea of how to do this?
Thank you very much,
Carlos HinestrosaHi Michael,
Thanks for your answer. I know there are lots of funtionalities in excel but I need the result of the query as simple as it is to work with it in SEM Balanced Scorecard / Management Cockpit because SEM does not support many BEX funtionalities.
I make some calculations in one query and others in the other query, and I need the result of thoese calculations to make further calculations in a new query.
I was trying to create a query but the dialog box only allows to create a query from a cube.
Thanks,
Carlos Hinestrosa -
Hello All
Can someone tell me how to join two queries without using a multicube
or infoprovider option.
First Query has x, y, z fields
Second Query has x,a,b fields
now can i join the first and second query by the common field x.
Without changing the infoprovider or multicube design,i want to join
the query at the query level.
I want to join the two queries using common field x and then replace
the second query a, b fields with y, z fields data of query1.
RegardsHi Santosh.
If you want to use the workbook solution you should create one sheet for each query.
Lets assume that you have X in column A in both sheets and Y and Z in column B and C in sheet1. For the columns D and E on sheet1 you should insert some VB-code that gets the data from sheet2 where X equals X from sheet1. Here is an example:
=IF(ISNA(VLOOKUP(A1;sheet2!$A$1:$B$200;2;FALSE));"";(VLOOKUP(A1;sheet2!$A$1:$B$200;2;FALSE)))
You will have to use this code for all the rows along the column. I.e. if A has an output of 200 rows, you will need the code from line 1->200. You will also have to consider that the results may be more in the future.
Hope it helps.
BR
Stefan -
Outer join: difference between two queries
Below two queries that should give the same results in my opinion. I want all the records from u_protocol and only the value of pval.u_protocol_variable_value if present.
Why does the outer join in query2 doesn't work like in query1?
Query1:
select p.u_protocol_id, i.u_protocol_variable_value
from lims_sys.u_protocol p,
select pval.u_protocol_id, pval.u_protocol_variable_value
from lims_sys.u_protocol_variable pvar, lims_sys.u_protocol_value_user pval
where pvar.u_protocol_variable_id = pval.u_protocol_variable_id
and pvar.name = 'VALUE_Protocol_Group'
) i
where p.u_protocol_id = i.u_protocol_id (+)
Query2:
select prt.u_protocol_id, pval.u_protocol_variable_value
from lims_sys.u_protocol prt, lims_sys.u_protocol_variable pvar, lims_sys.u_protocol_value_user pval
where pvar.u_protocol_variable_id = pval.u_protocol_variable_id
and prt.u_protocol_id = pval.u_protocol_id (+)
and pvar.name = 'VALUE_Protocol_Group'In the first query restriction pvar.name = 'VALUE_Protocol_Group' is limited to your inline view. So when you do a outer join with the u_protocol table you will get the number of records which are there in the u_protocol table.
But when you gave the restriction pvar.name = 'VALUE_Protocol_Group' outside the inline view, the restriction was based on the resultset as a whole. So you will get only those records which have pvar.name = 'VALUE_Protocol_Group' condition satisfied.
Hope the following illustration helps:
SQL> CREATE TABLE TEST_TAB
2 AS
3 SELECT level col_1, chr(65+level-1) col_2 FROM Dual
4 CONNECT BY LEVEL <= 10
5 /
Table created.
SQL> SELECT * FROM TEST_TAB
2 /
COL_1 COL_
1 A
2 B
3 C
4 D
5 E
6 F
7 G
8 H
9 I
10 J
10 rows selected.
SQL> CREATE TABLE TEST_TAB_B
2 AS
3 SELECT level col_3, chr(65+level-1) col_4 FROM Dual
4 WHERE Level NOT IN (2,3,4)
5 CONNECT BY LEVEL <= 10
6 /
Table created.
SQL> SELECT * FROM TEST_TAB_B
2 /
COL_3 COL_
1 A
5 E
6 F
7 G
8 H
9 I
10 J
7 rows selected.
SQL> SELECT a1.col_1, a1.col_2, a2.col_3, a2.col_4 FROM TEST_TAB a1,
2 TEST_TAB_B a2
3 where a1.col_1 = a2.col_3(+)
4 order by a1.col_1
5 /
COL_1 COL_ COL_3 COL_
1 A 1 A
2 B
3 C
4 D
5 E 5 E
6 F 6 F
7 G 7 G
8 H 8 H
9 I 9 I
10 J 10 J
10 rows selected.Notice the output without any extra conditions: You will get all the values from TEST_TAB and matching records from TEST_TAB_B. Non-matching records are outputed as NULL.
Following Query is resemblence to your first query
SQL> SELECT a1.col_1, a1.col_2, a2.col_3, a2.col_4 FROM TEST_TAB a1,
2 (SELECT * FROM TEST_TAB_B where col_4='A') a2
3 where a1.col_1 = a2.col_3(+)
4 order by a1.col_1
5 /
COL_1 COL_ COL_3 COL_
1 A 1 A
2 B
3 C
4 D
5 E
6 F
7 G
8 H
9 I
10 J
10 rows selected.Here TEST_TAB_B Table is restricted with a condition which will restrict the inline view to have only one record. So when you outer join the inline view you will get output as shown above.
The following query resembles to your second query.
SQL> SELECT a1.col_1, a1.col_2, a2.col_3, a2.col_4 FROM TEST_TAB a1,
2 TEST_TAB_B a2
3 where a1.col_1 = a2.col_3(+)
4 and a2.col_4 = 'A'
5 order by a1.col_1
6 /
COL_1 COL_ COL_3 COL_
1 A 1 A
1 row selected.
SQL> DROP TABLE TEST_TAB_B
2 /
Table dropped.
SQL> DROP TABLE TEST_TAB
2 /
Table dropped.
SQL> To understand this lets break up the resultset.
Resultset brought by join condition would be something like :
COL_1 COL_ COL_3 COL_
1 A 1 A
2 B
3 C
4 D
5 E 5 E
6 F 6 F
7 G 7 G
8 H 8 H
9 I 9 I
10 J 10 JAgreed?
Now when you add the extra condition a2.col_4 = 'A' thecondition will act upon the above resultset there by restricting the records to:
COL_1 COL_ COL_3 COL_
1 A 1 AHope this helps.
Regards,
Jo -
I have two queries. The First one is long but very simple. All it has is long list of columns.
First one:
SELECT CL.CLIENT_ID, CA.CASE_ID, to_char(SYSDATE,'RRMMDD'),
CL.MASTER_CLIENT_ID,
CL.NAME1,
nvl(CL.NAME2,' '),
nvl(CL.ADDRESS1,' '),
nvl(CL.ADDRESS2,' '),
CL.POCO_POSTAL_CODE, nvl(CL.CITY,' '),
nvl(CL.AUTO_SURV_CODE,0),
CA.Col_Collector_Id
FROM CLIENT CL, CASES CA
WHERE CA.CL_CLIENT_ID = CL.CLIENT_ID AND
CA.CASE_ID = :current_case_id
Second one:
SQL SELECT nvl(REF_NO,' '), nvl(to_char(DATE_OF_JUDGEMENT,'rrmmdd'),' ')
FROM CASE_INSTANCES
WHERE IN_INSTANCE_TYPE IN (1,8) AND
CA_CASE_ID = :current_case_id AND
PETITION_DATE = (SELECT max(PETITION_DATE)
FROM CASE_INSTANCES
WHERE IN_INSTANCE_TYPE IN (1,8)
AND CA_CASE_ID = :current_case_id
AND ROWNUM < 2 );
The :current_case_id is passed as a parameter. The table in the second query i.e. CASE_INSTANCES is related to the first query with the CA_CASE_ID. Not all records fetched by the first query have a record in the second query. So you might have to use a LEFT JOIN or something. trying to join these two has worn me out. Please help me out someone.Hi,
Simplified Solution:
SELECT * FROM (
SELECT CL.CLIENT_ID, CA.CASE_ID, TO_CHAR(SYSDATE,'RRMMDD'),
CL.MASTER_CLIENT_ID,
CL.NAME1,
NVL(CL.NAME2,' '),
NVL(CL.ADDRESS1,' '),
NVL(CL.ADDRESS2,' '),
CL.POCO_POSTAL_CODE, NVL(CL.CITY,' '),
NVL(CL.AUTO_SURV_CODE,0),
CA.Col_Collector_Id
FROM CLIENT CL, CASES CA
WHERE CA.CL_CLIENT_ID = CL.CLIENT_ID ) a,
(SELECT NVL(REF_NO,' '), NVL(TO_CHAR(DATE_OF_JUDGEMENT,'rrmmdd'),' '),CA_CASE_ID
FROM CASE_INSTANCES
WHERE IN_INSTANCE_TYPE IN (1,8) AND
CA_CASE_ID = :current_case_id AND
PETITION_DATE = (SELECT MAX(PETITION_DATE)
FROM CASE_INSTANCES
WHERE IN_INSTANCE_TYPE IN (1,8)
AND CA_CASE_ID = :current_case_id
AND ROWNUM < 2
) b
WHERE a.case_id = b.ca_case_id(+)
Regards
K.Rajkumar -
Joining these these two queries (one regular and one grouped)
Hello
I have these two queries I would like to join, however the later is a grouped query how can I join it with the first query?
Has to be joined on EventId. The second query is a total table scan.
SELECT AH.EventID,
AH.TechnicalAddress, AH.AlarmAlias, AH.AlarmPath as [OrgAlarmPath], AH.AlarmCounter as AlarmCount, AH.EventDateTime as EventTime,
AH.[Priority], AH.AlarmMessage, AH.EventText, AH.CallListName, AH.AlarmReadDate as EndTime,
AH.alh_EventEndedUserRemark as [EndRemark] --, SUM(seconds) here, and AlarmSessions here
FROM AlarmHistory AH
WHERE (AH.HeartbeatAlarm = 0 OR AH.HeartbeatAlarm IS NULL) AND
((AH.CallListID IS NOT NULL) OR (AH.alh_IsForStatistics = 1)) AND
(NOT (AH.alh_t_EventSubCode is NULL or AH.AlarmReadByUserID is NULL))
ORDER BY AH.EventID DESC
SELECT ia.eventID, SUM(DATEDIFF(SECOND,ia.eventTime,r.eventTime)) AS seconds, COUNT(*) as AlarmSessions
FROM alarmHistoryLog ia
INNER JOIN alarmHistoryLog r
ON ia.EventId = r.EventId
AND r.EventTypeId = 2
AND r.EventSeq = (SELECT MIN(eventSeq) FROM alarmHistoryLog WHERE eventSeq > ia.EventSeq AND EventTypeId = 2)
WHERE ia.EventTypeId = 0
group by ia.EventId
order by EventId desc
HenryTry the below:
;with ctefirst as
SELECT AH.EventID,
AH.TechnicalAddress, AH.AlarmAlias, AH.AlarmPath as [OrgAlarmPath], AH.AlarmCounter as AlarmCount, AH.EventDateTime as EventTime,
AH.[Priority], AH.AlarmMessage, AH.EventText, AH.CallListName, AH.AlarmReadDate as EndTime,
AH.alh_EventEndedUserRemark as [EndRemark] --, SUM(seconds) here, and AlarmSessions here
FROM AlarmHistory AH
WHERE (AH.HeartbeatAlarm = 0 OR AH.HeartbeatAlarm IS NULL) AND
((AH.CallListID IS NOT NULL) OR (AH.alh_IsForStatistics = 1)) AND
(NOT (AH.alh_t_EventSubCode is NULL or AH.AlarmReadByUserID is NULL))
), ctesecond as
SELECT ia.eventID, SUM(DATEDIFF(SECOND,ia.eventTime,r.eventTime)) AS seconds, COUNT(*) as AlarmSessions
FROM alarmHistoryLog ia
INNER JOIN alarmHistoryLog r
ON ia.EventId = r.EventId
AND r.EventTypeId = 2
AND r.EventSeq = (SELECT MIN(eventSeq)
FROM alarmHistoryLog WHERE eventSeq > ia.EventSeq AND EventTypeId = 2)
WHERE ia.EventTypeId = 0
group by ia.EventId
Select A.*,B.seconds,B.AlarmSessions From ctefirst A
Inner join ctesecond B On A.EventId = B.EventID
Please mark this reply as answer if it solved your issue or vote as helpful if it helped.
[Blog] -
Whats the difference between these two queries ? - for tuning purpose
Whats the difference between these two queries ?
I have huge amount of data for each table. its takeing such a long time (>5-6hrs).
here whice one is fast / do we have any other option there apart from listed here....
QUERY 1:
SELECT --<< USING INDEX >>
field1, field2, field3, sum( case when field4 in (1,2) then 1 when field4 in (3,4) then -1 else 0 end)
FROM
tab1 inner join tab2 on condition1 inner join tab3 on condition2 inner join tab4 on conditon3
WHERE
condition4..10 and
GROUP BY
field1, field2,field3
HAVING
sum( case when field4 in (1,2) then 1 when field4 in (3,4) then -1 else 0 end) <> 0;
QUERY 2:
SELECT --<< USING INDEX >>
field1, field2, field3, sum( decode(field4, 1, 1, 2, 1, 3, -1, 4, -1 ,0))
FROM
tab1, tab2, tab3, tab4
WHERE
condition1 and
condition2 and
condition3 and
condition4..10
GROUP BY
field1, field2,field3
HAVING
sum( decode(field4, 1, 1, 2, 1, 3, -1, 4, -1 ,0)) <> 0;
[pre]My feeling here is that simply changing join syntax and case vs decode issues is not going to give any significant improvement in performance, and as Tubby points out, there is not a lot to go on. I think you are going to have to investigate things along the line of parallel query and index vs full table scans as well any number of performance tuning methods before you will see any significant gains. I would start with the Performance Manual as a start and then follow that up with the hard yards of query plans and stats.
Alternatively, you could just set the gofast parameter to TRUE and everything will be all right.
Andre
Maybe you are looking for
-
FTP Firefox vs IE over BorderManager
BorderManager 3.9 SP1 proxy appears to have a problem with Firefox and FTP sites. On Firefox 3.0.5 through BMgr, this URL format: ftp://ftp.domainname.com/ returns: HTTP Error Status : 401.1 Invalid Logon Credentials Description : FTP: Login to serve
-
We have implemented a servlet that uses a RequestDispatcher to forward to a JSP. The JSP instantiates an object (passing the Request and Response as parameters) that uses the RequestDispatcher once again to include another JSP. This has been working
-
Cannot connect to MS SQL 2008 R2 locally or remotely.
When I try to connect to my SQL 2008 I get the following error: TITLE: Connect to Server Cannot connect to <machinename>. ADDITIONAL INFORMATION: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The
-
URGENT: Error with query on MultiProvider
Hi, I am designing a query on a MultiProvider that combines a cube and ODS. the common characterisitics in the cube & ODS are Material, fiscal period, country. I have 2 rows & 1 column and several free characterisitics (mostly navigational attributes
-
Differences between clint level vs company code level
Dear Gurus, Can you please explain the difference between- client level vs company code level Vs coustmer master data level Thanks in Advance Bhaskar Please search the forums before posting queries. Respect forum rules. Edited by: Zubin Shah on May 1