alohn
Thành viên chính thức


- Tham gia
- 21/12/07
- Bài viết
- 59
- Được thích
- 13
Mình có truy vấn CSDL trên SQL qua MS query trong Excel.
SELECT AccountingObject.AccountingObjectName, GeneralLedger.AccountNumber, GeneralLedger.DebitAmount, GeneralLedger.CreditAmount, GeneralLedger.RefDate, Case GeneralLedger.RefDate When '2010-06-01' Then GeneralLedger.DebitAmount Else 0 End
FROM BANK.dbo.AccountingObject AccountingObject, BANK.dbo.GeneralLedger GeneralLedger
WHERE AccountingObject.AccountingObjectID = GeneralLedger.AccountingObjectID
ORDER BY GeneralLedger.RefDate, AccountingObject.AccountingObjectName
Nếu dùng câu lệnh Case như trên thì sẽ tạo ra cột lấy dữ liệu DebitAmount có RefDate bằng ngày 2010-06-01, nhưng nếu mình thanh đổi thành
Case GeneralLedger.RefDate When < '2010-06-01' Then GeneralLedger.DebitAmount Else 0 End
Thì báo lỗi
SQL Query can't be represented graphically. Continue anyway?
Didn't expect 'When' after the Select column list
Nhờ các bạn chỉ giúp xem mình đang bĩ lỗi ở đoạn nào. Nếu muốn lấy theo điều kiện thì dùng câu lệnh iif với SQL trong MS query có được không ?
Với dữ liệu lấy theo khoảng thời gian, thì dùng câu lệnh Select case, hay dùng iif.
Thank cả nhà nhiều
SELECT AccountingObject.AccountingObjectName, GeneralLedger.AccountNumber, GeneralLedger.DebitAmount, GeneralLedger.CreditAmount, GeneralLedger.RefDate, Case GeneralLedger.RefDate When '2010-06-01' Then GeneralLedger.DebitAmount Else 0 End
FROM BANK.dbo.AccountingObject AccountingObject, BANK.dbo.GeneralLedger GeneralLedger
WHERE AccountingObject.AccountingObjectID = GeneralLedger.AccountingObjectID
ORDER BY GeneralLedger.RefDate, AccountingObject.AccountingObjectName
Nếu dùng câu lệnh Case như trên thì sẽ tạo ra cột lấy dữ liệu DebitAmount có RefDate bằng ngày 2010-06-01, nhưng nếu mình thanh đổi thành
Case GeneralLedger.RefDate When < '2010-06-01' Then GeneralLedger.DebitAmount Else 0 End
Thì báo lỗi
SQL Query can't be represented graphically. Continue anyway?
Didn't expect 'When' after the Select column list
Nhờ các bạn chỉ giúp xem mình đang bĩ lỗi ở đoạn nào. Nếu muốn lấy theo điều kiện thì dùng câu lệnh iif với SQL trong MS query có được không ?
Với dữ liệu lấy theo khoảng thời gian, thì dùng câu lệnh Select case, hay dùng iif.
Thank cả nhà nhiều