web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Supply chain | Supply Chain Management, Commerce
Suggested answer

Difficulty to get number of new customer only who has transaction

(0) ShareShare
ReportReport
Posted on by 2,382
Hi Experts,
 
Requirement is to get number(count) of new customer only who has transaction in date range. Below is my SQL. In output, Highlighted row "CustomerCount" is 2 and "TotalInvoiceAmount" is 11040 but TotalInvoiceAmount = 11040 is only for one customer out of "CustomerCount"=2. So I want "CustomerCount" should be 1.
 
And also I want to get "CustomerCount" & "TotalInvoiceAmount" for previous month only if customer is created in previous month but there first invoice happen in current month.
 
How to do that.
SELECT 
    CustTable.MainContactWorker,
    hcmWorker.RecId AS MainContactWorkerRecId,
    dirPartyTable.Name AS MainContactWorkerName,
    COUNT(DISTINCT CustTable.RecId) AS CustomerCount,  -- Use DISTINCT to avoid duplicates
    SUM(ISNULL(CustTrans.AmountMST, 0)) AS TotalInvoiceAmount
FROM 
    CustTable
LEFT JOIN 
    CustTrans ON CustTrans.AccountNum = CustTable.AccountNum
    AND CustTrans.TransDate >= '2023-12-01' 
    AND CustTrans.TransDate <= '2023-12-31'  -- Ensure the date range includes transactions
AND (CUSTTRANS.TRANSTYPE=2 OR CUSTTRANS.TRANSTYPE=36)
INNER JOIN 
    HcmWorker ON HcmWorker.RecId = CustTable.MainContactWorker
INNER JOIN 
    DirPartyTable ON DirPartyTable.RecId = HcmWorker.Person
WHERE 
    CustTable.CREATEDDATETIME >= '2023-12-01' 
    AND CustTable.CREATEDDATETIME <= '2023-12-31'
GROUP BY 
    CustTable.MainContactWorker,
    HcmWorker.RecId,
    DirPartyTable.Name;
 
output 
 
 
Thanks,
Faiz
I have the same question (0)
  • faiz7049 Profile Picture
    2,382 on at
    Difficulty to get number of new customer only who has transaction
    Hi Layan,
     
    I could not go through suggestion because my server have issue and trying to fix. Sure, I will update you.
     
    Thanks,
    Faiz
  • Layan Jwei Profile Picture
    8,034 Super User 2025 Season 2 on at
    Difficulty to get number of new customer only who has transaction
    Hi,
     
    Do you still need help? If your question is answered then please verify the answers that helped
  • Layan Jwei Profile Picture
    8,034 Super User 2025 Season 2 on at
    Difficulty to get number of new customer only who has transaction
    Hi,

    If you want the customer count to be 1 because the invoice amount is different, then just add CustTrans.AmountMST to the group by field.
    because if i understood you correctly, what u want is that if you have those three invoice amounts for the same customer
    500
    500
    2000
    then you want those to appear as two records in the sql, right?
    customerCount TotalInvoiceAmount
    2                           100
    1                           2000
     
    but i also think you need to add the customerAccount to the group by field, because currently if you have two different customers that have the same main contact worker, then they are being summed, are u sure u want to do that?

    ​​​​​​​and yes no need for the distinct keyword

    Thanks,
    Layan Jweihan
    Please mark this answer as "Verified" if it solved your issue. In order to help others who will face a similar issue in the future
  • Suggested answer
    Mohamed Amine Mahmoudi Profile Picture
    26,316 Super User 2025 Season 2 on at
    Difficulty to get number of new customer only who has transaction
     
    I don't think using distinct in count que is a good idea !
     
    But if you want avoid duplicates records i think you must add CustTable.RecId in Group By clause.
     
    Best regards,
    Mohamed Amine MAHMOUDI

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > Supply chain | Supply Chain Management, Commerce

#1
CA Neeraj Kumar Profile Picture

CA Neeraj Kumar 1,011

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 279 Super User 2025 Season 2

#3
BillurSamdancioglu Profile Picture

BillurSamdancioglu 215 Most Valuable Professional

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans