SQL:带分组的SubQuery

时间:2011-09-28 20:11:35

标签: subquery

  

可能重复:
  SQL : How can I use sub query in a query with group by section?

有人请帮助我。 SQL Server无法在子查询中识别t1.sen。错误消息:无法绑定多部分标识符“t1.sen”。

select
    t1.sen,
    sum(t1.d_rooz)as d1,
    sum(t1.d_shab)as d2,
    sum(t1.d_rooz+t1.d_shab) as d_sum,
    Round((sum((1000*(t1.d_rooz+t1.d_shab))/(9500-tc.c))),1) as SSS
from
    tbl_talafat_dan t1, (
       select sum(t2.t_shab+t2.t_rooz) as c
       from tbl_talafat_dan t2
       where FCode=81 AND DCode=1 AND t2.sen<=t1.sen
    ) as tc
where
    FCode = 81
    AND DCode = 1
group by
    t1.sen

1 个答案:

答案 0 :(得分:1)

我认为在t1之后你会遇到语法问题:

from
    tbl_talafat_dan t1, (
       select sum(t2.t_shab+t2.t_rooz) as c
       from tbl_talafat_dan t2
       where FCode=81 AND DCode=1 AND t2.sen<=t1.sen
    ) as tc

它试图找出可供选择的表格。你需要使用t1或tc并在适当的后加入。希望这会有所帮助。