这是我的代码:
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属性(到select语句),以便表t1的accountID等于表t2的accountID。左连接使得很难做到。请帮忙。任何帮助或指导将不胜感激
答案 0 :(得分: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
答案 1 :(得分: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
我也修正了一些错误:
BY
子句中添加了GROUP BY
; WHERE
子句中的冗余条件(p != q AND p < q
相当于p < q
)