Query Help with Parent, Child, Child's Child
Hi all,
Need some help with a query. I'm trying to create a stored procedure that is sort of like a Customer, Order, Order, Details. In my situation the tables are different but nevertheless, I want to grab all the fields from the Parent, Child,
and Childs' Child, where the Parent.ParentID = @Parameter. I tried this:
CREATE PROCEDURE [dbo].[spGetCompleteProjectXML]
@ProjectID int = 0
AS
SELECT *,
(SELECT *,
(SELECT *
FROM PageControls
WHERE (PageControls.ProjectPageID = ProjectPages.ProjectPageID))
FROM ProjectPages
WHERE (ProjectPages.ProjectID = @ProjectID))
FROM Projects
WHERE (ProjectID = @ProjectID)
FOR XML AUTO, ELEMENTS
RETURN 0
I think I'm close, but it was my best effort. Could someone help?
thanks in advance
Hi TPolo,
Regarding your description, are you looking for a sample like below?
CREATE TABLE customer(customerID INT, name VARCHAR(99))
INSERT INTO customer VALUES(1,'Eric')
INSERT INTO customer VALUES(2,'Nelson')
CREATE TABLE orders(orderID INT,customerID INT)
INSERT INTO orders VALUES(1,1);
INSERT INTO orders VALUES(2,1)
INSERT INTO orders VALUES(3,2)
INSERT INTO orders VALUES(4,2)
CREATE TABLE orderDetails(orderID INT,item VARCHAR(99))
INSERT INTO orderDetails VALUES(1,'APPLE1')
INSERT INTO orderDetails VALUES(1,'BANANA1')
INSERT INTO orderDetails VALUES(2,'APPLE2')
INSERT INTO orderDetails VALUES(2,'BANANA2')
INSERT INTO orderDetails VALUES(3,'APPLE3')
INSERT INTO orderDetails VALUES(3,'BANANA3')
INSERT INTO orderDetails VALUES(4,'APPLE4')
INSERT INTO orderDetails VALUES(4,'BANANA5')
SELECT customer.customerID,customer.name,
(SELECT orderId,
SELECT item FROM orderDetails WHERE orderID=orders.orderID FOR XML AUTO,TYPE,ELEMENTS
FROM orders Where customerID=customer.customerID FOR XML AUTO,TYPE,ELEMENTS)
FROM customer WHERE customerID=1
FOR XML AUTO,ELEMENTS
DROP TABLE customer,orderDetails,orders
If you have any feedback on our support, please click
here.
Eric Zhang
TechNet Community Support
Similar Messages
-
Hi Gurus,
Can someone please help me out.
I've a below tables.
1) tbl_link --> this table contains information at profile level
2) tbl_summary --> this table contains summary at parent profile level derived from tbl_link table
One parent profile contains multiple child profiles and each child profile links to a code (which is B, W, G or P) and the code is linked to a category (i.e. ONL and OFL). In this case code B is linked to category 'ONL' and codes W,G,P linked to OFL category.
ONL category needs 100 points. If it don't have enough points then i need to borrow from OFL category which i'm doing and populating into tbl_summary table at parent profile level.
Now i need to insert data into tbl_link table at profile level with howmany points used, expired based on tbl_summary table. Rule is at the end of month if we add points for each profile in tbl_link table it should come as 0.
with
tbl_SUMMARY as
select 1 as ppid,'ONL' as catgcode, 53 as earned_points,47 BORROWED_POINTS,100 CERT_POINTS,0 DISCARD_POINTS,100 used from dual
union
select 1 as ppid,'OFL' as catgcode, 223 as earned_points,0 BORROWED_POINTS,176 CERT_POINTS,76 DISCARD_POINTS,100 used from dual
union
select 2 as ppid,'ONL' as catgcode, 39 as earned_points,61 BORROWED_POINTS,100 CERT_POINTS,0 DISCARD_POINTS,100 used from dual
union
select 2 as ppid,'OFL' as catgcode, 90 as earned_points,0 BORROWED_POINTS,29 CERT_POINTS,29 DISCARD_POINTS,100 used from dual
union
select 3 as ppid,'ONL' as catgcode, 109 as earned_points,0 BORROWED_POINTS,109 CERT_POINTS,9 DISCARD_POINTS,100 used from dual
union
select 3 as ppid,'OFL' as catgcode, 223 as earned_points,0 BORROWED_POINTS,223 CERT_POINTS,23 DISCARD_POINTS,200 used from dual
union
select 4 as ppid,'ONL' as catgcode, 109 as earned_points,0 BORROWED_POINTS,109 CERT_POINTS,9 DISCARD_POINTS,100 used from dual
union
select 4 as ppid,'OFL' as catgcode, 169 as earned_points,0 BORROWED_POINTS,169 CERT_POINTS,69 DISCARD_POINTS,100 used from dual
tbl_link as
select 1 as ppid,1 as pid, 'B' as code,'ONL' as catgcode, 53 as earned_points from dual
union
select 1 as ppid,12 as pid, 'W' as code,'OFL' as catgcode, 26 as earned_points from dual
union
select 1 as ppid,13 as pid, 'G' as code,'OFL' as catgcode, 87 as earned_points from dual
union
select 1 as ppid,14 as pid, 'P' as code,'OFL' as catgcode, 110 as earned_points from dual
union
select 2 as ppid,2 as pid, 'B' as code,'ONL' as catgcode, 39 as earned_points from dual
union
select 2 as ppid,22 as pid, 'W' ,'OFL' as catgcode, 30 as earned_points from dual
union
select 2 as ppid,23 as pid, 'G' ,'OFL' as catgcode, 29 as earned_points from dual
union
select 2 as ppid,24 as pid, 'P' ,'OFL' as catgcode, 31 as earned_points from dual
union
select 3 as ppid,3 as pid, 'B' as code,'ONL' as tier_catgcode, 109 as earned_points from dual
union
select 3 as ppid,32 as pid, 'W' ,'OFL' , 26 as earned_points from dual
union
select 3 as ppid,33 as pid, 'G' ,'OFL', 87 as earned_points from dual
union
select 3 as ppid,34 as pid, 'P' ,'OFL' , 110 as earned_points from dual
union
select 4 as ppid,4 as pid, 'B' as code,'ONL' as catgcode, 109 as earned_points from dual
union
select 4 as ppid,42 as pid, 'W' as code,'OFL' , 26 as earned_points from dual
union
select 4 as ppid,43 as pid, 'G' as code,'OFL' , 87 as earned_points from dual
union
select 4 as ppid,44 as pid, 'P' as code,'OFL' , 56 as earned_points from dual
final (PARENT_PROFILE_ID,PROFILE_ID,catgcode,EARNED_POINTS,BORROWED_POINTS,CERT_POINTS,DISCARD_POINTS,USED)
as (
select A.PPID PARENT_PROFILE_ID,B.PID PROFILE_ID,A.catgcode,B.EARNED_POINTS,BORROWED_POINTS,CERT_POINTS,DISCARD_POINTS,USED
from tbl_SUMMARY a,tbl_link b where a.ppid=b.ppid AND A.catgcode=B.catgcode
ORDER BY PROFILE_ID
select * from final order by 1;
PARENT_PROFILE_ID PROFILE_ID CATGCODE EARNED_POINTS BORROWED_POINTS CERT_POINTS DISCARD_POINTS USED
1 1 ONL 53 47 100 0 100
1 14 OFL 110 0 176 76 100
1 13 OFL 87 0 176 76 100
1 12 OFL 26 0 176 76 100
2 2 ONL 39 61 100 0 100
2 24 OFL 31 0 29 29 100
2 23 OFL 29 0 29 29 100
2 22 OFL 30 0 29 29 100
3 32 OFL 26 0 223 23 200
3 33 OFL 87 0 223 23 200
3 34 OFL 110 0 223 23 200
3 3 ONL 109 0 109 9 100
4 42 OFL 26 0 169 69 100
4 43 OFL 87 0 169 69 100
4 44 OFL 56 0 169 69 100
4 4 ONL 109 0 109 9 100
Need Output as below :
For parent profile 1, whatever i mentioned above is not correct. Borrowed 47 points from OFL to ONL to make ONL and also from OFL category has 176 points remaining after lending to ONL and using only 100 points, remaining 76 points are discarded. Need to deduct these 76 points also from child profiles. Output will be as below.
PARENT_PROFILE_ID PROFILE_ID CATGCODE EARNED_POINTS BORROWED_POINTS CERT_POINTS DISCARD_POINTS USED BURN_PTS EXPIRE_PTS
1 1 ONL 53 47 100 0 100 -53 0
1 12 OFL 26 0 176 76 100 -26 0
1 13 OFL 87 0 176 76 100 -74 -13
1 14 OFL 110 0 176 76 100 -47 -63
For parent profile id 2 --> ONL category has 39 points, so borrowed 61 points from OFL category to make ONL points 100.
Now need to populate tbl_link table at child profile level (i.e. child profiles 22,23,24).
Borrowed 61 points from OFL and need to deduct this points from the profile which has highest earned points, in this case deduct from profile 24 which has 31 points, from profile 22 which has 30 points. Need output like below
PARENT_PROFILE_ID PROFILE_ID CATGCODE EARNED_POINTS BORROWED_POINTS CERT_POINTS DISCARD_POINTS USED BURN_PTS EXPIRE_PTS
2 2 ONL 39 61 100 0 100 -39 0
2 22 OFL 30 0 29 29 100 -30 0
2 23 OFL 29 0 29 29 100 0 -29
2 24 OFL 31 0 29 29 100 -31 0
For parent profile id 3 --> ONL category has 109 points, so no need to borrow points from OFL category
Now need to populate tbl_link table at child profile level (i.e. child profiles 32,33,34).
in this case ONL has 100 points, so move the remaining 9 points will be expired. OFL category has 223 points total. need only 200 points (i.e. mutiple of 100) for our process, 23 points will be expired and has to deduct from the profile which has highest earned points, in this case from profile 34. Output :
PARENT_PROFILE_ID PROFILE_ID CATGCODE EARNED_POINTS BORROWED_POINTS CERT_POINTS DISCARD_POINTS USED BURN_PTS EXPIRE_PTS
3 3 ONL 109 0 109 9 100 -100 -9
3 32 OFL 26 0 223 23 200 -26 0
3 33 OFL 87 0 223 23 200 -87 0
3 34 OFL 110 0 223 23 200 -87 -23
For parent profile id 4 --> ONL category has 109 points, so no need to borrow points from OFL category
Now need to populate tbl_link table at child profile level (i.e. child profiles 42,43,44).
in this case ONL has 100 points, so move the remaining 9 points will be expired. OFL category has 169 points total. need only 100 points (i.e. mutiple of 100) for our process, 69 points will be expired and has to deduct from the profile which has highest earned points, in this case from profile 43. Output :
PARENT_PROFILE_ID PROFILE_ID CATGCODE EARNED_POINTS BORROWED_POINTS CERT_POINTS DISCARD_POINTS USED BURN_PTS EXPIRE_PTS
4 4 ONL 109 0 109 9 100 100 9
4 42 OFL 26 0 169 69 100 -26 0
4 43 OFL 87 0 169 69 100 -18 -69
4 44 OFL 56 0 169 69 100 -56 0
Can someone help with the query. I googled about looping in sql and came to know that Oracle has a feature MODEL to loop in SQL, but i don't have idea on using MODEL clause.
Appreciate your help!
Thanks
SriHi Gurus,
Can someone please help me out.
I've a below tables.
1) tbl_link --> this table contains information at profile level
2) tbl_summary --> this table contains summary at parent profile level derived from tbl_link table
One parent profile contains multiple child profiles and each child profile links to a code (which is B, W, G or P) and the code is linked to a category (i.e. ONL and OFL). In this case code B is linked to category 'ONL' and codes W,G,P linked to OFL category.
ONL category needs 100 points. If it don't have enough points then i need to borrow from OFL category which i'm doing and populating into tbl_summary table at parent profile level.
Now i need to insert data into tbl_link table at profile level with howmany points used, expired based on tbl_summary table. Rule is at the end of month if we add points for each profile in tbl_link table it should come as 0.
with
tbl_SUMMARY as
select 1 as ppid,'ONL' as catgcode, 53 as earned_points,47 BORROWED_POINTS,100 CERT_POINTS,0 DISCARD_POINTS,100 used from dual
union
select 1 as ppid,'OFL' as catgcode, 223 as earned_points,0 BORROWED_POINTS,176 CERT_POINTS,76 DISCARD_POINTS,100 used from dual
union
select 2 as ppid,'ONL' as catgcode, 39 as earned_points,61 BORROWED_POINTS,100 CERT_POINTS,0 DISCARD_POINTS,100 used from dual
union
select 2 as ppid,'OFL' as catgcode, 90 as earned_points,0 BORROWED_POINTS,29 CERT_POINTS,29 DISCARD_POINTS,100 used from dual
union
select 3 as ppid,'ONL' as catgcode, 109 as earned_points,0 BORROWED_POINTS,109 CERT_POINTS,9 DISCARD_POINTS,100 used from dual
union
select 3 as ppid,'OFL' as catgcode, 223 as earned_points,0 BORROWED_POINTS,223 CERT_POINTS,23 DISCARD_POINTS,200 used from dual
union
select 4 as ppid,'ONL' as catgcode, 109 as earned_points,0 BORROWED_POINTS,109 CERT_POINTS,9 DISCARD_POINTS,100 used from dual
union
select 4 as ppid,'OFL' as catgcode, 169 as earned_points,0 BORROWED_POINTS,169 CERT_POINTS,69 DISCARD_POINTS,100 used from dual
tbl_link as
select 1 as ppid,1 as pid, 'B' as code,'ONL' as catgcode, 53 as earned_points from dual
union
select 1 as ppid,12 as pid, 'W' as code,'OFL' as catgcode, 26 as earned_points from dual
union
select 1 as ppid,13 as pid, 'G' as code,'OFL' as catgcode, 87 as earned_points from dual
union
select 1 as ppid,14 as pid, 'P' as code,'OFL' as catgcode, 110 as earned_points from dual
union
select 2 as ppid,2 as pid, 'B' as code,'ONL' as catgcode, 39 as earned_points from dual
union
select 2 as ppid,22 as pid, 'W' ,'OFL' as catgcode, 30 as earned_points from dual
union
select 2 as ppid,23 as pid, 'G' ,'OFL' as catgcode, 29 as earned_points from dual
union
select 2 as ppid,24 as pid, 'P' ,'OFL' as catgcode, 31 as earned_points from dual
union
select 3 as ppid,3 as pid, 'B' as code,'ONL' as tier_catgcode, 109 as earned_points from dual
union
select 3 as ppid,32 as pid, 'W' ,'OFL' , 26 as earned_points from dual
union
select 3 as ppid,33 as pid, 'G' ,'OFL', 87 as earned_points from dual
union
select 3 as ppid,34 as pid, 'P' ,'OFL' , 110 as earned_points from dual
union
select 4 as ppid,4 as pid, 'B' as code,'ONL' as catgcode, 109 as earned_points from dual
union
select 4 as ppid,42 as pid, 'W' as code,'OFL' , 26 as earned_points from dual
union
select 4 as ppid,43 as pid, 'G' as code,'OFL' , 87 as earned_points from dual
union
select 4 as ppid,44 as pid, 'P' as code,'OFL' , 56 as earned_points from dual
final (PARENT_PROFILE_ID,PROFILE_ID,catgcode,EARNED_POINTS,BORROWED_POINTS,CERT_POINTS,DISCARD_POINTS,USED)
as (
select A.PPID PARENT_PROFILE_ID,B.PID PROFILE_ID,A.catgcode,B.EARNED_POINTS,BORROWED_POINTS,CERT_POINTS,DISCARD_POINTS,USED
from tbl_SUMMARY a,tbl_link b where a.ppid=b.ppid AND A.catgcode=B.catgcode
ORDER BY PROFILE_ID
select * from final order by 1;
PARENT_PROFILE_ID PROFILE_ID CATGCODE EARNED_POINTS BORROWED_POINTS CERT_POINTS DISCARD_POINTS USED
1 1 ONL 53 47 100 0 100
1 14 OFL 110 0 176 76 100
1 13 OFL 87 0 176 76 100
1 12 OFL 26 0 176 76 100
2 2 ONL 39 61 100 0 100
2 24 OFL 31 0 29 29 100
2 23 OFL 29 0 29 29 100
2 22 OFL 30 0 29 29 100
3 32 OFL 26 0 223 23 200
3 33 OFL 87 0 223 23 200
3 34 OFL 110 0 223 23 200
3 3 ONL 109 0 109 9 100
4 42 OFL 26 0 169 69 100
4 43 OFL 87 0 169 69 100
4 44 OFL 56 0 169 69 100
4 4 ONL 109 0 109 9 100
Need Output as below :
For parent profile 1, whatever i mentioned above is not correct. Borrowed 47 points from OFL to ONL to make ONL and also from OFL category has 176 points remaining after lending to ONL and using only 100 points, remaining 76 points are discarded. Need to deduct these 76 points also from child profiles. Output will be as below.
PARENT_PROFILE_ID PROFILE_ID CATGCODE EARNED_POINTS BORROWED_POINTS CERT_POINTS DISCARD_POINTS USED BURN_PTS EXPIRE_PTS
1 1 ONL 53 47 100 0 100 -53 0
1 12 OFL 26 0 176 76 100 -26 0
1 13 OFL 87 0 176 76 100 -74 -13
1 14 OFL 110 0 176 76 100 -47 -63
For parent profile id 2 --> ONL category has 39 points, so borrowed 61 points from OFL category to make ONL points 100.
Now need to populate tbl_link table at child profile level (i.e. child profiles 22,23,24).
Borrowed 61 points from OFL and need to deduct this points from the profile which has highest earned points, in this case deduct from profile 24 which has 31 points, from profile 22 which has 30 points. Need output like below
PARENT_PROFILE_ID PROFILE_ID CATGCODE EARNED_POINTS BORROWED_POINTS CERT_POINTS DISCARD_POINTS USED BURN_PTS EXPIRE_PTS
2 2 ONL 39 61 100 0 100 -39 0
2 22 OFL 30 0 29 29 100 -30 0
2 23 OFL 29 0 29 29 100 0 -29
2 24 OFL 31 0 29 29 100 -31 0
For parent profile id 3 --> ONL category has 109 points, so no need to borrow points from OFL category
Now need to populate tbl_link table at child profile level (i.e. child profiles 32,33,34).
in this case ONL has 100 points, so move the remaining 9 points will be expired. OFL category has 223 points total. need only 200 points (i.e. mutiple of 100) for our process, 23 points will be expired and has to deduct from the profile which has highest earned points, in this case from profile 34. Output :
PARENT_PROFILE_ID PROFILE_ID CATGCODE EARNED_POINTS BORROWED_POINTS CERT_POINTS DISCARD_POINTS USED BURN_PTS EXPIRE_PTS
3 3 ONL 109 0 109 9 100 -100 -9
3 32 OFL 26 0 223 23 200 -26 0
3 33 OFL 87 0 223 23 200 -87 0
3 34 OFL 110 0 223 23 200 -87 -23
For parent profile id 4 --> ONL category has 109 points, so no need to borrow points from OFL category
Now need to populate tbl_link table at child profile level (i.e. child profiles 42,43,44).
in this case ONL has 100 points, so move the remaining 9 points will be expired. OFL category has 169 points total. need only 100 points (i.e. mutiple of 100) for our process, 69 points will be expired and has to deduct from the profile which has highest earned points, in this case from profile 43. Output :
PARENT_PROFILE_ID PROFILE_ID CATGCODE EARNED_POINTS BORROWED_POINTS CERT_POINTS DISCARD_POINTS USED BURN_PTS EXPIRE_PTS
4 4 ONL 109 0 109 9 100 100 9
4 42 OFL 26 0 169 69 100 -26 0
4 43 OFL 87 0 169 69 100 -18 -69
4 44 OFL 56 0 169 69 100 -56 0
Can someone help with the query. I googled about looping in sql and came to know that Oracle has a feature MODEL to loop in SQL, but i don't have idea on using MODEL clause.
Appreciate your help!
Thanks
Sri -
Query Help with Item Master & Warehouse Code
Forum,
I would like help with a query to identify any items within a database where a particular warehouse code does NOT exist against it. At present I have the following:
select T0.ItemCode, T1.WhsCode from OITM T0
INNER JOIN OITW T1 on T0.ItemCode = T1.ItemCode
where T0.ItemCode NOT IN ('WHS1')
This is returning all other instance and not just a list of item codes where 'WHS1' is missing from within the 'Stock Data' tab.
Thanks,
SarahHi Sarah...
Try This
SELECT T0.ItemCode, T0.ItemName, T1.WhsCode
FROM OITM T0 INNER JOIN OITW T1 ON T0.ItemCode = T1.ItemCode
WHERE T1.WhsCode not in ( 'WHS1')
Regards
Kennedy -
Query help with round up of dates
Hi,
I have a problem with my query.
I am calculating the difference between two dates and I want to get the hours.
The problem is that I want the following to happen:
if the difference is x such that x > 0 it should round it up to 1 (example: x = 0.1 it should be rounded up to 1)
if the difference is y such that y >1 it should be rounded to 1 (example y = 1.05 it should be rounded up to 2)
i have the following query at the momet:
select to_char(arrivaldate,'DD/MM/YYYY hh24:mi:ss') as "ARRIVAL", to_char(departuredate,'DD/MM/YYYY hh24:mi:ss') as "DEPARTURE", round(trunc(mod((departuredate-arrivaldate)*24,24),2)) as duration from stay_log;
thanks.Hi,
Format your code so that the physical appearance of the code gives some hint as to what you're doing, as I did below.
When posting code on this forum, always type {code} tags at the beginning and end of formatted sections.
Do you mean that, instead of 21, you want to use the flightid from the main query?
If so, that's called a "correllated query". The tables in the main query (and their columns) can be reference in the sub-query, like this:
select flightid as "FLIGHT",
select destairport_id
from journey,
flight
where flight.flightid = stay_log.flight_id -- correlated to main query
and flight.journeyid = journey.journeyid
) AS "AIRPORT",
to_char(arrivaldate,'DD/MM/YYYY hh24:mi:ss') as "ARRIVAL",
to_char(departuredate,'DD/MM/YYYY hh24:mi:ss') as "DEPARTURE",
ceil((departuredate-arrivaldate)*24) as "DURATION",
cost as "COST"
from stay_log;Often, people give a table alias to the main query table that is being referenced in the sub-query, as shown below.
That's only required when the table name appears in both FROM-clauses.
select flightid as "FLIGHT",
select destairport_id
from journey,
flight
where flight.flightid = s.flight_id -- table alias s used here
and flight.journeyid = journey.journeyid
) AS "AIRPORT",
to_char(arrivaldate,'DD/MM/YYYY hh24:mi:ss') as "ARRIVAL",
to_char(departuredate,'DD/MM/YYYY hh24:mi:ss') as "DEPARTURE",
ceil((departuredate-arrivaldate)*24) as "DURATION",
cost as "COST"
from stay_log s -- table alias s defined here
; -
I am trying to make a query that will list items and the property names that have been checked from the properties tab, but can't seem to find a way to link them correctly. Anyone ever done it successfully? If so please help on how I can do it.
ThanksHi DaytonHoneycutt,
Check this link.
Item master data - properties tab query
Thanks,
Srujal Patel -
Hi All,
I have 2 tables like below. now i have a requirement which i need to do it in CASE OR DECODE. because i have to implement this logic IN SELECT STATEMENT for a column.
In table T1 if Lang = 1 and State = 'P' then display Lang = 'english' in table T2(which is Lang=2 in table T1) Or else display null.
Can we do this logic using CASE or DECODE? pls help
T1
Lang State
1 P
2 N
T2
Lang Dscr
1 central
2 englishSomething like this ?
SQL> ed
Wrote file afiedt.buf
1 WITH t1 AS (SELECT 1 lang, 'P' STATE FROM DUAL UNION ALL
2 SELECT 2 lang, 'N' STATE FROM DUAL
3 )
4 , t2 AS (SELECT 1 lang, 'central' dscr FROM DUAL UNION ALL
5 SELECT 2 lang, 'english' dscr FROM DUAL
6 )
7 SELECT t1.lang,t1.state,CASE
8 WHEN t1.lang=1 AND t1.state='P'
9 THEN (SELECT t2.lang FROM t2 WHERE dscr='english')
10 ELSE NULL
11 END "lang"
12* FROM t1
SQL> /
LANG S lang
1 P 2
2 N -
HT201304 Help with parental controls
Hi there
My 7 year old daughter was playing a game called Bird Tale and midway through an advertisement appeared for Skyn condoms. It was totally inappropriate. Can anyone advise how to avoid advertisements appearing during kids games?
ThanksEnable restrictions in settings (please remember the passcode). It should limit add content to age appropriate material.
As a parent myself, I find it weird, annoying and sick that this kind of add appears on the device in a game suitable for kids. I encourage you to complain to the developer of the app. They should be more responsible for the advertisers on their games.
Go Mama Bear!!!! -
Union on tables with parent-child records and Sorting
Hi,
I have an application that has an existing query which returns org units (parent and child) from organization table with a sort on createddate + org_id combination
WITH Org_TREE AS (
SELECT *, null as 'IS_DELETED', convert (varchar(4000), convert(varchar(30),CREATED_DT,126) + Org_Id) theorderby
FROM Organization WHERE PARENT_Org_ID IS NULL and case_ID='43333'
UNION ALL
SELECT a1.*, null as 'IS_DELETED', convert (varchar(4000), a2.theorderby + convert(varchar(30),a1.CREATED_DT,126) + a1.Org_Id)
FROM Organization a1 INNER JOIN Org_TREE a2 ON a1.PARENT_Org_ID = a2.Org_Id and case_ID='43333'
SELECT * FROM Org_TREE order by theorderby
I have created a new log table for organization 'Organization_Log' with exact columns as Organization table with an additional 'IS_DELETED' bool column.
Questions:
I need to modiy the query,
1. To display the parent and child records both from the organization table and organization_log table.
2. the sort on the result should be based on 'Organization Name' column asc. First with parent org and the child org underneath it. For eg.
aaa
==>fff
==>ggg
bbb
==> aaa
==> hhh
Any help on how the query should be constructed?
Thanks
gkol@Visakh16,
I am getting...
All queries combined using a UNION, INTERSECT or EXCEPT operator must have an equal number of expressions in their target lists.
The problem is that you will have different number of columns in both log and Organization and Organizationlog tables. UNION/UNION ALL expect the same number of columns.
Try the below:
WITH Org_Log_TREE AS (
SELECT Organization_name,Org_id,Parent_Org_id, IS_DELETED, CAST(Organization_Name AS varchar(max)) AS theorderby,1 AS level
FROM Organization_Log WHERE PARENT_Org_ID IS NULL and case_ID='43333'
UNION ALL
SELECT a1.Organization_name,a1.Org_id,a1.Parent_Org_id, a1.IS_DELETED, CAST(a2.theorderby +'/' + CAST(a1.Organization_Name AS varchar(1000)) AS varchar(max)),a2.Level + 1
FROM Organization_Log a1 INNER JOIN Org_Log_TREE a2 ON a1.PARENT_Org_ID = a2.Org_Id and case_ID='43333'
) ,Org_TREE AS (
SELECT Organization_name,Org_id,Parent_Org_id, NULL AS IS_DELETED, CAST(Organization_Name AS varchar(max)) AS theorderby,1 AS level
FROM Organization WHERE PARENT_Org_ID IS NULL and case_ID='43333'
UNION ALL
SELECT a1.Organization_name,a1.Org_id,a1.Parent_Org_id,NULL AS IS_DELETED, CAST(a2.theorderby +'/' + CAST(a1.Organization_Name AS varchar(1000)) AS varchar(max)),a2.Level + 1
FROM Organization a1 INNER JOIN Org_TREE a2 ON a1.PARENT_Org_ID = a2.Org_Id and case_ID='43333'
SELECT * FROM Org_Log_TREE
UNION ALL
SELECT * FROM Org_TREE
ORDER BY LEFT(theorderby,CHARINDEX('/',theorderby + '/')-1),Level -
SQL to align child values with parent columns
Hi everyone,
I'm stumped on how to develop an sql query that generates a report where my child values are aligned with my parent columns based on matching years. Here's the example...
create table test_yr (yr_id number primary key, yr_nb number);
insert into test_yr values (1,2013);
insert into test_yr values (2,2014);
insert into test_yr values (3,2015);
create table test_parent (parent_id number primary key, parent_yr_id_begin number, parent_title varchar2(100));
alter table test_parent add foreign key (parent_yr_id_begin) refererences test_yr (yr_id);
insert into test_parent values (1,1,'This rec starts in 2013');
create table test_child (
child_id number primary key,
child_parent_id number,
child_yr_id_begin number,
child_title varchar2(100),
child_yr1_val number, child_yr2_val number, child_yr3_val number, child_yr4_val number, child_yr5_val number
alter table test_child add foreign key (child_parent_id) references test_parent (parent_id);
alter table test_child add foreign key (child_yr_id_begin) references test_yr (yr_id);
insert into test_child values (1,1,3,'This rec starts in 2015',10,20,30,40,50);
insert into test_child values (2,1,3,'This rec starts in 2015',15,25,35,45,55);
The child can start at a different begin year than the parent, making yr1 for parent and yr1 for child different. So for this example, year 1 of the child (2015) = year 3 of the parent.
I can get the values strictly from the child using the following...
select child_yr1_val, child_yr2_val, child_yr3_val, child_yr4_val, child_yr5_val
from test_child
where child_parent_id = 1;
However, I need the report to be based off of the first five years of the parent. And since the child doesn't start until 2015, the first two years should be zero. Here is the expected result:
yr1 | yr2 | yr3 | yr4 | yr5
0 | 0 | 10 | 20 | 30
0 | 0 | 15 | 25 | 35
I'm trying to do this using plain sql without having to use pl/sql to derive a dynamic query. But I need to somehow match up and align the years... such as in this example, yr1(child) = yr3(parent), yr2(child) = yr4(parent), etc. I'm hoping some joins and case statements will do the trick, but I don't quite know how to utilize them to get the result I need. Can anyone help? Oracle 11gR2.
Thanks,
MarkHi,
oramark14 wrote:
Hi Frank,
Thanks so much for your reply! I'll try to digest this solution and implement it this weekend. I also agree completely about the table design... I know apps shouldn't drive database design, ...
If you get indigestion, feel free to ask questions here.
The SELECT ... PIVOT feature isn't very convenient for dealing with missing values. You may prefer to change the main query like this:
WITH unpivoted_child AS
SELECT child_id, child_parent_id
, child_yr_id_begin + child_yr - 1 AS column_num
, val
FROM test_child
UNPIVOT ( val
FOR child_yr IN ( child_yr1_val AS 1
, child_yr2_val AS 2
, child_yr3_val AS 3
, child_yr4_val AS 4
, child_yr5_val AS 5
WHERE child_parent_id = 1
SELECT NVL (SUM (CASE WHEN column_num = 1 THEN val END), 0) AS yr1
, NVL (SUM (CASE WHEN column_num = 2 THEN val END), 0) AS yr2
, NVL (SUM (CASE WHEN column_num = 3 THEN val END), 0) AS yr3
, NVL (SUM (CASE WHEN column_num = 4 THEN val END), 0) AS yr4
, NVL (SUM (CASE WHEN column_num = 5 THEN val END), 0) AS yr5
FROM unpivoted_child
GROUP BY child_parent_id
, child_id
ORDER BY child_parent_id
, child_id
Also, if you want to see how SELECT ... PIVOT works, this can help show you.
Having 5 separate columns for input can be very conveninet, just like having 5 separate columns for display. But, as you said, that shouldn't drive the table design. If necessary, you could create a view that has 5 separate val columns, like your current table, and write an INSTEAD OF trigger for user input. -
Incorrect query forms in parent child relation
Hello
I am using jdev of RUP3 (Release 4) which is 11.1.1.6.2 and have following scenario
I have a ParentVO (ProfileVO) and a child VO(CreditReceiversVO) (child VO is basically extended from another VO not in this application and included as a jar library).
There a VL between ParentVO and ChildVO 1:* with two attributes ord_id and rule_id
On UI these two VO instances are dropped as parent child and I get exception on running page in logs saying RuleAssignementEO.ORG_ID invalid identifier.
Looking into the query formed by BC4J I see it as below which is clearly wrong as the alias RuleAssignmentEO is not visible for the outermost where clause. Please advise
"SELECT count(1) FROM (SELECT * FROM (SELECT /*+ FIRST_ROWS(10) */
RuleAssignmentsEO.RULE_ASSIGNMENT_ID,
RuleAssignmentsEO.OBJECT_VERSION_NUMBER,
RuleAssignmentsEO.RULE_ID,
RuleAssignmentsEO.ASSIGNED_OBJECT_ID,
RuleAssignmentsEO.ASSIGNED_OBJECT_TYPE,
RuleAssignmentsEO.ORG_ID,
RuleAssignmentsEO.ROLE_ID,
RuleAssignmentsEO.START_DATE,
RuleAssignmentsEO.END_DATE,
RuleAssignmentsEO.SPLIT_PCT,
RuleAssignmentsEO.REVENUE_TYPE,
RuleAssignmentsEO.ROLLUP_FLAG,
RuleAssignmentsEO.CREATED_BY,
RuleAssignmentsEO.CREATION_DATE,
RuleAssignmentsEO.LAST_UPDATED_BY,
RuleAssignmentsEO.LAST_UPDATE_DATE,
RuleAssignmentsEO.LAST_UPDATE_LOGIN,
RuleAssignmentsEO.ATTRIBUTE_CATEGORY,
RuleAssignmentsEO.ATTRIBUTE1,
RuleAssignmentsEO.ATTRIBUTE2,
RuleAssignmentsEO.ATTRIBUTE3,
RuleAssignmentsEO.ATTRIBUTE4,
RuleAssignmentsEO.ATTRIBUTE5,
RuleAssignmentsEO.ATTRIBUTE6,
RuleAssignmentsEO.ATTRIBUTE7,
RuleAssignmentsEO.ATTRIBUTE8,
RuleAssignmentsEO.ATTRIBUTE9,
RuleAssignmentsEO.ATTRIBUTE10,
RuleAssignmentsEO.ATTRIBUTE11,
RuleAssignmentsEO.ATTRIBUTE12,
RuleAssignmentsEO.ATTRIBUTE13,
RuleAssignmentsEO.ATTRIBUTE14,
RuleAssignmentsEO.ATTRIBUTE15,
RuleAssignmentsEO.GROUP_MEMBER_CREDIT_FLAG,
RuleAssignmentsEO.SUMMARY_FLAG,
ParticipantEO.PARTICIPANT_NAME,
ParticipantEO.PARTICIPANT_ID
FROM CN_RS_RULE_ASSIGNMENTS_ALL RuleAssignmentsEO, CN_SRP_PARTICIPANT_HDR_RO_V ParticipantEO
WHERE RuleAssignmentsEO.ASSIGNED_OBJECT_ID = ParticipantEO.PARTICIPANT_ID(+)) QRSLT WHERE RuleAssignmentsEO.RULE_ID = :Bind_RuleId AND RuleAssignmentsEO.ORG_ID = :Bind_OrgId) "
Edited by: Vik2 on Oct 23, 2012 11:30 AMinvalid identifier means ,it representating that field currently not available in db-table.
so please refer db-table. whether field avialable or not?.
edited lately:
djbo.debugoutput=console did you use this :) it will say the query is send to server.
Edited by: user707 on Oct 22, 2012 11:58 PM -
Error with Parent-Child Hierarchies in BIEE11.1.1.5
Hi ,
I am using BIEE11.1.1.5, I have created a Logical Dimensions with Parent-Child Hierarchies,
In answer,I draged the Hierarchies named 'H5 Sales Rep', then view the results,
It has error,error message as follows:
Odbc driver returned an error (SQLExecDirectW).
Error Details
Error Codes: OPR4ONWY:U9IM8TAC:OI2DL65P
State: HY000. Code: 10058. [NQODBC] [SQL_STATE: HY000] [nQSError: 10058] A general error has occurred. [nQSError: 43113] Message returned from OBIS. [nQSError: 43119] Query Failed: [nQSError: 22056] To use hierarchical functions, you need to associate a Closure Table with Logical Table Source 'LTS1 Sales Rep'. (HY000)
SQL Issued: SELECT 0 s_0, "Sample Sales"."Sales Rep"."Sales Rep Name" s_1, CASE WHEN ISLEAF("Sample Sales"."Sales Rep"."H5 Sales Rep") THEN 1 ELSE 0 END s_2, IDOF("Sample Sales"."Sales Rep"."H5 Sales Rep") s_3, PARENT("Sample Sales"."Sales Rep"."H5 Sales Rep") s_4 FROM "Sample Sales" WHERE ISROOT("Sample Sales"."Sales Rep"."H5 Sales Rep")
anyone know why???
Thank you!Hi Leo,
Did you configure closure table? http://www.rittmanmead.com/2010/11/oracle-bi-ee-11g-parent-child-hierarchies-multiple-modeling-methods/
Also make sure to perform the hierarchy changes in offline mode.
Regards,
Dpka -
Session expiration issue with parent and child window
Hi,
We have two applications (App1 - Implemented using JSF, Spring and Hibernate and App2 -Implemented using Spring MVC, Spring JDBC).
I have implemented 'Test Access Content' functionality in App1.
First admin user (Role- Manager) needs to login to App1
As part of 'Test Access Content' functionality, he enters URL with encrypted key and click 'Test Access' button.
It opens new window (implemented using java script - window.open) and hits spring controller of App2.
In spring controller of App2, I am decrypting the key to get user details. After that i am setting user details in session, constructing final URL to display actual content.
Problem is parent window maintains session till child window renders response. Once child window renders response, parent window loosing session.
So, if i click any button in parent window after child window renders response, it displays login page as there is no session for parent window.
Please note that App1 and App2 are sharing same domain. only context paths are different for both apps (app1 and app2).
Any suggestions on this issue are much appreciated. Please let us know if you have any questions.Hi,
When you open a child window from parent window then you can access child data in the parent window through javascript.
We have a main web page. There are quite many links on it from which >user can open new web pages but the data is saved when the submit >button on the main page is clicked. Also data that user entered on the >other sub pages is not displayed in the main window.
1 is this a case of parent and child window.case 1 When you are submitting the main page you need to run javascript function and get data from child windows and save that to database or session. next time when you are displaying the main window you need to query session or database to display the child window data in the main window.
Case 2> closing child window and you need data in the parent. This can be done in two ways.
1> When you are closing the child window populate some variables of the main window using javasript.
2> Save the values in the database or session and refresh the main window and extract data from database or session
Where should the data be saved that user enetered on the sub pages as the data is not shown in the main page - should it be stored in the session?It is design decision. You can store data in either session or database when you are closing the child window. But ifyou are saving the data on the main page then you can populate main page with child windows data using javascript. Save the data t database when main window data is saved. Depends on business requirements.
In short if you are saving child windows data in session or database then you need to refresh main screen to pull that data otherwise populate main screen using javascript.
I hope i answered your questions :) best of luck -
Stuck on sql query to find parent records that have the same child records
Oracle 10gR2 Enterprise Edition.
Hi,
I'm trying to write some logic to look for records in a parent table, which have the exact same values in a child table.
This is part of a bigger query, but I'm stuck on this part for now, so I've mocked up some simplified tables below to capture the core of the
problem I'm stuck on.
Let say I've got a parent table Manager, a child table Employee and there's a many to many relationship between them.
The aptly named Join_Table handles the relationship between them. So one manager can manage many employees, one employee can be managed by
many managers.
I've a feeling this is stupidly easy, but I seem to be suffering from a bad bout of brain freeze today!
-- parent table
CREATE TABLE manager (
id number primary key,
name varchar2(100));
-- child table
CREATE TABLE employee (
id number primary key,
name varchar2(100));
-- link table
CREATE TABLE join_table (
manager_id NUMBER,
employee_id NUMBER,
CONSTRAINT join_table_pk PRIMARY KEY (manager_id, employee_id),
CONSTRAINT manager_fk FOREIGN KEY (manager_id) REFERENCES manager(id),
CONSTRAINT employee_fk FOREIGN KEY (employee_id) REFERENCES employee(id)
-- Insert some managers
INSERT INTO manager (id, name) VALUES (1, 'John');
INSERT INTO manager (id, name) VALUES (2, 'Bob');
INSERT INTO manager (id, name) VALUES (3, 'Mary');
INSERT INTO manager (id, name) VALUES (4, 'Sue');
INSERT INTO manager (id, name) VALUES (5, 'Alan');
INSERT INTO manager (id, name) VALUES (6, 'Mike');
-- Insert some employees
INSERT INTO employee (id, name) VALUES (101, 'Paul');
INSERT INTO employee (id, name) VALUES (102, 'Simon');
INSERT INTO employee (id, name) VALUES (103, 'Ken');
INSERT INTO employee (id, name) VALUES (104, 'Kevin');
INSERT INTO employee (id, name) VALUES (105, 'Jack');
INSERT INTO employee (id, name) VALUES (106, 'Jennifer');
INSERT INTO employee (id, name) VALUES (107, 'Tim');
-- Insert the links
-- John manages Paul, Simon, Ken
INSERT INTO join_table (manager_id, employee_id) VALUES (1, 101);
INSERT INTO join_table (manager_id, employee_id) VALUES (1, 102);
INSERT INTO join_table (manager_id, employee_id) VALUES (1, 103);
-- Bob manages Paul, Simon, Kevin, Jack
INSERT INTO join_table (manager_id, employee_id) VALUES (2, 101);
INSERT INTO join_table (manager_id, employee_id) VALUES (2, 102);
INSERT INTO join_table (manager_id, employee_id) VALUES (2, 104);
INSERT INTO join_table (manager_id, employee_id) VALUES (2, 105);
-- Mary manages Jennifer, Tim
INSERT INTO join_table (manager_id, employee_id) VALUES (3, 106);
INSERT INTO join_table (manager_id, employee_id) VALUES (3, 107);
-- Sue manages Jennifer, Tim
INSERT INTO join_table (manager_id, employee_id) VALUES (4, 106);
INSERT INTO join_table (manager_id, employee_id) VALUES (4, 107);
-- Alan manages Paul, Simon, Ken, Jennifer, Tim
INSERT INTO join_table (manager_id, employee_id) VALUES (5, 101);
INSERT INTO join_table (manager_id, employee_id) VALUES (5, 102);
INSERT INTO join_table (manager_id, employee_id) VALUES (5, 103);
INSERT INTO join_table (manager_id, employee_id) VALUES (5, 106);
INSERT INTO join_table (manager_id, employee_id) VALUES (5, 107);
-- Mike manages Paul, Simon, Ken
INSERT INTO join_table (manager_id, employee_id) VALUES (6, 101);
INSERT INTO join_table (manager_id, employee_id) VALUES (6, 102);
INSERT INTO join_table (manager_id, employee_id) VALUES (6, 103);
-- For sanity
CREATE UNIQUE INDEX employee_name_uidx ON employee(name);So if I'm querying for manager John, I want to find the other managers who manage the exact same list of employees.
Answer should be Mike.
If I'm querying for manager Mary, answer should be Sue.
This query will give me the list of managers who manage some of the same employees as John, but not the exact same employees...
SELECT DISTINCT m.name AS manager
FROM manager m, join_table jt, employee e
WHERE m.id = jt.manager_id
AND jt.employee_id = e.id
AND e.id IN (
SELECT e.id
FROM manager m, join_table jt, employee e
WHERE m.id = jt.manager_id
AND jt.employee_id = e.id
AND m.name = 'John')
ORDER BY 1;I thought about using set operations to find managers whose list of employees minus my employees is null and where my employees minus their list of employees is null. But surely there's a simpler more elegant way.
Any ideas?
Btw, I need to run this as a batch job against tables with >20 million rows so query efficiency is key.What about...
WITH manager_list AS
SELECT name,
LTRIM(MAX(SYS_CONNECT_BY_PATH(id,','))
KEEP (DENSE_RANK LAST ORDER BY curr),',') AS employees
FROM (SELECT m.name,
e.id,
ROW_NUMBER() OVER (PARTITION BY m.name ORDER BY e.id) AS curr,
ROW_NUMBER() OVER (PARTITION BY m.name ORDER BY e.id) -1 AS prev
FROM manager m,
join_table jt,
employee e
WHERE m.id = jt.manager_id
AND jt.employee_id = e.id
AND m.name = :P_MANAGER)
GROUP BY name
CONNECT BY prev = PRIOR curr AND name = PRIOR name
START WITH curr = 1
), all_list AS
SELECT name,
LTRIM(MAX(SYS_CONNECT_BY_PATH(id,','))
KEEP (DENSE_RANK LAST ORDER BY curr),',') AS employees
FROM (SELECT m.name,
e.id,
ROW_NUMBER() OVER (PARTITION BY m.name ORDER BY e.id) AS curr,
ROW_NUMBER() OVER (PARTITION BY m.name ORDER BY e.id) -1 AS prev
FROM manager m,
join_table jt,
employee e
WHERE m.id = jt.manager_id
AND jt.employee_id = e.id)
GROUP BY name
CONNECT BY prev = PRIOR curr AND name = PRIOR name
START WITH curr = 1
SELECT a.*
FROM manager_list m,
all_list a
WHERE m.employees = a.employeesWould be easier in 11g, but I don't have an installation here so this is based on 10g.
Cheers
Ben -
Help with detecting child video playback end
I have a flash file that plays multiple videos. I'm trying to
set it up so that all of the videos are loaded by a second (child)
swf, imbedded in the first. The first (root) would call the second
(child) swf, play the video, and at the end of the video, playback
of the parent swf would resume. I can't seem to figure out how to
do this. If I try to make a call to the root from the second swf, I
of course can't because it doesn't know the root exists. The parent
(root) swf has the following code on the frame that calls the
child:
~~~
stop();
var loadVideo:URLRequest = new URLRequest("videoinset.swf");
var videoLoader:Loader = new Loader();
videoLoader.load(loadVideo);
// Identifies which frame to play in inset video
var vidclip:String = "intro";
function swfLoaded(myEvent:Event):void {
stage.addChild(videoLoader);
var mycontent:MovieClip=myEvent.target.content;
videoLoader.x = 210;
videoLoader.y = 150;
mycontent.gotoAndPlay(vidclip);
videoLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,
swfLoaded);
~~~
How can I tell it that when the child gets to frame(#), to go
back to the root and resume play?
I am using netstream to play the videos. There are 6, each
called by a different frame in the videoinset child swf.
Thanks for any help with this.Any help with this really would be appreciated; if I have not
explained it clearly enough, by all means please let me
know. -
Dimension with Parent-Child Hierarchy - not working
hi,
i tried making "Dimension with Parent-Child Hierarchy" as described in your tutorials.
i finished with the admin tool and database changes and with no errors.
when trying to create a new answer, by only puting the hierarchy column in the answer.
the result show "no result" exist.
i am not sure what i am doing wrong. maybe you can point me to the problem?
thanks
Mirit.956850 wrote:
hi,
i tried making "Dimension with Parent-Child Hierarchy" as described in your tutorials.
i finished with the admin tool and database changes and with no errors.
when trying to create a new answer, by only puting the hierarchy column in the answer.
the result show "no result" exist.
i am not sure what i am doing wrong. maybe you can point me to the problem?
thanks
Mirit.Hi Mirit,
What is the table that you created hierarchy on?
Which column in the hierarchy you pulled into your reports to see the No Result message?
Please query the table and see if has data.
Thanks,
G. SK
Maybe you are looking for
-
Why can't i use another email address to face time?
I am trying to face time and imessage someone with another email address that is connected to my apple id but it is not working. Please help!
-
Where did the 'sending mail' bar go?
I remember at some point, when I sent a msg with an attachment, there used to be a bar which would fill on the bottom lefthand side of the window showing how much of the message had been sent. Once it would fill (indicating that 100% of the msg was s
-
Is it possible to embed a Windows QT player on CD?
Is it possible to embed a Windows QT player on CD so the application doesn't have to be loaded on the host machine, but accessed from the CD. I have QTVR movies that must play on XP machines, but receipients are not permitted to permanently load "una
-
Implementinf Adobe Flex in Web dynpro
Hello.. Can anyone tell me how to implement adobe flex in webdynpro applications? I want to use the features of flex in my webdynpro project. Thanks for any help.. Regards, Rishu
-
WebDynpro SUBMIT button instead of Adobe SUBMIT button
Hi Exprts, I am using the WebDynpro SUBMIT button after many unsuccessful attempts of using the Adobe SUBMIT button. The WebDynpro button is easier to implement and use. Pease let me know if there are any negative points in taking this approach. Any