Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.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
Sql 将求和值与主表进行比较_Sql_Sql Server 2008 - Fatal编程技术网

Sql 将求和值与主表进行比较

Sql 将求和值与主表进行比较,sql,sql-server-2008,Sql,Sql Server 2008,表@t1是主表,导入后应具有表@t2的合计值 DECLARE @t1 TABLE ( typ int, total float ); insert into @t1 (typ,total) values(1,30.0) insert into @t1 (typ,total) values(2,70.0) insert into @t1 (typ,total) values(3,99.9) DECLARE @t2 TABLE ( typ

表@t1是主表,导入后应具有表@t2的合计值

DECLARE @t1 TABLE (
    typ         int,
    total       float
);

insert into @t1 (typ,total) values(1,30.0)
insert into @t1 (typ,total) values(2,70.0)
insert into @t1 (typ,total) values(3,99.9)

DECLARE @t2 TABLE  (
    typ     int,
    value   float
);

insert into @t2 (typ,value) values(1, 10.0)
insert into @t2 (typ,value) values(1, 20.0)
insert into @t2 (typ,value) values(2, 30.0)
insert into @t2 (typ,value) values(2, 40.0)
insert into @t2 (typ,value) values(3, 50.0)


select 
    t1.typ,
    t1.total, 
    t2.typ,
    t2.value,
    case when total = value then 'TRUE' else 'FALSE' end as Result
    from @t1 t1
left join @t2 t2 on t2.typ = t1.typ
结果:

typ|total|typ|value|Result
1  |30   |1  |10   |FALSE
1  |30   |1  |20   |FALSE
2  |70   |2  |30   |FALSE
2  |70   |2  |40   |FALSE
3  |99,9 |3  |50   |FALSE
当然,结果总是“FALSE”,因为t2.value尚未求和

我的第一个想法是:

select 
    t1.typ,
    t1.total, 
    -- t2.typ,

    (select sum(t2.value) 
     from @t2 t2 
     where t1.typ = t2.typ
     group by typ) as Summed,

    case when total = (select sum(t2.value) 
                                         from @t2 t2 
                                         where t1.typ = t2.typ
                                         group by typ) then 'TRUE' else 'FALSE' end as Result
    from @t1 t1
  left join @t2 t2 on t2.typ = t1.typ
但我明白了

typ|total|Summed|Result
1  |30   |30    |TRUE
1  |30   |30    |TRUE
2  |70   |70    |TRUE
2  |70   |70    |TRUE
3  |99,9 |50    |FALSE
正确的结果必须如此:

typ|total|Summed|Result
1  |30   |30    |TRUE
2  |70   |70    |TRUE
3  |99,9 |50    |FALSE

我很高兴得到这个问题的答复。

您可以使用
OUTER APPLY
进行以下操作:

SELECT t1.typ, t1.total, x.Summed,
       CASE 
          WHEN x.Summed = t1.total THEN 'TRUE'
          ELSE 'FALSE'
       END          
FROM @t1 AS t1
OUTER APPLY (
   SELECT SUM(value) AS Summed
   FROM @t2 AS t2
   WHERE t1.typ = t2.typ ) AS x

将case语句移动到派生表查询的外部查询:

select typ, total, 
    case when total = Summed then 'TRUE' else 'FALSE' end as Result 
from ( 
    select t1.typ,
        t1.total, 
        (select sum(t2.value) 
         from @t2 t2 
         where t1.typ = t2.typ
         group by typ) as Summed     
     from @t1 t1 
) t

这通常通过
左连接来完成:

select t1.typ, t1.total, t2.typ, t2.sumvalue,
       (case when t1.total = t2.sumvalue then 'TRUE' else 'FALSE' end) as Result
from @t1 t1 left join
     (select typ, sum(t2.value) as sumvalue
      from @t2 t2
      group by typ
     ) t2
     on t2.typ = t1.typ;

outer apply
可以,但它使用标准SQL。

这个解决方案也很有用。谢谢你,格兰特!