如何从连接到其他表和总计的数据库表中添加标题

时间:2017-01-12 07:36:39

标签: sql sql-server totals

这是我目前的代码:

Select
  InventoryTransactionDocuments.Name as 'GRV Name',
  InventoryTransactionDocuments.Date,
  InventoryItems.Name as 'Items',
  convert(decimal(16,0),InventoryTransactions.Quantity) as 'Units',
  convert(decimal(16,2),InventoryTransactions.Price) as 'Cost',
  convert(decimal(16,2),InventoryTransactions.Quantity*InventoryTransactions.Price) AS 'Total Cost'
From
  InventoryItems,
  InventoryTransactionDocuments,
  InventoryTransactions
Where
  InventoryTransactionDocuments.Id=InventoryTransactions.InventoryTransactionDocumentId
  And InventoryTransactions.InventoryItem_Id=InventoryItems.Id

返回:

GRV Name|Date                     |Items         |Units |Cost  |Total Cost

INV001  |2017-01-10 12:48:14.027  | Coke         | 10   | 9,00 | 90,00
INV001  | 2017-01-10 12:48:14.027 | Sprite       | 10   | 7,50 | 75,00
INV002  | 2017-01-11 14:36:12.036 | Fanta        | 5    | 8,50 | 42,50
INV002  | 2017-01-11 14:36:12.036 | Mountain Dew | 8    | 6,33 | 50,64

但我想要的是:

GRV: INV001
Date:2017-01-10 12:48:14.027
Items  | Units | Cost | Total Cost
Coke   | 10    | 9,00 | 90,00
Sprite | 10    | 7,50 | 75,00
Total: |              |165,00 

GRV: INV002
Date:2017-01-11 14:36:12.036 
Items        | Units | Cost | Total Cost
Fanta        | 5     | 8,50 | 42,50
Mountain Dew | 8     | 6,33 | 50,64
Total:       |              |93,14 

我尝试使用UNION All和sum函数添加总数,但我得到一个varchar到十进制错误。

Union All

Select
'Total For GRV','','','','',Sum([Total Cost])
FROM Temp

将temp设置为top select语句。 撇号也被改为每列为空,没有区别。

我很感激你能给我的任何帮助,以达到预期的效果。

0 个答案:

没有答案