Click here to Skip to main content
15,886,857 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
SQL
SELECT
  a.CustomerName
 ,a.AgreementValue
 ,a.TotalValue - a.AgreementValue AS BlackAmount
 ,(SELECT SUM(PaidAmount) AS Paid2Me FROM AmountMaster WHERE AmountTypeId = 2 AND PaymentToId = 1 AND CustomerId = 8 AND PlaceId = 1) AS Paid2Me
 ,(SELECT SUM(PaidAmount) AS Paid2Builder FROM AmountMaster WHERE AmountTypeId = 2 AND PaymentToId = 2 and CustomerId = 8 and PlaceId = 1) AS Paid2Builder
 ,(SELECT ((SELECT SUM(PaidAmount) AS Paid2Me FROM AmountMaster WHERE AmountTypeId = 2 AND PaymentToId = 1 AND CustomerId = 8 AND PlaceId = 1 GROUP BY CustomerId) - (SELECT SUM(PaidAmount) AS PaidAmount FROM AmountMaster WHERE AmountTypeId = 2 and PaymentToId = 2 AND CustomerId = 8 AND PlaceId = 1 GROUP BY CustomerId)) AS DueAmount) AS DueAmount 
FROM
 CustomerDetails a
 JOIN AmountMaster b ON a.CustomerId = b.CustomerId
WHERE
  a.CustomerId = 8 AND b.PlaceId = 1


This is my query and is executing same records many times, But i want only one record, How can I change this query to get only one record?

I tried using order by and group by but it's throwing error

And i'm getting answer is like this

Testing 800 200 20000   NULL    NULL
Testing 800 200 20000   NULL    NULL
Testing 800 200 20000   NULL    NULL
Posted
Updated 6-Sep-13 19:25pm
v2
Comments
AmitGajjar 7-Sep-13 1:43am    
problem with your join. or you can use distinct.

1 solution

Maybe you could try
SQL
SELECT
  a.CustomerName
 ,a.AgreementValue
 ,a.TotalValue - a.AgreementValue AS BlackAmount
 ,(SELECT SUM(PaidAmount) FROM AmountMaster WHERE AmountTypeId = 2 AND PaymentToId = 1 AND CustomerId = 8 AND PlaceId = 1) AS Paid2Me
 ,(SELECT SUM(PaidAmount) FROM AmountMaster WHERE AmountTypeId = 2 AND PaymentToId = 2 and CustomerId = 8 and PlaceId = 1) AS Paid2Builder
 ,Paid2Me - Paid2Builder AS DueAmount
FROM
 CustomerDetails a
 JOIN AmountMaster b ON a.CustomerId = b.CustomerId
WHERE
  a.CustomerId = 8 AND b.PlaceId = 1
 
Share this answer
 

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