Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

How to compare parent child data in SQL Expand / Collapse
Author
Message
Posted Wednesday, February 20, 2013 6:03 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, February 20, 2013 6:00 AM
Points: 1, Visits: 2
Hi;

I have two SQL tables like below;
T1: relationID, meterID, parentID, childID
T2: dataID, meterID, date, amount

Sample data of tables;
        T1                                   T2
----------------- -------------------------
1 | 1 | null | 2 * 1 | 1 | 01,01,2013 | 100 *
2 | 1 | null | 3 * 2 | 2 | 01,01,2013 | 60 *
3 | 2 | 1 | 4 3 | 3 | 01,01,2013 | 40 *
4 | 2 | 1 | 5 4 | 4 | 01,01,2013 | 35
5 | 3 | 1 | 6 5 | 5 | 01,01,2013 | 25
6 | 3 | 1 | 7 6 | 6 | 01,01,2013 | 15
7 | 4 | 2 | null 7 | 7 | 01,01,2013 | 25
8 | 5 | 2 | null
9 | 6 | 3 | null
10 | 7 | 3 | null

I want to compare if the sum of the children's amount is equal to parent's amount.

For example; meter1 is parent of meter2 and meter3 (lines with *). I want to check if 100 = 60 + 40.
How can i do this with SQL query.

You can see the relation between the meters at the image below.

Sorry for my poor English.
Post #1422045
Posted Wednesday, February 20, 2013 6:54 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Wednesday, December 18, 2013 8:10 AM
Points: 1,191, Visits: 995
Here is the raw form of the query using CTE's. Please tweak it to suit your requirement....I hope this helps

;WITH tblCTE_1
AS
(
SELECT T2.MeterID AS CMeterID, T1.MeterID AS PMeterID, Amount
FROM dbo.T2
LEFT JOIN dbo.T1
ON T2.MeterID = T1.ChildID
),
tblCTE_2 AS
(
SELECT PMeterID, SUM(Amount) AS Amount
FROM tblCTE_1
GROUP BY PMeterID
)
SELECT C1.CMeterID AS MeterID
FROM tblCTE_1 C1
INNER JOIN tblCTE_2 C2
ON C1.CMeterID = C2.PMeterID
WHERE C2.PMeterID IS NOT NULL
AND C1.Amount = C2.Amount -- C1.Amount <> C2.Amount -- in case you want those ID's whose SUM(Amount) do not match
Post #1422060
Posted Tuesday, February 26, 2013 9:49 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 4:46 PM
Points: 35,959, Visits: 30,250
AhmetEmre90 (2/20/2013)
Hi;

I have two SQL tables like below;
T1: relationID, meterID, parentID, childID
T2: dataID, meterID, date, amount

Sample data of tables;
        T1                                   T2
----------------- -------------------------
1 | 1 | null | 2 * 1 | 1 | 01,01,2013 | 100 *
2 | 1 | null | 3 * 2 | 2 | 01,01,2013 | 60 *
3 | 2 | 1 | 4 3 | 3 | 01,01,2013 | 40 *
4 | 2 | 1 | 5 4 | 4 | 01,01,2013 | 35
5 | 3 | 1 | 6 5 | 5 | 01,01,2013 | 25
6 | 3 | 1 | 7 6 | 6 | 01,01,2013 | 15
7 | 4 | 2 | null 7 | 7 | 01,01,2013 | 25
8 | 5 | 2 | null
9 | 6 | 3 | null
10 | 7 | 3 | null

I want to compare if the sum of the children's amount is equal to parent's amount.

For example; meter1 is parent of meter2 and meter3 (lines with *). I want to check if 100 = 60 + 40.
How can i do this with SQL query.

You can see the relation between the meters at the image below.

Sorry for my poor English.


I believe that the following article will give you what you want and much more.
http://www.sqlservercentral.com/articles/T-SQL/94570/


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

