0

这是我的代码:

SELECT a.accNumber,
    a.Rest,
    dateadd(day, 1, min(a.DATE)),
    min(b.DATE)
FROM t1 a
LEFT JOIN t1 b
    ON a.accNumber = b.accNumber
WHERE a.DATE <> b.DATE
    AND a.DATE < b.DATE
    AND a.Rest <> 0
    AND a.accNumber = b.accNumber
GROUP a.accNumber,
    a.Rest
ORDER BY a.accNumber,
    3

我想要做的是从表 t2 中添加 accName 属性(选择语句),以便表 t1 的 accountID 等于表 t2 的 accountID。左连接很难做到。请帮忙。任何帮助或指导将不胜感激

4

2 回答 2

0
SELECT a.accNumber,b.accName,
    a.Rest,
    dateadd(day, 1, min(a.DATE)),
    min(b.DATE)
FROM t1 a
LEFT JOIN t2 b
    ON a.accNumber = b.accNumber
        AND a.DATE <> b.DATE
        AND a.DATE < b.DATE
        AND a.Rest <> 0
GROUP BY a.accNumber,b.accName,
    a.Rest
ORDER BY a.accNumber,
    3
于 2013-10-28T07:06:06.990 回答
0

您可以简单地使用两个LEFT JOIN

SELECT a.accNumber, a.Rest, dateadd(day,1,MIN(a.Date)), MIN(b.Date), t2.accName
FROM t1 a
LEFT JOIN t1 b ON a.accNumber=b.accNumber 
LEFT JOIN t2 ON a.accId = t2.accId
WHERE a.Date<b.Date 
AND a.Rest<>0 
AND a.accNumber=b.accNumber
GROUP BY a.accNumber, a.Rest
ORDER BY a.accNumber, 3

我还修正了一些错误:

  • BYGROUP BY子句中添加了;
  • 我删除了WHERE子句中的冗余条件(p != q AND p < q相当于p < q
于 2013-10-28T07:09:17.430 回答