如何处理多个表的DATE列

时间:2014-06-26 06:16:55

标签: sql sql-server date inner-join sql-server-2008r2-express

我有5张表,即交易,收据,付款,wutran,mydates。 mydates表包含dates.It只有一个单独的列是dt。 并且所有剩余的表都有两列,即日期,金额。 现在我必须通过' dt'来计算这4个表组中的金额总和。这是mydates的列。 我的查询如下:

select 
m.dt,
(SUM(t.amount)+SUM(r.amount)+SUM(p.amount)+SUM(w.amount)) as sum_amount
from 
transaction t,
receipt r, 
payment p, 
wutran w 
inner join 
mydates m 
on t.[date]=m.Dt and r.[date]=m.Dt
and p.[date]=m.Dt and w.[Date]=m.Dt group by m.Dt

但是我收到以下错误:

Msg 4104, Level 16, State 1, Line 2
The multi-part identifier "t.date" could not be bound.
Msg 4104, Level 16, State 1, Line 2
The multi-part identifier "r.date" could not be bound.
Msg 4104, Level 16, State 1, Line 3
The multi-part identifier "p.date" could not be bound.

任何人都可以帮我解决这个错误......

3 个答案:

答案 0 :(得分:1)

Error因为JOIN仅适用于wutranmydates,您可以尝试 Where 的其他条件但不建议这样做。

试试这个

  SELECT m.dt,(SUM(t.amount)+SUM(r.amount)+SUM(p.amount)+SUM(w.amount)) AS sum_amount 
  FROM transaction t,receipt r, payment p, wutran w 
       INNER JOIN mydates m ON w.[Date] = m.Dt 
  WHERE t.[date] = m.Dt AND r.[date] = m.Dt AND p.[date] = m.Dt 
  GROUP BY m.Dt

或者您必须 JOIN 所有表格

  SELECT m.dt,(SUM(t.amount)+SUM(r.amount)+SUM(p.amount)+SUM(w.amount)) AS sum_amount 
  FROM transaction t
        JOIN mydates m on t.[date] = m.Dt
        JOIN receipt r on r.[date] = m.Dt
        JOIN payment p on p.[date] = m.Dt
        JOIN wutran w  on w.[Date] = m.Dt 
  GROUP BY m.Dt

答案 1 :(得分:0)

select m.dt,SUM(t.amount+r.amount+p.amount+w.amount) as sum_amount 
from transaction t
    inner join mydates m on t.[date]=m.Dt
    inner join receipt r on r.[date]=m.Dt
    inner join payment p on p.[date]=m.Dt
    inner join wutran w  on w.[Date]=m.Dt 
group by m.Dt

答案 2 :(得分:0)

select m.dt,(SUM(t.amount)+SUM(r.amount)+SUM(p.amount)+SUM(w.amount)) as sum_amount 
 from transaction t,
      receipt r, 
      payment p, 
      wutran w ,
      mydates m 
 where t.[date]=m.Dt and r.[date]=m.Dt
 and p.[date]=m.Dt and w.[Date]=m.Dt 
 group by m.Dt