Skip to main content

Notifications

Supply chain | Supply Chain Management, Commerce
Suggested answer

Difficulty to get number of new customer only who has transaction

(1) ShareShare
ReportReport
Posted on by 1,881
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
  • Suggested answer
    Mohamed Amine Mahmoudi Profile Picture
    Mohamed Amine Mahmoudi 9,689 Super User 2024 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

News and Announcements

Announcing Category Subscriptions!

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Verified Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,331 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,333 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans