Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何比较SQL中的父子数据_C#_Sql_Database_Sql Server 2008 - Fatal编程技术网

C# 如何比较SQL中的父子数据

C# 如何比较SQL中的父子数据,c#,sql,database,sql-server-2008,C#,Sql,Database,Sql Server 2008,我有两个如下所示的SQL表 columns of T1: meterID, parentID, childID columns of T2: dataID, meterID, date, amount 表格样本数据 T1 T2 ------------- ------------------------- 1 | null | 2 * 1 | 1 | 01,01,2013

我有两个如下所示的SQL表

columns of T1: meterID, parentID, childID  
columns of T2: dataID, meterID, date, amount  
表格样本数据

      T1                            T2
-------------              -------------------------
1 | null |  2  *           1 | 1 | 01,01,2013 | 100  *
1 | null |  3  *           2 | 2 | 01,01,2013 | 60   *
2 |   1  |  4              3 | 3 | 01,01,2013 | 40   *
2 |   1  |  5              4 | 4 | 01,01,2013 | 35
3 |   1  |  6              5 | 5 | 01,01,2013 | 25
3 |   1  |  7              6 | 6 | 01,01,2013 | 15
4 |   2  | null            7 | 7 | 01,01,2013 | 25
5 |   2  | null
6 |   3  | null  
7 |   3  | null  
我想比较一下孩子的金额和父母的金额是否相等

比如,;meter1是meter2和meter3的父级(带*的行)。我想检查100=60+40。
如何使用SQL查询实现这一点


对不起,我的英语很差。

你想把孩子的数量加起来,然后和父母比较一下。这需要两个到
t2
表的联接,一个用于父级,另一个用于子级

以下查询假设
t1
没有重复的条目:

select t1.parentId, t1.childId,
       parent.amount as parent_amount,
       SUM(child.amount) as child_amount,
       (case when parent.amount = SUM(child.amount) then 'Match'
             else 'NoMatch'
        end)
from t1 left outer join
     t2 parent
     on t1.parentId = t2.meterid left outer join
     t3 child
     on t1.childId = t2.meterId
group by t1.parent_id, t1.childId, parent.amount

您希望汇总子项金额并与父项进行比较。这需要两个到
t2
表的联接,一个用于父级,另一个用于子级

以下查询假设
t1
没有重复的条目:

select t1.parentId, t1.childId,
       parent.amount as parent_amount,
       SUM(child.amount) as child_amount,
       (case when parent.amount = SUM(child.amount) then 'Match'
             else 'NoMatch'
        end)
from t1 left outer join
     t2 parent
     on t1.parentId = t2.meterid left outer join
     t3 child
     on t1.childId = t2.meterId
group by t1.parent_id, t1.childId, parent.amount

此请求将子对象分组,并与父对象进行比较

SELECT t1.meterID, 
       CASE WHEN t1.amount = o.SumAmount THEN 'IsMatch' ELSE 'IsNotMatch' END
FROM T2 t1 OUTER APPLY (
                        SELECT SUM(t3.amount) AS SumAmount
                        FROM T1 t2 JOIN T2 t3 ON t2.childID = t3.meterID
                        WHERE t1.meterID = t2.meterID
                        GROUP BY t2.meterID
                        ) o
演示

测试后:可以使用不带GROUPBY子句的查询

SELECT t1.meterID, 
       CASE WHEN t1.amount = o.SumAmount THEN 'IsMatch' ELSE 'IsNotMatch' END
FROM T2 t1 OUTER APPLY (
                        SELECT SUM(t3.amount) AS SumAmount
                        FROM T1 t2 JOIN T2 t3 ON t2.childID = t3.meterID
                        WHERE t1.meterID = t2.meterID
                        ) o

此请求将子对象分组,并与父对象进行比较

SELECT t1.meterID, 
       CASE WHEN t1.amount = o.SumAmount THEN 'IsMatch' ELSE 'IsNotMatch' END
FROM T2 t1 OUTER APPLY (
                        SELECT SUM(t3.amount) AS SumAmount
                        FROM T1 t2 JOIN T2 t3 ON t2.childID = t3.meterID
                        WHERE t1.meterID = t2.meterID
                        GROUP BY t2.meterID
                        ) o
演示

测试后:可以使用不带GROUPBY子句的查询

SELECT t1.meterID, 
       CASE WHEN t1.amount = o.SumAmount THEN 'IsMatch' ELSE 'IsNotMatch' END
FROM T2 t1 OUTER APPLY (
                        SELECT SUM(t3.amount) AS SumAmount
                        FROM T1 t2 JOIN T2 t3 ON t2.childID = t3.meterID
                        WHERE t1.meterID = t2.meterID
                        ) o

感谢您的回复,您的查询有效,但仅适用于1=2+3。我还要检查2=4+5和3=6+7是否相等。显示米之间关系的图像:@AhmetEmre90,但这在查询中被选中:(2=4+5)=(2,IsMatch)和(3=6+7)=(3,IsMatch)噢,对不起。我的想法完全不同。现在有道理了。再次感谢您的帮助。感谢您的回复,您的查询有效,但仅适用于1=2+3。我还要检查2=4+5和3=6+7是否相等。显示米之间关系的图像:@AhmetEmre90,但这在查询中被选中:(2=4+5)=(2,IsMatch)和(3=6+7)=(3,IsMatch)噢,对不起。我的想法完全不同。现在有道理了。再次感谢你的帮助。