This is my Invoice
table:
CREATE TABLE [dbo].[InvoiceTable]
(
[Id] INT IDENTITY (1, 1) NOT NULL,
[CodeColumn] NVARCHAR (50) NOT NULL,
[NameColumn] NVARCHAR (50) NOT NULL,
[QTYColumn] INT NULL,
[UnitCostColumn] INT NOT NULL,
[DiscountRateColumn] FLOAT (53) DEFAULT ((0)) NULL,
PRIMARY KEY CLUSTERED ([Id] ASC)
);
I want to have a Total Row
, TotalQTY
, and a Total Cost
column. I created the Total Row
and TotalQTY
with this code (adding a dynamic column) in a stored procedure:
CREATE PROCEDURE GetInvoice
AS
SELECT
ROW_NUMBER() OVER (ORDER BY NameColumn) AS RowNumberColumn,
CodeColumn,
NameColumn,
SUM(QTYColumn) AS TotalQTY,
UnitCostColumn,
DiscountRateColumn,
UnitCostColumn * SUM(QTYColumn) AS TotalRow
FROM
InvoiceTable
GROUP BY
CodeColumn, NameColumn, UnitCostColumn, DiscountRateColumn
My problem: I'm not able to have Total Row
's value in the invoice table. Why? Because when I do SELECT * FROM INVOICE TABLE
, it shows all columns except TotalRow
and TotalQTY
. So when I don't have these in the table how can I have the Total Cost
column? I was thinking that if I had the total Row column, I would be able to use ExecuteScalar
somehow in my C# program to sum up Total Row
as Total Column
. I would appreciate if someone could tell me how to use ExecuteScalar
in this example.