"Change is inevitable. Change for the better is not." -- 04 August 2013
(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1424326
Posted Wednesday, February 27, 2013 5:53 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Yesterday @ 6:03 PM
Points: 3,590, Visits: 5,098
A couple of quick points:

1. Since you're a first time poster, you should understand that to get better, faster and more accurate help you should follow the first link in Jeff signature to learn forum etiquette for providing DDL and consumable sample data (like in my first SQL shown below).

2. Jeff's article on Hierarchies on Steroids is incredibly useful for complex hiearchies were a lot of information between nodes must be retained. However I think it may be a bit overkill in this case where all you need to do is sum the children of a parent to one level deep (assuming this is right).

DDL and consumable sample data:

DECLARE @T1 TABLE
(relationID INT, meterID INT, parentID INT, childID INT)
DECLARE @T2 TABLE
(dataID INT, meterID INT, [date] DATE, amount INT)

INSERT INTO @T1
SELECT 1 , 1 , null , 2
UNION ALL SELECT 2 , 1 , null , 3
UNION ALL SELECT 3 , 2 , 1 , 4
UNION ALL SELECT 4 , 2 , 1 , 5
UNION ALL SELECT 5 , 3 , 1 , 6
UNION ALL SELECT 6 , 3 , 1 , 7
UNION ALL SELECT 7 , 4 , 2 , null
UNION ALL SELECT 8 , 5 , 2 , null
UNION ALL SELECT 9 , 6 , 3 , null
UNION ALL SELECT 10 , 7 , 3 , null

INSERT INTO @T2
SELECT 1 , 1 , '2013-01-01' , 100
UNION ALL SELECT 2 , 2 , '2013-01-01' , 60
UNION ALL SELECT 3 , 3 , '2013-01-01' , 40
UNION ALL SELECT 4 , 4 , '2013-01-01' , 35
UNION ALL SELECT 5 , 5 , '2013-01-01' , 25
UNION ALL SELECT 6 , 6 , '2013-01-01' , 15
UNION ALL SELECT 7 , 7 , '2013-01-01' , 25


A possibly simpler solution than the one provided by Mr. AsiaIndian, made possible because you keep all of your child nodes in the relations table:

SELECT a.meterID, b.[date]  
,MeterAmt=MAX(b.amount)
,SumOfChildren=SUM(c.amount)
FROM @T1 a
INNER JOIN @T2 b ON a.meterID = b.meterID
LEFT JOIN @T2 c ON a.childID = c.meterID
GROUP BY a.meterID, b.[date]


Change the LEFT JOIN to an INNER JOIN if you don't want leaf nodes displayed in the results.



My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
Post #1424828
Posted Wednesday, February 27, 2013 8:47 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 4:46 PM
Points: 35,959, Visits: 30,250
dwain.c (2/27/2013)
However I think it may be a bit overkill in this case where all you need to do is sum the children of a parent to one level deep (assuming this is right).


If that's all they'll EVER want to do, then I agree. While it’s true that I tend to “over” plan by making such "simple" things bullet proof to scope changes, scale changes, and future requirements, let's hope that the complexity of the doubly-linked (each row has the parent and child listed) hierarchical Adjacency List in T1 isn't the foreboding of more complex requests that I think it will be.


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

"Change is inevitable. Change for the better is not." -- 04 August 2013
(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1424864
Posted Wednesday, February 27, 2013 8:57 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Yesterday @ 6:03 PM
Points: 3,590, Visits: 5,098
Jeff Moden (2/27/2013)
dwain.c (2/27/2013)
However I think it may be a bit overkill in this case where all you need to do is sum the children of a parent to one level deep (assuming this is right).


If that's all they'll EVER want to do, then I agree. While it’s true that I tend to “over” plan by making such "simple" things bullet proof to scope changes, scale changes, and future requirements, let's hope that the complexity of the doubly-linked (each row has the parent and child listed) hierarchical Adjacency List in T1 isn't the foreboding of more complex requests that I think it will be.


The thought did occur to me that maintaining those child nodes in the relations table was probably going to incur some overhead (and headaches).

But I also figured that you need to learn to crawl before you can learn to run.



My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
Post #1424870
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse