tungthoi15 > 16-05-22, 10:11 AM
ongke0711 > 16-05-22, 02:38 PM
SELECT tbHomNay.MaKH, tbHomNay.HopDong,0 As NgayTruoc, nz(tbHomNay.SoTien) AS HomNay
FROM tbHomNay
UNION ALL
SELECT tbNgayTruoc.MaKH, tbNgayTruoc.HopDong, nz(tbNgayTruoc.SoTien) AS NgayTruoc,0 As HomNay
FROM tbNgayTruoc
SELECT Query1.MaKH, Query1.HopDong, Sum(Query1.NgayTruoc) AS SumOfNgayTruoc, Sum(Query1.HomNay) AS SumOfHomNay, Sum([HomNay]-[NgayTruoc]) AS ChenhLech
FROM Query1
GROUP BY Query1.MaKH, Query1.HopDong;
tungthoi15 > 16-05-22, 02:53 PM
(16-05-22, 02:38 PM)ongke0711 Đã viết: Bạn tạo 2 Query:
1. Union Query: dùng gộp tất cả record của 2 table và 1. [Query1]
Mã PHP:SELECT tbHomNay.MaKH, tbHomNay.HopDong,0 As NgayTruoc, nz(tbHomNay.SoTien) AS HomNay
FROM tbHomNay
UNION ALL
SELECT tbNgayTruoc.MaKH, tbNgayTruoc.HopDong, nz(tbNgayTruoc.SoTien) AS NgayTruoc,0 As HomNay
FROM tbNgayTruoc
2. Query kết quả:
Mã PHP:SELECT Query1.MaKH, Query1.HopDong, Sum(Query1.NgayTruoc) AS SumOfNgayTruoc, Sum(Query1.HomNay) AS SumOfHomNay, Sum([HomNay]-[NgayTruoc]) AS ChenhLech
FROM Query1
GROUP BY Query1.MaKH, Query1.HopDong;
SELECT Query1.MaKH, Sum(Query1.NgayTruoc) AS SumOfNgayTruoc, Sum(Query1.HomNay) AS SumOfHomNay, Sum([HomNay]-[NgayTruoc]) AS ChenhLech
FROM Query1
GROUP BY Query1.MaKH;