如何在sql server中查询此报告?

时间:2013-04-25 13:58:35

标签: sql-server sql-server-2008

我有这个表“CashMovementTbl”,“SalesTbl”& “ExpensesTbl”包括类似的数据:

enter image description here

我希望显示以下数据:

enter image description here

那么如何在SQL Server中进行查询以显示这样的数据。

1 个答案:

答案 0 :(得分:1)

SELECT
   c.CashDate,
   ISNULL(s.BillNo,e.ExpnsType) As Details,
   CASE WHEN c.SaleID IS NOT NULL THNE c.Amount ELSE 0 END AS [In],
   CASE WHEN c.ExpenseID IS NOT NULL THNE c.Amount ELSE 0 END AS [Out] 
FROM CashMovementTbl c
LEFT JOIN SalesTbl s
 ON c.SaleID = s.SalesIS
LEFT JOIN ExpensensTbl e
 ON c.ExpenseID = e.ExpnsID