Hi lingers,
Use ISNULL function.
Refer below query.
SQL
ALTER PROCEDURE [dbo].[Total_MealLog]
@Start VARCHAR(10),
@End VARCHAR(10)
AS
BEGIN
SET NOCOUNT ON;
SELECT CAST(ROW_NUMBER() OVER(ORDER BY (SELECT 0)) AS VARCHAR(100)) 'S/N',*
INTO #Data
FROM
(
SELECT DISTINCT DATENAME(dw, Time1)+', ' + CONVERT(VARCHAR(12), Time1, 107) QUERYDATE,
ISNULL((SELECT SUM(Exact) FROM MealLog WHERE Time1 = ml.Time1 AND Company = 'ARPN'),'0') ARPN,
ISNULL((SELECT SUM(Exact) FROM MealLog WHERE Time1 = ml.Time1 AND Company = 'ARPN'),'0')*ISNULL((SELECT MAX(VALUE) FROM MealLog WHERE Time1 = ml.Time1 AND Company = 'ARPN'),'0') ARPNCOST,
ISNULL((SELECT SUM(Exact) FROM MealLog WHERE Time1 = ml.Time1 AND Company = 'ARPNBULK'),'0') ARPNBULK,
ISNULL((SELECT SUM(Exact) FROM MealLog WHERE Time1 = ml.Time1 AND Company = 'ARPNBULK'),'0')*ISNULL((SELECT TOP 1 VALUE FROM MealLog WHERE Time1 = ml.Time1 AND Company = 'ARPNBULK'),'0') ARPNBULKCOST,
ISNULL((SELECT SUM(Exact) FROM MealLog WHERE Time1 = ml.Time1 AND Company = 'AGGREY'),'0') AGGREY,
ISNULL((SELECT SUM(Exact) FROM MealLog WHERE Time1 = ml.Time1 AND Company = 'AGGREY'),'0')*ISNULL((SELECT TOP 1 VALUE FROM MealLog WHERE Time1 = ml.Time1 AND Company = 'AGGREY'),'0') AGGREYCOST,
ISNULL((SELECT SUM(Exact) FROM MealLog WHERE Time1 = ml.Time1 AND Company = 'CRIMETECH'),'0') CRIMETECH,
ISNULL((SELECT SUM(Exact) FROM MealLog WHERE Time1 = ml.Time1 AND Company = 'CRIMETECH'),'0')*ISNULL((SELECT TOP 1 VALUE FROM MealLog WHERE Time1 = ml.Time1 AND Company = 'CRIMETECH'),'0') CRIMETECHCOST,
ISNULL((SELECT SUM(Exact) FROM MealLog WHERE Time1 = ml.Time1 AND Company = 'DELOG'),'0') DELOG,
ISNULL((SELECT SUM(Exact) FROM MealLog WHERE Time1 = ml.Time1 AND Company = 'DELOG'),'0')*ISNULL((SELECT TOP 1 VALUE FROM MealLog WHERE Time1 = ml.Time1 AND Company = 'DELOG'),'0') DELOGCOST,
ISNULL((SELECT SUM(Exact) FROM MealLog WHERE Time1 = ml.Time1 AND Company = 'DELOGBULK'),'0') DELOGBULK,
ISNULL((SELECT SUM(Exact) FROM MealLog WHERE Time1 = ml.Time1 AND Company = 'DELOGBULK'),'0')*ISNULL((SELECT TOP 1 VALUE FROM MealLog WHERE Time1 = ml.Time1 AND Company = 'DELOGBULK'),'0') DELOGBULKCOST,
ISNULL((SELECT SUM(Exact) FROM MealLog WHERE Time1 = ml.Time1 AND Company = 'RDELOG'),'0') RDELOG,
ISNULL((SELECT SUM(Exact) FROM MealLog WHERE Time1 = ml.Time1 AND Company = 'RDELOG'),'0')*ISNULL((SELECT TOP 1 VALUE FROM MealLog WHERE Time1 = ml.Time1 AND Company = 'RDELOG'),'0') RDELOGCOST,
ISNULL((SELECT SUM(Exact) FROM MealLog WHERE Time1 = ml.Time1 AND Company = 'TUSKEEGEE'),'0') TUSKEEGEE,
ISNULL((SELECT SUM(Exact) FROM MealLog WHERE Time1 = ml.Time1 AND Company = 'TUSKEEGEE'),'0')*ISNULL((SELECT TOP 1 VALUE FROM MealLog WHERE Time1 = ml.Time1 AND Company = 'TUSKEEGEE'),'0') TUSKEEGEECOST,
ISNULL((SELECT SUM(Exact) FROM MealLog WHERE Time1 = ml.Time1 AND Company = 'TUSKEEGEEBULK'),'0') TUSKEEGEEBULK,
ISNULL((SELECT SUM(Exact) FROM MealLog WHERE Time1 = ml.Time1 AND Company = 'TUSKEEGEEBULK'),'0')*ISNULL((SELECT TOP 1 VALUE FROM MealLog WHERE Time1 = ml.Time1 AND Company = 'TUSKEEGEEBULK'),'0') TUSKEEGEEBULKCOST,
ISNULL((SELECT SUM(Exact) FROM MealLog WHERE Time1 = ml.Time1 AND Company = 'VISITOR'),'0') VISITOR,
ISNULL((SELECT SUM(Exact) FROM MealLog WHERE Time1 = ml.Time1 AND Company = 'VISITOR'),'0')*ISNULL((SELECT TOP 1 VALUE FROM MealLog WHERE Time1 = ml.Time1 AND Company = 'VISITOR'),'0') VISITORCOST,
ISNULL((SELECT SUM(Exact) FROM MealLog WHERE Time1 = ml.Time1 AND Company = 'MOPOL'),'0') MOPOL,
ISNULL((SELECT SUM(Exact) FROM MealLog WHERE Time1 = ml.Time1 AND Company = 'MOPOL'),'0')*ISNULL((SELECT TOP 1 VALUE FROM MealLog WHERE Time1 = ml.Time1 AND Company = 'MOPOL'),'0') MOPOLCOST
FROM MealLog ml
WHERE Time1 >= @Start and Time1 <= @End
GROUP BY Time1, Company, Exact
)t
SELECT * FROM #Data
UNION ALL
SELECT '','Total',SUM(ARPN),SUM(ARPNCOST),SUM(ARPNBULK),SUM(ARPNBULKCOST),
SUM(AGGREY),SUM(AGGREYCOST),SUM(CRIMETECH),SUM(CRIMETECHCOST),SUM(DELOG),SUM(DELOGCOST),
SUM(DELOGBULK),SUM(DELOGBULKCOST),SUM(RDELOG),SUM(RDELOGCOST),SUM(TUSKEEGEE),SUM(TUSKEEGEECOST),
SUM(TUSKEEGEEBULK),SUM(TUSKEEGEEBULKCOST),SUM(VISITOR),SUM(VISITORCOST),SUM(MOPOL),SUM(MOPOLCOST)
FROM #Data
DROP TABLE #Data
END