One of my fields are null and I am trying to use distinct to filter out the redundant and records which are repeated. This is my SQL statement which calculates checks value based on being drawn and not!
SELECT CASE SUM(Money)
WHEN null THEN sum(0)
ELSE SUM(Money)
END AS MoneyTaken
FROM (SELECT Money
FROM tblPayment AS tblPayment_1
WHERE (CheckStatus = 0) AND (IDBuyer = @BuyerID) AND (IDSource = @SourceID)
GROUP BY ID, IDBuyer, IDSeller, Money) AS derivedtbl_1_1) AS [Cheques Which didnt have credit], tblPurchase.Price AS SellPrice
but this doesn't work and I still get NULL
in MoneyTaken
Column when executing the function.
This is my full stored procedure which still shows repeated results :
ALTER PROCEDURE [dbo].[SGetListofKharIDBuyer]
@SurceId bigint,
@BuyerId bigint
AS
SET NOCOUNT ON;
/*
0 = check(cheque) dosent have credit-so couldntget money out of it
1 = check(cheque) has credit- got the money
-1 = in cashe - paid in cash
*/
SELECT distinct vPatmentForReport.Account, vPatmentForReport.PaymentStatus, vPatmentForReport.Description, vPatmentForReport.Bank, vPatmentForReport.Serial,
vPatmentForReport.PaymentMethod, vPatmentForReport.Date, vPatmentForReport.مبلغ * Money AS [Payment in Cash], tblPurchase.Date AS [PurchaseDate],
tblReserve.Fee , tblReserve.NumberOfReserve , tblReserve.TotalMoney ,
(SELECT COALESCE( SUM(Money),0) AS [PaidMoney]
FROM (SELECT Money
FROM tblPardakht
WHERE (CheckStatus <> 0) AND (IDBuyer = @BuyerId) AND (IDSource = @SurceId)
GROUP BY ID, IDBuyer, IDAccount, Money) AS derivedtbl_1) AS [Sum of Cheques and in cash payments],
(SELECT COALESCE( SUM(Money),0) AS [PaidMoney]
FROM (SELECT Money
FROM tblPardakht AS tblPardakht_1
WHERE (CheckStatus = 0) AND (IDBuyer = @BuyerId) AND (IDSource = @SurceId)
GROUP BY ID, IDBuyer, IDAccount, Money) AS derivedtbl_1_1) AS [sum of cheques which are not drawn], tblPurchase.Money ,
tblReserve.Description , tblReserve.Date
FROM tblPurchase INNER JOIN
tblReserve ON tblPurchase.IDReserve = tblReserve.ID inner JOIN
vPatmentForReport ON tblReserve.IDSource = vPatmentForReport.[SourceId] AND tblReserve.IDBuyer = vPatmentForReport.[BuyerId]
WHERE (vPatmentForReport.[BuyerId] = @BuyerId) AND (vPatmentForReport.[SourceId] = @SurceId)
vPatmentForReport
is a view which is made by a function without any parameters.
3条答案
按热度按时间eagi6jfj1#
use
COALESCE
wmtdaxz32#
Try below code :
093gszye3#
as far as you will get NULL in case your subselects won't return any row, use ISNULL outside of the subselects: e.g.