我有两个表table1和table2。每个表都包含带有itemPrice的列。我需要将这两列加在一起。
下面的SQL查询返回正确的SUM。
SELECT SUM(item1+ item2) FROM
(select SUM(t1.itemPrice) item1 from table1 t1 WHERE t1.userid=='jonh') tableA
CROSS JOIN
(select SUM(t2.itemPrice) item2 from table2 t2 WHERE t1.userid=='jonh') tableB
我并不懒惰,但是上面的查询有这么多的SUM函数,我不知道从哪里开始编写LINQ查询。
有人可以帮忙吗?
这,
希望这会给您您想要的...
from f in (
from x in ( from t1 in Table1
where t1.Userid.Equals("John")
select new { Userid = t1.Userid }
).Distinct()
select new { item1 = ( from z in Table1
where z.Userid.Equals("John")
select z.ItemPrice ).Sum() ??0 ,
item2 = ( from z in Table2
where z.Userid.Equals("John")
select z.ItemPrice ).Sum() ??0 }
) select new { total = f.item1 + f.item2 }
在一个表中没有“ john”记录的情况下,它将带回0并汇总其他表。
希望这可以帮助。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句