Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Problem

How to make query Substract AmountDue For Invoice from Customer Balance .

Balance Customer query :

SQL
SELECT VTargetDetail.BranchCode ,VTargetDetail.SubLdgCode as UnitCode ,(SUM(dbo.VTargetDetail.Debit * dbo.VTargetDetail.CurrencyRate)  -  SUM(dbo.VTargetDetail.Credit * dbo.VTargetDetail.CurrencyRate)  ) as Balance
                                FROM         dbo.VTargetDetail LEFT OUTER JOIN
                              dbo.TypeTrxSafe ON dbo.VTargetDetail.BranchCode = dbo.TypeTrxSafe.BranchCode AND dbo.VTargetDetail.TrxTypeCode = dbo.TypeTrxSafe.SafeTrxTypeCode WHERE     (1 = 1) AND (VTargetDetail.TrxDate <=  '2018/09/17') AND (VTargetDetail.SubLdgCodeType = 28) AND (VTargetDetail.SubLdgCode=5639)  group by VTargetDetail.SubLdgCode,VTargetDetail.BranchCode

AmountDue For Invoice query as following :

SQL
select BranchCode,UnitCode,RequiredAmount, Serial,Year,Month,CurrentReadingDate,CurrentMeterReading,LastMeterReading,CurrentConsumption,CurrentConsumptionValue ,VATValue,CleaningFees from WAHInvoice where 1=1 AND year=2018 AND  BranchCode = 1 and Month=5 and UnitCode=5639


How to substract Balance from First Query represent Balance Customer query

From

RequiredAmount on second Query AmountDue For Invoice query

Meaning Balance - RequiredAmount

and I need result display on one query .
SQL
this query above must return one record meaning final result  one record

BranchCode  UnitCode  Difference

1                   5639      CustomerBalance - requiredamount


How To Do That by union

What I have tried:

select BranchCode,UnitCode,RequiredAmount, Serial,Year,Month,CurrentReadingDate,CurrentMeterReading,LastMeterReading,CurrentConsumption,CurrentConsumptionValue ,VATValue,CleaningFees 
from
(
select BranchCode,UnitCode,RequiredAmount, Serial,Year,Month,CurrentReadingDate,CurrentMeterReading,LastMeterReading,CurrentConsumption,CurrentConsumptionValue ,VATValue,CleaningFees from WAHInvoice where 1=1 AND year=2018 AND  BranchCode = 1 and Month=5 and UnitCode=5639
union
SELECT VTargetDetail.BranchCode ,VTargetDetail.SubLdgCode as UnitCode ,(SUM(dbo.VTargetDetail.Debit * dbo.VTargetDetail.CurrencyRate)  -  SUM(dbo.VTargetDetail.Credit * dbo.VTargetDetail.CurrencyRate)  ) as Balance,'' AS Serial,'' as YEAR,'' AS Month,'' as CurrentReadingDate,'' as CurrentMeterReading,'' as LastMeterReading,'' as CurrentConsumption,'' as CurrentConsumptionValue,'' as VATValue,'' as CleaningFees
                                FROM         dbo.VTargetDetail LEFT OUTER JOIN
                              dbo.TypeTrxSafe ON dbo.VTargetDetail.BranchCode = dbo.TypeTrxSafe.BranchCode AND dbo.VTargetDetail.TrxTypeCode = dbo.TypeTrxSafe.SafeTrxTypeCode WHERE     (1 = 1) AND (VTargetDetail.TrxDate <=  '2018/09/17') AND (VTargetDetail.SubLdgCodeType = 28) AND (VTargetDetail.SubLdgCode = 5639) group by VTargetDetail.SubLdgCode,VTargetDetail.BranchCode
							  )as INV

I face more problem
Error converting data type varchar to numeric.

also how to get substract balance field from second query - reuired amount from first query
Posted
Comments
ahmed_sa 16-Sep-18 22:32pm    
select BranchCode,UnitCode,RequiredAmount, Serial,Year,Month,CurrentReadingDate,CurrentMeterReading,LastMeterReading,CurrentConsumption,CurrentConsumptionValue ,VATValue,CleaningFees
from
(
select BranchCode,UnitCode,RequiredAmount, Serial,Year,Month,CurrentReadingDate,Convert(varchar,CurrentMeterReading) as CurrentMeterReading,Convert(varchar,LastMeterReading) as LastMeterReading
,Convert(varchar,CurrentConsumption) as CurrentConsumption,Convert(varchar,CurrentConsumptionValue) as CurrentConsumptionValue ,Convert(varchar,VATValue) as VATValue,
Convert(varchar,CleaningFees) as CleaningFees from WAHInvoice invo where 1=1 AND year=2018 AND BranchCode = 1/* and Month=5 and UnitCode=5639*/
union
SELECT VTargetDetail.BranchCode ,VTargetDetail.SubLdgCode as UnitCode ,
(SUM(dbo.VTargetDetail.Debit * dbo.VTargetDetail.CurrencyRate) - SUM(dbo.VTargetDetail.Credit * dbo.VTargetDetail.CurrencyRate) ) as Balance ,'' AS Serial ,'' as YEAR,'' AS Month,'' as CurrentReadingDate,'' as CurrentMeterReading,'' as LastMeterReading,'' as CurrentConsumption,'' as CurrentConsumptionValue,'' as VATValue,'' as CleaningFees
FROM dbo.VTargetDetail LEFT OUTER JOIN
dbo.TypeTrxSafe ON dbo.VTargetDetail.BranchCode = dbo.TypeTrxSafe.BranchCode AND dbo.VTargetDetail.TrxTypeCode = dbo.TypeTrxSafe.SafeTrxTypeCode
WHERE (1 = 1) AND (VTargetDetail.TrxDate <= '2018/09/17') AND (VTargetDetail.SubLdgCodeType = 28) /*AND (VTargetDetail.SubLdgCode = 5639) */
group by VTargetDetail.SubLdgCode,VTargetDetail.BranchCode
)as INV where Month=5 and UnitCode=5639
my question how to select Balance alias from inside union

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900